Difference between revisions of "Clock"

From Second Life Wiki
Jump to navigation Jump to search
m
(Improved clock script)
Line 3: Line 3:
=====Say the current time (PST)=====
=====Say the current time (PST)=====
<lsl>
<lsl>
//Simple clock by Beverly Larkin to show example of how to use llGetWallClock()
// 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 Hours;
            H = T / 3600; //get hours
         integer Mins;
            M = (T - (H * 3600)) / 60; //get minutes
         string  AMPM;
            if(H == 0) //if the hour is 0
        float  fSecs = llGetWallclock();             // Get SL time in seconds (will be either PST or PDT)
            {
        string  PST_PDT = " PST";                     // Assume Pacific Standard Time
                H = 12; // make the hour 12
        if ( (llGetGMTclock() - fSecs) < 27000.0)     // If difference between SLT and UTC is less then 7.5 hours, then SLT is currently PDT
            }
             PST_PDT = " PDT";                         // Pacific Daylight Time
        }
        Mins    = (integer) fSecs / 60;
        else
        Hours  = Mins / 60;
        {
        Mins    = Mins % 60;
             AP = "AM"; //set to AM
         AMPM    = " AM";
            H = T / 3600; //get the hour
         if (Hours > 11)           // Convert to 12-hour format with PM indication
            M = (T - (H * 3600)) / 60; //get minutes
            if(H == 0) //if the hour is 0
            {
                H = 12; // make the hour 12
            }
         }
         if(M < 10)
        {
            llOwnerSay((string)H + ":" + "0" + (string)M + AP); //if the minutes is less than 10 add the extra 0 (so it doesn't say 1:3PM) for example
        }
        else
         {
         {
             llOwnerSay((string)H + ":" + (string)M + AP); // otherwise just say the time
             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);
     }
     }
}
}

Revision as of 13:44, 17 December 2013

Say the current time (PST)

<lsl> // 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 Hours;
       integer Mins;
       string  AMPM;
       float   fSecs = llGetWallclock();             // Get SL time in seconds (will be either PST or PDT)
       string  PST_PDT = " PST";                     // Assume Pacific Standard Time
       if ( (llGetGMTclock() - fSecs) < 27000.0)     // If difference between SLT and UTC is less then 7.5 hours, then SLT is currently PDT
           PST_PDT = " PDT";                         //  Pacific Daylight Time
       Mins    = (integer) fSecs / 60;
       Hours   = Mins / 60;
       Mins    = Mins % 60;
       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);
   }

} </lsl>