[TZInfo-users] TimeZone offset reported after utc_to_local conversion
phil.ross at gmail.com
Mon Sep 24 16:03:05 EDT 2007
Your message didn't make it to the list because you are not
subscribed. Please see below for my reply.
> I was wondering if it was possible to somehow change the offset of a
> converted date using utc_to_local.
> ENV['TZ'] = 'UTC'
> t = DateTime.now
> tz1 = TZInfo::Timezone.get('America/New_York')
> tz2 = TZInfo::Timezone.get('Europe/Moscow')
> Even though the time is right, both offsets show +00:00. Is it possible
> to somehow reset offset to the proper value, i.e. -05:00 and +03:00?
TZInfo always returns times in UTC because it is not possible to have
a Ruby Time object that represents a timezone other than UTC or the
current local timezone. DateTime does support arbitrary offsets, but
since Time doesn't I decided to go for consistency.
TZInfo also always returns the type of argument you pass in. If you
pass in a Time, you get a Time back. If you pass in a DateTime, you
get a DateTime back. Since you are using DateTime in your example, you
could manually adjust the result using the new_offset method.
new_offset also adjusts the time, so this needs to be compensated for:
p = tz1.period_for_utc(t)
d = tz1.utc_to_local(t).new_offset(p.utc_total_offset_rational) -
Note the -04:00 offset rather than -05:00, since New York is currently
observing daylight savings time.
http://tzinfo.rubyforge.org/ -- DST-aware timezone library for Ruby
More information about the TZInfo-users