在Django的學習過程中的時間處理過程中遇到了strftime函數,於是結合《python cookbook》和python docs 對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)
將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)
可以實現在python程式中的延時,單位為秒,並支援浮點數非整數秒的延時。例:
>>> for i in range(3):
time.sleep(2.5)
print "hello world!"
hello world!
hello world!
hello world!
>>>
附一:struct_time
Index |
Attribute |
Values |
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
Directive |
Meaning |
Notes |
%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