A Date-Sorted ID (DSID) is a 48-bit number composed of two components:
- a 16-bit date.
- a 32-bit random number.
DSIDs can be safely used in IEEE-754 floating-point data types, such as Number in Javascript.
Binary structure:
+--------+--------+--------+--------+--------+--------+
| date (16 bits) | random (32 bits) |
+--------+--------+--------+--------+--------+--------+
date: 2^16 = ~179 years, from 1970 AD to 2149 AD
random: 2^32 = 4,294,967,296 possible values per day
The date component represents the number of days since January 1, 1970 UTC, which can be calculated by dividing the number of seconds elapsed since January 1, 1970 UTC by 86400, which in turn is the number of seconds in a day.
The random component is a 32-bit number generated by the best PRNG available, ideally a cryptographically secure PRNG.
Example of a DSID sequence:
88242194172852
88241059419953
88242057272236
88241578265425
88242059047069
88241327595009
88241972335982
88240975936467
88240699942290
88240809328269
Bash code example:
echo "(($EPOCHSECONDS / 86400) * 2^32) + ($RANDOM * 2^16) + $RANDOM" | bc
Python code example:
import time
import random
(int(time.time() / 86400) \* 2\*\*32) + random.randint(0, 2\*\*32-1)
Google AppSheet code example:
NUMBER(TOTALSECONDS(NOW() - DATETIME('1970-01-01')) / 86400) * POWER(2, 32) + RANDBETWEEN(0, POWER(2, 32) - 1)
MIT License