我需要获得UTC的位置偏移。我得到的麻烦与来自不同值的结果不一致。所有我需要的格式为+HHMM
值(例如,+0100
为“欧洲/罗马”)。
func main() {
loc, _:= time.LoadLocation("Asia/Kathmandu")
offset, others:= time.Now().In(loc).Zone()
fmt.Println(offset, others)
}
我能得到什么:
+0545
(合适的)+07
(应该是+0700
)MST
(应该是-0700
)CET
(应该是+0100
)Reference Timezone country names
请帮我。
您正在使用的Zone()
方法是工作exactly as advertized。
区计算有效的时区在时间t,返回区的缩写名称(如“CET”)及其在UTC东秒的偏移。
你一个更好的办法是使用Format
方法。就像是:
zone := time.Now().In(loc).Format("-0700")
当然,要注意:即使这不会是100%一致,由于夏令时。