From phil.ross at gmail.com Mon Sep 24 16:03:05 2007 From: phil.ross at gmail.com (Philip Ross) Date: Mon, 24 Sep 2007 21:03:05 +0100 Subject: [TZInfo-users] TimeZone offset reported after utc_to_local conversion Message-ID: <4ac540410709241303o9f957c6v53a7674f51c1efbb@mail.gmail.com> Hi Danila, 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. > > > Example: > > ENV['TZ'] = 'UTC' > > t = DateTime.now > tz1 = TZInfo::Timezone.get('America/New_York') > tz2 = TZInfo::Timezone.get('Europe/Moscow') > > tz1.utc_to_local(t).to_s > "2007-09-24T12:33:40+00:00" > > tz2.utc_to_local(t).to_s > "2007-09-24T20:33:40+00:00" > > 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) - p.utc_total_offset_rational d.to_s => "2007-09-24T12:33:40-04:00" Note the -04:00 offset rather than -05:00, since New York is currently observing daylight savings time. Regards, Phil -- Phil Ross http://tzinfo.rubyforge.org/ -- DST-aware timezone library for Ruby