时间模块

发布时间 2023-07-08 15:36:39作者: hacker_dvd
import time, random

# 传入时间戳,返回格式化时间
def getDate(stamp):
    return time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(stamp))

print(getDate(int(time.time())))

# 传入格式化时间,返回时间戳
def getStamp(date):
    return int(time.mktime(time.strptime(date, "%Y-%m-%d %H:%M:%S")))

print(getStamp("2023-01-01 00:00:00"))

# 获取一个不重复的名字
def getUniqueName(name=""):
    suf = '_' if name else ''
    rands = random.randint(10000, 99999)
    return name + suf + str(time.time()).replace(".", "") + str(rands)

print(getUniqueName())
import time, random
import hashlib
ls = [1, 2, 3, 4, 5, 6, 7]
print(random.sample(ls, 3))

sr = 'hello'
md5 = hashlib.md5()
md5.update(sr.encode('utf-8'))
result = md5.hexdigest()
print('加密结果', result)

sr = 'world'
sha1 = hashlib.sha1()
sha1.update(sr.encode('utf-8'))
result = sha1.hexdigest()
print(result)