时间模块
须知:(时间戳:指定时间距离1970年1月1日0点的总秒数;UTC:国际标准时间)
A.time模块
import time #导入time模块
print(time.time()) # 当前时间的时间戳
# => 1596508484.2142832
sleep(0.4)功能:
阻塞程序,括号内0.4代表程序阻塞时间是0.4秒
time.sleep(0.4)
B.datetime模块
import datetime #导入time模块
创建日期对象
d = datetime.datetime.now() # 当前时间
d = datetime.datetime(year=2030, month=1, day=1)
日期对象的属性和方法
print(d.year, d.month, d.day) # 日期:年月日
print(d.hour, d.minute, d.second) # 时间:时分秒
print(d.date(), d.time())
print(d.year, d.month, d.day) # 日期:年月日
print(d.hour, d.minute, d.second) # 时间:时分秒
print(d.date(), d.time())
print( d.strftime('%Y{}%m{}%d{}').format('年', '月', '日') )
=> 2020年08月04日
时间戳
print( d.timestamp() )
时间戳 => 日期对象: 了解
print(datetime.datetime.fromtimestamp(2596509826))
#时间差
d2 = datetime.timedelta(days=7, hours=10)
print(d + d2) # 7天10小时后的日期
print(d - d2) # 7天10小时前的日期
加密模块
import hashlib
# md5加密: 没有解密算法, 不可逆, 明文和密文一一对应
# RSA加密: 非对称加密, 私钥和公钥
# DES, AES加密: 对称加密, 需要使用key进行加密和解密
m = hashlib.md5()
m.update('123456'.encode())
print(m.hexdigest()) # 32位的16进制
# => e10adc3949ba59abbe56e057f20f883e