如何在不同的时区内存储事件数据? [英] How should I store data for events in different timezones?

查看:202
本文介绍了如何在不同的时区内存储事件数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个概念性问题,所以这里没有代码片段。

This is a conceptual question, so no code snippets here.

说我创建一个事件数据库。其中有些在纽约,有的在芝加哥,有的在凤凰城等等。

Say I create a database of events. Some of them are in New York, some in Chicago, some in Phoenix, etc...

我的服务器的时区设置为纽约。

My server's timezone is set to New York.

在我看来,在为所有这些事件创建UNIX时间戳时,我有两个选项。

In my mind, I have two options when creating UNIX timestamps for all these events.


  1. 考虑时区。 (即,芝加哥1月1日午夜的活动和Pheonix将有不同的时间戳)。然后,当我想以文本格式显示日期时,我必须再次考虑时区。

  1. Take the timezone into account. (i.e., An event at midnight on January 1 in Chicago and Pheonix would have different timestamps). Then I'd have to take the timezone into account again whenever I want to display the date in text format.

通过假装所有事件发生在纽约。 1月1日午夜在芝加哥和Pheonix举行的活动将具有相同的时间戳。由于我的服务器设置为纽约,我不需要为每个事件考虑时区。

Fudge it by pretending that all events happen in New York. An event at midnight on January 1 in Chicago and Pheonix would have the same timestamp. Since my server is set to New York, I wouldn't have to take the timezone into account for each event.

哪种方法更好?方法1给出了更多的真实时间戳,但是方法2似乎不太复杂,同时仍然给出相同的结果。

Which approach is better? Approach 1 gives a more "true" timestamp, but approach 2 seems less complex while still giving the same result.

推荐答案

意味着您正在记录事件的发生时间,或者记录事件发生的时间,那么paxdiablo的答案是正确的。通常,UTC就足够了。在一些情况下,您可能希望在某些平台中存储本地datetime +偏移量(DateTimeOffset),但这取决于您正在使用的数据。

If you mean that you are recording the time of events as they happen, or recording the times of events in the past, then paxdiablo's answer is correct. Usually, UTC will suffice for that. In a few cases, you might want to store a local datetime + offset (a "DateTimeOffset" in some platforms), but that depends on exactly what you're using the data for.

但是,如果您要安排将来会发生的事件,特别是如果它是一个重复的事件,那么UTC并不完全足够。我已经写了几次,所以我建议你阅读这些文章的细节:

However, if you are asking about scheduling an event that will happen in the future, especially if it's a recurring event, then UTC is not entirely sufficient. I've already written about this several times, so I suggest you read these articles for details:

  • http://stackoverflow.com/a/20828804
  • http://stackoverflow.com/a/19627330
  • http://stackoverflow.com/a/19170823
  • http://serverfault.com/a/554761

这篇关于如何在不同的时区内存储事件数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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