首页 > 解决方案 > 如何通过 utc 偏移量加载位置

问题描述

我想通过 UTC 偏移量加载位置,例如

func main() {
    L, err := time.LoadLocation("UTC+8")
    if err != nil {
        panic(err)
    }
    fmt.Println(L)
}

代码恐慌:panic: unknown time zone UTC+8

如何通过 UTC 偏移量(不是本地或 UTC,而是另一个位置)获取位置对象,而不必以 IANA 时区格式指定它,例如“America/New_York”

我的目的很简单:我有一个在固定位置运行的 Web 服务器,我可以轻松地获取本地时间。现在我想根据访问者的位置(由 url 参数发送,例如http://myserver.com/do_something?timezone=-8)进行时间计算。

标签: gotimezone

解决方案


我找到了:

time.FixedZone(name string, offset int)

这将创建一个具有给定名称(这并不重要)和偏移量的位置。


推荐阅读