进阶第五课 Python模块之time
time模块,顾名思义就是提供时间的模块。
1、时间戳time.time()
>>> import time >>> time.time() 1522277717.8651874
返回值很大,单位是“秒”。以1970年1月1日零时为计时起点到你使用time.time()为止,之间的秒数。
那这个时间戳的作用是什么?可以用来计时,比如某个操作用时是多少。
2、休眠time.sleep()
一起输入两端代码
>>> a=10 >>> if a>1: print(a) 10
在输入print(a)回车后,系统会立刻打印a的值10。
再看一段代码
>>> a=10 >>> if a>1: time.sleep(5) print(a) 10
在print(a)之前,加入一行代码time.sleep(5)。意思是在打印a的值之前,系统先休眠5秒钟。录入代码,大家自己感受下上述两段代码的不同。
3、time.clock()
看个例子:打开IDLE,按ctrl+n,打开一个新窗口,输入下面的代码。保存为clock.py。
>>>import time >>>time.sleep(5) >>>time.clock()
回到IDLE,执行import clock,回车
可以看到一个很小的时间,这个时间是CPU执行time.sleep(5)这个语句所用的时间。
4、time.gmtime()
>>> time.gmtime()
time.struct_time(tm_year=2018, tm_mon=3, tm_mday=29, tm_hour=22, tm_min=16, tm_sec=4, tm_wday=3, tm_yday=88, tm_isdst=0)
英国以自己作为时间的参照点(格林尼治时间),根据经度把地球分为24个时区(东一区到东十二区,西一区到西十二区),相邻的时区相差1个小时。时区自东向西排列。
gmtime()打印的是UTC,也就是格林尼治时间,而不是本地时间。
打印的结果是结构化时间,按照键=值这样成对儿的形式。
5、time.localtime()
>>> time.localtime()
time.struct_time(tm_year=2018, tm_mon=3, tm_mday=30, tm_hour=6, tm_min=25, tm_sec=3, tm_wday=4, tm_yday=89, tm_isdst=0)
以结构化时间的格式,打印本地时间。
6、time.mktime()
根据结构化时间,转换为时间戳。
>>> time.mktime(time.localtime())
1522364438.0
7、time.strftime()
>>> help(time.strftime) Help on built-in function strftime in module time: strftime(...) strftime(format[, tuple]) -> string Convert a time tuple to a string according to a format specification. See the library reference manual for formatting codes. When the time tuple is not present, current time as returned by localtime() is used. Commonly used format codes: %Y Year with century as a decimal number. %m Month as a decimal number [01,12]. %d Day of the month as a decimal number [01,31]. %H Hour (24-hour clock) as a decimal number [00,23]. %M Minute as a decimal number [00,59]. %S Second as a decimal number [00,61]. %z Time zone offset from UTC. %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 representation. %I Hour (12-hour clock) as a decimal number [01,12]. %p Locale's equivalent of either AM or PM. Other codes may be available on your platform. See documentation for the C library strftime function.
%Y 四位数的年份表示(000-9999)
%m 月份(01-12)
%d 月内中的一天(0-31)
%H 24小时制小时数(0-23)
%M 分钟数(00=59)
%S 秒(00-59)
%z 时区的名称
%a 本地简化星期名称
%A 本地完整星期名称
%b 本地简化的月份名称
%B 本地完整的月份名称
%c 本地相应的日期表示和时间表示
%I 12小时制小时数(01-12)
%p 本地A.M.或P.M.的等价符
看看help给出的解释,然后写代码:
>>> import time >>> time.strftime('%Y--%m--%d %H:%M:%S') '2018--03--30 06:32:58'
我们根据help中的参数,可以自己订制显示的时间格式。
8、time.strptime()
改功能和strftime相反。
>>> import time >>> time.strptime('2018--03--30 06:32:58','%Y--%m--%d %H:%M:%S') time.struct_time(tm_year=2018, tm_mon=3, tm_mday=30, tm_hour=6, tm_min=32, tm_sec=58, tm_wday=4, tm_yday=89, tm_isdst=-1)
返回的结果是一个结构化时间,这个返回值可以赋值给变量:
>>> a=time.strptime('2018--03--30 06:32:58','%Y--%m--%d %H:%M:%S') >>> a time.struct_time(tm_year=2018, tm_mon=3, tm_mday=30, tm_hour=6, tm_min=32, tm_sec=58, tm_wday=4, tm_yday=89, tm_isdst=-1)
并且可以把结构化时间中的字段单独提取出来:
>>> a.tm_year 2018 >>> a.tm_wday 4
9、time.ctime()
先看代码:
>>> time.ctime() 'Fri Mar 30 06:55:42 2018'
大家还记得时间戳吧?
>>> time.ctime(123456789) 'Fri Nov 30 05:33:09 1973'
把时间戳输入到ctime后面的括号中,得出时间戳对应的时间。