Welcome to the Second Life Forums Archive

These forums are CLOSED. Please visit the new forums HERE

PST/PDT function

Jon Marlin
Builder, Coder, RL & SL
Join date: 10 Mar 2005
Posts: 297
07-27-2005 19:41
On the wiki page for llGetDate, is a very simple function that returns the date in PST instead of GMT.

However, this function does not handle daylight savings time, nor does it handle the first day of any given month during the first 8 or so hours of the day.

I wrote a function that properly answers the date in PST/PDT form. It handles daylight savings, as well as the first of each month. It will work correctly for the next 5 years (up to the end of 2010).

Please let me know if this works for you or not, and if you have any problems post in the discussion thread for this.

- Jon

CODE
//////////////////////////////////////////////////
//
// Answer today's date in PST/PDT
//
// This function works for any date between Jan. 1, 2005
// and Dec. 31, 2010. It handles leap years as well as first
// day of month and first day of year rollbacks.
//
// Based on the wiki GetPSTDate script.
//
// Jon Marlin
//

string GetPSTDate () {

// aprilDays is the day of the month of the first Sunday, from 2005 to 2010
list aprilDays = [3, 2, 1, 6, 5, 4];

// octoberDays is the day of the month of the last Sunday, from 2005 to 2010
list octoberDays = [30, 29, 28, 26, 25, 31];

// monthDays is the number of days in each month, not counting leap years
list monthDays = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];

string dateUTC = llGetDate ();

// Parse the date string into individual year, month, and day integers
list dateList = llParseString2List (dateUTC, ["-"], []);
integer year = llList2Integer (dateList, 0);
integer month = llList2Integer (dateList, 1);
integer day = llList2Integer (dateList, 2);

// Bounds check - the lists can't handle past 2010, so just return the UTC date for that case
if (year > 2010)
return (dateUTC);

// Note - pstTime is actually pst/pdt time, depending on whether we are in DST or not
float pstTime = llGetWallclock ();

// The standard non-DST offset is 8 hours, which is 28,800 seconds
integer offset = 28800;
integer yearOffset = year - 2005;

// During daylight savings time, the time difference is 7 hours... Are we in DST?
// First case - May to September, inclusive, is always DST
if (month > 4 && month < 10)
offset = 25200;

// Second case - April
else if (month == 4) {
integer aprilDay = llList2Integer (aprilDays, yearOffset);
if (day > aprilDay)
offset = 25200;

// After 2am PST it switches to DST
else if (day == aprilDay && pstTime > 7200.0)
offset = 25200;}

// Third case - October
else if (month == 10) {
integer octoberDay = llList2Integer (octoberDays, yearOffset);
if (day < octoberDay)
offset = 25200;

// After 2am PDT it switches back to PST
else if (day == octoberDay && pstTime < 7200)
offset = 25200;}

// Handle the offset, and rollback (if it occurs)
if (llGetGMTclock () < offset) {
day = day - 1;
// Handle month rollback
if (day == 0) {
month = month - 1;
// Handle year rollback
if (month == 0) {
year = year - 1;
month = 12;
day = 31;}
else {
// During a normal month rollback, the last day of the month varies
day = llList2Integer (monthDays, month - 1);

// Between 2005 and 2010, 2008 is the only leap year
if (month == 2 && year == 2008)
day = 29;}}

// Between midnight and the time difference hour, answer the corrected date
return ((string)year + "-" + (string)month + "-" + (string)day);}
else
// If we're past the GMT - PST time difference for the day,
// just answer the GMT date
return (dateUTC);
}
_____________________
Come visit Marlin Engineering at Horseshoe (222, 26) to see my line of flying vehicles.
Nada Epoch
The Librarian
Join date: 4 Nov 2002
Posts: 1,423
Discussion Thread
07-28-2005 07:21
/54/84/55595/1.html
_____________________
i've got nothing. ;)