golang中的時間和時區
摘要:
先寫一段測試程式碼:
const TIME_LAYOUT = "2006-01-02 15:04:05"
func parseWithLocation(name string, timeStr string) (time.Time, error) {
locati...
先寫一段測試程式碼:
const TIME_LAYOUT = "2006-01-02 15:04:05" func parseWithLocation(name string, timeStr string) (time.Time, error) { locationName := name if l, err := time.LoadLocation(locationName); err != nil { println(err.Error()) return time.Time{}, err } else { lt, _ := time.ParseInLocation(TIME_LAYOUT, timeStr, l) fmt.Println(locationName, lt) return lt, nil } } func testTime() { fmt.Println("0. now: ", time.Now()) str := "2018-09-10 00:00:00" fmt.Println("1. str: ", str) t, _ := time.Parse(TIME_LAYOUT, str) fmt.Println("2. Parse time: ", t) tStr := t.Format(TIME_LAYOUT) fmt.Println("3. Format time str: ", tStr) name, offset := t.Zone() name2, offset2 := t.Local().Zone() fmt.Printf("4. Zone name: %v, Zone offset: %v\n", name, offset) fmt.Printf("5. Local Zone name: %v, Local Zone offset: %v\n", name2, offset2) tLocal := t.Local() tUTC := t.UTC() fmt.Printf("6. t: %v, Local: %v, UTC: %v\n", t, tLocal, tUTC) fmt.Printf("7. t: %v, Local: %v, UTC: %v\n", t.Format(TIME_LAYOUT), tLocal.Format(TIME_LAYOUT), tUTC.Format(TIME_LAYOUT)) fmt.Printf("8. Local.Unix: %v, UTC.Unix: %v\n", tLocal.Unix(), tUTC.Unix()) str2 := "1969-12-31 23:59:59" t2, _ := time.Parse(TIME_LAYOUT, str2) fmt.Printf("9. str2:%v,time: %v, Unix: %v\n", str2, t2, t2.Unix()) fmt.Printf("10. %v, %v\n", tLocal.Format(time.ANSIC), tUTC.Format(time.ANSIC)) fmt.Printf("11. %v, %v\n", tLocal.Format(time.RFC822), tUTC.Format(time.RFC822)) fmt.Printf("12. %v, %v\n", tLocal.Format(time.RFC822Z), tUTC.Format(time.RFC822Z)) //指定時區 parseWithLocation("America/Cordoba", str) parseWithLocation("Asia/Shanghai", str) parseWithLocation("Asia/Beijing", str) } testTime()
輸出:
0. now:2018-09-19 19:06:07.3642781 +0800 CST m=+0.005995601 1. str:2018-09-10 00:00:00 2. Parse time:2018-09-10 00:00:00 +0000 UTC 3. Format time str:2018-09-10 00:00:00 4. Zone name: UTC, Zone offset: 0 5. Local Zone name: CST, Local Zone offset: 28800 6. t: 2018-09-10 00:00:00 +0000 UTC, Local: 2018-09-10 08:00:00 +0800 CST, UTC: 2018-09-10 00:00:00 +0000 UTC 7. t: 2018-09-10 00:00:00, Local: 2018-09-10 08:00:00, UTC: 2018-09-10 00:00:00 8. Local.Unix: 1536537600, UTC.Unix: 1536537600 9. str2:1969-12-31 23:59:59,time: 1969-12-31 23:59:59 +0000 UTC, Unix: -1 10. Mon Sep 10 08:00:00 2018, Mon Sep 10 00:00:00 2018 11. 10 Sep 18 08:00 CST, 10 Sep 18 00:00 UTC 12. 10 Sep 18 08:00 +0800, 10 Sep 18 00:00 +0000 America/Cordoba 2018-09-10 00:00:00 -0300 -03 Asia/Shanghai 2018-09-10 00:00:00 +0800 CST cannot find Asia/Beijing in zip file C:\Go\/lib/time/zoneinfo.zip
從以上程式碼的測試結果可以得出幾點:
-
time.Now
得到的當前時間的時區跟電腦的當前時區一樣。 -
time.Parse
把時間字串轉換為Time,時區是UTC時區。 -
不管Time變數儲存的是什麼時區,其
Unix()
方法返回的都是距離UTC時間:1970年1月1日0點0分0秒的秒數。 -
Unix()
返回的秒數可以是負數,如果時間小於1970-01-01 00:00:00的話。 -
Zone
方法可以獲得變數的時區和時區與UTC的偏移秒數,應該支援夏令時和冬令時。 -
time.LoadLocation
可以根據時區名建立時區Location
,所有的時區名字可以在$GOROOT/lib/time/zoneinfo.zip
檔案中找到,解壓zoneinfo.zip
可以得到一堆目錄和檔案,我們只需要目錄和檔案的名字,時區名是目錄名+檔名,比如"Asia/Shanghai" 。中國時區名只有"Asia/Shanghai" 和"Asia/Chongqing" ,而沒有"Asia/Beijing" 。 -
time.ParseInLocation
可以根據時間字串和指定時區轉換Time。 - 感謝中國只有一個時區而且沒有夏令時和冬令時,可怕的美國居然有6個時區,想想都可怕。