siliconid/siliconid.py

21 lines
627 B
Python

import time
import random
BASE62 = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
def to_base62(num):
result = ""
while num > 0:
result = BASE62[num % 62] + result
num //= 62
return result
def generate_id():
epoch_start = int(time.mktime((2020, 1, 1, 0, 0, 0, 0, 0, 0))) * 1000
now = int(time.time() * 1000) - epoch_start
random_num = random.randint(0, 999) # 3-digit random number
id_num = now * 1000 + random_num # Combine timestamp + randomness
return to_base62(id_num).zfill(8)[:8] # Ensure 8 chars
print(generate_id()) # Example output: "8F2T5MkQ"