[TZInfo-users] DST start/stop times?

Philip Ross phil.ross at gmail.com
Tue Oct 4 19:13:03 EDT 2011

Hi Eric,

On 4 October 2011 19:09, Eric Schwartz <eschwartz at comverge.com> wrote:
> I need to program a device that tracks DST by setting start/stop times, but I don't see a way to get at that information from TZInfo... am I missing something or is there no way other than iterating over every day to figure out when DST starts and ends each year?

Your best bet would be to use the period_for_utc method. For a given
date and time, this method will return a TimezonePeriod object that
will give you the UTC offset at that time and when the period starts
and ends (i.e. the previous and next daylight savings transitions).

You can find the previous or next TimezonePeriod by either subtracting
a second from the start time or adding a second to the end time and
calling period_for_utc again.

The following example will print all the daylight savings and standard
time periods for New York in the years 2011 to 2020:

zone = TZInfo::Timezone.get('America/New_York')
start_year = 2011
end_year = 2020

period = zone.period_for_utc(Time.utc(start_year, 1, 1))

while period
  offset_hours = period.utc_total_offset / 3600
  offset_mins = period.utc_total_offset % 3600
  offset_sign = period.utc_total_offset >= 0 ? '+' : ''
  offset = "#{offset_sign}#{offset_hours}:#{offset_mins.to_s.rjust(2, '0')}"

  puts "GMT#{offset} #{period.abbreviation} " +
       "from #{period.utc_start} #{period.utc_end ?
                                   "until #{period.utc_end}" : ''}"

  # If this period has an end time (i.e. daylight savings hasn't been
  # add a second to the end time and find the next period.
  period = period.utc_end && period.utc_end.year <= end_year ?
              zone.period_for_utc(period.utc_end + Rational(1,86400)) :

Kind regards,


More information about the TZInfo-users mailing list