Daylight saving time and Timezone best practices
- by Oded
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.
Include the local time offset (including DST offset) in stored timestamps.
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
Check if your DBMS needs to be shutdown during transition.
Business rules should always work on civil time.
Internally, keep timestamps in something like civil-time-seconds-from-epoch. See answer
Only convert to local times at the last possible moment.
Don't:
Do not use javascript date and time calculations in web apps unless you ABSOLUTELY have to.
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).
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
Other:
Lobby your representative to end the abomination that is DST. We can always hope...