I am hoping to make this question and the answers to it the definitive guide to dealing with daylight saving time, in particular for dealing with the actual change overs.
If you have anything to add, please do
Many systems are dependent on keeping accurate time, the problem is with changes to time due to daylight savings - moving the clock forward or backwards.
For instance, one has business rules in an order taking system that depend on the time of the order - if the clock changes, the rules might not be as clear. How should the time of the order be persisted? There is of course an endless number of scenarios - this one is simply an illustrative one.
- How have you dealt with the daylight saving issue?
- What assumptions are part of your solution? (looking for context here)
As important, if not more so:
- What did you try that did not work?
- Why did it not work?
I would be interested in programming, OS, data persistence and other pertinent aspects of the issue.
General answers are great, but I would also like to see details especially if they are only available on one platform.
Summary of answers and other data: (please add yours)
Do:
- Always persist time according to a unified standard that is not affected by daylight savings. GMT and UTC have been mentioned by different people, though UTC seems to be mentioned most often.
- Include the local time offset as is (including DST offset) when storing timestamps.
- Include the original timezone name, so you can reconstruct the original time at a later point and display correct offsets if needed.
- Remember that DST offsets are not always an integer number of hours (e.g. Indian Standard Time is UTC+05:30).
- If using Java, use JodaTime. - http://joda-time.sourceforge.net/
- Create a table TZOffsets with three columns: RegionClassId, StartDateTime, and OffsetMinutes (int, in minutes). See answer
- Business rules should always work on civil time (UTC/GMT).
- Internally, keep timestamps in something like civil-time-seconds-from-epoch. See answer.
- Only convert to local times at the last possible moment.
- Remember that timezones and offsets are not fixed and may change.
- Consider the type of time (actual event time, broadcast time, relative time, historical time, recurring time) what elements (timestamp, timezone offset and timezone name) you need to store for correct retrieval - see "Types of Time" in answer.
- Check if your DBMS needs to be shutdown during transition.
- Keep your OS, database and application tzdata files in sync, between themselves and the rest of the world.
- Set hardware clocks and OS clocks to UTC.
- Use NTP services on all servers.
- Store server time, not client time.
- If doing historical auditing store both UTC and local time (this allows exact pinpointing of time, as conversion tables will change).
- If using FAT32, remember that timestamps are stored in local time, not UTC.
- When dealing with recurring events (weekly TV show, for example), remember that the time changes with DST and will be different across time zones.
Don't:
- Do not use javascript date and time calculations in web apps unless you ABSOLUTELY have to.
- Never trust client datetime. It may very well be incorrect.
- Do not compare client datetimes with server datetimes.
Testing:
- When testing make sure you test countries in the Western and Eastern hemispheres, with both DST in progress and not and a country that does not use DST (6 in total).
- Test all third party libraries and applications and make sure they handle timezone data correctly.
Reference:
- Olson database, aka Tz_database - ftp://elsie.nci.nih.gov/pub
- Sources for Time Zone and DST - http://www.twinsun.com/tz/tz-link.htm
- ISO format (ISO 8601) - http://en.wikipedia.org/wiki/ISO_8601
- Mapping between Olson database and Windows TimeZone Ids, from the Unicode Consortium - http://unicode.org/repos/cldr-tmp/trunk/diff/supplemental/windows_tzid.html
- TimeZone page on WikiPedia - http://en.wikipedia.org/wiki/Tz_database
- StackOverflow questions tagged
dst
- http://stackoverflow.com/questions/tagged/dst - StackOverflow questions tagged
timezone
- http://stackoverflow.com/questions/tagged/timezone - Dealing with DST - microsoft DateTime best practices - http://msdn.microsoft.com/en-us/library/ms973825.aspx#datetime_topic6
- Network Time Protocol on wikipedia - http://en.wikipedia.org/wiki/Network_Time_Protocol
Other:
- Lobby your representative to end the abomination that is DST. We can always hope...