How to reduce date in yyyyMMddHHmmss format to 5 bytes?

1.2k views Asked by At

I need to generate a suffix to uniquify a value. I thought of using the current data and time but need the suffix to be no more than 5 bytes long. Are there any hashing methods that can produce a hash of 5 bytes or less from a date in yyyyMMddHHmmss format?

Any other ideas? It would be simple to maintain a running counter and use the next value but this I would prefer not to have to rely on any kind of stored value.

1

There are 1 answers

2
bjoernz On BEST ANSWER

In case you do not need to rely on printable characters, I would suggest, that you simply use the Unix timestamp. That will work great even with 4 Bytes (until January 19, 2038).

If you want to use only a subset of characters, I would suggest, that you create a list of values that you want to use.

  • Let's say you want to use the letters (capital and small) and the digits -> 62 values.
  • Now you need to convert the timestamp into base-62. Let's say your timestamp is 100:
  • 100 = (1 * 62^1) + (38 * 62^0)
  • If you have stored your printable value in an array, you could use the coefficients 1 and 38 as an index into that array.

If you chose your base to small, five bytes will not be enough. In that case you can either substract a constant from the timestamp (which will buy you some time) or you can estimate when duplicate timestamps will occur and if that date is past your retirement date ;-)