给定一个任意的时间偏移,如何创建一个time.Location表示该时间偏移的可用对象?
以下代码使用偏移量解析时间,但fmt.Println(t.Location())随后不返回任何信息:
func main() {
offset := "+1100"
t, err := time.Parse("15:04 GMT-0700","15:06 GMT"+offset)
if err != nil {
fmt.Println("fail", err)
}
fmt.Println(t)
fmt.Println(t.UTC())
fmt.Println(t.Location())
}
Run Code Online (Sandbox Code Playgroud)
游乐场:https : //play.golang.org/p/j_E28qJ8Vgy
基本上我有一些带有时间偏移的时间数据,但没有位置数据,我想创建一个time.Location对象来确保记录 GMT 偏移。然后能够输出相对于最终用户实际位置时间偏移的时间。
用:
loc := time.FixedZone("UTC+11", +11*60*60)
Run Code Online (Sandbox Code Playgroud)
然后设置到这个位置:
t = t.In(loc)
Run Code Online (Sandbox Code Playgroud)
试试这个:
package main
import (
"fmt"
"time"
)
func main() {
loc := time.FixedZone("UTC+11", +11*60*60)
t := time.Now()
fmt.Println(t)
fmt.Println(t.Location())
t = t.In(loc)
fmt.Println(t)
fmt.Println(t.Location())
fmt.Println(t.UTC())
fmt.Println(t.Location())
}
Run Code Online (Sandbox Code Playgroud)
输出:
2009-11-10 23:00:00 +0000 UTC m=+0.000000001
UTC
2009-11-11 10:00:00 +1100 UTC+11
UTC+11
2009-11-10 23:00:00 +0000 UTC
UTC+11
Run Code Online (Sandbox Code Playgroud)