Difference between revisions of "GetTimestampOffset"

From Second Life Wiki
Jump to navigation Jump to search
m (Removed accidentally-included text from borrowed page)
m (<lsl> tag to <source>)
 
(3 intermediate revisions by one other user not shown)
Line 1: Line 1:
{{LSL Header}}
{{LSL Header}}


This function returns [[llGetTimestamp|llGetTimestamp()]] with an hour offset. It will wrap day, month and year as required. Offsets of more than 24 hours either way are not supported.
This function returns [[llGetTimestamp|llGetTimestamp()]] with an hour offset. It will wrap day, month and year as required. If it is passed an offset of more than 24 hours either way, it will use the offset for US Pacific Time (SL time), automatically adjusting for DST.


<lsl>
<source lang="lsl2">
string  sbGetTimestamp(integer  intOffset) {
string  sbGetTimestamp(integer  intOffset) {
     // Start with December for purposes of wrapping
     // Start with December for purposes of wrapping
Line 14: Line 14:
     integer intMonth = llList2Integer(lstTime, 1);
     integer intMonth = llList2Integer(lstTime, 1);
     integer intDay  = llList2Integer(lstTime, 2);
     integer intDay  = llList2Integer(lstTime, 2);
     integer intHour  = llList2Integer(lstTime, 3) + intOffset;
     integer intHour  = llList2Integer(lstTime, 3);


     string  strYear;
     string  strYear;
Line 22: Line 22:


     if (intOffset == 0) { return strTimestamp; }
     if (intOffset == 0) { return strTimestamp; }
    if (intOffset < -24 || intOffset > 24) {
        intOffset = ((integer)llGetWallclock() - (integer)llGetGMTclock()) / 3600;
    }
    intHour+= intOffset;


     // Add a day to February in leap years
     // Add a day to February in leap years
Line 78: Line 84:
         llList2String(lstTime, 5) + "." +  
         llList2String(lstTime, 5) + "." +  
         llList2String(lstTime, 6) + "Z";
         llList2String(lstTime, 6) + "Z";
        // Obviously this isn't really Z time anymore, but I left it there in case there
        // are scripts expecting it.
}
}
</lsl>
</source>


{{LSLC|Library}}{{LSLC|Examples}}
{{LSLC|Library}}{{LSLC|Examples}}

Latest revision as of 14:09, 24 January 2015

This function returns llGetTimestamp() with an hour offset. It will wrap day, month and year as required. If it is passed an offset of more than 24 hours either way, it will use the offset for US Pacific Time (SL time), automatically adjusting for DST.

string  sbGetTimestamp(integer  intOffset) {
    // Start with December for purposes of wrapping
    list    lstDays  = [31, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];

    string  strTimestamp = llGetTimestamp();

    list    lstTime  = llParseString2List(strTimestamp, ["-", ":", ".", "T"], []);
    integer intYear  = llList2Integer(lstTime, 0);
    integer intMonth = llList2Integer(lstTime, 1);
    integer intDay   = llList2Integer(lstTime, 2);
    integer intHour  = llList2Integer(lstTime, 3);

    string  strYear;
    string  strMonth;
    string  strDay;
    string  strHour;

    if (intOffset == 0) { return strTimestamp; }

    if (intOffset < -24 || intOffset > 24) {
        intOffset = ((integer)llGetWallclock() - (integer)llGetGMTclock()) / 3600;
    }

    intHour+= intOffset;

    // Add a day to February in leap years
    if (intYear % 4 == 0 && (intYear % 100 != 0 || intYear % 400 == 0)) {
        lstDays = llListReplaceList(lstDays, [29], 2, 2);
    }

    if (intOffset < 0) {
        if (intHour < 0) { 
            intHour+= 24;
            --intDay;
        }

        if (intDay < 1) {
            intDay = llList2Integer(lstDays, --intMonth);
        }

        if (intMonth < 1) {
            intMonth = 12;
            --intYear;
        }
    }

    if (intOffset > 0) {
        if (intHour > 23) {
            intHour-= 24;
            ++intDay;
        }

        if (intDay > llList2Integer(lstDays, intMonth)) {
            intDay = 1;
            ++intMonth;
        }

        if (intMonth > 12) {
            intMonth = 1;
            ++intYear;
        }
    }

    strYear  = (string)intYear;
    strMonth = (string)intMonth;
    strDay   = (string)intDay;
    strHour  = (string)intHour;

    if (llStringLength(strMonth) < 2) { strMonth = "0" + strMonth; }
    if (llStringLength(strDay)   < 2) { strDay   = "0" + strDay;   }
    if (llStringLength(strHour)  < 2) { strHour  = "0" + strHour;  }

    return
        strYear                   + "-" + 
        strMonth                  + "-" + 
        strDay                    + "T" + 
        strHour                   + ":" + 
        llList2String(lstTime, 4) + ":" + 
        llList2String(lstTime, 5) + "." + 
        llList2String(lstTime, 6) + "Z";
        // Obviously this isn't really Z time anymore, but I left it there in case there
        // are scripts expecting it.
}