Unix2DateTime
De DigiWiki.
Function to generate the date and time values from a Unix timestamp.
// Unix2DateTime.lsl // Convert the Unix time (seconds since 1-1-1970 00:00:00) into a list : [ year, month, day, hour, minute, second]. // HISTORY: // Version: 1.2 // Added TimeString(list unix2datetime_list) and DateString(list unix2datetimelist) functions. // Version: 1.1 // Fixed incorrect date when adding 1 day at last day of month. integer DAYS_PER_YEAR = 365; // Non leap year integer SECONDS_PER_DAY = 86400; integer SECONDS_PER_HOUR = 3600; integer LeapYear(integer year) { if (year % 4 == 0) { if (year % 100 == 0) { if (year % 400 == 0) { return 1; } else { return 0; } } else { return 1; } } else { return 0; } } integer DaysPerMonth(integer year,integer month) { if (month < 8) { if (month % 2 == 0) { if (month == 2) { if (LeapYear(year)) { return 29; } else { return 28; } } else { return 30; } } else { return 31; } } else { if (month % 2 == 0) { return 31; } else { return 30; } } } integer DaysPerYear(integer year) { if (LeapYear(year)) return DAYS_PER_YEAR + 1; else return DAYS_PER_YEAR; } list Unix2DateTime(integer unixtime) { integer days_since_1_1_1970 = unixtime / SECONDS_PER_DAY; integer day = days_since_1_1970 + 1; // Add 1 day since days are 1 based, not 0! integer year = 1970; integer days_per_year = DaysPerYear(year); // Calculate year, month and day while (day > days_per_year) { day -= days_per_year; ++year; days_per_year = DaysPerYear(year); } integer month = 1; integer days_per_month = DaysPerMonth(year,month); while (day > days_per_month) { day -= days_per_month; if (++month > 12) { ++year; month = 1; } days_per_month = DaysPerMonth(year,month); } // Calculate the hours, minutes and seconds integer seconds_since_midnight = unixtime % SECONDS_PER_DAY; integer hour = seconds_since_midnight / SECONDS_PER_HOUR; integer second = seconds_since_midnight % SECONDS_PER_HOUR; integer minute = second / SECOND_PER_MINUTE; second = second % SECONDS_PER_MINUTE; return [ year, month, day, hour, minute, second ]; } ///////////////////////////////// MonthName() //////////////////////////// list MonthNameList = [ "JAN", "FEB", "MAR", "APR", "MAY", "JUN", "JUL", "AUG", "SEP", "OCT" , "NOV", "DEC" ]; string MonthName(integer month) { if (month >= 0 && month < 12) return llList2String(MonthNameList, month); else return ""; } ///////////////////////////////// DateString() /////////////////////////// string DateString(list timelist) { integer year = llList2Integer(timelist,0); integer month = llList2Integer(timelist,1); integer day = llList2Integer(timelist,2); return (string)day + "-" + MonthName(month - 1) + "-" + (string)year; } ///////////////////////////////// TimeString() //////////////////////////// string TimeString(list timelist) { integer hour = llList2Integer(timelist,3); integer minute = llList2Integer(timelist,4); integer second = llList2Integer(timelist,5); string hourstr = (string)hour; string minutestr = (string)minute; string secondstr = (string)second; if (hour < 10) hourstr = "0" + hourstr; if (minute < 10) minutestr = "0" + minutestr; if (second < 10) secondstr = "0" + secondstr; return hourstr + ":" + minutestr + ":" + secondstr; }