Storing Date/Times in Databases
derickrethans.nl
derickrethans.nl
The cost of dealing with this greatly exceeds whatever marginal value is hypothetically being realized by adjusting the clock every several months. If there was a global standard for how this is handled it would be one thing but in practice the adjustment of clocks is arbitrary and somewhat random by country. It is a waste of resources.
If, 1 billion people spent on average 10 minutes 2 times a year for 50 years ~1.9 million man years was spent adjusting clocks.
Where I live, in winter, without daylight saving it would be dark until 9.30am or after in the mornings, meaning all the kids need to go to school in the dark, which is more dangerous.
Then at the peak of summer, it would probably still be daylight until close to midnight.
The closer you are to the equator, the less swing there is between the number of hours of daylight between summer and winter. Even between the very north of the UK and the very south there is an appreciable difference.
I wonder what the psychological differences would be if daylight saving was abandoned and whether there might be a real economic impact. I spent quite a bit of time up in the Pacific NW of the USA and a sunny day in February lifted everybodies spirits. I hesitate to say that the work that got done was better but certainly the mood at work was a lot more congenial.
Time is supposed to have a relatively consistent meaning, and we jump through a lot of hoops to give it an inconsistent meaning. Schedules are much more fluid and easy to change with little external side-effects.
People will need to learn to partially decouple their routine from the numbers on the clock, but that seems like a much simpler/cheaper problem to solve than our current time mess.
Technically we could all do that now anyway, just start using GMT rather than our local timezone :)
We basically had a summer and winter timetable.
The governments could do a better job of keepung things predictable, but overall it is easier to teach a very small percentage of (hopefully) intelligent developers to deal with the complexity, than the uneducated masses.
Unix has had this for years. Microsoft have had close to a decade and a half to get this fixed - but have never done so. Even though Exchange and calendaring software is entirely screwed most times DST changes, they still haven't done anything about it.
I'm curious whether anyone has written DST software for Windows that is based on tzinfo?
UTC is clearly not suitable for this.
On the other hand, no-one is looking to add 8 months to a localtime and have it make sense. This kinds of scheduling decisions will always be supervised by humans.
And the "Chile" example just goes to show you should be prepared to update a schedule. Airlines do this all the time.
Food for thought.
When I schedule an appointment for 5pm on November 4th in San Francisco, I expect it to stay 5pm on November 4th regardless of what the offset from UTC happens to be on that date. If for some reason, the California decides PST starts on the 5th after I've made my appointment, I do not want to show up an hour late.
Worse, if I schedule a repeating appointment for November 4th at 5 pm, then I do not want to show up an hour late or early every year depending on what date DST falls on.
This is why calendaring software often stores in local time or local time + Olson location (e.g. America/Los_Angeles) or time zone id (US-Western).
You'd have to indicate that this is "locally specified time", and that in the event of any DST / local presentation rules changes, the UTC time should be adjusted to keep the local presentation constant.
The obverse problem would be, say, specifying astronomical events in such a way that they're accurately presented when the time comes for them. In this case, the UTC time would be definitive, but the local presentation would change if time presentation rules changed.
Yeah, it's a mess.
A point in time should be stored as a timestamp (unixtime) and converted to local at the point of presentation.
A name of a time should be stored as a datetime column with no timezone and should not be adjusted by timezone.
The hard part is when you combine them: You have a name of a time (an appointment) and you want to correlate that with a point in time to match with someone else's calendar. The only way to do that is store the datetime plus the name of the timezone area (not the offset, the name).
If you want to have an extra hour of daylight, wake up an hour earlier.
It's instances like these where I wish people would push back and say, "No, we're not going to change our clocks." I was in Chile in late March. Cell phones showed different times depending on which tower they were talking to. I'm guessing some cell providers patched their systems in time and others didn't.
Relative time is a sticky mess. I think The Doctor referred to it as wibbly wobby timey wimey stuff -- and it really is. Just store this crap as a string along with the user's location. Don't bother storing the timezone, because DST changes every other day and some retard or another is always adding/removing timezones. When it comes to appointments and the like, you only ever have to bother with conversions when sharing it with users in a different location/timezone. That's when you perform the relevant magic. Not before. Not ever.
When it comes to absolute time, things are lot easier. Use UTC. Always. No exceptions. 'nuff said.
I have been bitten more than once. Now my rule is to always store universal time without timezone.
The title should say something about PHP not handling date/time correctly. This is nothing new.
I'm not positive if MySQL, PostgreSQL, Oracle, or similar would perform the same date and time manipulation correctly. However, I know from personal experience that MySQL handles date/times more consistently than PHP.
I'm curious to see if someone with more knowledge than myself can chime in on the issue.
The article isn't "picking on" PHP, it's highlighting that timezone rules are constantly changing (which is not a language-specific problem) and details how to handle them (e.g. using an extension that's updated more frequently).
Postgres boasts some very pedantically excellent timestamp and timezone support. If there is a bug, it will be rectified in all possible haste once reported.
For example, from http://www.postgresql.org/docs/9.2/static/functions-datetime...:
The first century starts at 0001-01-01 00:00:00 AD, although they
did not know it at the time. This definition applies to all
Gregorian calendar countries. There is no century number 0, you go
from -1 century to 1 century. If you disagree with this, please
write your complaint to: Pope, Cathedral Saint-Peter of Roma,
Vatican.
PostgreSQL releases before 8.0 did not follow the conventional
numbering of centuries, but just returned the year field divided
by 100.Essentially timestamps are stored in UTC but automatically converted to the user's time zone at the interface/template level, which requires adding just a couple of lines in your base template. It's very nicely implemented.