Mar 21, 2010 9:48 PM
Dealing with time changes from daylight savings
-
Like (0)
Hi-
I've searched around for a solution to this and have come up empty. How do you deal with the effect of daylight saving in your queries that involve a time-zone shift? I'm hoping to find a solution like MySQL where you can set a time_zone session and have it apply to all subsequent references to time in your queries.
For example, in Netezza, I may want count(*) for today, where the time zone is PST:
select count(*) from mytable where date(data_ts - interval '8 hour') = date(now() - interval '8 hour');
Of course this works fine, but with the recent change, I should replace the 8 with a 7.
Is there a way to do this without replacing this in all of my queries?? This seems straightforward, but I'm unsure if this is possible with Netezza.
Thanks!
-Scott
I'm wondering if anyone has written a UDF to handle this? but even if this was done, it would still require a change to your SQL queries to incorporate the UDF.
How many timezones do you have to cope with? If it's not for the general case, but only a few specific timezones, it should be relatively easy to either code some sort of case statement or where clause, or maybe use a lookup table which defines the rigth offset for the dates or date ranges.
But any of these approaches will need a change to the SQL example you gave..
D.
We have created a lookup table and use a case statement to handle Daylight saving. The best solution is to write a UDF and I was hoiping that someone might have created one already but last time I posted the similar question,I did not hear from anyone. I think Netezza should provide this with their SQL add-ons.
Thanks for the feedback, everybody. We're really only dealing with daylight saving in one time zone, so maybe the simplest way to do this is to use the case statement and lookup method. It's slightly annoying that this isn't natively supported, but at least there's a workaround.
In a broader context, some of this is locale-oriented. For example, the UK has daylight savings, but not on the same boundaries as the US. Arizona doesn't do daylight savings at all. Some townships nationwide have opted out of daylight savings. There is a time zone standard in the US of Eastern Standard Time and Eastern Daylight Time etc.
It is also important to keep in mind that no one group is responsible for owning the time-zone lines or standards. These are generally agreed upon but not standardized in the same sense as currency and other regulated domains. Alaska for example is all-inside the international date line not because of geography, but because everyone thinks they should be, and so they are. The IDL is actually drawn-around Alaska on the map.
Many enterprises solve their time zone and time/date issues with a time dimension entity as a one-stop shop, containing all business holidays/bank holidays, designating weekends, quarters, sales and business periods etc. along with the designations for time zones and countries, hourly offsets and the like. This is usually very site/enterprise-specific.
And in case people have been asleep, global enterprises are moving away from nightly processing and are instead using time-zone offsets. This means that when the Hong-Kong markets close, their processes start kicking in while the UK markets are still open, likewise when UK markets close it is 5 hours before the USA markets close, and the UK folks want their data. So the whole idea of a nightly process is washed away in favor of processing "as the world turns" (and all the daytime drama that goes with it)
Good point. For my case, I'm only dealing with one time zone (my own), so I really am just interested in dealing with how to convert UTC to PST. The case/lookup solution works well for my situation, but given that exact time zone and daylight savings boundaries can be products of governing, and thus likely to change, it could be useful to many users to have an integrated and centrally maintainable way of dealing with time shifts.
I agree. From my experience, though, using a lookup table with the dates and offsets is the best approach - this can then be incorporated in the ETL and the converted local times can be stored (you can, of course, do the conversions on the fly, but this extra processing (esp if using a BI tool that will generate multi-pass SQL (we were using MSTR)) slowed things quite a bit.
This project required a conversion of their data (all timestamps were in UTC) to the local time (the client had facilities around the world) - the problem was that each locale had a different offset (of course), and a different date (and even time of day) as to when DST would happen (and this would change from year to year). Because there were so many variables, having all of the conversions contained within case statements was just too bulky and difficult to manage; instead, I created a lookup table with the DST timestamp associated w/ each locale, then the appropriate hour offset - this would then be added or substracted from the actual data as need to derive the local time.
Since you are only converting to PST, this may be overkill for you; however, I suggest you check the dates and times for prior DST dates (if you have historical data) and future ones so that you don't hard code logic that may incorrect in future years. If the dates going forward are all over the map, then storing all this in a lookup table may be the cleaner (and more maintainable) approach.
Hope this helps.
Not that I'm worried about Russian time zones in my data, but this is very relevant for the conversation:
http://www.google.com/hostednews/ap/article/ALeqM5gzRFuIFuhDvG4ZgZiBe0235uQevQD9ENKP800
This would imply that not only does the time zone need to be data driven, it also needs to be responsive to dynamic changes from bored world leaders, or at least those leaders with too much time on their hands, per the article.
What if Greenwich Mean Time did something really mean?
This just underscores the necessity for it being (meta)data-driven and reasonably dynamic - because there is no clearinghouse or regulatory commission for global time.

