您可以使用
time.FixedZone()构造
time.Location具有固定偏移量的。
例:
loc := time.FixedZone("myzone", -8*3600)nativeDate := time.Date(2019, 2, 6, 0, 0, 0, 0, loc)fmt.Println(nativeDate)输出(在Go Playground上尝试):
2019-02-06 00:00:00 -0800 myzone
如果区域偏移量为字符串,则可以使用
time.Parse()它来解析。使用仅包含参考区域偏移量的布局字符串:
t, err := time.Parse("-0700", "-0800")fmt.Println(t, err)输出(在Go Playground上尝试):
0000-01-01 00:00:00 -0800 -0800 <nil>
如您所见,结果
time.Time的区域偏移为-0800小时。
因此我们的原始示例也可以写成:
t, err := time.Parse("-0700", "-0800")if err != nil { panic(err)}nativeDate := time.Date(2019, 2, 6, 0, 0, 0, 0, t.Location())fmt.Println(nativeDate)输出(在Go Playground上尝试):
2019-02-06 00:00:00 -0800 -0800



