Don't click here unless you want to be banned.

LSL Wiki : LibraryTimestampDifference

HomePage :: PageIndex :: RecentChanges :: RecentlyCommented :: UserSettings :: You are crawl427.us.archive.org
A quicky to find the amount of time in seconds between two timestamps. Makes use of a simplified Julian Date formula from Weisstein's World of Science (website listed in the code) that works for years between 1901 and 2099. I include an example function illustrating converting a timestamp to unixtime.

// Find the difference between two timestamps in seconds
// Works for years between 1901 and 2099
//
// Ben Fassbinder  Oct 17, 2006
//    

integer timestamp_diff(string ts1, string ts2)
{
    list ts = llParseString2List( ts1, ["-","T",":","Z"] , [] );
    
    integer y = (integer)llList2String(ts, 0);
    integer l = (integer)llList2String(ts, 1);
    integer d = (integer)llList2String(ts, 2);
    integer h = (integer)llList2String(ts, 3);
    integer m = (integer)llList2String(ts, 4);
    integer s1 = llRound((float)llList2String(ts, 5));

    s1 += 60*m + 3600*h;
    
    integer jd1 = julian_date(y, l, d);
    
    ts = llParseString2List( ts2, ["-","T",":","Z"] , [] );
    
    y = (integer)llList2String(ts, 0);
    l = (integer)llList2String(ts, 1);
    d = (integer)llList2String(ts, 2);
    h = (integer)llList2String(ts, 3);
    m = (integer)llList2String(ts, 4);
    integer s2 = llRound((float)llList2String(ts, 5));

    s2 += 60*m + 3600*h;
    
    integer jd2 = julian_date(y, l, d);
    
    return 86400*(jd2 - jd1) + s2 - s1;
}


// Julian date minus a constant (1721013.5)
// since we are only doing differences, the constant is unnecessary.
// valid for dates between 1901 and 2099
//
// source:  http://scienceworld.wolfram.com/astronomy/JulianDate.html

integer julian_date(integer y, integer m, integer d)
{
    integer result = 367 * y - llFloor(7.0 *
                                        (y + llFloor( (m+9.0)/12.0 ) )/4.0
                                    );
                                    
    result += llFloor(275.0*m/9.0) + d;
                    
    return result;
}


// timestamp to unix time.
// unix time = 0 corresponds to midnight GMT Jan 1, 1970
         
integer timestamp_to_unixtime(string ts)
{
    return timestamp_diff("1970-01-01T00:00:00Z", ts);
}


default
{
    touch_start(integer total_number)
    {
        integer ut = llGetUnixTime();
        integer ts = timestamp_to_unixtime(llGetTimestamp());
        
        llSay(0, (string)ts + " vs. " + (string)ut);
    }
}

Creative Commons License
This work is licensed under a Creative Commons Public Domain License.
Comments [Hide comments/form]
Attach a comment to this page: