Difference between revisions of "User:Void Singer/Functions"

From Second Life Wiki
Jump to navigation Jump to search
m (perils of copypasta)
m (new goodies for timestamp handling)
Line 162: Line 162:
       (list)";",
       (list)";",
       [] ) != []) - 1;
       [] ) != []) - 1;
}
/*//--                      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 ]  //*/
/*//--                                                                --//*/</lsl>
:[[User:Void_Singer/Functions#Return_to_Void_Singers_user_page|Return to top]]
}}
{{void-box
|title=Arbitrary Timestamp Converters
|content=
* These Functions are companions to [[llGetUnixTime]] and [[llGetTimestamp]].
=== Unix Timecode to <nowiki>[Y, M, D, h, m, s]</nowiki> list format. ===
<lsl>
/*//-- Notes:
  Time codes before the year 1902 or past the end of 2037
  are capped to the first second of 1902 and 2038 respectively
  This version could be improved.
//*/
list uUnix2StampLst( integer vIntDate ){
if (-llAbs( vIntDate ) < -2145916800){
vIntDate = 2145916800 * (1 | -(vIntDate < 0));
}
integer vIntYear = 126230400;
integer vIntDays = 86400;
integer vIntHour = 3600;
integer vIntMnit = 60;
integer vIntScnd = vIntDate % vIntMnit;
vIntMnit = vIntDate % vIntHour / vIntMnit;
vIntHour = vIntDate % vIntDays / vIntHour;
vIntDays = vIntDate % vIntYear / vIntDays;
vIntYear = 1970 + (( vIntDate / vIntYear ) << 2);
if (789 == llAbs( vIntDays )){
vIntDays = 29;
vIntDate = 2;
vIntYear +=2;
}else{
vIntYear += ((vIntDays -= (llAbs( vIntDays ) > 789)) / 365);
vIntDays %= 365;
vIntDays += vIntDate = 1;
integer vIntTmp;
@Loop; if (llAbs( vIntDays ) > (vIntTmp = (30 | (vIntDate & 1) ^ (vIntDate > 7)) - ((vIntDate == 2) << 1))){
vIntDays -= vIntTmp;
++vIntDate;
jump Loop;
}
}
return [vIntYear, vIntDate, vIntDays, vIntHour, vIntMnit, vIntScnd];
}
/*//--                      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 ]  //*/
/*//--                                                                --//*/</lsl>
=== <nowiki>[Y, M, D, h, m, s]</nowiki> list format to Unix Timecode. ===
<lsl>/*//-- Notes:
Time codes before the year 1902 or past the end of 2037
  are capped to the first second of 1902 and 2038 respectively
Input format is [Y, M, D, h, m, s] in numerals. Strings are converted.
Elements past the end are safely ignored, for compatibility with
  llParseString2List( llGetTimestamp(), ["-", "T", ":", "."], [] )
//*/
integer uStamp2UnixInt( list vLstStp ){
integer vIntYear = llList2Integer( vLstStp, 0 );
integer vIntRtn;
if (-llAbs(vIntYear - 1970) < -68){
vIntRtn = 2145916800 * (1 | -(vIntYear < 1902));
}else{
integer vIntMnth = llList2Integer( vLstStp, 1 );
integer vIntDays = ~-llList2Integer( vLstStp, 2 );
vIntRtn = llList2Integer( vLstStp, 3 ) * 3600 +
          llList2Integer( vLstStp, 4 ) * 60 +
          llList2Integer( vLstStp, 5 );
@Loop; if (--vIntMnth){
vIntDays += (30 | (vIntMnth & 1) ^ (vIntMnth > 7)) + (vIntMnth == 2) * ~!!(vIntYear & 3);
jump Loop;
}
vIntYear -= 1902;
vIntRtn += ((vIntYear >> 2) * 1461 + (vIntYear & 3) * 365 + ((vIntYear & 3) == 3) + vIntDays) *
          86400 - 2145916800;
}
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 ]  //*/
/*//--                                                                --//*/</lsl>
:[[User:Void_Singer/Functions#Return_to_Void_Singers_user_page|Return to top]]
}}
{{void-box
|title=Get Weekday from timestamp
|content=
* These Functions are related to [[llGetUnixTime]] and [[llGetTimestamp]].
=== Weekday from Unix timestamp ===
<lsl>/*//-- Note:
Accurate from 1901 to 2099 (no limiting code)
//*/
string uUnix2WeekdayStr( integer vIntDat ){
    return llList2String( ["Thursday", "Friday", "Saturday", "Sunday", "Monday", "Tuesday", "Wednesday"],
                          vIntDate % 604800 / 86400  - (vIntDate < 0) );
}
/*//--                      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 ]  //*/
/*//--                                                                --//*/</lsl>
=== Weekday from ( Y, M, D ) format ===
<lsl>/*//-- Note:
Accurate from 1901 to 2099 (no limiting code)
This version could be improved.
//*/
string uStamp2WeekdayStr( integer vIntYear, integer vIntMonth, integer vIntDay ){
return llList2String ( ["Friday", "Saturday", "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday"],
                      (vIntYear + (vIntYear >> 2) - ((vIntMonth < 3) & !(vIntYear & 3)) + vIntDay
                      + (integer)llGetSubString( "_033614625035", vIntMonth, vIntMonth )) % 7 );
}
}
/*//--                      Anti-License Text                        --//*/
/*//--                      Anti-License Text                        --//*/

Revision as of 04:55, 13 January 2010

String: Get Reverse Order

Reverse String Order (faster version)

<lsl>string uStringRevF( string vStrSrc ){

   integer vIntCnt = llStringLength( vStrSrc );
   @Loop; if (vIntCnt--){
       vStrSrc += llGetSubString( vStrSrc, vIntCnt, vIntCnt );
       jump Loop;
   }
   return llGetSubString( vStrSrc, llStringLength( vStrSrc ) >> 1, -1 );

} /*//-- 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 ] //*/ /*//-- --//*/</lsl>

Reverse String Order (smaller memory version)

<lsl>string uStringRevS( string vStrSrc ){

   integer vIntCnt = llStringLength( vStrSrc );
   @Loop; if (vIntCnt--){
       vStrSrc += llGetSubString( vStrSrc, vIntCnt, vIntCnt );
       vStrSrc = llDeleteSubString( vStrSrc, vIntCnt, vIntCnt );
       jump Loop;
   }
   return vStrSrc;

} /*//-- 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 ] //*/ /*//-- --//*/</lsl>

Return to top

String: Find Last Index

Get Last Index of String Test in String Source

<lsl>integer uGetStrIdxRev1( string vStrSrc, string vStrTst ){

   integer vIdxFnd =
     llStringLength( vStrSrc ) -
     llStringLength( vStrTst ) -
     llStringLength(
       llList2String(
         llParseStringKeepNulls( vStrSrc, (list)vStrTst, [] ),
         -1)
       );
return (vIdxFnd

List: Get Reverse Order

Reverse List Order (faster version)

<lsl>list uListRevF( list vLstSrc ){

   integer vIntCnt = (vLstSrc != []);
   @Loop; if (vIntCnt--){
       vLstSrc += llList2List( vLstSrc, vIntCnt, vIntCnt );
       jump Loop;
   }
   return llList2List( vLstSrc, (vLstSrc != []) >> 1, -1 );

} /*//-- 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 ] //*/ /*//-- --//*/</lsl>

Reverse List Order (smaller memory version)

<lsl>list uListRevS( list vLstSrc ){

   integer vIntCnt = (vLstSrc != []);
   @Loop; if (vIntCnt--){
       vLstSrc += llList2List( vLstSrc, vIntCnt, vIntCnt );
       vLstSrc = llDeleteSubList( vLstSrc, vIntCnt, vIntCnt );
       jump Loop;
   }
   return vLstSrc;

} /*//-- 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 ] //*/ /*//-- --//*/</lsl>

Return to top

List: Find Last Index

  • This function is a companion to llListFindList.
    • SPECIAL NOTE: this is only designed to work on string data that will NOT contain the "•" character(alt+7)... please use a different character to suit your needs.

Get Last Index of List Test in List Source

<lsl>integer uGetLstIdxRev( list vLstSrc, list vLstTst ){

   integer vIdxFnd =
     (vLstSrc != []) +
     ([] != vLstTst) +
     ([] != llParseString2List(
              llList2String(
                llParseStringKeepNulls(
                  llDumpList2String( vLstSrc, "•" ),
                  (list)llDumpList2String( vLstTst, "•" ),
                  [] ),
                -1 ),
              (list)"•",
              [] ));
return (vIdxFnd

List: Multi-Find Index (First or Last)

Get First Index in List Source of any element in List Test

<lsl>integer uMatchLstIdxFwd( list vLstSrc, list vLstTst ){

 return ((llParseString2List( 
            llList2String(
              llParseStringKeepNulls(
                llDumpList2String( vLstSrc, ";" ),
                vLstTst, [] ),
              0 ),
            (list)";", [] ) != []) + 1) %
        ((vLstSrc != []) + 1) - 1;

} /*//-- 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 ] //*/ /*//-- --//*/</lsl>

Get Last Index in List Source of any element in List Test

<lsl>integer uMatchLstIdxRev( list vLstSrc, list vLstTst ){

 return (vLstSrc != []) -
   (llParseString2List(
      llList2String(
        llParseString2List( 
          llDumpList2String( vLstSrc, ";" ),
          vLstTst, [] ),
        -1 ),
      (list)";",
      [] ) != []) - 1;

} /*//-- 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 ] //*/ /*//-- --//*/</lsl>

Return to top

Arbitrary Timestamp Converters

Unix Timecode to [Y, M, D, h, m, s] list format.

<lsl> /*//-- Notes:

 Time codes before the year 1902 or past the end of 2037
  are capped to the first second of 1902 and 2038 respectively
 This version could be improved.

//*/

list uUnix2StampLst( integer vIntDate ){ if (-llAbs( vIntDate ) < -2145916800){

vIntDate = 2145916800 * (1

Get Weekday from timestamp

Weekday from Unix timestamp

<lsl>/*//-- Note:

Accurate from 1901 to 2099 (no limiting code)

//*/

string uUnix2WeekdayStr( integer vIntDat ){

   return llList2String( ["Thursday", "Friday", "Saturday", "Sunday", "Monday", "Tuesday", "Wednesday"],
                          vIntDate % 604800 / 86400  - (vIntDate < 0) );

} /*//-- 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 ] //*/ /*//-- --//*/</lsl>

Weekday from ( Y, M, D ) format

<lsl>/*//-- Note:

Accurate from 1901 to 2099 (no limiting code)
This version could be improved.

//*/

string uStamp2WeekdayStr( integer vIntYear, integer vIntMonth, integer vIntDay ){ return llList2String ( ["Friday", "Saturday", "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday"],

                      (vIntYear + (vIntYear >> 2) - ((vIntMonth < 3) & !(vIntYear & 3)) + vIntDay
                      + (integer)llGetSubString( "_033614625035", vIntMonth, vIntMonth )) % 7 );

} /*//-- 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 ] //*/ /*//-- --//*/</lsl>

Return to top

Questions or Comments?

Feel free to leave me a note on my User Talk page.