[TZInfo-users] Getting dst? for different time zones

Philip Ross phil.ross at gmail.com
Sat Jul 7 12:38:36 EDT 2007

Sorry for the delay in replying, I've been away for a few days.

On 02/07/07, John Lauck <john-subscribed at recaffeinated.com> 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

> 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

There will be multiple zones for each offset because the different
areas either currently or have historically used different time zone

> 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.



Phil Ross
http://tzinfo.rubyforge.org/ -- DST-aware timezone library for Ruby

More information about the TZInfo-users mailing list