This commit is contained in:
huangzhenpc
2025-03-27 10:20:06 +08:00
parent 6bcfedeb04
commit cc2a3a34e3
21 changed files with 1791 additions and 0 deletions

32
services/uuid.py Normal file
View File

@@ -0,0 +1,32 @@
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)