python的time模块

Posted Long Time

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python的time模块相关的知识,希望对你有一定的参考价值。

 1 #encoding=utf-8
 2 import time
 3 
 4 # 返回时间戳
 5 print time.time()
 6 
 7 # 延迟运行单位为s,如下为延迟3s
 8 time.sleep(3)
 9 
10 # 转换时间戳为时间元组(时间对象),自1970+second,其second为秒数
11 print time.gmtime(4)
12 
13 # 转换时间戳为本地对象
14 print time.localtime()
15 
16 # 将时间戳转换为字符串
17 print time.asctime(time.localtime())
18 
19 # 将时间戳转换为字符串
20 print time.ctime(5)
21 
22 # 将本地时间转换为时间戳
23 t = (2009, 2, 17, 17, 3, 38, 1, 48, 0)
24 print time.mktime(t)
25 
26 # 将时间对象转换为规范性字符串(常用),格式如下
27 ‘‘‘
28 %Y Year with century as a decimal number.
29 %m Month as a decimal number [01,12].
30 %d Day of the month as a decimal number [01,31].
31 %H Hour (24-hour clock) as a decimal number [00,23].
32 %M Minute as a decimal number [00,59].
33 %S Second as a decimal number [00,61].
34 %z Time zone offset from UTC.
35 %a Locale‘s abbreviated weekday name.
36 %A Locale‘s full weekday name.
37 %b Locale‘s abbreviated month name.
38 %B Locale‘s full month name.
39 %c Locale‘s appropriate date and time representation.
40 %I Hour (12-hour clock) as a decimal number [01,12].
41 %p Locale‘s equivalent of either AM or PM.
42 ‘‘‘
43 print time.strftime(%Y-%m-%d %H-%M-%S,t)
44 
45 # 将时间字符串根据指定的格式化符转换成数组形式的时间
46 struct_time = time.strptime("30 Nov 00", "%d %b %y")
47 print struct_time

 

以上是关于python的time模块的主要内容,如果未能解决你的问题,请参考以下文章

python+spark程序代码片段

Python学习——02-Python基础——7-模块——time与random等常用模块与包

python模块解析

python如何计算程序(代码块)的运行时间?

python-time模块sys模块os模块以及大量实例

Python—time模块介绍