Welcome to the Second Life Forums Archive

These forums are CLOSED. Please visit the new forums HERE

Equivalent of mktime() and date() for LSL

Keiki Lemieux
I make HUDDLES
Join date: 8 Jul 2005
Posts: 1,490
04-28-2006 00:07
I just realized that LSL now has llGetUnixTime which is great! However, it's a little limited in its usefulness without a functions to do the following:
  1. A function like php's mktime() function, where you can enter a year, month, day, hour, minute and second and you will get the time in Unix time.
  2. A function like php's date() function, where you can enter in a time in Unix time (integer) and get a formatted date as a string.


It would be great to get atual lsl functions to do this, but if these could also be written by one of us. I would take a stab at it, but I wondered if anyone had already done the work and was willing to share. If not, can anyone point me to some example code in another language that does either of these things?
_____________________
imakehuddles.com/wordpress/
Jigsaw Partridge
A man of parts
Join date: 3 Apr 2005
Posts: 69
04-28-2006 00:35
They would be useful, yes, but given LSL's lethargy in conducting even the simplest of string operations, I would be hesitant about trying to implement them at the application level. Plus, the source code would have to be included in every script that employed them, which might also prove to be a deterrent to their use. However, I would be happy to be proved wrong :)
Keiki Lemieux
I make HUDDLES
Join date: 8 Jul 2005
Posts: 1,490
04-30-2006 09:54
Well, I took a stab at the mktime() function. I've tested this against several known UNIX times and it appears to work accurately. If anyone notices a problem with it, let me know.

CODE
// This function takes 8 arguments, the first six are the date and time.
// GMToffset is adjust it based on the timezome. Use -8 for the SL clock.
// DST should be TRUE or FALSE depending on daylight savings time.
integer MakeUNIXTime( integer year, integer month, integer day, integer hour, integer minute, integer second, integer GMToffset, integer DST ) {
list month_days = [ 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 ];
integer time = ( year - 1970 ) * 31536000;
time += ( day + llList2Integer( month_days, month - 1 ) - 1 ) * 86400;
time += ( hour - GMToffset ) * 3600 + minute * 60 + second;
if ( DST == TRUE ) {
time += -3600;
}
if ( year > 1972 ) {
integer past_leap_years = (integer)( ( year - 1969 ) / 4 );
time += past_leap_years * 86400;
}
if ( year % 4 == 0 && month > 2 ) {
time += 86400;
}
return time;
}
_____________________
imakehuddles.com/wordpress/
Strife Onizuka
Moonchild
Join date: 3 Mar 2004
Posts: 5,887
04-30-2006 10:14
You're leap year detection is wrong, it's every 4 years, except every 100 years but every 400 years (confused yet?).
_____________________
Truth is a river that is always splitting up into arms that reunite. Islanded between the arms, the inhabitants argue for a lifetime as to which is the main river.
- Cyril Connolly

Without the political will to find common ground, the continual friction of tactic and counter tactic, only creates suspicion and hatred and vengeance, and perpetuates the cycle of violence.
- James Nachtwey
Keiki Lemieux
I make HUDDLES
Join date: 8 Jul 2005
Posts: 1,490
04-30-2006 10:29
Except that there are no such leap years between 1970 and 2038 when UNIX time will become obsolete. That's why I didn't include a check for that.
_____________________
imakehuddles.com/wordpress/
Keiki Lemieux
I make HUDDLES
Join date: 8 Jul 2005
Posts: 1,490
05-19-2006 13:57
Here are two more useful functions in case you want to know the day of the week (Monday-Sunday):

CODE
integer GetDay( integer UnixTime ) {
integer days = (integer)( ( UnixTime + 8 * 3600 ) / ( 3600 * 24 ) );
integer day_of_week = days % 7;
return day_of_week; // 0 = Thursday
// 1 = Friday
// 2 = Saturday
// 3 = Sunday
// 4 = Monday
// 5 = Tuesday
// 6 = Wednesday
}

string GetStringDay( integer UnixTime ) {
integer days = (integer)( ( UnixTime + 8 * 3600 ) / ( 3600 * 24 ) );
integer day_of_week = days % 7;
if ( day_of_week == 0 ) {
return "Thursday";
}
else if ( day_of_week == 1 ) {
return "Friday";
}
else if ( day_of_week == 2 ) {
return "Saturday";
}
else if ( day_of_week == 3 ) {
return "Sunday";
}
else if ( day_of_week == 4 ) {
return "Monday";
}
else if ( day_of_week == 5 ) {
return "Tuesday";
}
else if ( day_of_week == 6 ) {
return "Wednesday";
}
else {
return "";
}
}

default {
state_entry() {
integer today = GetDay( llGetUnixTime() );
llSay( 0, (string)today );
string sToday = GetStringDay( llGetUnixTime() );
llSay( 0, sToday );
}
}


The first function returns a number 0-6 which corresponds to each day of the week. The second actually returns the string. Note this is set up for PST/SL time. If you want GMT, then remove "+ 8 * 3600" from the first line of both functions.
_____________________
imakehuddles.com/wordpress/
Merlin Alphabeta
Registered User
Join date: 12 Feb 2006
Posts: 83
date function
02-26-2007 15:06
I too desperately needed a date function - seeing there wasn't one handy... this one is just for GMT w/o DST - anyone want to add in timezone and DST handling be my guest just please repost here (and maybe IM me in world!)

CODE
string padInt(integer a, integer l) {
string s=(string)a;
while(llStringLength(s) < l)
s = (s = "") + "0" + s;
return s;
}

string date(integer epoch) {
list month_days = [ 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 ];
integer total = epoch;
integer year = (integer)(total / 31536000) + 1970;
total -= ((year - 1970) * 31536000);
if(year > 1972) {
integer past_leap_years = (integer)( ( year - 1969 ) / 4 );
total -= past_leap_years * 86400;
}
integer month=0;
while(month < llGetListLength(month_days) && total > llList2Integer(month_days, month) * 86400)
month++;
total -= llList2Integer(month_days, month) * 86400;
if ( year % 4 == 0 && month > 2 ) {
total -= 86400;
}
integer day = (integer)(total / 86400);
total -= day * 86400;
integer hour = (integer)(total / 3600);
total -= hour * 3600;
integer minute = (integer)(total / 60);
total -= minute * 60;
integer second = total;

return padInt(month, 2) + "/" + padInt(day, 2) + "/" + padInt(year, 4) + " " + padInt(hour, 2) + ":" + padInt(minute, 2) + ":" + padInt(second, 2) + " GMT";
}