Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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