我以為是圍棋時間。格式應根據布局設置時間格式。但似乎它根據時區信息返回不同的值。package mainimport ( "fmt" "time")func main() { formats := []string{ time.RFC3339, } times := []string{ "2020-03-08T01:59:50-08:00", "2020-03-08T01:59:59-08:00", //daylight saving starts 1 second later "2020-03-08T05:59:59-08:00", } for _, f := range formats { for _, t := range times { fmt.Printf("Format: %s\n", f) t, err := time.Parse(f, t) if err != nil { panic(err) } fmt.Printf("unix: %d\n", t.UnixNano()) fmt.Printf("time: %s\n", t.Format(f)) t = t.Add(time.Second) fmt.Printf("time + 1s: %s\n", t.Format(f)) } }}運行輸出:? go versiongo version go1.15 darwin/amd64? TZ=UTC go run main.goFormat: 2006-01-02T15:04:05Z07:00unix: 1583661590000000000time: 2020-03-08T01:59:50-08:00time + 1s: 2020-03-08T01:59:51-08:00Format: 2006-01-02T15:04:05Z07:00unix: 1583661599000000000time: 2020-03-08T01:59:59-08:00time + 1s: 2020-03-08T02:00:00-08:00 (a: this is not expected)unix: 1583675999000000000time: 2020-03-08T05:59:59-08:00time + 1s: 2020-03-08T06:00:00-08:00? TZ=America/Los_Angeles go run main.goFormat: 2006-01-02T15:04:05Z07:00unix: 1583661590000000000time: 2020-03-08T01:59:50-08:00time + 1s: 2020-03-08T01:59:51-08:00Format: 2006-01-02T15:04:05Z07:00 unix: 1583661599000000000time: 2020-03-08T01:59:59-08:00time + 1s: 2020-03-08T03:00:00-07:00 (b: this is expected)Format: 2006-01-02T15:04:05Z07:00unix: 1583675999000000000time: 2020-03-08T05:59:59-08:00time + 1s: 2020-03-08T06:00:00-08:00 (c: this contradicts with the b)
1 回答

守候你守候我
TA貢獻1802條經驗 獲得超10個贊
行為已記錄在案。的輸出只是一個結果,而不是混淆的來源 - 這是:time.Format
time.Parse
使用區域偏移量(如 -0700)解析時間時,如果偏移量對應于當前位置(本地)使用的時區,則 Parse 將在返回的時間中使用該位置和區域。否則,它將時間記錄為在制造位置,時間固定在給定的區域偏移處。
進一步的說明可以在類型位置下找到:
本地表示系統的本地時區。在 Unix 系統上,Local 會查閱 TZ 環境變量以查找要使用的時區。沒有 TZ 意味著使用系統默認的 /etc/localtime。TZ=“” 表示使用 UTC。TZ=“foo” 表示在系統時區目錄中使用文件 foo。
基本上,go的解析器試圖從UTC偏移量推斷時區。如果解析的 UTC 偏移量與環境變量設置的時區的偏移量匹配,則此時區將在返回的 中設置。在處理日期和時間時,簡單性似乎總是結束。TZ
time
- 1 回答
- 0 關注
- 116 瀏覽
添加回答
舉報
0/150
提交
取消