python之time常用方法

python之time常用方法

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-IvLMvtje-1602246988584)(C:\Users\c1751\AppData\Roaming\Typora\typora-user-images\image-20201009200421377.png)]

时间获取

time

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-0Mol6Bfc-1602246988588)(C:\Users\c1751\AppData\Roaming\Typora\typora-user-images\image-20201009200912922.png)]

image-20201009201200366
print(time.time())
#1602245486.8395054 每次都不同,从1970年1月1日00:00 开始计算

ctime

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-NXVkINNH-1602246988591)(C:\Users\c1751\AppData\Roaming\Typora\typora-user-images\image-20201009201459118.png)]

print(time.ctime())
#Fri Oct  9 20:15:18 2020

gmtime

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-iKFjNSxg-1602246988598)(C:\Users\c1751\AppData\Roaming\Typora\typora-user-images\image-20201009201728790.png)]

print(time.gmtime())
#time.struct_time(tm_year=2020, tm_mon=10, tm_mday=9, tm_hour=12, #tm_min=20, tm_sec=21, tm_wday=4, tm_yday=283, tm_isdst=0)

时间格式化

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-zgoGGnp3-1602246988602)(C:\Users\c1751\AppData\Roaming\Typora\typora-user-images\image-20201009202948932.png)]

image-20201009203013545

strftime

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-fswqlz84-1602246988604)(C:\Users\c1751\AppData\Roaming\Typora\typora-user-images\image-20201009202625650.png)]

cs =time.gmtime()
print(time.strftime("%Y-%m-%d %H:%M:%S",cs))
#2020-10-09 12:28:23
cs =time.gmtime()
print(time.strftime("%Y-%m-%d %a %p %I:%M:%S",cs))
#2020-10-09 Fri PM 12:32:00

strptime

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-quKVOwPN-1602246988605)(C:\Users\c1751\AppData\Roaming\Typora\typora-user-images\image-20201009203314999.png)]

tm ="2020-10-9 20:33:25"
print(time.strptime(tm,"%Y-%m-%d %H:%M:%S"))
#time.struct_time(tm_year=2020, tm_mon=10, tm_mday=9, tm_hour=20, #tm_min=33, tm_sec=25, tm_wday=4, tm_yday=283, tm_isdst=-1)

程序计时

sleep

单位是秒,可以为小数

perf_counter

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-kB1hLRmk-1602246988608)(C:\Users\c1751\AppData\Roaming\Typora\typora-user-images\image-20201009202243927.png)]

单位是 秒

start = time.perf_counter()
time.sleep(3)
end = time.perf_counter()

print("时间间隔为:",end - start)
#时间间隔为: 3.0000780999999996

你可能感兴趣的:(基础,python)