python-time模块里的函数

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) 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值