gpt4 book ai didi

ubuntu - Golang 时区解析未在 ubuntu 服务器上返回正确的区域

转载 作者:IT王子 更新时间:2023-10-29 00:59:21 26 4
gpt4 key购买 nike

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

在服务器上,名称返回空,而偏移量是正确的 -25200。clientSendTime 打印为“2015-04-01 10:04:00 -0700 -0700”。

在我的 mac 上本地运行它会正确返回名称和偏移量。该名称返回正确的区域“PDT”。在本地,clientSendTime 打印为“2015-04-01 10:04:00 -0700 PDT”

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

有人知道可能导致这些差异的原因是什么吗?

最佳答案

好吧,所以我想我知道为什么会发生这种情况 - 但不是很好的解决方法。

documentation for 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”的原因。

从根本上说,您无法仅根据偏移量来确定时区 - 同一时刻可能存在多个具有相同偏移量的时区,因此您无法预测原始时间的偏移量在任何其他时间区域。我建议您只记录您实际知道的内容 - 即偏移量 - 而不要尝试推断实际不存在的信息。

关于ubuntu - Golang 时区解析未在 ubuntu 服务器上返回正确的区域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30740401/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com