I needed a way to create a sortable timestamp as a short string to be used as part of a filename.
Just call fn_libBase36_SystemtimeToBase36 with or without a supplied SystemTime to return a 9 character sortable string.
FYI: Chose Base36 as these file names may be viewed and/or parsed inside and outside the file system. Use of Base36 alleviates any issues with algorithms that sort lowercase/uppercase with same weight, which could confuse the actual order.
Just call fn_libBase36_SystemtimeToBase36 with or without a supplied SystemTime to return a 9 character sortable string.
FYI: Chose Base36 as these file names may be viewed and/or parsed inside and outside the file system. Use of Base36 alleviates any issues with algorithms that sort lowercase/uppercase with same weight, which could confuse the actual order.
Code:
$libBase36_Chars = "0123456789abcdefghijklmnopqrstuvwxyz" '----------------------------------------------------------------------------(') FUNCTION fn_libBase36_GetChar( BYREF v_qudDividend AS QUAD, BYREF v_qudDivisor AS QUAD ) AS STRING ' +-------------------------------------------------------------------------------+ ' | INPUT: | ' | Dividend as QUAD | ' | Divisor as QUAD | ' +-------------------------------------------------------------------------------+ ' |OUTPUT: | ' | Function returns a single Base36 character based on the calculation | ' | Dividend returned as MOD of Dividend/Divisor to prepare for next calculation | ' +-------------------------------------------------------------------------------+ LOCAL v_bytChar AS BYTE v_bytChar = v_qudDividend \ v_qudDivisor FUNCTION = MID$( $libBase36_Chars, v_bytChar + 1, 1 ) v_qudDividend = v_qudDividend MOD v_qudDivisor END FUNCTION '----------------------------------------------------------------------------(') FUNCTION fn_libBase36_SystemtimeToBase36( OPT BYREF v_optST AS SYSTEMTIME ) AS STRING ' +-------------------------------------------------------------------------------+ ' | INPUT: | ' | Supply a valid SystemTime structure | ' | If left blank, the current SystemTime will be used | ' +-------------------------------------------------------------------------------+ ' | OUTPUT: | ' | Base36 representation of the SystemTime structure (9 char len) | ' +-------------------------------------------------------------------------------+ ' | NOTE: | ' | Will work at least up through 9999-12-31 23:59:59 | ' +-------------------------------------------------------------------------------+ LOCAL v_udtST AS SYSTEMTIME LOCAL v_qudST AS QUAD LOCAL v_strBase36 AS STRING LOCAL v_bytVal AS BYTE IF IsMissing( v_optST ) THEN GETSYSTEMTIME v_udtST ELSE v_udtST = v_optST v_qudST = VAL( FORMAT$( v_udtST.wYear, "0000" ) & _ FORMAT$( v_udtST.wMonth, "00" ) & _ FORMAT$( v_udtST.wDay, "00" ) & _ FORMAT$( v_udtST.wHour, "00" ) & _ FORMAT$( v_udtST.wMinute, "00" ) & _ FORMAT$( v_udtST.wSecond, "00" )) v_strBase36 &= fn_libBase36_GetChar( v_qudST, 2821109907456&& ) v_strBase36 &= fn_libBase36_GetChar( v_qudST, 78364164096&& ) v_strBase36 &= fn_libBase36_GetChar( v_qudST, 2176782336&& ) v_strBase36 &= fn_libBase36_GetChar( v_qudST, 60466176&& ) v_strBase36 &= fn_libBase36_GetChar( v_qudST, 1679616&& ) v_strBase36 &= fn_libBase36_GetChar( v_qudST, 46656&& ) v_strBase36 &= fn_libBase36_GetChar( v_qudST, 1296&& ) v_strBase36 &= fn_libBase36_GetChar( v_qudST, 36&& ) v_strBase36 &= MID$( $libBase36_Chars, v_qudST + 1, 1 ) FUNCTION = v_strBase36 END FUNCTION
Comment