time库部分函数含义(一)
import time
时间获取
函数内容
time.time()
time.ctime()
time.gmtime()
time.asctime()
time.localtime()
示例
>>>time.time()
1587020718.2042131
>>> time.ctime()
'Thu Apr 16 15:07:50 2020'
>>> time.gmtime()
time.struct_time(tm_year=2020, tm_mon=4, tm_mday=16, tm_hour=7, tm_min=8, tm_sec=4, tm_wday=3, tm_yday=107, tm_isdst=0)
>>> time.localtime()
time.struct_time(tm_year=2020, tm_mon=4, tm_mday=16, tm_hour=15, tm_min=20, tm_sec=53, tm_wday=3, tm_yday=107, tm_isdst=0)
>>> time.asctime()
'Thu Apr 16 15:22:05 2020'
时间格式化
函数内容
time.strftime(tpl,ts)
time.strptime(string,tpl)
tpl格式化控制符
符号 | 含义 | 范围 |
---|
%Y | 年份 | 0000-9999 |
%m | 月份 | 01-12 |
%B | 月份名称 | January-December |
%b | 月份名称缩写 | Jan-Dec |
%d | 日期 | 01-31 |
%A | 星期 | Monday-Sunday |
%a | 星期缩写 | Mon-Sun |
%H | 小时(24h制) | 00-23 |
%I | 小时(12h制) | 01-12 |
%p | 上/下午 | AM,PM |
%M | 分钟 | 00-59 |
%S | 秒 | 00-59 |
示例
>>> time.strftime("%Y-%m-%d %A %H:%M:%S",time.gmtime())
'2020-04-16 Thursday 07:11:43'
>>> time.strftime("%B %b %A %a %I %p",time.gmtime())
'April Apr Thursday Thu 07 AM'
>>> time.strptime('2020-04-16 Thursday 07:11:43',"%Y-%m-%d %A %H:%M:%S")
time.struct_time(tm_year=2020, tm_mon=4, tm_mday=16, tm_hour=7, tm_min=11, tm_sec=43, tm_wday=3, tm_yday=107, tm_isdst=-1)
>>> time.strptime('April Apr Thursday Thu 07 AM',"%B %b %A %a %I %p")
time.struct_time(tm_year=1900, tm_mon=4, tm_mday=1, tm_hour=7, tm_min=0, tm_sec=0, tm_wday=3, tm_yday=91, tm_isdst=-1)
时间计时
函数内容
time.perf_counter()
time.sleep(a)
示例
>>> t1 = time.perf_counter()
>>> t2 = time.perf_counter()
>>> print("经历了{}秒".format(t2-t1))
经历了6.396373254447276秒