python-time模块里的函数

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/shelbaydo/article/details/84668618

Python time localtime()方法

#!/usr/bin/python
import time
print "time.localtime() : %s" % time.localtime()
time.localtime() : time.struct_time(tm_year=2016, tm_mon=11, tm_mday=27, tm_hour=10, tm_min=26, tm_sec=5, tm_wday=6, tm_yday=332, tm_isdst=0)

Python time mktime()方法

#!/usr/bin/python
import time
t = (2009, 2, 17, 17, 3, 38, 1, 48, 0)
secs = time.mktime( t )
print "time.mktime(t) : %f" %  secs
print "asctime(localtime(secs)): %s" % time.asctime(time.localtime(secs))
time.mktime(t) : 1234915418.000000
asctime(localtime(secs)): Tue Feb 17 17:03:38 2009

Python time sleep()方法

Python time sleep() 函数推迟调用线程的运行,可通过参数secs指秒数,表示进程挂起的时间

#!/usr/bin/python
import time
 
print "Start : %s" % time.ctime()
time.sleep( 5 )
print "End : %s" % time.ctime()
Start : Tue Feb 17 10:19:18 2013
End : Tue Feb 17 10:19:23 2013

Python time strftime()方法

Python time strftime() 函数接收以时间元组,并返回以可读字符串表示的当地时间,格式由参数format决定。

#!/usr/bin/python
import time
t = (2009, 2, 17, 17, 3, 38, 1, 48, 0)
t = time.mktime(t)
print time.strftime("%b %d %Y %H:%M:%S", time.gmtime(t))
Feb 17 2009 09:03:38

Python time strptime()方法

Python time strptime() 函数根据指定的格式把一个时间字符串解析为时间元组。

#!/usr/bin/python
# -*- coding: UTF-8 -*-
import time
struct_time = time.strptime("30 Nov 00", "%d %b %y")
print "返回的元组: %s " % struct_time
返回的元组: time.struct_time(tm_year=2000, tm_mon=11, tm_mday=30, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=3, tm_yday=335, tm_isdst=-1) 

猜你喜欢

转载自blog.csdn.net/shelbaydo/article/details/84668618