设为首页 收藏本站
查看: 1573|回复: 0

[经验分享] python 中的时间模块time

[复制链接]

尚未签到

发表于 2015-4-20 12:07:25 | 显示全部楼层 |阅读模式
  在Django的学习过程中的时间处理过程中遇到了strftime函数,于是结合《python cookbook》和python docs 对time模块中常用的一些操作和函数做了一点总结和归纳。


  • time.time()
  
  代表了从特定时间点,也被称作纪元(epoch:[英] [?i:p?k] [美] [??p?k, ?i?pɑk] )开始所经历的秒数,是一个看起来不够直观的浮点数,这个时间根据不同的平台有所不同,一般为1970年1月1日午夜。例:




>>> import time

>>> time.time()
1299080804.953

>>> print time.asctime(time.gmtime(0))
Thu Jan 01 00:00:00 1970

  • time.gmtime([secs]),  time.localtime([secs])
  time.gmtime将任何时间戳(参数secs所代表的秒数)转化为一个元组,该元组为GMT(格林威治标准时),亦即UTC(世界标准时间)。也可以传递一个时间戳(从纪元开始经历的秒数)给time.localtime,会根据当前时区进行时间转化。




>>> time.gmtime(0)
time.struct_time(tm_year=1970, tm_mon=1, tm_mday=1, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=3, tm_yday=1, tm_isdst=0)
>>> time.localtime(0)
time.struct_time(tm_year=1970, tm_mon=1, tm_mday=1, tm_hour=8, tm_min=0, tm_sec=0, tm_wday=3, tm_yday=1, tm_isdst=0)

  • time.asctime([t])
  将struct_time转换为一个由24字符组成的字符串(24-character string),格式如:
  




Thu Mar 03 00:16:04 2011

  • time.strftime(format[, t])
  将一个元组(tuple)或者struct_time类,即参数t,转换为指定格式的字符串


  • time.striptime(string[, format])
  跟time.strftime相反的操作,解析给定的字符串并返回一个struct_time类。例:





>>> from time import strftime, gmtime

>>> strftime("%a %d %b %Y  %H:%M:%S")
'Thu 03 Mar 2011  00:29:41'
>>> strftime("%a %d %b %Y  %H:%M:%S", gmtime())
'Wed 02 Mar 2011  16:30:09'
>>> time.strptime("30 Feb 11", "%d %b %y")
...
ValueError: day is out of range for month
>>> time.strptime("28 Feb 11", "%d %b %y")
time.struct_time(tm_year=2011, tm_mon=2, tm_mday=28, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=0, tm_yday=59, tm_isdst=-1)

  • time.sleep(secs)
   可以实现在python程序中的延时,单位为秒,并支持浮点数非整数秒的延时。例:




>>> for i in range(3):
    time.sleep(2.5)
    print "hello world!"
   
hello world!
hello world!
hello world!
>>>
  
附一:struct_time


IndexAttributeValues




0
tm_year
(for example, 1993)


1
tm_mon
range [1, 12]


2
tm_mday
range [1, 31]


3
tm_hour
range [0, 23]


4
tm_min
range [0, 59]


5
tm_sec
range [0, 61]; see (1) in strftime()description


6
tm_wday
range [0, 6], Monday is 0


7
tm_yday
range [1, 366]


8
tm_isdst
0, 1 or -1; see below
  附二:directives can be embedded in the format string


DirectiveMeaningNotes




%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.



%d
Day of the month as a decimal number [01,31].



%H
Hour (24-hour clock) as a decimal number [00,23].



%I
Hour (12-hour clock) as a decimal number [01,12].



%j
Day of the year as a decimal number [001,366].



%m
Month as a decimal number [01,12].



%M
Minute as a decimal number [00,59].



%p
Locale’s equivalent of either AM or PM.
(1)


%S
Second as a decimal number [00,61].
(2)


%U
Week number of the year (Sunday as the first day of the week) as a decimal number [00,53]. All days in a new year preceding the first Sunday are considered to be in week 0.
(3)


%w
Weekday as a decimal number [0(Sunday),6].



%W
Week number of the year (Monday as the first day of the week) as a decimal number [00,53]. All days in a new year preceding the first Monday are considered to be in week 0.
(3)


%x
Locale’s appropriate date representation.



%X
Locale’s appropriate time representation.



%y
Year without century as a decimal number [00,99].



%Y
Year with century as a decimal number.



%Z
Time zone name (no characters if no time zone exists).



%%
A literal '%' character.
  
  参考:1. python cookbook
          2. docs:http://docs.python.org/library/time.html#time.struct_time

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-58846-1-1.html 上篇帖子: Python和Lua的默认作用域以及闭包 下篇帖子: 布同:Python函数帮助查询小工具[v1和v2]
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表