Announcement

Collapse

Forum Guidelines

This forum is for finished source code that is working properly. If you have questions about this or any other source code, please post it in one of the Discussion Forums, not here.
See more
See less

SystemTime to Sortable Base36 String

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • SystemTime to Sortable Base36 String

    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.

    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
    <b>George W. Bleck</b>
    <img src='http://www.blecktech.com/myemail.gif'>

  • #2
    Base62? First time I've ever seen it actually used!
    How come you only have 36 characters if it's Base62?

    I thought Base62 also used the lower case alphabet?

    Comment


    • #3
      Good catch on my typo... I originally looked at Base64, then rolled a Base62 (10+26+26), then finalized to Base36 (10+26) due to the aforementioned "FYI" sorting issues I encountered. I just forgot to rename the Base## in my code. It is now corrected.
      <b>George W. Bleck</b>
      <img src='http://www.blecktech.com/myemail.gif'>

      Comment


      • #4
        Wondered why I was't always getting a sortable list when I tested it looping over several seconds. Then I spotted it:

        "0123456789ABCDEFGIHJKLMNOPQRSTUVWXYZ"

        Comment


        • #5
          Damn humans and their typos Thanks for the keen eyes.

          Fixed... Also switched to lowercase for the 10+26 as it looks better in what I am doing (date_md5hash)
          <b>George W. Bleck</b>
          <img src='http://www.blecktech.com/myemail.gif'>

          Comment

          Working...
          X