User:Very Keynes

From Second Life Wiki
Jump to navigation Jump to search
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.
//
// dbLib.Date.Time
//
//    Function:    integer dbDateTime(string date, float time)
//    Compresses a Date and Time into a single 32bit Integer value
//
//    Range of dates supported is 2000-03-01 to 2058-12-16
//    
//    Concieved as a DataType for VK-DBMS it allows for Compact staorage
//    of a DateTime String in a form that allows for Numerical compares
//    and easy sorting of data by Date and Time, as well as making other
//    Date Time Functions such as Day of Week and Differance between two
//    Dates perform as simple Numerical Computations.
//
//    Input Formats Supported:
//
//    LSL functions useualy supply the date in string format and the
//    time as  float. This function is compatable with the following:
//
//    A string formated as Date and Time such as the output of
//    llGetTimeStamp() - "YYYY-MM-DDThh:mm:ss.ff..fZ"
//        Only the date and hh:mm:ss are encoded, the fractions are discarded
//        if the time is provided as part of the date string the format is:
//
//            dbDateTime(llGetTimeStamp(), 0)
//            dbDateTime("2010-08-12T13:04:27", 0) 
//
//        Valid Seperators are ["-","/","T",":",".",","] so this format works:
//
//            dbDateTime("2010/08/12,13:04", 0)
//
//        AM/PM format is Also Suppoerted in addition to 24Hour Clock
//
//            dbDateTime("2010/08/12,1:04PM", 0)
//
//    The Date and Time may also be suppled seperately as a string, float pair
//    where the float represents seconds past midnight as provided by LSL.
//
//        dbDateTime(llGetDate(), llGetGMTClock())
//        dbDateTime(llGetDate(), llGetWallClock())
//        dbDateTime{"2010,8,12", 47040.0000)
//
integer dbDateTime(string date, float time)    
{
    list DateTime = llParseString2List(date, ["-","/","T",":",".",","],[]);
    if(llToUpper(llGetSubString(llList2String(DateTime, -1),-2,-1)) == "PM")
        DateTime = llListReplaceList(DateTime,[llList2Integer(DateTime,3) + 12], 3, 3);
    integer Y = llList2Integer(DateTime,0) - 2000 - ((llList2Integer(DateTime,1) + 9) % 12) / 10;
    return (Y * 365 + Y / 4 - Y / 100 + Y/400 + (((llList2Integer(DateTime,1) + 9) % 12) * 306 + 5) / 10 + (llList2Integer(DateTime,2) - 1))
        * 100000 + llList2Integer(DateTime,3) * 3600 + llList2Integer(DateTime,4) * 60 + llList2Integer(DateTime,5) + (integer)time;
}
//
//    Function:    string asDateTime(integer dt)
//    Expand 32bit Integer DateTime into a TimeStamp Format String
//
//    output format "YYYY-MM-DDThh:mm:ss"
//
string asDateTime(integer dt)
{
    integer t = dt % 100000;
    integer y = (10000 * (dt / 100000) + 14780)/3652425;
    if ((dt / 100000) - (y*365 + y/4 - y/100 + y/400) < 0){y--;}
    dt = (dt / 100000) - (y * 365 + y / 4 - y / 100 + y / 400);
    list DateTime = [(2000 + y + ((52 + 100 * dt) / 3060 + 2) / 12),
        (((52 + 100 * dt) / 3060 + 2) % 12 + 1),
        (dt - ((52 + 100 * dt) / 3060 * 306 + 5) / 10 + 1),
        ((t - (((t - (t % 60)) % 3600) / 60) - (t % 60)) / 3600),
        (((t - (t % 60)) % 3600) / 60), (t % 60)];
    dt = llGetListLength(DateTime);
    while(--dt > 0)if(llStringLength(llList2String(DateTime, dt)) < 2)
        DateTime = llListReplaceList(DateTime,["0" + llList2String(DateTime, dt)], dt, dt);
    return
        llList2String(DateTime,0) + "-" +
        llList2String(DateTime,1) + "-" +
        llList2String(DateTime,2) + "T" +
        llList2String(DateTime,3) + ":" +
        llList2String(DateTime,4) + ":" +
        llList2String(DateTime,5);
}
//
//    Function:    integer GetDofW(string date)
//    Find the Day of the week for the Date provided
//
//    intput may be a date srting or any of the LSL date functions
//
//        GetDow("2010/8/12")
//        GetDow(llGetTimStamp())
//        GetDoW(llGetDate())
//
//    output format 0 -> 6 where Sunday = day 0
//
integer GetDoW(string date){return ((3 + (dbDateTime(date,0)/100000))%7);}
//
//    Function:    integer GetDif(string date1, string date2)
//    Find the Differance between the two dates Date1 - Date2
//
//    intput may be a date srting "2010/8/12" or any of the LSL date functions
//
//        GetDow(llGetDate(), "2009,12,31")     = Day Number in Year 2010
//        GetDow("2010-12-31", llGetTimStamp()) = Number of days remaining in Year 
//
//    output format integer in the range 0 --> 21474
//    being the date diferance (2058-12-16) - (2000-03-01)
//
integer GetDif(string date1, string date2){return((dbDateTime(date1,0) - dbDateTime(date2,0))/100000);}
//
//    Function:    string GetSLTimeStamp()
//        Return a TimeStamp corrected to PST Time including Daylight Saving
//
string  GetSLTimeStamp(){
    if (llGetGMTclock() -  llGetWallclock() < 0)return asDateTime(dbDateTime(llGetDate(),llGetWallclock()) - 100000);
    else return asDateTime(dbDateTime(llGetDate(),llGetWallclock()));}
