Go每日一庫之dateparse處理時間
簡介
不管什么時候,處理時間總是讓人頭疼的一件事情。因?yàn)闀r間格式太多樣化了,再加上時區(qū),夏令時,閏秒這些細(xì)枝末節(jié)處理起來更是困難。所以在程序中,涉及時間的處理我們一般借助于標(biāo)準(zhǔn)庫或第三方提供的時間庫。今天要介紹的dateparse專注于一個很小的時間處理領(lǐng)域——解析日期時間格式的字符串。
快速使用
本文代碼使用 Go Modules。
創(chuàng)建目錄并初始化:
$ mkdir dateparse && cd dateparse $ go mod init github.com/darjun/go-daily-lib/dateparse
安裝dateparse庫:
$ go get -u github.com/araddon/dateparse
使用:
package main import ( "fmt" "log" "github.com/araddon/dateparse" ) func main() { t1, err := dateparse.ParseAny("3/1/2014") if err != nil { log.Fatal(err) } fmt.Println(t1.Format("2006-01-02 15:04:05")) t2, err := dateparse.ParseAny("mm/dd/yyyy") if err != nil { log.Fatal(err) } fmt.Println(t2.Format("2006-01-02 15:04:05")) }
ParseAny()方法接受一個日期時間字符串,解析該字符串,返回time.Time類型的值。如果傳入的字符串dateparse庫無法識別,則返回一個錯誤。上面程序運(yùn)行輸出:
$ go run main.go
2014-03-01 00:00:00
2021/06/24 14:52:39 Could not find format for "mm/dd/yyyy"
exit status 1
需要注意,當(dāng)我們寫出"3/1/2014"這個時間的時候,可以解釋為2014年3月1日,也可以解釋為2014年1月3日。這就存在二義性,dateparse默認(rèn)采用mm/dd/yyyy這種格式,也就是2014年3月1日。我們也可以使用ParseStrict()函數(shù)讓這種具有二義性的字符串解析失敗:
func main() { t, err := dateparse.ParseStrict("3/1/2014") if err != nil { log.Fatal(err) } fmt.Println(t.Format("2006-01-02 15:04:05")) }
運(yùn)行:
$ go run main.go
2021/06/24 14:57:18 This date has ambiguous mm/dd vs dd/mm type format
exit status 1
格式
dateparse支持豐富的日期時間格式,基本囊括了所有常用的格式。它支持標(biāo)準(zhǔn)庫time中預(yù)定義的所有格式:
// src/time/format.go const ( ANSIC = "Mon Jan _2 15:04:05 2006" UnixDate = "Mon Jan _2 15:04:05 MST 2006" RubyDate = "Mon Jan 02 15:04:05 -0700 2006" RFC822 = "02 Jan 06 15:04 MST" RFC822Z = "02 Jan 06 15:04 -0700" // RFC822 with numeric zone RFC850 = "Monday, 02-Jan-06 15:04:05 MST" RFC1123 = "Mon, 02 Jan 2006 15:04:05 MST" RFC1123Z = "Mon, 02 Jan 2006 15:04:05 -0700" // RFC1123 with numeric zone RFC3339 = "2006-01-02T15:04:05Z07:00" RFC3339Nano = "2006-01-02T15:04:05.999999999Z07:00" Kitchen = "3:04PM" // Handy time stamps. Stamp = "Jan _2 15:04:05" StampMilli = "Jan _2 15:04:05.000" StampMicro = "Jan _2 15:04:05.000000" StampNano = "Jan _2 15:04:05.000000000" )
支持的完整格式查看dateparse README。
時區(qū)
dateparse支持在特定時區(qū)解析日期時間字符串。我們可以通過調(diào)用標(biāo)準(zhǔn)庫的time.LoadLocation()方法,傳入時區(qū)標(biāo)識字符串來獲得時區(qū)對象。時區(qū)標(biāo)識字符串是類似Asia/Shanghai,America/Chicago這樣的格式,它表示一個具體的時區(qū),前者上海,后者洛杉磯。調(diào)用dateparse.ParseIn()方法傳入時區(qū)對象,在指定時區(qū)中解析。time包中還預(yù)定義了兩個時區(qū)對象,time.Local表示本地時區(qū),time.UTC表示 UTC 時區(qū)。時區(qū)的權(quán)威數(shù)據(jù)請看IANA。
func main() { tz1, _ := time.LoadLocation("America/Chicago") t1, _ := dateparse.ParseIn("2021-06-24 15:50:30", tz1) fmt.Println(t1.Local().Format("2006-01-02 15:04:05")) t2, _ := dateparse.ParseIn("2021-06-24 15:50:30", time.Local) fmt.Println(t2.Local().Format("2006-01-02 15:04:05")) }
運(yùn)行:
$ go run main.go
2021-06-25 04:50:30
2021-06-24 15:50:30
美國洛杉磯時區(qū)的"2021年6月24日 15時30分30秒"等于本地時區(qū)(北京時間)的"2021年6月25日 04時50分30秒"。
cli
dateparse還提供了一個命令行工具,用于極快地查看日期時間格式。安裝:
$ go install github.com/araddon/dateparse/dateparse
默認(rèn)會安裝在$GOPATH路徑下,我習(xí)慣上把$GOPATH/bin放到$PATH中。所以dateparse命令可以直接使用。
dateparse命令接收一個字符串,和一個可選的時區(qū)選項(xiàng):
$ dateparse --timezone="Asia/Shanghai" "2021-06-24 06:46:08" Your Current time.Local zone is CST Layout String: dateparse.ParseFormat() => 2006-01-02 15:04:05 Your Using time.Local set to location=Asia/Shanghai CST +-------------+---------------------------+-------------------------------+-------------------------------------+ | method | Zone Source | Parsed | Parsed: t.In(time.UTC) | +-------------+---------------------------+-------------------------------+-------------------------------------+ | ParseAny | time.Local = nil | 2021-06-24 06:46:08 +0000 UTC | 2021-06-24 06:46:08 +0000 UTC day=4 | | ParseAny | time.Local = timezone arg | 2021-06-24 06:46:08 +0000 UTC | 2021-06-24 06:46:08 +0000 UTC day=4 | | ParseAny | time.Local = time.UTC | 2021-06-24 06:46:08 +0000 UTC | 2021-06-24 06:46:08 +0000 UTC day=4 | | ParseIn | time.Local = nil | 2021-06-24 06:46:08 +0000 UTC | 2021-06-24 06:46:08 +0000 UTC | | ParseIn | time.Local = timezone arg | 2021-06-24 06:46:08 +0800 CST | 2021-06-23 22:46:08 +0000 UTC | | ParseIn | time.Local = time.UTC | 2021-06-24 06:46:08 +0000 UTC | 2021-06-24 06:46:08 +0000 UTC | | ParseLocal | time.Local = nil | 2021-06-24 06:46:08 +0000 UTC | 2021-06-24 06:46:08 +0000 UTC | | ParseLocal | time.Local = timezone arg | 2021-06-24 06:46:08 +0800 CST | 2021-06-23 22:46:08 +0000 UTC | | ParseLocal | time.Local = time.UTC | 2021-06-24 06:46:08 +0000 UTC | 2021-06-24 06:46:08 +0000 UTC | | ParseStrict | time.Local = nil | 2021-06-24 06:46:08 +0000 UTC | 2021-06-24 06:46:08 +0000 UTC | | ParseStrict | time.Local = timezone arg | 2021-06-24 06:46:08 +0000 UTC | 2021-06-24 06:46:08 +0000 UTC | | ParseStrict | time.Local = time.UTC | 2021-06-24 06:46:08 +0000 UTC | 2021-06-24 06:46:08 +0000 UTC | +-------------+---------------------------+-------------------------------+-------------------------------------+
輸出當(dāng)前本地時區(qū),格式字符串(可用于生成同樣格式的日期時間字符串)和一個表格。表格里面的數(shù)據(jù)是分別對ParseAny/ParseIn/ParseLocal/ParseStrict在不同的時區(qū)下調(diào)用的結(jié)果。
method列表示調(diào)用的方法,Zone Source列表示將本地時區(qū)設(shè)置的值,Parsed列是以日期時間字符串調(diào)用ParseAny()返回的time.Time對象的Format()方法調(diào)用結(jié)果,Parsed: t.In(time.UTC)列在返回的time.Time對象調(diào)用Format()方法前將其轉(zhuǎn)為 UTC 時間。
由于ParseAny/ParseStrict不會考慮本地時區(qū),都是在 UTC 下解析字符串,所以這 6 行的最后兩列結(jié)果都一樣。
ParseIn的第二行,將time.Local設(shè)置為我們通過命令行選項(xiàng)設(shè)置的時區(qū),上面我設(shè)置為Asia/Shanghai,對應(yīng)的 UTC 時間相差 8 小時。ParseLocal也是如此。
下面是dateparse命令行的部分源碼,可以對照查看:
func main() { parsers := map[string]parser{ "ParseAny": parseAny, "ParseIn": parseIn, "ParseLocal": parseLocal, "ParseStrict": parseStrict, } for name, parser := range parsers { time.Local = nil table.AddRow(name, "time.Local = nil", parser(datestr, nil, false), parser(datestr, nil, true)) if timezone != "" { time.Local = loc table.AddRow(name, "time.Local = timezone arg", parser(datestr, loc, false), parser(datestr, loc, true)) } time.Local = time.UTC table.AddRow(name, "time.Local = time.UTC", parser(datestr, time.UTC, false), parser(datestr, time.UTC, true)) } } func parseIn(datestr string, loc *time.Location, utc bool) string { t, err := dateparse.ParseIn(datestr, loc) if err != nil { return err.Error() } if utc { return t.In(time.UTC).String() } return t.String() }
注意輸出的本地時區(qū)為 CST,它可以代表不同的時區(qū):
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
CST 可以同時表示美國、澳大利亞、中國和古巴四個國家的標(biāo)準(zhǔn)時間。
總結(jié)
使用dateparse可以很方便地從日期時間字符串中解析出時間對象和格式(layout)。同時dateparse命令行可以快速的查看和轉(zhuǎn)換相應(yīng)時區(qū)的時間,是一個非常不錯的小工具。
大家如果發(fā)現(xiàn)好玩、好用的 Go 語言庫,歡迎到 Go 每日一庫 GitHub 上提交 issue😄
參考
dateparse GitHub:https://github.com/araddon/dateparse
Go 每日一庫 GitHub:https://github.com/darjun/go-daily-lib
到此這篇關(guān)于Go每日一庫之dateparse處理時間的文章就介紹到這了,更多相關(guān)Go dateparse處理時間內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Go 自定義package包設(shè)置與導(dǎo)入操作
這篇文章主要介紹了Go 自定義package包設(shè)置與導(dǎo)入操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-05-05golang 實(shí)現(xiàn)struct、json、map互相轉(zhuǎn)化
這篇文章主要介紹了golang 實(shí)現(xiàn)struct、json、map互相轉(zhuǎn)化,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-12-12