Welcome to the Second Life Forums Archive

These forums are CLOSED. Please visit the new forums HERE

Discussion: 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
Original Thread
07-28-2005 07:21
/15/95/55540/1.html
_____________________
i've got nothing. ;)
DoteDote Edison
Thinks Too Much
Join date: 6 Jun 2004
Posts: 790
07-28-2005 17:24
Just a heads-up, the US intends to change the length of Daylight Saving Time beginning in 2007. It passed the House today, and should pass the Senate tomorrow. DST will begin earlier in the Spring, and end later in the Fall.

Washington Post Article
Harleen Gretzky
Registered User
Join date: 27 Oct 2005
Posts: 51
Why doesn't the original function allow for DST?
03-15-2006 12:30
Since llGetWallClock() is correctly on PST/PDT, why doesn't subtracting it from llGetGMTClock() produce the correct result?
Jon Marlin
Builder, Coder, RL & SL
Join date: 10 Mar 2005
Posts: 297
03-15-2006 13:41
From: Harleen Gretzky
Since llGetWallClock() is correctly on PST/PDT, why doesn't subtracting it from llGetGMTClock() produce the correct result?


Well, because those functions return strings. I could probably parse the strings, and figure out from that, but this is how I did it...

- Jon
_____________________
Come visit Marlin Engineering at Horseshoe (222, 26) to see my line of flying vehicles.
Harleen Gretzky
Registered User
Join date: 27 Oct 2005
Posts: 51
Still don't understand
03-21-2006 22:50
They return floats, not strings. The original function on the Wiki page correctly returns PST/PDT as far as I can see. Since llGetWallClock() returns time in seconds from midnight Pacific (regardless of Daylight Savings) subtracting it from llGetGMTClock() and checking whether it is negative produces the correct result.
Catherine Omega
Geometry Ninja
Join date: 10 Jan 2003
Posts: 2,053
03-21-2006 23:09
From: Harleen Gretzky
They return floats, not strings. The original function on the Wiki page correctly returns PST/PDT as far as I can see. Since llGetWallClock() returns time in seconds from midnight Pacific (regardless of Daylight Savings) subtracting it from llGetGMTClock() and checking whether it is negative produces the correct result.
Someone want to wiki this? I'm passing out here, and can't even be bothered to see if it's already been done. But by writing this, I'll probably remember. Isn't memory a funny thing?

</worst. post. evar.>
_____________________
Need scripting help? Visit the LSL Wiki!
Omega Point - Catherine Omega's Blog
Ratus Waydelich
Registered User
Join date: 7 Mar 2007
Posts: 14
04-30-2007 01:12
But if you have the correct PST in seconds, how to transfer it into a readable date like "2007-28-04 13:08:23"?
Kianta Silverfall
Registered User
Join date: 15 Mar 2009
Posts: 1
05-09-2009 11:07
Funny, i have a syntax error (92,1)
Void Singer
Int vSelf = Sing(void);
Join date: 24 Sep 2005
Posts: 6,973
05-09-2009 11:45
on a two year old script? hardly surprising....
_____________________
|
| . "Cat-Like Typing Detected"
| . This post may contain errors in logic, spelling, and
| . grammar known to the SL populace to cause confusion
|
| - Please Use PHP tags when posting scripts/code, Thanks.
| - Can't See PHP or URL Tags Correctly? Check Out This Link...
| -
Rolig Loon
Not as dumb as I look
Join date: 22 Mar 2007
Posts: 2,482
05-09-2009 12:06
Gotta love necroposts. :p

If you're just compiling what's posted here, of course you'll get an error. This is a function, not a stand alone script. The compiler is just asking you where the rest of the script is.
_____________________
It's hard to tell gender from names around here but if you care, Rolig = she. And I exist only in SL, so don't ask.... ;)

Look for my work in XStreetSL at