//        
default
{
    state_entry()
    {
        // formating and range tests
        llSay(0, "Today & 2:15PM            "+asDateTime(dbDateTime(llGetDate()+"T2:15PM",0)));
        llSay(0, "Today & 2:15am            "+asDateTime(dbDateTime(llGetDate()+"T2:15am",0)));
        llSay(0, "minimum date              "+asDateTime(0));
        llSay(0, "maximum date              "+asDateTime(0x7FFFFFFF));
        llSay(0, "maximum date Differance   "+GetDif(asDateTime(0x7FFFFFFF), asDateTime(0)));
        llSay(0, "Overflow                  "+asDateTime(0x80000000));
        llSay(0, "\n\ntouch for inforamtion on the current day\n");
    }
    touch_start(integer total_number)
    {
        // examples of use
        //
        string SLdate = llGetSubString(GetSLTimeStamp(), 0, 9);
        llSay(0, "The Date Today in Second Life is " + SLdate);
        llSay(0, "The Day of the Week Today is "+llList2String(["Sun","Mon","Tue","Wed","Thu","Fri","Sat"],GetDoW(SLdate)));
        llSay(0, "it is day number "+(string)GetDif(SLdate,"2009,12,31")+" of the Year");
        llSay(0, "in Week "+(string)(GetDif(SLdate,"2009,12,31")/7) + " of 52");       
        llSay(0, "with "+(string)GetDif("2010-12-31", SLdate)+" Days left this year\n");
        integer UTC = dbDateTime(llGetTimestamp(),0);
        llSay(0, (string)UTC + " is the integer Date & Time now in UTC" );
        integer PST = dbDateTime(GetSLTimeStamp(),0);
        llSay(0, (string)PST + " is the integer Date & Time now in PST" );
        llSay(0, llGetSubString(asDateTime(UTC - PST), 11, -1) + " is the time differance\n");
        llSay(0, asDateTime(dbDateTime(llGetDate(),llGetGMTclock()))+" is the date and time derived from llGetDate & llGetGMTclock ");
        llSay(0, asDateTime(dbDateTime(llGetDate(),llGetWallclock()))+" is the date and time derived from llGetDate & llGetWallclock ");
        llSay(0, "llGetTimeStamp() = " + llGetTimestamp());
        llSay(0, "GetSLTimeStamp() = " + GetSLTimeStamp() + " with GetSLTimeStamp() both Date and Time are corrected for daylight Saving");
    }
}