python中当地时间_R 与 Python 中与时间相关内容的梳理
約萬字長文預警,如果你沒時間,就不用看了,這就是一個梳理的文章,方便我后來找資料。
因為工作的關系,近期需要梳理一些 Python 的知識(可能有小伙伴知道了,LI-6800 搞了個大動作,支持 Python 編程了),首先要認真系統的梳理的是關于時間和日期的處理,但本著一次也是做工作,兩次也是做工作,反正都同樣是知識,二者也是密切相關,所幸把 R 相關的知識也一并梳理了。
時間日期的基礎知識
先把經常用到的一些時間的概念進行一下整理。因為不用不知道,一用嚇一跳,時間的標準、名詞、定義還真不少。
Gregorian calendar
格里高里歷,這個名字不熟悉,實際上就是標準的公立。
ISO 時間
對于我國人民來講,ISO 8601 這個標準雖然沒怎么聽過,但是實際我們一直在用,這就是我國使用的時間日期所采取的格式。年月日和時間的格式我們不再贅述,它可以作為下面項目的標準用法:
?日期?時間?世界調整時間(UTC)?與 UTC 有補償的當期時間(如夏令時)?時間和日期?時間間隔?循環的時間間隔
UT (世界時)
以下是百度百科內容
世界時UT即格林尼治平太陽時間,是指格林尼治所在地的標準時間,也是表示地球自轉速率的一種形式。以地球自轉為基礎的時間計量系統。地球自轉的角度可用地方子午線相對于地球上的基本參考點的運動來度量。為了測量地球自轉,人們在地球上選取了兩個基本參考點:春分點(見分至點)和平太陽點,由此確定的時間分別稱為恒星時和平太陽時。
UTC 時間
UTC 是一個時間標準,以下是百度百科內容
國際原子時的準確度為每日數納秒,而世界時的準確度為每日數毫秒。許多應用部門要求時間系統接近世界時UT,對于這種情況,一種稱為協調世界時的折衷時標于1972年面世。為確保協調世界時與世界時相差不會超過0.9秒,在有需要的情況下會在協調世界時內加上正或負閏秒。因此協調世界時與國際原子時之間會出現若干整數秒的差別,兩者之差逐年積累,便采用跳秒(閏秒)的方法使協調時與世界時的時刻相接近,其差不超過1s。它既保持時間尺度的均勻性,又能近似地反映地球自轉的變化。中國大陸、中國香港、中國澳門、中國臺灣、蒙古國、新加坡、馬來西亞、菲律賓、西澳大利亞州的時間與UTC的時差均為+8,也就是UTC+8。
也就是他的確定有兩大核心要素:
?International Atomic Time (TAI):簡單了講就是用 400 個高精原子時鐘的輸出結果,作為我們使用時鐘追蹤的標準。?Universal Time (UT): 指的是地球自轉一周的時間。
unix time
unix 時間又被成為 Epoch 時間,POSIX 時間,特征十分明顯,記錄自1970年1月1日起已經過多少秒 00:00:00 的協調世界時(UTC),減去閏秒(“閏秒,是指為保持協調世界時接近于世界時時刻,由國際計量局統一規定在年底或年中(也可能在季末)對協調世界時增加或減少1秒的調整。)。
每天被視為完全包含86 400秒,所以從紀元開始減去閏秒。所以我們看到 LI-6800 有一列時間很奇怪,現在看來那沒什么好奇怪的,因為距離 1970 年,換算成秒已經很久遠了,在手冊里也寫到了,這是 Epoch time,因為 LI-6800 基于 BSD。
至于為什么會用這么奇怪的時間格式,根本原因就是這玩意對計算機太友好了,不用為了不同的時間格式轉換來轉換去,匹配了才能計算時間,雖然對人類不太友好。但如果我們知道是 unix 時間,那么很多軟件轉換它也就是一行代碼而已。
夏令時
我直接復制百度百科的內容:
夏令時,表示為了節約能源,人為規定時間的意思。也叫夏時制,夏時令(Daylight Saving Time:DST),又稱“日光節約時制”和“夏令時間”,在這一制度實行期間所采用的統一時間稱為“夏令時間”。一般在天亮早的夏季人為將時間調快一小時,可以使人早起早睡,減少照明量,以充分利用光照資源,從而節約照明用電。
我覺得這就是自欺欺人的玩意,你想節約能源,讓人早睡早起的話,直接下令早起一小時就好了,何必非得折騰時間呢?反正都是早睡早起,哪有這么多切換時間的麻煩,還好我國已經廢止了。現在美帝還有這些事,我們應該沒機會處理這堆爛事。
GMT 時間
GMT 也就是我們所熟悉的格林威治時間,在 1972 年以前,它是一個時間標準,在那之后,他就僅僅指它所在的地方的時區。所以我們應該幾乎不會再處理這種格式的數據了。
時區
這看上去是最好理解的概念,但實際上又不然,一幅圖可以展示所有時區:
但我覺得需要注意的有下面幾點:
1.當地時間與 UTC 有差異,比如我們常見到的我們這邊的時間 UTC+8,因為我們在東八區嘛。2.如果嚴格的按照經度 15° 來算,應為 24 個時區,但是人類世界從來不會這么簡單的遵循科學的規律,我們要考慮政治邊界等因素,所以,數量要遠高于 24.3.另一個是夏令時,我覺得很虛偽的做法,遠不如行政命令,簡化時間算法來的直接。4.另一個是所在地區的時間未必與時區相同,這個也是無解的。例如,新疆應該屬于東六區,但就是習慣用東八區計時。
CST
CST 是很常見的一個縮寫,以下抄襲自百度百科:
CST可以為如下4個不同的時區的縮寫:
?美國中部時間:Central Standard Time (USA) UT-6:00?澳大利亞中部時間:Central Standard Time (Australia) UT+9:30?中國標準時間:China Standard Time UT+8:00?古巴標準時間:Cuba Standard Time UT-4:00
我們說的自然是指? UTC+8 的時間。
R 日期與時間
從這里進入正式的處理,這里梳理的目的不是做到面面俱到,只是期望有一個思路,有問題也方便查找。
日期
日期最常用的轉換就是文字轉換為日期,有幾種方法
as.Date
作為 base R 的基本函數,as.Date?的作用就是將字符轉換為日期類。對于儀器類的時間日期,它其中非常有用的參數是?format,但它又牽扯到?strptime,我們通過例子查看:
# 前三行后面解釋lct Sys.getlocale("LC_TIME")Sys.setlocale("LC_TIME", lct)Sys.setlocale("LC_TIME", "C")# 這類儀器的時間日期還能接受x "1jan1960", "2jan1960", "31mar1960", "30jul1960")z as.Date(x, "%d%b%Y")z# 這類儀器的廠家簡直就沒有國際視野x "02/27/19", "02/27/19", "01/14/19", "02/28/19", "02/01/19")z as.Date(x, "%m/%d/%y")z# 這不管公司大小,至少是有估計視野的# 如果需要日期計算,我們就將字符轉換文字就行y "2019-12-31", "2020-1-1", "2020-1-2")y## [1] "Chinese (Simplified)_China.936"## [1] "C"## [1] "1960-01-01" "1960-01-02" "1960-03-31" "1960-07-30"## [1] "2019-02-27" "2019-02-27" "2019-01-14" "2019-02-28" "2019-02-01"## [1] "2019-12-31" "2020-1-1" "2020-1-2"這個代碼本身非常簡單,除了下面要解釋的兩點(其實本質上就是?strptime?一點):
?format?參數中這些帶 % 號的字符來自于?POSIX?標準,在 R 和 Python內都是一樣的符號,即使用 % + 字母,來規定時間與日期的格式,有需要查閱??strptime
?在?Sys.setlocale?中使用 "C",以及 "LC_TIME",幫助文檔有解釋:
Locale-specific conversions to and from character strings are used where appropriate and available. This affects the names of the days and months, the AM/PM indicator (if used) and the separators in output formats such as %x and %X, via the setting of the LC_TIME locale category. The ‘current locale’ of the descriptions might mean the locale in use at the start of the R session or when these functions are first used. (For input, the locale-specific conversions can be changed by calling Sys.setlocale with category LC_TIME (or LC_ALL). For output, what happens depends on the OS but usually works.)
也就是說我們使用格式帶有某些字符,如上面的 Jan 等,以及與系統的區域語言設置相關的輸出格式,可以通過使用設置 LC_TIME,輸出的結果依賴于我們系統的設置。
有點拗口,直接的從結果上來將會比較容易,那就是代碼命名沒問題,結果輸出的結果是 NA,那就要考慮是不是沒有按照上面的區域時間日期的設置來更改。這是系統之間和不同地區之間最令人頭疼的區域語言設置問題。
另外設置為 C 則是關閉特定區域的順序,幫助文檔里也有:
Sys.setlocale("LC_TIME", "C") # turn off locale-specific sorting, # usually (but not on all platforms)所有的區域語言設置,目的均是為實現我們上面代碼輸出的不是 NA,如果不信邪,國內多數人用的電腦應該前兩個例子的代碼都為 NA。至于標準的 ISO 8601 數據,至少對我們區域語言設置選擇中文的 Windows 電腦是非常友好的。
日期的差
在實際應用過程中,時間的差值計算非常多,在 R base 中也有很多方法計算:
例如,你的小學數學沒學好,又想學偶像劇搞浪漫,要告訴女友在你們相戀的第 n 天的感受,那么你可以這樣做:
Sys.Date() - as.Date("2009-01-18")## Time difference of 4101 days更浪漫的精確到 s:
difftime(Sys.time(), strptime("2009-01-18 15:20:22", "%Y-%m-%d %H:%M:%S"), units = "secs")## Time difference of 354312449 secs當然,第二條要求你要有驚人的記憶力,或者一個好筆頭,如果連天都精確不到,這說明你沒什么求生欲,我也拯救不了你。
某個時間點以后第 n 天是哪一天:
as.Date(329, origin = "2019-12-01")## [1] "2020-10-25"日期與時間
上面的內容是日期,相比來說,只處理日期的應用情形比較少,因為我們的儀器的采集頻率都非常高,一般都是高于 1 Hz 的,所以多數據情況下我們都要日期和時間一起處理,下面我們看一下日期與時間(當然為使得內容不是那么的無聊,日期上我們稍微加了一個時間的內容,實際上應該讓單位?units = days?才符合上一節的標題)。
日期與時間處理的軟件包
R base 中的?POSIXt,chron,lubridate?都是很多人常用的工具,data.table?中的?IDateTime?主要偏重于速度,而且處于實驗版本,不做介紹。
POSIXt
POSIXt 是 R 的一個類,又分為 "POSIXlt" 和 "POSIXct"。與此相關的函數有:
?as.POSIXct?as.POSIXlt?strptime
這三個函數有令人很容易迷惑混淆的地方,但是 stackoverflow 總是個能讓人答疑解惑的地方:
First, there are two internal implementations of date/time: POSIXct, which stores seconds since UNIX epoch (+some other data), and POSIXlt, which stores a list of day, month, year, hour, minute, second, etc.
strptime?is a function to directly convert character vectors (of a variety of formats) to POSIXlt format.
as.POSIXlt?converts a variety of data types to POSIXlt. It tries to be intelligent and do the sensible thing - in the case of character, it acts as a wrapper to strptime.
as.POSIXct?converts a variety of data types to POSIXct. It also tries to be intelligent and do the sensible thing - in the case of character, it runs strptime first, then does the conversion from POSIXlt to POSIXct.
簡單的說就是?as.POSIXlt?中 lt 指的是 local time,存儲的是我們年月日星期等的屬性,as.POSIXct?中 ct 指的是 calendar time,存儲的是自 1970 年 1 月 1 日 0 時開始的秒數。
查看其屬性我們就能直觀的看到他們的差別:
x as.POSIXlt(Sys.time())y as.POSIXct(Sys.time())attributes(x)attributes(y)x$mday#y$mday## $names## [1] "sec" "min" "hour" "mday" "mon" "year" "wday" "yday"
## [9] "isdst" "zone" "gmtoff"
##
## $class
## [1] "POSIXlt" "POSIXt"
##
## $tzone
## [1] "" "CST" "CDT"## $class
## [1] "POSIXct" "POSIXt"
## [1] 11
## [1]?Error in y$mday : $ operator is invalid for atomic vectors
格式的問題
時間與日期格式的多變性是處理很多儀器數據時的麻煩事情,我覺得兩個很重要的函數能幫我們解決大部分問題。
?format
# 24h - 12 h 的轉換as.POSIXlt("2020-03-08 10:11:01 PM", format = "%Y-%m-%d %I:%M:%S %p")# 轉換格式format(as.POSIXlt("08/04/06 22:11:00", format = "%m/%d/%y %H:%M:%S"), "%Y-%m-%d %H:%M:%S")## [1] "2020-03-08 22:11:01 CST"
## [1] "2006-08-04 22:11:00"?strptime
strptime("08/04/06 22:11:00", format = "%m/%d/%y %H:%M:%S")## [1] "2006-08-04 22:11:00 CST"對于我們使用 ISO 8601 的國家來講,默認的輸出不用更改就是我們喜歡的格式。
DST
夏令時是我討厭的東西,在我國的儀器也沒這玩意,所以不討論。我沒查怎么處理,我覺得最簡單的方法就是把時間加 1 h 就可以了。
時區
時區問題在我們國內基本也不會遇到,因為我們都適用一個時區的時間,但如果使用 gps 時鐘,遇到了時區錯誤的問題,也可以通過指定 tz 方便的解決,例如,我們在 UTC +8,但是 GPS 時鐘把我們搞成了 UTC+3,我們可以直接加上 5 個小時就可以修正這個錯誤了。
df "2020-3-8 18:12:12", "2020-3-8 18:13:12", "2020-3-8 18:14:12"))df$tz3 as.POSIXct(df$tz3)df$tz8 5*60*60df$tz8## [1] "2020-03-08 23:12:12 CST" "2020-03-08 23:13:12 CST"## [3] "2020-03-08 23:14:12 CST"
lubridate
lubridate?最大的優勢是使得時間處理起來更容易了,函數名更容易記住:
例如將字符轉為時間日期不需要指定格式:
library(lubridate)ymd("20101215")mdy("4/1/17")ymd_hms("20200314 17:01:05")## [1] "2010-12-15"## [1] "2017-04-01"## [1] "2020-03-14 17:01:05 UTC"上文提到的時間錯誤很容易直接更改時區解決,
df "2020-3-8 18:12:12", "2020-3-8 18:13:12", "2020-3-8 18:14:12"))df$tz3 "Antarctica/Syowa")df$tz3df$tz8 "Asia/Shanghai")df$tz8## [1] "2020-03-08 18:12:12 +03" "2020-03-08 18:13:12 +03"## [3] "2020-03-08 18:14:12 +03"## [1] "2020-03-08 23:12:12 CST" "2020-03-08 23:13:12 CST"
## [3] "2020-03-08 23:14:12 CST"
當然,如果你不知道哪是 UTC +3,哪是 UTC +8,有一個很好的網站 (https://timezonedb.com/time-zones)[https://timezonedb.com/time-zones]。更多用法參考 (R for Data Science)[https://r4ds.had.co.nz/dates-and-times.html] 時間日期的章節,不贅述
chron
chron?是 chronological 的縮寫,顧名思義是按時間發生順序排列的。
chron?函數
用于產生時間序列的對象:
library(chron)dts "2020/02/20", "2020/02/21", "2020/03/1","2020/03/2", "2020/03/10"), format="y/m/d")dtstms "23:03:20", "22:29:56", "01:03:30","18:21:03", "16:56:26"))tmsx xattributes(x)## [1] 02/20/20 02/21/20 03/01/20 03/02/20 03/10/20## [1] 23:03:20 22:29:56 01:03:30 18:21:03 16:56:26## [1] (02/20/20 23:03:20) (02/21/20 22:29:56) (03/01/20 01:03:30)## [4] (03/02/20 18:21:03) (03/10/20 16:56:26)## $format
## dates times
## "m/d/y" "h:m:s"
##
## $origin
## month day year
## 1 1 1970
##
## $class
## [1] "chron" "dates" "times"
生成了一個時間序列,不過日期的格式不那么讓人喜歡。不過他支持時間直接從 day 加減:
dts[1]+2dts[1]+33dts[1]-99diff(x)## [1] 02/22/20## [1] 03/24/20## [1] 11/13/19## Time in days:## [1] 0.9768056 8.1066435 1.7205208 7.9412384
結果也是以天來計算,有時候很方便。還可以直接按給定的標簽返回日期的歸屬:
cut(dates("02/01/20") + 18:25, "weeks")dts "02/01/20")+ seq(5, 105, 15)dtscut(dts, "months")## [1] week 1 week 1 week 1 week 1 week 1 week 2 week 2 week 2## Levels: week 1 < week 2## [1] 02/06/20 02/21/20 03/07/20 03/22/20 04/06/20 04/21/20 05/06/20## [1] Feb 20 Feb 20 Mar 20 Mar 20 Apr 20 Apr 20 May 20
## Levels: Feb 20 < Mar 20 < Apr 20 < May 20
總而言之,恕我愚鈍,我感覺這個包沒太大用處。
python 時間日期
Python 的官方文檔寫的很好,歸納了掌握 Python 時間日期處理的四大殺器:datetime, calendar, time, dateutil。我們從牽扯到最開始基本知識的?time?開始,它提供了一系列的與時間處理相關的函數,然后簡單介紹一下?calendar,著重介紹一下?datetime?和?dateutil,因為后兩者才是我們頻繁要用的,前面兩個也只是后面的基礎。另外,據說?pytz?處理時區要比?datetime?自帶的好用多了。
time
time?比較特殊,有些函數是分系統的,并不是所有函數都一定在電腦上有效,他們的官方文檔里會專門標注:avalability:unix 的字樣,以提醒我們 win 下用不了。簡單看一下幾個常用的函數:
?ctime?將 s 轉換為英語國家喜歡的時間日期(還是 1970.1.1 為基準),如果不加參數返回當前時間:
from time import*ctime(888888)ctime()## 'Sun Jan 11 14:54:48 1970'## 'Sat Apr 11 11:27:52 2020'?gmtime?將自基準日期的 s 轉換為 UTC 時間。
gmtime(888888)## time.struct_time(tm_year=1970, tm_mon=1, tm_mday=11, tm_hour=6, tm_min=54, tm_sec=48, tm_wday=6, tm_yday=11, tm_isdst=0)?當然,如果格式不滿意,time?也有?strptime strftime,和 R 一樣都遵循相同的標準,這里摘抄一下官方文檔,就不再制作表格了:
%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. | (1) |
%S | Second as a decimal number [00,61]. | (2) |
%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. | (3) |
%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. | (3) |
%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). | |
%% | A literal?‘%’?character. |
通過上面的例子我們已經可以看出,time?所謂的 UTC 格式并不是那么友好,他們屬于?struct_time?類, 雖然我們能猜出其中的含義來,但是這里也列一下,算作備忘:
0 | tm_year | (for example, 1993) |
1 | tm_mon | range [1, 12] |
2 | tm_mday | range [1, 31] |
3 | tm_hour | range [0, 23] |
4 | tm_min | range [0, 59] |
5 | tm_sec | range [0, 61]; see?(2)?in?strftime()?description |
6 | tm_wday | range [0, 6], Monday is 0 |
7 | tm_yday | range [1, 366] |
8 | tm_isdst | 0, 1 or -1; see below |
N/A | tm_zone | abbreviation of timezone name |
N/A | tm_gmtoff | offset east of UTC in seconds |
也有時區相關設置,我們放在后面的內容內。
calendar
calendar?提供給我們與日歷相關的模塊,例如星期,默認是從周一開始作為,周日作為結束,當然可以使用?setfirstweekday()?來修改。我覺得和我們儀器數據的處理相關不大,我們簡單看一下其中的一個函數,就明白了,它處理的內容,一般儀器都不這么顯示:
from calendar import day_namefor i in day_name:print("today is ", i)## today is Monday## today is Tuesday
## today is Wednesday
## today is Thursday
## today is Friday
## today is Saturday
## today is Sunday
datetime
這應該是 Python 中使用最廣泛的時間日期處理了,雖然感覺它處理的的方式有些別扭,但還是包含了我們所需要的常用的功能,datetime?有兩個名字挺奇怪的對象,按照我的理解(當然不一定對,我看官方文檔解釋的挺繁瑣的):
?aware,該類包含的信息很全,包括算法上的和政治時間上的調整,例如時區和 DST。也就是說該類對象能夠這么應用:我可以通過該類對象包含的時區,換算為其他的時區的時間,也能夠換算稱準確的 unix 時間的秒。?naive,也就是說沒有包含相關的時間信息,僅僅是一個時間。類似于 1 后面可以是 mm,m 或者 km,完全取決于使用者。如果不牽扯到時間轉換,使用該類應該是非常方便的。
我們用簡單的例子來看一下它的基本用法:
from datetime import datetime# 將字符轉換為日期并提取信息str_today = "2020-3-21"today = datetime.strptime(str_today, "%Y-%m-%d")today.monthtoday.day## 3## 21對我們來講,提取月和日用到的不多,但是將 unix 時間和正常時間的互換用到的地方就多了。
# 當前時間日期now = datetime.now()datetime.timestamp(now)dt_stamp = 1234354353.1999datetime.fromtimestamp(dt_stamp)## 1586575672.42083## datetime.datetime(2009, 2, 11, 20, 12, 33, 199900)需要經常計算的時間差,例如上文時區給搞錯的例子,我們可以使用 timedelta 搞定:
from datetime import timedeltautc3_time = datetime.strptime("2020-3-21 9:10:30", "%Y-%m-%d %H:%M:%S")utc8_time = utc3_time + timedelta(hours = 5)utc8_time## datetime.datetime(2020, 3, 21, 14, 10, 30)當然?python?也可以浪漫的對紀念日直接計算
date_from = datetime(2009, 1, 18, 15, 20, 22)now = datetime.now()now - date_from## datetime.timedelta(4100, 72450, 475784)如果非要處理時區,可以使用?pytz(這不是我說的)。我們用它來添加更改時區:
from pytz import timezoneutc8 = timezone("Asia/Shanghai")bj_time = utc8.localize(datetime(2020, 3, 21, 20, 45, 10))bj_time# 轉換為美國東部時間east_us = timezone("US/Eastern")bj_time.astimezone(east_us)## datetime.datetime(2020, 3, 21, 20, 45, 10, tzinfo=)dateutil
dateutil?將自己定義為?datetime?的擴展,特別需要注意的是,他的安裝為
pip install python-dateutil# 或conda install python-dateutil而它處理起日期和時間來確實非常方便,我們只介紹比較重要的幾個模塊。
parser
parser?的目的在于直接處理一系列的雜亂的日期格式,而得到我們所需要的格式。也就是簡化我們要操作的識別的步驟:
from dateutil.parser import*from dateutil.tz import*from datetime import*TZOFFSETS = {"CST": +28800}DEFAULT = datetime(2020, 4, 10)parse("Thu Apr 10 10:36:28 CST 2020", tzinfos=TZOFFSETS)parse("Thu Apr 10 10:36:28 CST 2020", tzinfos=TZOFFSETS)parse("2020 10:36:28 CST 25 Apr Thu", tzinfos=TZOFFSETS)# 當然可以忽略時區,多數情況下我們也不需要處理時區parse("Thu Apr 10 10:36:28 CST 2020", ignoretz=True)# 如果沒有年,我們可以提供一個默認的選項,這在處理# 只有時間的數據時非常有幫助,或者說我們可以不用合并# 數據里的日期和時間,只用時間列,并給其設定一個默認# 值即可parse("Thu Sep 25 10:36:28", default=DEFAULT)# 對 ISO 格式的解析葉室毫無壓力parse("2019-09-25T10:49:41")parse("2019-09-25T10:49:41.5-03:00")## datetime.datetime(2020, 4, 10, 10, 36, 28, tzinfo=tzoffset('CST', 28800))## datetime.datetime(2020, 4, 10, 10, 36, 28, tzinfo=tzoffset('CST', 28800))## datetime.datetime(2020, 4, 25, 10, 36, 28, tzinfo=tzoffset('CST', 28800))## datetime.datetime(2020, 4, 10, 10, 36, 28)## datetime.datetime(2020, 9, 25, 10, 36, 28)## datetime.datetime(2019, 9, 25, 10, 49, 41)## datetime.datetime(2019, 9, 25, 10, 49, 41, 500000, tzinfo=tzoffset(None, -10800))parser?的形式為:parser.parse(parserinfo=None, **kwargs),上面的例子中使用了?**kwargs?的?tzinfos?選項,可以提供一個時區名或別名,例如上面我們使用的 CST,在我們看來是中國標準時間,具體的時區名縮寫可參考: https://www.timeanddate.com/time/zones/,該參數還可以提供一個時區的 offsets(以秒計量的),這就是我們上面解析的前兩個日期的目的,轉換為標準的時區。
rrule
rrule?是名副其實的時間尺,可以生成一系列不同尺度的時間序列:
from dateutil.rrule import*from dateutil.parser import*from datetime import*import pprintimport syssys.displayhook = pprint.pprint# 從起始時間開始的 3 天的時間list(rrule(DAILY, count=3, dtstart=parse("20200401T090000")))# 有截止日期的時間序列list(rrule(DAILY, dtstart=parse("20200401T090000"), until=parse("20200405T000000")))list(rrule(DAILY, interval=2, count=5, dtstart=parse("20200405T090000")))## [datetime.datetime(2020, 4, 1, 9, 0),## datetime.datetime(2020, 4, 2, 9, 0),
## datetime.datetime(2020, 4, 3, 9, 0)]## [datetime.datetime(2020, 4, 1, 9, 0),
## datetime.datetime(2020, 4, 2, 9, 0),
## datetime.datetime(2020, 4, 3, 9, 0),
## datetime.datetime(2020, 4, 4, 9, 0)]## [datetime.datetime(2020, 4, 5, 9, 0),
## datetime.datetime(2020, 4, 7, 9, 0),
## datetime.datetime(2020, 4, 9, 9, 0),
## datetime.datetime(2020, 4, 11, 9, 0),
## datetime.datetime(2020, 4, 13, 9, 0)]
relativedelta
relativedelta?的設計是用于替代已有的時間日期的特定組分或者表示時間的間隔:
from datetime import*from dateutil.relativedelta import*import calendarnow = datetime.now()today = date.today()nowdatetime(2019, 9, 17, 20, 54, 47, 282310)todaydate(2019, 9, 17)# 解決上文的 utc+3 和 utc+8 的問題now+relativedelta(hours=+5)# 可以指定月,軸,以及小時,注意不是賦值的話必須有正負號today+relativedelta(months=+1, weeks=+1, hour=10)# 可以直接顯示日期差relativedelta(datetime(2009, 1, 18, 16, 0), today)## datetime.datetime(2020, 4, 11, 11, 27, 52, 774166)## datetime.datetime(2019, 9, 17, 20, 54, 47, 282310)## datetime.date(2020, 4, 11)## datetime.date(2019, 9, 17)## datetime.datetime(2020, 4, 11, 16, 27, 52, 774166)## datetime.datetime(2020, 5, 18, 10, 0)## relativedelta(years=-11, months=-2, days=-23, hours=-8)本文屬于整理相關有用的軟件包和基礎信息,并非學習使用,具體用法還請看各個軟件包的幫助文檔。關于 R 和 Python 的時間處理,常用的就是這些內容。
參考資料
https://docs.python.org/3/library/datetime.htmlhttps://dateutil.readthedocs.io/en/latest/Grolemund, Garrett, and Hadley Wickham. 2011. “Dates and Times Made Easy with lubridate.”?Journal of Statistical Software?40 (3): 1–25.?http://www.jstatsoft.org/v40/i03/.James, David, and Kurt Hornik. 2020.?Chron: Chronological Objects Which Can Handle Dates and Times.?https://CRAN.R-project.org/package=chron.R Core Team. 2020.?R: A Language and Environment for Statistical Computing. Vienna, Austria: R Foundation for Statistical Computing.?https://www.R-project.org/.總結
以上是生活随笔為你收集整理的python中当地时间_R 与 Python 中与时间相关内容的梳理的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: idea 导出war包_使用IDEA实现
- 下一篇: python读取多个文件夹图片_pyth