Skip to content

Instantly share code, notes, and snippets.

@intrnl
Created August 25, 2020 02:50
Show Gist options
  • Select an option

  • Save intrnl/2e6b228f6d04fd552d802a00dc27a511 to your computer and use it in GitHub Desktop.

Select an option

Save intrnl/2e6b228f6d04fd552d802a00dc27a511 to your computer and use it in GitHub Desktop.
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