From john-subscribed at recaffeinated.com Mon Jul 2 13:17:22 2007 From: john-subscribed at recaffeinated.com (John Lauck) Date: Mon, 2 Jul 2007 13:17:22 -0400 Subject: [TZInfo-users] Getting dst? for different time zones Message-ID: I have a list of locations and a boolean that tells me if the location observes day light savings. Each location is identified by a GMT offset for its time zone. I need to check if each time zone is currently within day light savings or not. I know I could simply check my local time using Time.now.dst?, but that won't help on the day of the day light savings switch. For instance, if it's 2:00AM on the east coast on the day of DST "jump ahead" it becomes 3:00AM (and the time zone changes from -5 to -4), then it's still 11PM on the west coast. The west coast doesn't change to -7 from -8 until 2AM within their local time. This means it's 6:00AM on the east coast when the west coast finally switches to DST, jumping from 2AM to 3AM hours later than the west coast. Although this is simply a several hour gap where a bug could occur it has to be handled. Ruby's Time and DateTime etc are not easy to deal with. I've been trying to use tzinfo, but I can't find the correct methods. I think I'm just not finding the correct documentation because a lot of time/date libraries use a vocabulary that's not always familiar to everyone. Is what I'm trying to do possible with the current Ruby libraries or do I need tzinfo? Does anyone know of a way to create a Time/DateTime object for a location/time zone so I can simply use Time.dst? ? Also, I'd really like to be able to use the offset integer -4...-8 (and others, limited to US now, but could be necessary internationally) rather than using the time zone names "America/New_York" since I don't have the offsets mapped to names. Ideas anyone? Thanks, John -------------- next part -------------- An HTML attachment was scrubbed... URL: http://rubyforge.org/pipermail/tzinfo-users/attachments/20070702/a7342cb2/attachment.html From phil.ross at gmail.com Sat Jul 7 12:38:36 2007 From: phil.ross at gmail.com (Philip Ross) Date: Sat, 7 Jul 2007 17:38:36 +0100 Subject: [TZInfo-users] Getting dst? for different time zones In-Reply-To: References: Message-ID: <4ac540410707070938h384d49c3rb02aaa15b0bf0fee@mail.gmail.com> Sorry for the delay in replying, I've been away for a few days. On 02/07/07, John Lauck wrote: > I have a list of locations and a boolean that tells me if the location > observes day light savings. Each location is identified by a GMT offset for > its time zone. For the US, identifying zones by GMT offset will be more or less ok. Internationally, this will cause problems because time zones that have the same GMT offset will often observe different daylight savings rules. In particular, the northern hemisphere will typically use summer time whilst the southern hemisphere is using standard time and vice-versa. > I need to check if each time zone is currently within day light savings or > not. I know I could simply check my local time using Time.now.dst?, but > that won't help on the day of the day light savings switch. For instance, > if it's 2:00AM on the east coast on the day of DST "jump ahead" it becomes > 3:00AM (and the time zone changes from -5 to -4), then it's still 11PM on > the west coast. The west coast doesn't change to -7 from -8 until 2AM > within their local time. This means it's 6:00AM on the east coast when the > west coast finally switches to DST, jumping from 2AM to 3AM hours later than > the west coast. With TZInfo, you can do the following: >> TZInfo::Timezone.get('America/New_York').current_period.dst? => true >> TZInfo::Timezone.get('America/Lima').current_period.dst? => false This will tell you whether a zone is currently observing daylight savings time given its identifier. > Although this is simply a several hour gap where a bug could occur it has to > be handled. Ruby's Time and DateTime etc are not easy to deal with. I've > been trying to use tzinfo, but I can't find the correct methods. I think > I'm just not finding the correct documentation because a lot of time/date > libraries use a vocabulary that's not always familiar to everyone. > > Is what I'm trying to do possible with the current Ruby libraries or do I > need tzinfo? Does anyone know of a way to create a Time/DateTime object for > a location/time zone so I can simply use Time.dst? ? Also, I'd really > like to be able to use the offset integer -4...-8 (and others, limited to US > now, but could be necessary internationally) rather than using the time zone > names "America/New_York" since I don't have the offsets mapped to names. Ruby's Time and DateTime classes only know about the daylight savings rules of the environment being used. It is not possible to construct a Time or DateTime object for a specific time zone. On a Unix/Linux machine, you can though set the TZ environment variable to make all Time and DateTime objects use the new time zone. This does not work on Windows though. The following code uses TZInfo to map GMT offsets to US Timezones: TZInfo::Country.get('US').zones.inject({}) do |map, z| zones = map[z.current_period.utc_offset] ||= [] zones << z.identifier map end There will be multiple zones for each offset because the different areas either currently or have historically used different time zone rules. > Ideas anyone? Firstly, I'd recommend switching to using zone identifiers rather than GMT offsets, especially if you will eventually be supporting non-US time zones. Once you're using the identifiers, either switch the TZ environment variable and then check Time.dst? or use TZInfo. Regards, Phil -- Phil Ross http://tzinfo.rubyforge.org/ -- DST-aware timezone library for Ruby