Right Place, Wrong Time
November 13, 2007 6:37 PM
Subscribe
I have a set of dates/times in a MS SQL table that lack a timezone offset. I want to convert them to UTC, but I have to be mindful of Daylight Time for each of these dates (they're in the future). How can I convert them to UTC correctly?
The nuts and bolts are these:
We have a calendaring application with an MS SQL Server 2000 backend. When event dates get entered into the system, they're entered without a timezone code, e.g.:
2007-11-13 12:00:00.000
Each of these events is in the future. Some are in March or April. We observe DST.
I'm making changes to the data feed so that we can implement the hCalendar microformat on our calendars. The problem is the time zone. hCalendar calls for dates like:
2007-11-13T12:00:00-08:00
Originally, one of the engineers slapped the current time offset from UTC on there. Unfortunately, if you're looking at the calendar while we're on standard time, an event that will be in DST will not be offset correctly, being an hour earlier or later.
So, back to the drawing board. We decided to convert all the dates to UTC before putting them in the data feed for the microformat. But the time zone problem came right back.
Is there an easy way to convert these future dates to the correct UTC that's mindful of what the DST offset is in the future? There's no function in MS SQL 2000 that allows you to look up whether a date is PDT or PST, is there?
posted by dw to computers & internet (3 comments total)
1 user marked this as a favorite
Ok, so now you write the function you need. You'll write it as a view.
Now, I don't know what range of dates you're working with, so I'm just going to give you a dead simple one-off answer that lacks flexibility or elegance.
Assuming your dates are all after the last time DST ended and prior to the next time DST ends, we can partition the problem dates into two parts: those in Standard Time, and those in DST. Those in DST are those on or after 2008-03-09 2:00.
Ok, I'll assume your base table is called event_date with a datetime column d.
Since I don't know what TZ you're in, I'll assume it's UTC-5
So to find out UTC when not in DST, I'll add 5 hours; in DST I'll add 4 hours.
so:
create view event_date_utc as
select d, dateadd(hh, case when d < '2008-03-09 2:00' then 5 else 4 end, d) as d_UTC
from event_date;
Of course, that's not tested. You should spend some time testing it.
A better solution, which would work for any date, not just those prior to the end of 2008 DST is to look up the offset in a table of DST times, but I suspect that's beyond the scope of what you need (and certainly beyond the scope of what you're paying for ;) )
posted by orthogonality at 7:37 PM on November 13, 2007