Day of the Week

From Second Life Wiki

Jump to: navigation, search

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.

 
// 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));
    }
}

And a function version:

 
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) + ".");
    }
}
 

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.

 
// 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();
    }
}
 
Personal tools