python time模块详解
python time模塊詳解
轉自:http://blog.csdn.net/kiki113/article/details/4033017
python 的內嵌time模板翻譯及說明
??
一、簡介
? 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介紹:http://wenda.tianya.cn/wenda/thread?tid=283921a9da7c5aef&clk=wttpcts
?? ?
二、函數介紹
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
? 該函數有兩個功能,
? 在第一次調用的時候,返回的是程序運行的實際時間;
? 以第二次之后的調用,返回的是自第一次調用后,到這次調用的時間間隔
??
? 示例:
?
? 輸出:
? 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
? 將一個時間戳(默認為當前時間)轉換成一個時間字符串
? 例如:
? 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中,夏令時好像沒有用,例如
? 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形式
? time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())?
??
? 格式化成Sat Mar 28 22:24:24 2009形式
? time.strftime("%a %b %d %H:%M:%S %Y", time.localtime())?
??
3.將格式字符串轉換為時間戳
? a = "Sat Mar 28 22:24:24 2009"
? b = time.mktime(time.strptime(a,"%a %b %d %H:%M:%S %Y"))
?
轉載于:https://www.cnblogs.com/DjangoBlog/p/3527981.html
總結
以上是生活随笔為你收集整理的python time模块详解的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 我国东风-41洲际导弹
- 下一篇: 歼16和周边重型战斗机的性能对比