25 lines
689 B
Python
25 lines
689 B
Python
import time
|
|
import random
|
|
import string
|
|
|
|
BASE62 = string.digits + string.ascii_uppercase + string.ascii_lowercase
|
|
|
|
def to_base62(num):
|
|
encoded = ""
|
|
while num > 0:
|
|
encoded = BASE62[num % 62] + encoded
|
|
num //= 62
|
|
return encoded
|
|
|
|
def generate_id():
|
|
epoch_start = int(time.mktime((2020, 1, 1, 0, 0, 0, 0, 0, 0))) * 1000
|
|
now = int(time.time() * 1000)
|
|
timestamp = (now - epoch_start) % 1_000_000_000_000 # Keep last 12 digits
|
|
|
|
random_num = random.randint(0, 99) # 2-digit random number
|
|
final_num = timestamp * 100 + random_num
|
|
|
|
return to_base62(final_num).zfill(8) # Ensure 8-char output
|
|
|
|
print(generate_id()) # Example output: "A1b2C3D4"
|