问题
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.
In my mind, I have two options when creating UNIX timestamps for all these events.
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.
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.
Which approach is better? Approach 1 gives a more "true" timestamp, but approach 2 seems less complex while still giving the same result.
回答1:
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.
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:
- https://stackoverflow.com/a/20828804
- https://stackoverflow.com/a/19627330
- https://stackoverflow.com/a/19170823
- https://serverfault.com/a/554761
回答2:
Everything should be stored in UTC so that there's a consistent basis for comparison and calculation. It should then only be converted to local time when some human gets involved.
The use of UTC will greatly simplify your code since you won't have to carry timezones around with every date/time and perform complex calculations across many different zones - we did this on a project once and I'll quit before I ever do it again :-)
In other words, if a user enters New York time, convert that to UTC as soon as possible. When presenting a date/time to a user, change it back to their local time as late as possible.
And, unless you have a real, pressing need (for example) to tell someone in Mumbai what the local time was in New York when the NY event happened (or will happen), you don't need to store the timezone for the event. If you do have such a need (a), by all means store the timezone as well, so you can get both local time where you are and local time where the event is/was.
(a) CJBS points out a few good examples in comments below, such as:
- a flight where it's useful to know the local time of arrival at the destination such that a taxi can be ordered there; or
- arranging with people at the event to meet at a certain time.
来源:https://stackoverflow.com/questions/22061999/how-should-i-store-data-for-events-in-different-timezones