日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python time模块详解

發(fā)布時間:2024/10/12 python 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python time模块详解 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

python time模塊詳解

轉自:http://blog.csdn.net/kiki113/article/details/4033017

python 的內(nèi)嵌time模板翻譯及說明
??
一、簡介

? time模塊提供各種操作時間的函數(shù)
? 說明:一般有兩種表示時間的方式:
?????? 第一種是時間戳的方式(相對于1970.1.1 00:00:00以秒計算的偏移量),時間戳是惟一的
?????? 第二種以數(shù)組的形式表示即(struct_time),共有九個元素,分別表示,同一個時間戳的struct_time會因為時區(qū)不同而不同
?? ?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
?? ?
二、函數(shù)介紹
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
? 該函數(shù)有兩個功能,
? 在第一次調用的時候,返回的是程序運行的實際時間;
? 以第二次之后的調用,返回的是自第一次調用后,到這次調用的時間間隔
??
? 示例:
?

[python]?view plaincopy
  • 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()??

  • ? 輸出:
    ? clock1:3.35238137808e-006
    ? clock2:1.00004944763
    ? clock3:2.00012040636
    ? 其中第一個clock輸出的是程序運行時間
    ? 第二、三個clock輸出的都是與第一個clock的時間間隔
    ??
    3.sleep(...)
    ? sleep(seconds)
    ? 線程推遲指定的時間運行,經(jīng)過測試,單位為秒,但是在幫助文檔中有以下這樣一句話,這關是看不懂
    ? “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時區(qū)(0時區(qū))的struct_time,如果seconds參數(shù)未輸入,則以當前時間為轉換標準
    ??
    ?? ?
    6.localtime(...)
    ? localtime([seconds]) -> (tm_year,tm_mon,tm_day,tm_hour,tm_min,tm_sec,tm_wday,tm_yday,tm_isdst)
    ? 將一個時間戳轉換成一個當前時區(qū)的struct_time,如果seconds參數(shù)未輸入,則以當前時間為轉換標準
    ??
    ?????? ?
    7.mktime(...)
    ? mktime(tuple) -> floating point number
    ? 將一個以struct_time轉換為時間戳
    ?????? ?
    8.strftime(...)
    ? strftime(format[, tuple]) -> string
    ? 將指定的struct_time(默認為當前時間),根據(jù)指定的格式化字符串輸出
    ? python中時間日期格式化符號:
    ? %y 兩位數(shù)的年份表示(00-99)
    ? %Y 四位數(shù)的年份表示(000-9999)
    ? %m 月份(01-12)
    ? %d 月內(nèi)中的一天(0-31)
    ? %H 24小時制小時數(shù)(0-23)
    ? %I 12小時制小時數(shù)(01-12)?
    ? %M 分鐘數(shù)(00=59)
    ? %S 秒(00-59)
    ??
    ? %a 本地簡化星期名稱
    ? %A 本地完整星期名稱
    ? %b 本地簡化的月份名稱
    ? %B 本地完整的月份名稱
    ? %c 本地相應的日期表示和時間表示
    ? %j 年內(nèi)的一天(001-366)
    ? %p 本地A.M.或P.M.的等價符
    ? %U 一年中的星期數(shù)(00-53)星期天為星期的開始
    ? %w 星期(0-6),星期天為星期的開始
    ? %W 一年中的星期數(shù)(00-53)星期一為星期的開始
    ? %x 本地相應的日期表示
    ? %X 本地相應的時間表示
    ? %Z 當前時區(qū)的名稱
    ? %% %號本身?
    ?? ?
    9.strptime(...)
    ? strptime(string, format) -> struct_time
    ? 將時間字符串根據(jù)指定的格式化符轉換成數(shù)組形式的時間
    ? 例如:
    ? 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模块详解的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。