Python3time库函数
形式:
import time
time.<b>() #下⾯即将介绍各种<b>()函数
⼀、时间获取:
1.time():获取当前计算机内部时间,返回浮点数
>>> time.time()
1524146321.7763612 #看不懂的时间表⽰。。。品牌包包排行榜
依托的近义词
>>> ime()
'Thu Apr 19 21:59:34 2018' #星期四 2018-4-19 21:59:34
>>> ime()
time.struct_time(tm_year=2018, tm_mon=4, tm_mday=19, tm_hour=14, tm_min=1, tm_c=8, tm_wday=3, tm_yday=109, tm_isdst=0) #不知为什么,这个时间有误4.localtime():获取计算机当前时间,以当地时间计
>>> time.localtime()
鹊桥仙纤云弄巧
time.struct_time(tm_year=2018, tm_mon=4, tm_mday=20, tm_hour=10, tm_min=12, tm_c=38, tm_wday=4, tm_yday=110, tm_isdst=0)
⼆、时间格式化:
1.strftime(tpl,ts):tpl是格式化模板字符串,⽤来定义输出效果,ts是时间变量
Directive Meaning Notes
%a Locale’s abbreviated weekday name.
%A Locale’s full weekday name.
%b Locale’s abbreviated month name.
%B Locale’s full month name.
%c Locale’s appropriate date and time reprentation.
%d Day of the month as a decimal number [01,31].
足球进校园%H Hour (24-hour clock) as a decimal number [00,23].
%I Hour (12-hour clock) as a decimal number [01,12].
%j Day of the year as a decimal number [001,366].
%m Month as a decimal number [01,12].
%M Minute as a decimal number [00,59].
%p Locale’s equivalent of either AM or PM.(1)
茫无边际
%S Second as a decimal number [00,61].(2)
%U Week number of the year (Sunday as the first day of the week) as a decimal number [00,53]. All days in a new year preceding the first Sunday are considered to be in week 0.
(3)
%w Weekday as a decimal number [0(Sunday),6].
Week number of the year (Monday as the first day of the week) as a decimal number [00,53]. All days in a new
%W Week number of the year (Monday as the first day of the week) as a decimal number [00,53]. All days in a new year preceding the first Monday are considered to be in week 0.
(3)
%x Locale’s appropriate date reprentation.
%X Locale’s appropriate time reprentation.
给领导送礼送什么
%y Year without century as a decimal number [00,99]. %Y Year with century as a decimal number.
%z Time zone offt indicating a positive or negative time difference from UTC/GMT of the form +HHMM or -HHMM, where H reprents decimal hour digits and M reprents decimal minute digits [-23:59, +23:59].
会议设备未来的英文%Z Time zone name (no characters if no time zone exists).
%% A literal '%' character.
Directive Meaning Notes
>>> time.strftime('%Y-%m-%d %H:%M:%S',time.localtime())
'2018-04-20 10:34:57'
2.strptime(str,tpl):str是字符串形式的时间值,tpl是格式化模板字符串,定义输出效果
>>> time.strptime('2018-4-19 22:14:15','%Y-%m-%d %H:%M:%S')
time.struct_time(tm_year=2018, tm_mon=4, tm_mday=19, tm_hour=22, tm_min=14, tm_c=15, tm
_wday=3, tm_yday=109, tm_isdst=-1)
三、程序计时:
1.sleep(s):休眠时间,以秒为单位,可以是浮点数,即等待s秒
2.perf_counter():返回⼀个CPU级别的精确时间计数,单位为秒,通过连续求差值可获得程序执⾏时间
>>> start=time.perf_counter()
>>> end=time.perf_counter()
>>> end-start
10.702251718395534