Last active
January 17, 2023 16:31
-
-
Save aoirint/29c5788a122eecbd7855b8beef7f403b 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
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()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://okumuralab.org/~okumura/python/snowflakes.html