Difference between revisions of "Day of the Week"

From Second Life Wiki
Jump to navigation Jump to search
m (Add to Examples)
Line 2: Line 2:
Method to get the day of the week from a unix timestamp - {{LSLG|llGetUnixTime}}. The timestamp returns the number of seconds elapsed beginning Thursday, January 1, 1970 UTC. This script first converts the seconds to hours, then adds the GMT offset (if desired), then converts the hours to days, and finally grabs the day of the week from a list.
Method to get the day of the week from a unix timestamp - {{LSLG|llGetUnixTime}}. The timestamp returns the number of seconds elapsed beginning Thursday, January 1, 1970 UTC. This script first converts the seconds to hours, then adds the GMT offset (if desired), then converts the hours to days, and finally grabs the day of the week from a list.


<pre>
<lsl>
// Give day of the week
// Give day of the week
// DoteDote Edison
// DoteDote Edison
Line 19: Line 19:
         llSay(0, "Today is " + llList2String(weekdays, day_of_week));
         llSay(0, "Today is " + llList2String(weekdays, day_of_week));
     }
     }
}</pre>
}</lsl>
And a function version:
And a function version:
<pre>
<lsl>
string getDay(integer offset) {
string getDay(integer offset) {
     list weekdays = ["Thursday", "Friday", "Saturday", "Sunday", "Monday", "Tuesday", "Wednesday"];
     list weekdays = ["Thursday", "Friday", "Saturday", "Sunday", "Monday", "Tuesday", "Wednesday"];
Line 36: Line 36:
     }
     }
}
}
</pre>
</lsl>
 
The following code compensates for daylight savings time that changes the offset between -7 and -8 hours throughout the year. Touching the object will yield a message indicating the weekday name for Pacific time, UTC offset of the Pacific Timezone (SL Time), and if daylight savings time is in effect in the pacific time zone.
 
<lsl>
// Dedric Mauriac
 
float minuteSpan = 60; // 60 seconds
float hourSpan = 3600; // 60 seconds * 60 minutes
float daySpan = 86400; // 60 seconds * 60 minutes * 24 hours
 
float ptOffset()
{
    // get the time for each timezone
    float gmt = llGetGMTclock();
    float pt = llGetWallclock();
   
    // adjust since GMT date may be tomorrow
    if(pt > gmt) gmt += daySpan;
   
    // find the difference in seconds between the two timezones
    return pt - gmt;
}
integer isDaylightSavings()
{
    // UTC-8 pacific standard time (PST: november - march)
    // UTC-7 pacific daylight time (PDT: march - november)
   
    return ptOffset() == -7 * hourSpan;
}
string getWeekday()
{
    // find the number of seconds since Jan 1, 1970 12:00 PST
    float seconds = llGetUnixTime() + ptOffset();
   
    // find the number of days since Jan 1, 1970 (PST)
    integer days = llFloor(seconds / daySpan);
   
    // All weekday names in order (Jan 1, 1970 starts on a Thursday)
    list weekdays = ["Thursday", "Friday", "Saturday", "Sunday", "Monday", "Tuesday", "Wednesday"];
   
    // find the weekday
    integer weekday = days % 7;
    return llList2String(weekdays, weekday);
}
sayTheWeekday()
{
    llSay(0, "Today is " + getWeekday() + ".");
    llSay(0, "Offset is " + (string)(ptOffset() / hourSpan) + " hours.");
 
    if(isDaylightSavings())
        llSay(0, "It is currently daylight savings time.");
    else
        llSay(0, "It is currently standard time.");
}
default
{
    state_entry()
    {
        sayTheWeekday();
    }
    touch_start(integer num_detected)
    {
        sayTheWeekday();
    }
}
</lsl>
{{LSLC|Library|Day of the Week}}{{LSLC|Examples|Day of the Week}}
{{LSLC|Library|Day of the Week}}{{LSLC|Examples|Day of the Week}}

Revision as of 00:25, 29 December 2007

Method to get the day of the week from a unix timestamp - llGetUnixTime. The timestamp returns the number of seconds elapsed beginning Thursday, January 1, 1970 UTC. This script first converts the seconds to hours, then adds the GMT offset (if desired), then converts the hours to days, and finally grabs the day of the week from a list.

<lsl> // Give day of the week // DoteDote Edison

list weekdays = ["Thursday", "Friday", "Saturday", "Sunday", "Monday", "Tuesday", "Wednesday"]; integer offset = -4; // offset from UTC

default {

   state_entry() {
       //
   }
   touch_start(integer total_number) {
       integer hours = llGetUnixTime()/3600;
       integer days = (hours + offset)/24;
       integer day_of_week = days%7;
       llSay(0, "Today is " + llList2String(weekdays, day_of_week));
   }

}</lsl> And a function version: <lsl> string getDay(integer offset) {

   list weekdays = ["Thursday", "Friday", "Saturday", "Sunday", "Monday", "Tuesday", "Wednesday"];
   integer hours = llGetUnixTime()/3600;
   integer days = (hours + offset)/24;
   integer day_of_week = days%7;
   return llList2String(weekdays, day_of_week);

}

default {

   touch_start(integer total_number) {
       integer offset = -4; // offset from UTC
       llSay(0, "Today is " + getDay(offset) + ".");
   }

} </lsl>

The following code compensates for daylight savings time that changes the offset between -7 and -8 hours throughout the year. Touching the object will yield a message indicating the weekday name for Pacific time, UTC offset of the Pacific Timezone (SL Time), and if daylight savings time is in effect in the pacific time zone.

<lsl> // Dedric Mauriac

float minuteSpan = 60; // 60 seconds float hourSpan = 3600; // 60 seconds * 60 minutes float daySpan = 86400; // 60 seconds * 60 minutes * 24 hours

float ptOffset() {

   // get the time for each timezone
   float gmt = llGetGMTclock();
   float pt = llGetWallclock();
   
   // adjust since GMT date may be tomorrow
   if(pt > gmt) gmt += daySpan;
   
   // find the difference in seconds between the two timezones
   return pt - gmt;

} integer isDaylightSavings() {

   // UTC-8 pacific standard time (PST: november - march)
   // UTC-7 pacific daylight time (PDT: march - november)
   
   return ptOffset() == -7 * hourSpan;

} string getWeekday() {

   // find the number of seconds since Jan 1, 1970 12:00 PST
   float seconds = llGetUnixTime() + ptOffset();
   
   // find the number of days since Jan 1, 1970 (PST)
   integer days = llFloor(seconds / daySpan);
   
   // All weekday names in order (Jan 1, 1970 starts on a Thursday)
   list weekdays = ["Thursday", "Friday", "Saturday", "Sunday", "Monday", "Tuesday", "Wednesday"];
   
   // find the weekday
   integer weekday = days % 7;
   return llList2String(weekdays, weekday);

} sayTheWeekday() {

   llSay(0, "Today is " + getWeekday() + ".");
   llSay(0, "Offset is " + (string)(ptOffset() / hourSpan) + " hours.");
   if(isDaylightSavings())
       llSay(0, "It is currently daylight savings time.");
   else
       llSay(0, "It is currently standard time.");

} default {

   state_entry()
   {
       sayTheWeekday();
   }
   touch_start(integer num_detected)
   {
       sayTheWeekday();
   }

} </lsl>