时间获取-------time() ctime() gmtime()
时间格式化-------strftime() strptime()
程序计时-------sleep() perf_counter()
时间获取函数
time()-------获取当前时间戳,浮点数形式
print(time.time()) # 输出: 1575428957.5652914
ctime()-------以可读的方式返回字符串时间
print(time.ctime()) # 输出: Wed Dec 4 11:09:54 2019
gmtime()-------计算机可以处理的时间格式
print(time.gmtime()) #输出: time.struct_time(tm_year=2019, tm_mon=12, tm_mday=4, tm_hour=3, tm_min=11, tm_sec=11, tm_wday=2, tm_yday=338, tm_isdst=0)
时间格式化
strftime()-------将时间进行合理输出
p = time.gmtime() print(time.strftime("%Y-%m-%d %H:%M:%S",p)) # 输出: 2019-12-04 03:11:11
strptime()-------自定义时间
程序计时
perf_counter()-------测量时间函数
import time start = time.perf_counter() time.sleep(0.5) end = time.perf_counter() print(end - start)
sleep()-------产生时间函数,s拟休眠的时间,单位是秒,可以是浮点数