MySQL datetime fields and daylight savings time — how do I reference the “extra” hour?

I’ve got it figured out for my purposes. I’ll summarize what I learned (sorry, these notes are verbose; they’re as much for my future referral as anything else).

Contrary to what I said in one of my previous comments, DATETIME and TIMESTAMP fields do behave differently. TIMESTAMP fields (as the docs indicate) take whatever you send them in “YYYY-MM-DD hh:mm:ss” format and convert it from your current timezone to UTC time. The reverse happens transparently whenever you retrieve the data. DATETIME fields do not make this conversion. They take whatever you send them and just store it directly.

Neither the DATETIME nor the TIMESTAMP field types can accurately store data in a timezone that observes DST. If you store “2009-11-01 01:30:00” the fields have no way to distinguish which version of 1:30am you wanted — the -04:00 or -05:00 version.

Ok, so we must store our data in a non DST timezone (such as UTC). TIMESTAMP fields are unable to handle this data accurately for reasons I’ll explain: if your system is set to a DST timezone then what you put into TIMESTAMP may not be what you get back out. Even if you send it data that you’ve already converted to UTC, it will still assume the data’s in your local timezone and do yet another conversion to UTC. This TIMESTAMP-enforced local-to-UTC-back-to-local roundtrip is lossy when your local timezone observes DST (since “2009-11-01 01:30:00” maps to 2 different possible times).

With DATETIME you can store your data in any timezone you want and be confident that you’ll get back whatever you send it (you don’t get forced into the lossy roundtrip conversions that TIMESTAMP fields foist on you). So the solution is to use a DATETIME field and before saving to the field convert from your system time zone into whatever non-DST zone you want to save it in (I think UTC is probably the best option). This allows you to build the conversion logic into your scripting language so that you can explicitly save the UTC equivalent of “2009-11-01 01:30:00 -04:00” or “”2009-11-01 01:30:00 -05:00”.

Another important thing to note is that MySQL’s date/time math functions don’t work properly around DST boundaries if you store your dates in a DST TZ. So all the more reason to save in UTC.

In a nutshell I now do this:

When retrieving the data from the database:

Explicitly interpret the data from the database as UTC outside of MySQL in order to get an accurate Unix timestamp. I use PHP’s strtotime() function or its DateTime class for this. It can not be reliably done inside of MySQL using MySQL’s CONVERT_TZ() or UNIX_TIMESTAMP() functions because CONVERT_TZ will only output a ‘YYYY-MM-DD hh:mm:ss’ value which suffers from ambiguity problems, and UNIX_TIMESTAMP() assumes its input is in the system timezone, not the timezone the data was ACTUALLY stored in (UTC).

When storing the data to the database:

Convert your date to the precise UTC time that you desire outside of MySQL. For example: with PHP’s DateTime class you can specify “2009-11-01 1:30:00 EST” distinctly from “2009-11-01 1:30:00 EDT”, then convert it to UTC and save the correct UTC time to your DATETIME field.

Phew. Thanks so much for everyone’s input and help. Hopefully this saves someone else some headaches down the road.

BTW, I am seeing this on MySQL 5.0.22 and 5.0.27

Leave a Comment