Python time模块
Python time模块
Time模块
表示方式:
1. 浮点型: 以1970.1.1到现在经过的秒数(Unix格式),如time.time(), time.mktime(struct_time)
2. struct类元组:如time.localtime(), time.gmtime()
对于struct_time形式输出函数可用分片的方法限制输出,如:
view sourceprint?1 import time
2 time.localtime() #struct类元组
3 time.localtime()[:6] #(2011, 10, 17, 16, 26, 58)
time.time() 取从1970-1-1到当前时间的秒数,以浮点类型表示
view sourceprint?1 import time
2 time.time() #输出1318840169.1617351
time.localtime() 取本地当前时间以struct_time类型表示
view sourceprint?1 import time
2 time.localtime() #输出time.struct_time(tm_year=2011, tm_mon=10, tm_mday=17, tm_hour=16, tm_min=31, tm_sec=17, tm_wday=0, tm_yday=290, tm_isdst=0)
time.gmtime([sec])->[struct_time] sec表示从1970-1-1以来的秒数,默认值为time.time(),即time.gmtime()=time.gmtime(time.time()),返回struct_time类型对象;
view sourceprint?1 import time
2 time.gmtime()
3 time.gmtime(time.time()) #当前时刻
4 time.gmtime(0) #1970-1-1当天
5 time.gmtime(24*60*60) #1970-1-2
6 time.gmtime(time.time()-24*60*60) #昨天的这个时刻
time.mktime([struct_time])->[sec] 与gmtime()相反,接收struct_time对象为参数,返回用秒数来表示时间的浮点数
view sourceprint?1 import time
2 timne.mktime(time.localtime())
3 time.time() #输出相同,当前时刻1318840944.0
4 time.gmtime() #参数不能为空
time.strftime(“format”,struct_time) 接收struct_time类型对象,将日期转换为字符串表示,struct_time缺省时默认值为time.localtime()
view sourceprint?1 import time
2 time.strftime("Y%m%d%")
3 time.strftime("Y%m%d%",time.localtime) #输出“20111017”
4 time.strftime("Weekday:%w";"Day of the year:%j") #输出“Weekday:1;Day of the year:290"
time.strptime("string","format")->[struct_time] 按照指定格式解析一个表示时间的字符串,返回struct_time对象,两个参数都是字符串,且格式要一致
view sourceprint?1 import time
2 time.strptime('20110909','%Y%m%d')
3 time.strptime('20110909','%Y%m%d_%H') #Error:doesn't match
4 time.strptime('20110909_10:47:12','%Y%m%d_%H:%M:%S')
time.ctime([sec])->【文本日期格式】 将秒数转换成文本描述的日期格式,不带参数时取当前时间
view sourceprint?1 import time
2 time.ctime() #输出'Mon Oct 17 17:01:58 2011'
3 time.ctime(12345) #输出'Thu Jan 1 11:55:45 1970'
4 time.ctime(0) #输出'Thu Jan 1 08:30:00 1970'
time.asctime([struct_time])->[文本日期格式] 将struct_time类型时间转换成文本描述的日期格式,不带参数时取当前时间
view sourceprint?1 import time
2 time.asctime()
3 time.asctime(time.localtime()) #输出当前时间'Mon Oct 17 17:05:57 2011'
4 time.asctime(time.gmtime(12345)) #输出'Thu Jan 1 03:25:45 1970'
time.sleep([sec]) 挂起当前进程,参数为浮点型表示进程挂起时间
time.clock() 在win系统上,time.clock()返回第一次调用该方法到现在的秒数,精度高于1微妙;可用该函数记录进程执行时间 PS:未懂
view sourceprint?1 import time
2 time.clock() #0.068503999999999995
3 time.sleep(2) #等待两秒
4 time.clock() #0.070831000000000005
作者:lwxIvy的博客
相关内容
- 暂无相关文章
评论关闭