python 中的时间模块time

在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
0tm_year(for example, 1993)
1tm_monrange [1, 12]
2tm_mdayrange [1, 31]
3tm_hourrange [0, 23]
4tm_minrange [0, 59]
5tm_secrange [0, 61]; see (1) in strftime()description
6tm_wdayrange [0, 6], Monday is 0
7tm_ydayrange [1, 366]
8tm_isdst0, 1 or -1; see below

附二:directives can be embedded in the format string

DirectiveMeaningNotes
%aLocale’s abbreviated weekday name. 
%ALocale’s full weekday name. 
%bLocale’s abbreviated month name. 
%BLocale’s full month name. 
%cLocale’s appropriate date and time representation. 
%dDay of the month as a decimal number [01,31]. 
%HHour (24-hour clock) as a decimal number [00,23]. 
%IHour (12-hour clock) as a decimal number [01,12]. 
%jDay of the year as a decimal number [001,366]. 
%mMonth as a decimal number [01,12]. 
%MMinute as a decimal number [00,59]. 
%pLocale’s equivalent of either AM or PM.(1)
%SSecond as a decimal number [00,61].(2)
%UWeek 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)
%wWeekday as a decimal number [0(Sunday),6]. 
%WWeek 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)
%xLocale’s appropriate date representation. 
%XLocale’s appropriate time representation. 
%yYear without century as a decimal number [00,99]. 
%YYear with century as a decimal number. 
%ZTime 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

转载于:https://www.cnblogs.com/moinmoin/archive/2011/03/03/python_time.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值