Created
August 25, 2020 02:50
-
-
Save intrnl/2e6b228f6d04fd552d802a00dc27a511 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| let epoch = 1420070400000; | |
| let increment = 0; | |
| function timestamp_to_snowflake (timestamp = Date.now()) { | |
| let snowflake = ''; | |
| // timestamp | |
| snowflake += (timestamp - epoch).toString(2).padStart(42, '0'); | |
| // worker id | |
| snowflake += '00001'; | |
| // process id | |
| snowflake += '00000'; | |
| // increment | |
| snowflake += (increment++ % 2**12).toString(2).padStart(12, '0'); | |
| return BigInt('0b' + snowflake); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment