Difference between revisions of "Clock"

From Second Life Wiki
Jump to navigation Jump to search
m (<lsl> tag to <source>)
 
(14 intermediate revisions by 5 users not shown)
Line 1: Line 1:
{{LSL Header}}
{{LSL Header|ml=*}}


=====Say the current time (PST)=====
=====Show the current SL time (PST/PDT)=====
<pre><lsl>
<source lang="lsl2">
 
// Simple SLT 12-hour clock with AM/PM and PDT/PST indicators.  Uses llGetWallClock() and llGetGMTclock()
 
// Omei Qunhua
integer H; //Hours
integer M; //Minutes
string AP; //AM or PM


default
default
Line 13: Line 10:
     state_entry()
     state_entry()
     {
     {
         integer T = (integer)llGetWallclock(); // Get time PST
        llSetTimerEvent(1);
         if (T > 43200) //If it's after noon
    }
         {
    timer()
            T = T - 43200; //Subtract 12 hours
    {
             AP = "PM"; //set to PM
         integer sltSecs = (integer) llGetWallclock();               // Get SL time in seconds (will be either PST or PDT)
            H = T / 3600; //get hours
         integer diff    = (integer) llGetGMTclock() - sltSecs;      // Compute the difference between UTC and SLT
            M = (T - (H * 3600)) / 60; //get minutes
         string  PST_PDT = " PST";                                   // Assume Pacific Standard Time
            if(H == 0) //if the hour is 0
 
            {
        // If the difference between SLT and UTC is 7 hours or -17 hours, then SLT is currently PDT
                H = 12; // make the hour 12
        if (diff == 25200 || diff == -61200)       
            }
             PST_PDT = " PDT";                         // Pacific Daylight Time
        }
        integer Mins  = sltSecs / 60;
        else
        integer Hours = Mins / 60;
        Mins = Mins % 60;
        string AMPM = " AM";
        if (Hours > 11)           // Convert to 12-hour format with PM indication
         {
         {
             AP = "AM"; //set to AM
             AMPM = " PM";
             H = T / 3600; //get the hour
             Hours -= 12;
            M = (T - (H * 3600)) / 60; //get minutes
            if(H == 0) //if the hour is 0
            {
                H = 12; // make the hour 12
            }
         }
         }
         llOwnerSay((string)H + ":" + (string)M + AP);
         if (Hours == 0)
            Hours = 12;
        llSetText( (string) Hours + ":" + llGetSubString("0" + (string) Mins,  -2, -1) + AMPM + PST_PDT, <1,1,1> ,1);
     }
     }
}
}
</source>
[[Category:LSL Examples]]

Latest revision as of 14:16, 24 January 2015

Show the current SL time (PST/PDT)
// Simple SLT 12-hour clock with AM/PM and PDT/PST indicators.   Uses llGetWallClock() and llGetGMTclock()
// Omei Qunhua

default
{
    state_entry()
    {
        llSetTimerEvent(1);
    }
    timer()
    {
        integer sltSecs = (integer) llGetWallclock();               // Get SL time in seconds (will be either PST or PDT)
        integer diff    = (integer) llGetGMTclock() - sltSecs;      // Compute the difference between UTC and SLT
        string  PST_PDT = " PST";                                   // Assume Pacific Standard Time

        // If the difference between SLT and UTC is 7 hours or -17 hours, then SLT is currently PDT
        if (diff == 25200 || diff == -61200)         
            PST_PDT = " PDT";                         //  Pacific Daylight Time
        integer Mins  = sltSecs / 60;
        integer Hours = Mins / 60;
        Mins = Mins % 60;
        string AMPM = " AM";
        if (Hours > 11)            // Convert to 12-hour format with PM indication
        {
            AMPM = " PM";
            Hours -= 12;
        }
        if (Hours == 0)
            Hours = 12;
        llSetText( (string) Hours + ":" + llGetSubString("0" + (string) Mins,  -2, -1) + AMPM + PST_PDT, <1,1,1> ,1);
    }
}