考虑以下代码:
package main
import (
"time"
"fmt"
)
const (
format = "2006 01 02 15:04 MST"
date = "2018 08 01 12:00 EDT"
)
func main() {
aloc, _ := time.LoadLocation("America/New_York")
eloc, _ := time.LoadLocation("Europe/Berlin")
tn, _ := time.Parse(format, date)
tl, _ := time.ParseInLocation(format, date, aloc)
fmt.Println(tn) // Says +0000 despite EDT being -0400
fmt.Println(tn.In(eloc)) // Expect 18:00, but get 14:00
fmt.Println(tl) // Correctly -0400
fmt.Println(tl.In(eloc)) // Correctly 18:00
}
Run Code Online (Sandbox Code Playgroud)
您也可以在Go Playground上试用。
当我运行它时,我得到了这个结果(在我自己的系统上和通过 Playground):
2018-08-01 12:00:00 +0000 …
Run Code Online (Sandbox Code Playgroud) go ×1