shorturl_be/lib/systemSwitch.py
2020-08-04 22:47:34 +08:00

32 lines
976 B
Python
Raw Permalink 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 math
import random
import datetime
char_arr = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", 'A', 'B', 'C', 'D', 'E', 'F', 'G',
'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']
# 获取36进制字符串
def systemSwitch(num):
num = int(num)
char = ''
while num > 0:
key = (num - 1) % 36
char += char_arr[key]
num = math.floor((num - key) / 36)
return char
# 生成唯一不重名字符串
def randomId():
nowTime = datetime.datetime.now().strftime("%H%M%S") # 生成当前时间
randomNum = random.randint(0, 99) # 生成的随机整数n其中0<=n<=99
if randomNum < 10:
randomNum = str(0) + str(randomNum)
uniqueNum = str(nowTime) + str(randomNum)
return uniqueNum
# 获取随机字符串
def getTail():
return systemSwitch(randomId())
# 获取当前时间
def getTime():
return datetime.datetime.now().strftime("%Y%m%d%H%M%S")