Golang时区解析未在ubuntu服务器上返回正确的时区 [英] Golang time zone parsing not returning the correct zone on ubuntu server

查看:57
本文介绍了Golang时区解析未在ubuntu服务器上返回正确的时区的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Ubuntu 12.04

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()

在服务器上,该名称返回空,而offset的正确值为-25200.clientSendTime打印为"2015-04-01 10:04:00 -0700 -0700".

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".

在我的mac机上本地运行它可以正确返回名称和偏移量.该名称返回正确的区域"PDT".在本地clientSendTime打印为"2015-04-01 10:04:00 -0700 PDT"

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"

在本地运行时,我还从服务器复制了/usr/share/zoneinfo文件夹,以确保它不是由于差异而造成的.

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?

推荐答案

好,所以我想我知道为什么发生这种情况-但是还不能解决这个问题.

Okay, so I think I know why this is happening - but not a decent fix for it.

Parse 文档说:

The documentation for Parse says:

当解析时间偏移为-0700的时间时,如果偏移量对应于当前位置(本地)使用的时区,则Parse在返回的时间中使用该位置和时区.否则,它会将时间记录为处于伪造位置,并将时间固定在给定的区域偏移处.

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.

当您处于太平洋时间时,时区偏移量确实与您的位置匹配-因此它可以使用您当地的时区名称"(例如,PDT实际上不是时区名称,但我们将其放在一边).当服务器解析它时,它必须伪造位置-这就是为什么您看到"-0700 -0700"的原因.

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".

从根本上讲,您不能仅从偏移量确定时区-可能有多个时区在同一时间具有相同的偏移量,因此您无法预测原始时间的偏移量 other 时间的任何区域.我建议您只记录您实际知道的信息(即偏移量),不要试图推断出实际上不存在的信息.

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.

这篇关于Golang时区解析未在ubuntu服务器上返回正确的时区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