Difference between revisions of "Unix2DateTime"
Jump to navigation
Jump to search
Huney Jewell (talk | contribs) m (Added to Category: Script Library) |
Huney Jewell (talk | contribs) (Formatted to comply with LSL Functions style) |
||
Line 1: | Line 1: | ||
{{LSL Header}} | {{LSL Header}} __NOTOC__ | ||
== Unix2DateTime | <div id="box"> | ||
{{#vardefine:p_unixtime_desc|number of seconds elapsed since 00:00 hours, Jan 1, 1970 UTC, ie. return value of llGetUnixTime()}} | |||
{{#vardefine:p_time_desc|list of integers [year, month, day, hour, minute, second]}} | |||
== Function: [[list]] Unix2DateTime([[integer]] {{LSL Param|unixtime}}); == | |||
<div style="padding: 0.5em;"> | |||
Returns a [[list]] of [[integer|integers]] comprising the date [year, month, day, hour, minute, second]. | |||
{| | |||
{{LSL DefineRow|[[integer]]|unixtime|{{#var:p_unixtime_desc}}}} | |||
|} | |||
</div></div> | |||
<div id="box"> | |||
{{#vardefine:p_year_desc|range from 1970 to 2036}}{{#vardefine:p_month_desc|range from 1 to 12}}{{#vardefine:p_day_desc|range from 1 to 31}}{{#vardefine:p_hour_desc|range from 0 to 23}}{{#vardefine:p_minute_desc|range from 0 to 59}}{{#vardefine:p_second_desc|range from 0 to 59}} | |||
== Function: [[integer]] DateTime2Unix([[integer]] {{LSL Param|year}},[[integer]] {{LSL Param|month}},[[integer]] {{LSL Param|day}},[[integer]] {{LSL Param|hour}},[[integer]] {{LSL Param|minute}},[[integer]] {{LSL Param|second}});); == | |||
<div style="padding: 0.5em;"> | |||
Returns an [[integer]] comprising the date in number of seconds elapsed since 00:00 hours, Jan 1, 1970 UTC. | |||
{| | |||
{{LSL DefineRow|[[integer]]|year|{{#var:p_year_desc}}}} | |||
{{LSL DefineRow|[[integer]]|month|{{#var:p_month_desc}}}} | |||
{{LSL DefineRow|[[integer]]|day|{{#var:p_day_desc}}}} | |||
{{LSL DefineRow|[[integer]]|hour|{{#var:p_hour_desc}}}} | |||
{{LSL DefineRow|[[integer]]|minute|{{#var:p_minute_desc}}}} | |||
{{LSL DefineRow|[[integer]]|second|{{#var:p_second_desc}}}} | |||
|} | |||
</div></div> | |||
<div id="box"> | |||
== Function: [[string]] DateString([[list]] {{LSL Param|time}}); == | |||
<div style="padding: 0.5em;"> | |||
Returns a [[string]] comprising the date as DD-MON-YYYY. | |||
{| | |||
{{LSL DefineRow|[[list]]|time|{{#var:p_time_desc}}}} | |||
|} | |||
</div></div> | |||
<div id="box"> | |||
== Function: [[string]] TimeString([[list]] {{LSL Param|time}}); == | |||
<div style="padding: 0.5em;"> | |||
Returns a [[string]] comprising the time as HH24:MI:SS. | |||
{| | |||
{{LSL DefineRow|[[list]]|time|{{#var:p_time_desc}}}} | |||
|} | |||
</div></div> | |||
<div id="box"> | |||
== Specification == | |||
<div style="padding: 0.5em;"> | |||
<pre> | <pre> | ||
///////////////////////////////////////////////////////////////////// | ///////////////////////////////////////////////////////////////////// | ||
Line 217: | Line 263: | ||
while (mt < month) | while (mt < month) | ||
{ | { | ||
days = DaysPerMonth(mt++); | days = DaysPerMonth(mt++, year); | ||
time += days * SECONDS_PER_DAY; | time += days * SECONDS_PER_DAY; | ||
} | } | ||
Line 233: | Line 279: | ||
////////////////////////////////////////////// | ////////////////////////////////////////////// | ||
</pre> | </pre> | ||
</div></div> | |||
<div id="box"> | |||
== Example == | |||
<div style="padding: 0.5em;"> | |||
Trivial example to display data and time converted from system time when touched. | |||
<pre> | |||
default | |||
{ | |||
state_entry() | |||
{ | |||
llSay(0, "Hello, Avatar!"); | |||
} | |||
touch_start(integer total_number) | |||
{ | |||
llOwnerSay("Date: " + DateString(Unix2DateTime(llGetUnixTime()))); // displays date as DD-MON-YYYY | |||
llOwnerSay("Time: " + TimeString(Unix2DateTime(llGetUnixTime()))); // displays time as HH24:MI:SS | |||
} | |||
} | |||
</pre> | |||
</div></div> | |||
{{LSLC|Library}} | {{LSLC|Library}} |
Revision as of 06:05, 20 September 2007
LSL Portal | Functions | Events | Types | Operators | Constants | Flow Control | Script Library | Categorized Library | Tutorials |
Function: integer DateTime2Unix(integer year,integer month,integer day,integer hour,integer minute,integer second););
Returns an integer comprising the date in number of seconds elapsed since 00:00 hours, Jan 1, 1970 UTC.
• integer | year | – | range from 1970 to 2036 | |
• integer | month | – | range from 1 to 12 | |
• integer | day | – | range from 1 to 31 | |
• integer | hour | – | range from 0 to 23 | |
• integer | minute | – | range from 0 to 59 | |
• integer | second | – | range from 0 to 59 |
Specification
///////////////////////////////////////////////////////////////////// // Script Library Contribution by Flennan Roffo // Logic Scripted Products & Script Services // Peacock Park (183,226,69) // (c) 2007 - Flennan Roffo // // Distributed as GPL, donated to wiki.secondlife.com on 19 sep 2007 // // SCRIPT: Unix2DateTimev1.0.lsl // // FUNCTION: // Perform conversion from return value of llGetUnixTime() to // date and time strings and vice versa. // // USAGE: // list timelist=Unix2DateTime(llGetUnixTime()); // llSay(0, "Date: " + DateString(timelist); // displays date as DD-MON-YYYY // llSay(0, "Time: " + TimeString(timelist); // displays time as HH24:MI:SS ///////////////////////////////////////////////////////////////////// ///////////////////////////// Unix Time conversion ////////////////// integer DAYS_PER_YEAR = 365; // Non leap year integer SECONDS_PER_YEAR = 31536000; // Non leap year integer SECONDS_PER_DAY = 86400; integer SECONDS_PER_HOUR = 3600; integer SECONDS_PER_MINUTE = 60; list MonthNameList = [ "JAN", "FEB", "MAR", "APR", "MAY", "JUN", "JUL", "AUG", "SEP", "OCT", "NOV", "DEC" ]; ////////////////////////////// LeapYear() ///////////////////////////// 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; } } ////////////////////////////// DaysPerMonth() /////////////////////////////////////// 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; } } } /////////////////////////// DaysPerYear() //////////////////////////////////// integer DaysPerYear(integer year) { if (LeapYear(year)) return DAYS_PER_YEAR + 1; else return DAYS_PER_YEAR; } /////////////////////////////////////////////////////////////////////////////////////// // Convert Unix time (integer) to a Date and Time string /////////////////////////////////////////////////////////////////////////////////////// /////////////////////////////// Unix2DataTime() /////////////////////////////////////// list Unix2DateTime(integer unixtime) { integer days_since_1_1_1970 = unixtime / SECONDS_PER_DAY; integer day = days_since_1_1_1970 + 1; integer year = 1970; integer days_per_year = DaysPerYear(year); 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); } 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 / SECONDS_PER_MINUTE; second = second % SECONDS_PER_MINUTE; return [ year, month, day, hour, minute, second ]; } ///////////////////////////////// MonthName() //////////////////////////// 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; } /////////////////////////////////////////////////////////////////////////////// // Convert a date and time to a Unix time integer /////////////////////////////////////////////////////////////////////////////// ////////////////////////// DateTime2Unix() //////////////////////////////////// integer DateTime2Unix(integer year, integer month, integer day, integer hour, integer minute, integer second) { integer time = 0; integer yr = 1970; integer mt = 1; integer days; while(yr < year) { days = DaysPerYear(yr++); time += days * SECONDS_PER_DAY; } while (mt < month) { days = DaysPerMonth(mt++, year); time += days * SECONDS_PER_DAY; } days = day - 1; time += days * SECONDS_PER_DAY; time += hour * SECONDS_PER_HOUR; time += minute * SECONDS_PER_MINUTE; time += second; return time; } ////////////////////////////////////////////// // End Unix2DateTimev1.0.lsl //////////////////////////////////////////////
Example
Trivial example to display data and time converted from system time when touched.
default { state_entry() { llSay(0, "Hello, Avatar!"); } touch_start(integer total_number) { llOwnerSay("Date: " + DateString(Unix2DateTime(llGetUnixTime()))); // displays date as DD-MON-YYYY llOwnerSay("Time: " + TimeString(Unix2DateTime(llGetUnixTime()))); // displays time as HH24:MI:SS } }