Skip to content

Instantly share code, notes, and snippets.

@aoirint
Last active January 17, 2023 16:31
Show Gist options
  • Save aoirint/29c5788a122eecbd7855b8beef7f403b to your computer and use it in GitHub Desktop.
Save aoirint/29c5788a122eecbd7855b8beef7f403b to your computer and use it in GitHub Desktop.
from datetime import datetime
import random
def generate_snowflake_id() -> int:
origin_epoch_millis = 1288834974657 # 2010-11-04T01:42:54.657Z
current_epoch_millis = int(datetime.utcnow().timestamp() * 1000)
timestamp = current_epoch_millis - origin_epoch_millis
return (timestamp << 22) | random.getrandbits(22) # last 22 bits are not random bits actually
print(generate_snowflake_id())
@aoirint
Copy link
Author

aoirint commented Jan 17, 2023

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment