问题描述
鉴于诸如EDT或CEST的时区,有一种方法可以获取 time.Location
引用,以将其用于 func(t Time)In(loc * Location)Time
?
Given a time zone such as EDT or CEST is there a way to get a time.Location
reference to use it to with func (t Time) In(loc *Location) Time
?
例如可以初始化位置使用 time.LoadLocation("Europe/Berlin")
进行CEST,但如何对实际时区符号执行相同的操作?
It is possible to initialize the location for e.g. CEST with time.LoadLocation("Europe/Berlin")
but how to do the same for the actual time zone notation?
鉴于@Svip非常有见地的评论,有没有什么明智的方法可以返回代表位置的列表?那是为了 WET
返回例如 [欧洲/伦敦,亚特兰蒂克/雷克雅未克]
.所有其他 WET
位置将遵循与这两个位置之一相同的时区安排.
Given the very insightful comment by @Svip is there any sensible way to return a list of representative location? That is for WET
return e.g. [Europe/London, Atlantik/Reykjavík]
. All other WET
locations would follow the same time zone arrangements as one of those two.
推荐答案
可以选择解析给定位置的时间.( https://golang.org/pkg/time/#LoadLocation )
There is option to parse time for given Location.(https://golang.org/pkg/time/#LoadLocation)
type CustomTime struct {
time.Time
}
const ctLayout = "Jan 2, 2006 at 3:04pm (MST)"
func (ct *CustomTime) UnmarshalJSON(b []byte) (err error) {
s := strings.Trim(string(b), "\"")
if s == "null" {
ct.Time = time.Time{}
return
}
location, err := time.LoadLocation("Local")
if err != nil {
return err
}
ct.Time, err = time.ParseInLocation(ctLayout, s, location)
return err
}
这篇关于在Go中将时区解析为Location结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!