在日常的代碼中,咱們經常須要與時間打交道。在Python中,與時間處理有關的模塊就包括:time,datetime,calendar(不多用,不作介紹)。python
咱們寫程序時對時間的處理能夠歸爲如下3種:git
一、時間的顯示:在屏幕顯示,記錄日誌等。app
二、時間的轉換:好比把字符串格式的日期轉換成Python中的日期類型。ide
三、時間的運算:計算兩個日期間的差值等。ui
在Python中,一般有如下3種方式來表示時間:線程
一、時間戳(timestamp),表示的是從1970年1月1日00:00:00開始按秒計算的偏移量。例如:1554864776.161901日誌
二、格式化的時間字符串,例如:2019-10-03 17:54code
三、元祖(struct_time)共九個元素。因爲Python的time模塊實現主要調用C庫,因此各個平臺可能有所不一樣,mac上:time.struct_time(tm_year=2019, tm_mon=10, tm_mday=3, tm_hour=18, tm_sec=15, tm_wday=4, tm_yday=100,tm_isdst=0)
orm
索引(Index) 屬性(Attribute) 值(Values) 0 tm_year(年) 好比2011 1 tm_mon(月) 1 - 12 2 tm_mday(日) 1 - 31 3 tm_hour(時) 0 - 23 4 tm_min(分) 0 - 59 5 tm_sec(秒) 0 - 61 6 tm_wday(weekday) 0 - 6(0表示週日) 7 tm_yday(一年中的第幾天) 1 - 366 8 tm_isdst(是不是夏令時) 默認爲-1
UTC時間blog
UTC(Coordinated Universal Time,世界協調時)亦即格林威治天文時間,世界標準時間。在中國爲UTC+8,又稱東8區。DST(Daylight Saving Time)即夏令時。
time模塊的方法
time.localtime([secs]):將一個時間戳轉換爲當前時區的struct_time。若secs參數未提供,則以當前時間爲準。 time.gmtime([secs]):和localtime()方法相似,gmtime()方法是將一個時間戳轉換爲UTC時區(0時區)的struct_time。 time.time():返回當前時間的時間戳。 time.mktime(t):將一個struct_time轉化爲時間戳。 time.sleep(secs):線程推遲指定的時間運行,單位爲秒。 time.asctime([t]):把一個表示時間的元組或者struct_time表示爲這種形式:’Sun Oct 1 12:04:38 2019’。若是沒有參數,將會將time.localtime()做爲參數傳入。 time.ctime([secs]):把一個時間戳(按秒計算的浮點數)轉化爲time.asctime()的形式。若是參數未給或者爲None的時候,將會默認time.time()爲參數。它的做用至關於time.asctime(time.localtime(secs))。 time.strftime(format[, t]):把一個表明時間的元組或者struct_time(如由time.localtime()和time.gmtime()返回)轉化爲格式化的時間字符串。若是t未指定,將傳入time.localtime()。 舉例:time.strftime(「%Y-%m-%d %X」, time.localtime()) #輸出’2017-10-01 12:14:23’ time.strptime(string[, format]):把一個格式化時間字符串轉化爲struct_time。實際上它和strftime()是逆操做。 舉例:time.strptime(‘2017-10-3 17:54’,」%Y-%m-%d %H:%M」) #輸出 time.struct_time(tm_year=2017, tm_mon=10, tm_mday=3, tm_hour=17, tm_min=54, tm_sec=0, tm_wday=1, tm_yday=276, tm_isdst=-1)
字符串轉時間格式對應表:
Meaning | |
---|---|
%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. |
%S |
Second as a decimal number [00,61]. |
%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. |
%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. |
%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 offset indicating a positive or negative time difference from UTC/GMT of the form +HHMM or -HHMM, where H represents decimal hour digits and M represents decimal minute digits [-23:59, +23:59]. |
%Z |
Time zone name (no characters if no time zone exists). |
最後爲了容易記住轉換關係,請看下圖:
相比於time模塊,datetime模塊的接口則更直觀、更容易調用。
datetime模塊定義瞭如下的類:
咱們須要記住的方法僅如下幾個:
一、d=datetime.datetime.now()
返回當前的datetime日期類型。
d.timestamp(),d.today(), d.year,d.timetuple()等方法能夠調用
二、時間運算
>>> datetime.datetime.now() datetime.datetime(2017, 10, 1, 12, 53, 11, 821218) >>> datetime.datetime.now() + datetime.timedelta(4) #當前時間 +4天 datetime.datetime(2017, 10, 5, 12, 53, 35, 276589) >>> datetime.datetime.now() + datetime.timedelta(hours=4) #當前時間+4小時 datetime.datetime(2017, 10, 1, 16, 53, 42, 876275)
三、時間替換
>>> d.replace(year=2999,month=11,day=30) datetime.date(2999, 11, 30)