Welcome to the Second Life Forums Archive

These forums are CLOSED. Please visit the new forums HERE

Help Understanding

Dahlia Genira
Registered User
Join date: 29 May 2009
Posts: 8
07-01-2009 18:16
I was looking for a time and date script to use for reference and to learn from. I found this one but I dont quite understand how to call the time and date in the actual script default state. Could someone please fill me in a bit so I understand. Thanks

Code
// Take a timestamp and return it in PST string format
// Example of the output format is "Sun 7/15/2007 1:02 pm"
string timestamp2PST(string timestamp) {
// Set up constants required for the function (could be made global)
list weekday_names = [ "Saturday", "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday" ];
list days_in_month = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];

// Parse the timestamp
list parts = llParseString2List(timestamp, ["-", "T", ":", ".", "Z"], []);

// If it's not a timestamp, then just return the original string
if (llGetListLength(parts) != 7) {
return timestamp;
} else {
// Set up the local variables we need
integer year = llList2Integer(parts, 0);
integer month = llList2Integer(parts, 1);
integer day = llList2Integer(parts, 2);
integer hour = llList2Integer(parts, 3);
integer minute = llList2Integer(parts, 4);
integer second = llList2Integer(parts, 5);

// Account for time difference from simulator to UTC->PST
integer difference = (integer)((llGetWallclock() - llGetGMTclock()) / 3600);
if (difference > 0) {
difference -=24;
}
hour += difference;

// Deal with day/month/year boundaries crossed by the adjustment for the time difference
if (hour < 0) {
day--;
hour += 24;
if (day <= 0) {
month--;
if (month <= 0) {
year--;
month += 12;
}

day = llList2Integer(days_in_month, month - 1);

// Do a leap year adjustment if required
if (month == 2 && year % 4 == 0 && (year % 100 != 0 || year % 400 == 0)) {
day++;
}
}
}

// Determine the day of the week
// This highly non-intuitive code is based on an algorithm
// from http://mathforum.org/library/drmath/view/55837.html
if (month < 3) {
month += 12;
--year;
}
integer weekday_number = (day + 2 * month + (3 * (month + 1)) / 5
+ year + year / 4 - year / 100 + year / 400 + 2) % 7;

if (month > 12) {
month -= 12;
++year;
}

// Build the PST date string (fixed format)
string date = llGetSubString(llList2String(weekday_names, weekday_number), 0, 2);
date += " " + (string)month + "/" + (string)day + "/" + (string)year;

// Convert the hour to am/pm
string suffix = " pm";
if (hour < 12) {
suffix = " am";
if (hour == 0) {
hour = 12;
}
} else if (hour > 12) {
hour %= 12;
}

date += " " + (string)hour + ":";
if (minute < 10) {
date += "0" + (string)minute;
} else {
date += (string)minute;
}
date += suffix;
return date;
}
}
Darien Caldwell
Registered User
Join date: 12 Oct 2006
Posts: 3,127
07-01-2009 19:39
This line is the key:

string timestamp2PST(string timestamp) {


this tells you the function is called timestamp2PST, and that it takes a string as an argument which contains a timestamp, and returns a string which i presume is the time/date in Pacific Standard Time.

So to use it you would do something like this (note this is a code snip, not a full program):

state_entry()
{
string input;
string returned;

input=llGetTimestamp();

returned=timestamp2PST(input); // converts the timestamp inside the variable 'input'

llSay(0,"The string returned was: "+returned); // prints out the contents of the variable 'returned'

}
_____________________
Dahlia Genira
Registered User
Join date: 29 May 2009
Posts: 8
07-01-2009 19:55
Thank You Darien for showing me. I knew it had something to do with the top string but just wasn't sure how you would call it in the script.