英文:
Golang time zone parsing not returning the correct zone on ubuntu server
问题
使用Ubuntu 12.04
clientSendTimeHeaderFormat := "2006-01-02T15:04:05-0700"
ctx := "2015-04-01T10:04:00-0700"
clientSendTime, err := time.Parse(clientSendTimeHeaderFormat, ctx)
name, offset := clientSendTime.Zone()
在服务器上,name返回为空,而offset正确为-25200。
clientSendTime打印出为"2015-04-01 10:04:00 -0700 -0700"。
在我的Mac上本地运行时,name和offset都正确返回。name返回正确的时区为"PDT"。在本地,clientSendTime打印出为"2015-04-01 10:04:00 -0700 PDT"。
我还在本地运行时从服务器复制了/usr/share/zoneinfo文件夹,以确保不是由于其中的差异导致的。
有人知道可能是什么原因导致这些差异吗?
英文:
Using Ubuntu 12.04
clientSendTimeHeaderFormat := "2006-01-02T15:04:05-0700"
ctx := "2015-04-01T10:04:00-0700"
clientSendTime, err := time.Parse(clientSendTimeHeaderFormat, ctx)
name, offset := clientSendTime.Zone()
On the server, the name returns empty, whereas offset is correct at -25200.
The clientSendTime prints out as "2015-04-01 10:04:00 -0700 -0700".
Running it locally on my mac returns both name and offset correctly. The name returns the correct zone "PDT". Locally the clientSendTime prints out as "2015-04-01 10:04:00 -0700 PDT"
I also copied over the /usr/share/zoneinfo folder from the server when running locally to make sure it wasnt due to differences in that.
Anyone know what might be causing these differences?
答案1
得分: 5
好的,我会为你翻译以下内容:
好的,所以我知道为什么会发生这种情况,但没有一个合适的解决方法。
关于Parse
的文档中写道:
> 当解析带有时区偏移的时间(例如-0700)时,如果该偏移对应于当前位置(Local)使用的时区,则Parse会在返回的时间中使用该位置和时区。否则,它会将时间记录为位于一个虚构的位置,时间固定在给定的时区偏移上。
由于你在太平洋时区,时区偏移确实与你的位置匹配,所以它可以使用你的本地时区的“名称”(不好的是,PDT实际上不是一个时区名称,但我们将忽略这一点)。当你的服务器解析它时,它必须虚构位置,这就是为什么你看到“-0700 -0700”。
从根本上讲,你无法仅凭偏移确定时区 - 在同一时刻可能有多个具有相同偏移的时区,因此你无法预测原始时区中在任何其他时间的偏移将是什么。我建议你只记录你实际知道的内容 - 即偏移量 - 不要试图推断实际上不存在的信息。
英文:
Okay, so I think I know why this is happening - but not a decent fix for it.
The documentation for Parse
says:
> When parsing a time with a zone offset like -0700, if the offset corresponds to a time zone used by the current location (Local), then Parse uses that location and zone in the returned time. Otherwise it records the time as being in a fabricated location with time fixed at the given zone offset.
As you're in Pacific time, the zone offset does happen to match your location - so it can use your local time zone "name" (ick; PDT isn't actually a time zone name, but we'll leave that to one side). When your server parses it, it has to fabricate the location - that's why you're seeing "-0700 -0700".
Fundamentally, you can't determine the time zone just from an offset - there can be multiple time zones with the same offset at the same moment in time, so you can't predict what the offset will be in the original time zone at any other time. I suggest you just record what you actually know - i.e. the offset - and don't try to infer information that isn't actually present.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论