首页 > 编程语言 > 详细

python基础 — time库

时间:2019-12-04 15:29:09      阅读:60      评论:0      收藏:0      [点我收藏+]

时间获取-------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)

  

 

时间格式化  

%Y   年份
%m  月份
%B   月份名称 January
%b   月份名称缩写  Jan
%d   日期
%A   星期 Monday
%a   星期缩写 Mon
%H   小时 24
%h   小时 12
%p   上下午
%M  分钟
%S   秒

strftime()-------将时间进行合理输出

p = time.gmtime()
print(time.strftime("%Y-%m-%d %H:%M:%S",p))

# 输出:  2019-12-04 03:11:11

strptime()-------自定义时间

print(time.strftime("%Y-%B-%d-%A-%H-%p-%S")
‘2018-April-21-Saturday-16-PM-10‘

print( time.strftime("%A-%p"))
‘Saturday-PM‘

print(time.strftime("%M:%S"))
‘15:39‘

 

程序计时

perf_counter()-------测量时间函数

import time

start = time.perf_counter()
time.sleep(0.5)
end = time.perf_counter()
print(end - start)

  

sleep()-------产生时间函数,s拟休眠的时间,单位是秒,可以是浮点数

 

python基础 — time库

原文:https://www.cnblogs.com/chen-jun552/p/11981718.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!