python的時間處理-time模組

來源:互聯網
上載者:User

標籤:spec   使用   module   方法   you   ecif   sdn   部落格   form   

time模組

時間的表示方法有三種:

  • 時間戳記:表示的是從1970年1月1日0點至今的秒數
  • 格式化字串表示:這種表示更習慣我們通常的讀法,如2018-04-24 00:00:00
  • 格式化元祖表示:是一個具有九個元素的元祖
時間戳記與格式元組的互相轉換
import time   #匯入time模組d = time.time()  # 顯示當前的時間戳記dOut[16]: 1524570062.944023time.localtime(d)    #把時間戳記轉換為含9個元素的元組,轉換為當地時間Out[17]: time.struct_time(tm_year=2018, tm_mon=4, tm_mday=24, tm_hour=19, tm_min=41, tm_sec=2, tm_wday=1, tm_yday=114, tm_isdst=0)time.gmtime(d)     #把時間戳記轉換為含9個元素的元組,轉換為UTC時間Out[18]: time.struct_time(tm_year=2018, tm_mon=4, tm_mday=24, tm_hour=11, tm_min=41, tm_sec=2, tm_wday=1, tm_yday=114, tm_isdst=0)ld = time.localtime(d)  #ld.tm_year    #轉換的元組,可以根據需要單獨計算對應的時間Out[20]: 2018time.mktime(ld)  # 把元組在轉換回時間戳記Out[21]: 1524570062.0

備忘:UTC時間為格林威治時間,比東八區晚8個小時!

 

時間戳記與格式化字串的轉換:

時間戳記-------->格式化元組---------->格式化字串

help(time.strftime)Help on built-in function strftime in module time:strftime(...)    strftime(format[, tuple]) -> string   #函數的格式使用        Convert a time tuple to a string according to a format specification.    See the library reference manual for formatting codes. When the time tuple    is not present, current time as returned by localtime() is used.        Commonly used format codes:        %Y  Year with century as a decimal number.     %m  Month as a decimal number [01,12].    %d  Day of the month as a decimal number [01,31].    %H  Hour (24-hour clock) as a decimal number [00,23].    %M  Minute as a decimal number [00,59].    %S  Second as a decimal number [00,61].    %z  Time zone offset from UTC.    %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.    %I  Hour (12-hour clock) as a decimal number [01,12].    %p  Locale‘s equivalent of either AM or PM.        Other codes may be available on your platform.  See documentation for    the C library strftime function.
ld   #格式化元組的資料Out[22]: time.struct_time(tm_year=2018, tm_mon=4, tm_mday=24, tm_hour=19, tm_min=41, tm_sec=2, tm_wday=1, tm_yday=114, tm_isdst=0)time.strftime("%Y-%m-%d %H:%M:%S", ld)Out[25]: ‘2018-04-24 19:41:02‘time.strftime("%Y-%m-%d %X", ld)Out[26]: ‘2018-04-24 19:41:02‘time.strftime("%Y-%m-%d %X %p", ld) #時間戳記轉換為格式化元組Out[27]: ‘2018-04-24 19:41:02 PM‘#字串轉換為格式化元組,注意格式對應time.strptime(‘2018-04-24 19:41:02 PM‘,"%Y-%m-%d %H:%M:%S %p")Out[32]: time.struct_time(tm_year=2018, tm_mon=4, tm_mday=24, tm_hour=19, tm_min=41, tm_sec=2, tm_wday=1, tm_yday=114, tm_isdst=-1)
根據時間戳記轉換為字元格式設定
help(time.ctime)Help on built-in function ctime in module time:ctime(...)    ctime(seconds) -> string        Convert a time in seconds since the Epoch to a string in local time.    This is equivalent to asctime(localtime(seconds)). When the time tuple is    not present, current time as returned by localtime() is used.time.ctime(time.time())Out[34]: ‘Tue Apr 24 20:02:46 2018‘
根據struct_time格式的元群組轉換為字串
help(time.asctime)Help on built-in function asctime in module time:asctime(...)    asctime([tuple]) -> string        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.time.asctime(ld)Out[36]: ‘Tue Apr 24 19:41:02 2018‘
datetime模組

dateimte模組對time模組進行了封裝,提供了更多功能的介面:

在csdn上看到一個部落格,講的蠻詳細的,博主禁止轉載,因此附上連結。datetime模組

 

python的時間處理-time模組

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.