gpt4 book ai didi

timezone - 时区只是一个偏移量还是 "more information"?

转载 作者:行者123 更新时间:2023-12-02 22:10:34 32 4
gpt4 key购买 nike

我生活在一个每年更改两次时间的国家。即:一年中有一段时期与 UTC 的偏移量为 -3 小时(-180 分钟),其他时期的偏移量为 -4 小时(-240 分钟)

从图形上看:

       |------- (offset = -3) -------|------- (offset is -4) -------|
start of year mid end of year

我的问题是:
“时区”只是代表偏移量的数字?那就是:我国有两个时区?或者时区包含此信息?

这很重要,因为我将 UTC 时区(偏移 = 0)中的每个日期保存在数据库中。

我是否应该使用本地时区保存日期并保存它们的偏移量(在保存时)?

以下是我通过使用 UTC 时区保存日期时遇到的问题的示例:
假设我有一个人们发送消息的系统。
我想要一个统计部分,在其中绘制“发送的消息与小时的关系”(即:“常规日期按小时发送的消息”)

假设整个数据库中只有两条消息:

  1. 消息 1,于 3 月 1 日,UTC 时间下午 5 点(本地时间下午 2 点)发送
  2. 消息 2,于 8 月 1 日,UTC 时间下午 5 点(本地时间下午 1 点)发送

然后,如果我在 8 月 2 日创建绘图,将这些 UTC 日期转换为本地日期将给出:“2 条消息于下午 1 点发送”,这是不稳定的信息!

最佳答案

来自标签维基 here on StackOverflow :

TimeZone != Offset

A time zone can not be represented solely by an offset from UTC. Many time zones have more than one offset due to "daylight savings time" or "summer time" rules. The dates that offsets change are also part of the rules for the time zone, as are any historical offset changes. Many software programs, libraries, and web services disregard this important detail, and erroneously call the standard or current offset the "zone". This can lead to confusion, and misuse of the data. Please use the correct terminology whenever possible.

有两个常用的数据库,Microsoft Windows 时区数据库和 IANA/Olson 时区数据库。请参阅the wiki了解更多详情。

您的具体问题:

the "timezone" is just the number representing the offset? that is: my country has two timezones? or the timezone includes this information?

您有一个“时区”。它包括两个“偏移量”。

Should I, instead, be saving the dates with local timezone and saving their offset (at the moment of saving) too?

如果您要记录事件发生或将要发生的精确时刻,那么您应该存储该特定时间的偏移量。在 .NetSQL Server ,这是使用 DateTimeOffset 表示的。其他平台也有类似的数据类型。它仅包含偏移信息 - 不包含偏移源自的时区。通常以ISO8601格式序列化,如:

2013-05-09T13:29:00-04:00

如果您可能需要编辑那个时间,那么您不能只存储偏移量。在系统中的某个位置,您还需要具有时区标识符。否则,您无法确定编辑完成后新的偏移量应该是多少。如果您愿意,可以将其与值本身一起存储。有些平台有专门用于此目的的对象 - 例如 NodaTime 中的 ZonedDateTime 。示例:

2013-05-09T13:29:00-04:00  America/New_York

即使存储区域 id,您仍然需要记录偏移量。这是为了解决从日光偏移到标准偏移的“回退”过渡期间的歧义。

或者,您可以使用时区名称存储 UTC 时间:

2013-05-09T17:29:00Z  America/New_York

这也同样有效,但您必须在向任何人显示该值之前应用时区。 Oracle 中的带有时区的时间戳PostgreSQL按照这种方式工作。

您可以在this post中阅读更多相关信息。 ,而以 .Net 为重点 - 这个想法也适用于其他平台。您给出的示例问题就是我所说的“保持观察者的视角” - 这在同一篇文章中进行了讨论。

关于timezone - 时区只是一个偏移量还是 "more information"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16467114/

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