Stamp2UnixInt

From Second Life Wiki
Jump to navigation Jump to search

Summary

User-Defined Function: integer uStamp2UnixInt( list vLstStp );

Returns an integer that is the Unix time code represented by the input list

  • vLstStmp: source time stamp list of format [Y, M, D, h, m, s]


Unsafe Version

(Missing day/month or bad values not handled)

  • LSO: 465 bytes
  • MONO: 1024 bytes
integer uStamp2UnixInt( list vLstStp ){
    integer vIntYear = llList2Integer( vLstStp, 0 ) - 1902;
    integer vIntRtn;
    if (vIntYear >> 31 | vIntYear / 136){
        vIntRtn = 2145916800 * (1 | vIntYear >> 31);
    }else{
        integer vIntMnth = ~-llList2Integer( vLstStp, 1 );
        vIntRtn = 86400 * ((integer)(vIntYear * 365.25 + 0.25) - 24837 +
          vIntMnth * 30 + (vIntMnth - (vIntMnth < 7) >> 1) + (vIntMnth < 2) -
          (((vIntYear + 2) & 3) > 0) * (vIntMnth > 1) +
          (~-llList2Integer( vLstStp, 2 )) ) +
          llList2Integer( vLstStp, 3 ) * 3600 +
          llList2Integer( vLstStp, 4 ) * 60 +
          llList2Integer( vLstStp, 5 );
    }
    return vIntRtn;
}
/*//--                       Anti-License Text                         --//*/
/*//     Contributed Freely to the Public Domain without limitation.     //*/
/*//   2009 (CC0) [ http://creativecommons.org/publicdomain/zero/1.0 ]   //*/
/*//  Void Singer [ https://wiki.secondlife.com/wiki/User:Void_Singer ]  //*/
/*//--                                                                 --//*/

Minimal Version

(Safely supports missing Day/Month)

  • LSO: 505 bytes
  • MONO: 1024 bytes
integer uStamp2UnixInt( list vLstStp ){
    integer vIntYear = llList2Integer( vLstStp, 0 ) - 1902;
    integer vIntRtn;
    if (vIntYear >> 31 | vIntYear / 136){
        vIntRtn = 2145916800 * (1 | vIntYear >> 31);
    }else{
        integer vIntMnth = ~-llList2Integer( vLstStp, 1 );
        integer vIntDays = ~-llList2Integer( vLstStp, 2 );
        vIntMnth += !~vIntMnth;
        vIntRtn = 86400 * ((integer)(vIntYear * 365.25 + 0.25) - 24837 +
          vIntMnth * 30 + (vIntMnth - (vIntMnth < 7) >> 1) + (vIntMnth < 2) -
          (((vIntYear + 2) & 3) > 0) * (vIntMnth > 1) +
          vIntDays + !~vIntDays ) +
          llList2Integer( vLstStp, 3 ) * 3600 +
          llList2Integer( vLstStp, 4 ) * 60 +
          llList2Integer( vLstStp, 5 );
    }
    return vIntRtn;
}
/*//--                       Anti-License Text                         --//*/
/*//     Contributed Freely to the Public Domain without limitation.     //*/
/*//   2009 (CC0) [ http://creativecommons.org/publicdomain/zero/1.0 ]   //*/
/*//  Void Singer [ https://wiki.secondlife.com/wiki/User:Void_Singer ]  //*/
/*//--                                                                 --//*/

Safe(er) Version

(Supports missing Day/Month and bad inputs are capped)

  • LSO: 640 bytes
  • MONO: 1536 bytes
integer uStamp2UnixInt( list vLstStp ){
    integer vIntYear = llList2Integer( vLstStp, 0 ) - 1902;
    integer vIntRtn;
    if (vIntYear >> 31 | vIntYear / 136){
        vIntRtn = 2145916800 * (1 | vIntYear >> 31);
    }else{
        integer vIntMnth = ~-llList2Integer( vLstStp, 1 );
        integer vIntDays = ~-llList2Integer( vLstStp, 2 );
        vIntMnth = llAbs( (vIntMnth + !~vIntMnth) % 12 );
        vIntRtn = 86400 * ((integer)(vIntYear * 365.25 + 0.25) - 24837 +
          vIntMnth * 30 + (vIntMnth - (vIntMnth < 7) >> 1) + (vIntMnth < 2) -
          (((vIntYear + 2) & 3) > 0) * (vIntMnth > 1) +
          llAbs( (vIntDays + !~vIntDays) % 31 ) ) +
          llAbs( llList2Integer( vLstStp, 3 ) % 24 ) * 3600 +
          llAbs( llList2Integer( vLstStp, 4 ) % 60 ) * 60 +
          llAbs( llList2Integer( vLstStp, 5 ) % 60 );
    }
    return vIntRtn;
}
/*//--                       Anti-License Text                         --//*/
/*//     Contributed Freely to the Public Domain without limitation.     //*/
/*//   2009 (CC0) [ http://creativecommons.org/publicdomain/zero/1.0 ]   //*/
/*//  Void Singer [ https://wiki.secondlife.com/wiki/User:Void_Singer ]  //*/
/*//--                                                                 --//*/

Caveats

  • Time codes before the year 1902 or past the end of 2037 are capped to the first second of 1902 and 2038 respectively

Notes

  • Input list elements past the first six are ignored
  • Input format is compattible with:
    llParseString2List( llGetTimestamp(), ["-", "T", ":", "."], [] )
    
    and
    llParseString2List( llGetDate(), ["-"], [] )