Best practices when building timezone enabled applications
I am building a timezone enabled web application, and I am looking for best practices around that.
For example, I have user A and User B, who have timezone set. User A enters a time. (in his timezone.) Now how do you开发者_Go百科 store this in the DB.
- [option 1] Original time with original timezone
- [Option 2] UCT time stored in UCT.
Use cases are
- User A needs to be shown this time.
- User B needs to be shown this time.
- Anon user needs to be show this time.
- Timezone differences need to be calculated.
I can see the advantages of both ways. I am not particularly looking for answer to this question only but info about building Timezone aware web apps in particular.
I am using Python, but looking for Language agnostic tips as well.
Semantically, almost all of the datetimes you manipulate are absolute times (for example: billing, creations and modifications, last seen, api throttles, SO bounties…). They should be stored in UTC so that there is no ambiguity about them. Otherwise, when some government changes a DST rule and your system rules get updated, the value you stored will change meaning.
If the datetime is referring to a time and place as well (eg a meeting, calendaring in general), you can store the full symbolic date in RFC 3339, including a timezone name (not the same as an UTC offset), and convert to UTC on demand.
In general, I think it makes more sense to convert times to the common format (UTC) to store them, then convert them to the user's own time zone when displaying them. You may be able to guess for anonymous users based on their IP address, or default to a likely timezone.
Of course, it depends on what you're doing. For many things, the absolute time is important: e.g. if there's a video conference, its start time has to be the same for people in each time zone. But there are a few cases where something's done by local time. Earth Hour, for example, is 8.30-9.30pm in each time zone.
精彩评论