Files
auto_cursor/services/uuid.py
huangzhenpc cc2a3a34e3 x
2025-03-27 10:20:06 +08:00

33 lines
1012 B
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import random
import time
class ULID:
def __init__(self):
# 定义字符集使用Crockford's Base32字符集
self.encoding = "0123456789ABCDEFGHJKMNPQRSTVWXYZ"
def generate(self) -> str:
# 获取当前时间戳(毫秒)
timestamp = int(time.time() * 1000)
# 生成随机数部分
randomness = random.getrandbits(80) # 80位随机数
# 转换时间戳为base32字符串10个字符
time_chars = []
for _ in range(10):
timestamp, mod = divmod(timestamp, 32)
time_chars.append(self.encoding[mod])
time_chars.reverse()
# 转换随机数为base32字符串16个字符
random_chars = []
for _ in range(16):
randomness, mod = divmod(randomness, 32)
random_chars.append(self.encoding[mod])
random_chars.reverse()
# 组合最终结果
return ''.join(time_chars + random_chars)