本文實例講述了Python常用時間操作。分享給大家供大家參考,具體如下:
我們先導入必須用到的一個module
1
|
>>> import time |
設置一個時間的格式,下面會用到
1
|
>>>ISOTIMEFORMAT = '%Y-%m-%d %X' |
看一下當前的時間,和其他很多語言相似這是從epoch(1970 年 1 月 1 日 00:00:00)開始到當前的秒數。
1
2
|
>>> time.time() 1180759620.859 |
上面的看不懂,換個格式來看看
1
2
|
>>> time.localtime() ( 2007 , 6 , 2 , 12 , 47 , 7 , 5 , 153 , 0 ) |
localtime返回tuple格式的時間,有一個和它類似的函數叫gmtime(),2個函數的差別是時區,gmtime()返回的是0時區的值,localtime返回的是當前時區的值。
1
2
|
>>> time.strftime( ISOTIMEFORMAT, time.localtime() ) ' 2007 - 06 - 02 12 : 54 : 29 ′ |
用上我們的時間格式定義了,使用strftime對時間做一個轉換,如果取現在的時間,time.localtime() 可以不用。
1
2
3
4
|
>>> time.strftime( ISOTIMEFORMAT, time.localtime( time.time() ) ) ' 2007 - 06 - 02 12 : 54 : 31 ′ >>> time.strftime( ISOTIMEFORMAT, time.gmtime( time.time() ) ) ' 2007 - 06 - 02 04 : 55 : 02 ′ |
上面展示了gmtime和localtime的區別。
查看時區用
1
2
|
>>> time.timezone - 28800 |
上面的值是一個秒值,是當前時區和0時區相差的描述,-28800=-8*3600,即為東八區。
帖幾個簡單的函數
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
def ISOString2Time( s ): ''' convert a ISO format time to second from:2006-04-12 16:46:40 to:23123123 把一個時間轉化為秒 ''' return time.strptime( s, ISOTIMEFORMAT ) def Time2ISOString( s ): ''' convert second to a ISO format time from: 23123123 to: 2006-04-12 16:46:40 把給定的秒轉化為定義的格式 ''' return time.strftime( ISOTIMEFORMAT, time.localtime( float ( s) ) ) def dateplustime( d, t ): ''' d=2006-04-12 16:46:40 t=2小時 return 2006-04-12 18:46:40 計算一個日期相差多少秒的日期,time2sec是另外一個函數,可以處理,3天,13分鐘,10小時等字符串,回頭再來寫這個,需要結合正則表達式。 ''' return Time2ISOString( time.mktime( ISOString2Time( d )) + time2sec( t ) ) def dateMinDate( d1, d2 ): ''' minus to iso format date,return seconds 計算2個時間相差多少秒 ''' d1 = ISOString2Time( d1 ) d2 = ISOString2Time( d2 ) return time.mktime( d1 ) - time.mktime( d2 ) |
一、簡介
time模塊提供各種操作時間的函數
說明:一般有兩種表示時間的方式:
第一種是時間戳的方式(相對于1970.1.1 00:00:00以秒計算的偏移量),時間戳是惟一的
第二種以數組的形式表示即(struct_time),共有九個元素,分別表示,同一個時間戳的struct_time會因為時區不同而不同
year (four digits, e.g. 1998)
month (1-12)
day (1-31)
hours (0-23)
minutes (0-59)
seconds (0-59)
weekday (0-6, Monday is 0)
Julian day (day in the year, 1-366)
DST (Daylight Savings Time) flag (-1, 0 or 1) 是否是夏令時
If the DST flag is 0, the time is given in the regular time zone;
if it is 1, the time is given in the DST time zone;
if it is -1, mktime() should guess based on the date and time.
百度關于夏令時介紹:http://baike.baidu.com/view/100246.htm
UTC介紹可參考下文中關于UTC的介紹:http://www.jfrwli.cn/article/40758.htm
二、函數介紹
1.asctime()
asctime([tuple]) -> string
將一個struct_time(默認為當時時間),轉換成字符串
Convert a time tuple to a string, e.g. 'Sat Jun 06 16:26:11 1998'.
When the time tuple is not present, current time as returned by localtime()
is used.
2.clock()
clock() -> floating point number
該函數有兩個功能,
在第一次調用的時候,返回的是程序運行的實際時間;
以第二次之后的調用,返回的是自第一次調用后,到這次調用的時間間隔
示例:
1
2
3
4
5
6
7
8
|
import time if __name__ = = '__main__' : time.sleep( 1 ) print "clock1:%s" % time.clock() time.sleep( 1 ) print "clock2:%s" % time.clock() time.sleep( 1 ) print "clock3:%s" % time.clock() |
輸出:
1
2
3
|
clock1: 3.35238137808e - 006 clock2: 1.00004944763 clock3: 2.00012040636 |
其中第一個clock輸出的是程序運行時間
第二、三個clock輸出的都是與第一個clock的時間間隔
3.sleep(...)
sleep(seconds)
線程推遲指定的時間運行,經過測試,單位為秒,但是在幫助文檔中有以下這樣一句話,這關是看不懂
“The argument may be a floating point number for subsecond precision.”
4.ctime(...)
ctime(seconds) -> string
將一個時間戳(默認為當前時間)轉換成一個時間字符串
例如:
1
|
time.ctime() |
輸出為:'Sat Mar 28 22:24:24 2009'
5.gmtime(...)
gmtime([seconds]) -> (tm_year, tm_mon, tm_day, tm_hour, tm_min,tm_sec, tm_wday, tm_yday, tm_isdst)
將一個時間戳轉換成一個UTC時區(0時區)的struct_time,如果seconds參數未輸入,則以當前時間為轉換標準
6.localtime(...)
localtime([seconds]) -> (tm_year,tm_mon,tm_day,tm_hour,tm_min,tm_sec,tm_wday,tm_yday,tm_isdst)
將一個時間戳轉換成一個當前時區的struct_time,如果seconds參數未輸入,則以當前時間為轉換標準
7.mktime(...)
mktime(tuple) -> floating point number
將一個以struct_time轉換為時間戳
8.strftime(...)
strftime(format[, tuple]) -> string
將指定的struct_time(默認為當前時間),根據指定的格式化字符串輸出
python中時間日期格式化符號:
%y 兩位數的年份表示(00-99)
%Y 四位數的年份表示(000-9999)
%m 月份(01-12)
%d 月內中的一天(0-31)
%H 24小時制小時數(0-23)
%I 12小時制小時數(01-12)
%M 分鐘數(00=59)
%S 秒(00-59)
%a 本地簡化星期名稱
%A 本地完整星期名稱
%b 本地簡化的月份名稱
%B 本地完整的月份名稱
%c 本地相應的日期表示和時間表示
%j 年內的一天(001-366)
%p 本地A.M.或P.M.的等價符
%U 一年中的星期數(00-53)星期天為星期的開始
%w 星期(0-6),星期天為星期的開始
%W 一年中的星期數(00-53)星期一為星期的開始
%x 本地相應的日期表示
%X 本地相應的時間表示
%Z 當前時區的名稱
%% %號本身
9.strptime(...)
strptime(string, format) -> struct_time
將時間字符串根據指定的格式化符轉換成數組形式的時間
例如:
2009-03-20 11:45:39 對應的格式化字符串為:%Y-%m-%d %H:%M:%S
Sat Mar 28 22:24:24 2009 對應的格式化字符串為:%a %b %d %H:%M:%S %Y
10.time(...)
time() -> floating point number
返回當前時間的時間戳
三、疑點
1.夏令時
在struct_time中,夏令時好像沒有用,例如
1
2
|
a = ( 2009 , 6 , 28 , 23 , 8 , 34 , 5 , 87 , 1 ) b = ( 2009 , 6 , 28 , 23 , 8 , 34 , 5 , 87 , 0 ) |
a和b分別表示的是夏令時和標準時間,它們之間轉換為時間戳應該相關3600,但是轉換后輸出都為646585714.0
四、小應用
1.python獲取當前時間
time.time()
獲取當前時間戳
time.localtime()
當前時間的struct_time形式
time.ctime()
當前時間的字符串形式
2.python格式化字符串
格式化成2009-03-20 11:45:39形式
1
|
time.strftime( "%Y-%m-%d %H:%M:%S" , time.localtime()) |
格式化成Sat Mar 28 22:24:24 2009形式
1
|
time.strftime( "%a %b %d %H:%M:%S %Y" , time.localtime()) |
3.將格式字符串轉換為時間戳
1
2
|
a = "Sat Mar 28 22:24:24 2009" b = time.mktime(time.strptime(a, "%a %b %d %H:%M:%S %Y" )) |
希望本文所述對大家Python程序設計有所幫助。