[TZInfo-users] TzInfo timezone base question

Philip Ross phil.ross at gmail.com
Wed Feb 6 15:11:39 EST 2008

On 06/02/2008, Mark Van De Vyver <mvyver at gmail.com> wrote:
> On Feb 7, 2008 12:43 AM, Philip Ross <phil.ross at gmail.com> wrote:
> > On Unix based architectures, the local timezone can be changed at
> > runtime by setting the TZ environment variable. This will affect all
> > Times constructed after changing the environment variable. For
> > example:
> >
> > > ENV['TZ']='Australia/Melbourne'
> > => "Australia/Melbourne"
> > > Time.parse("09:30").iso8601
> > => "2008-02-07T09:30:00+11:00"
> > > ENV['TZ']='Asia/Karachi'
> > => "Asia/Karachi"
> > > Time.parse("09:30").iso8601
> > => "2008-02-06T09:30:00+05:00"
> Thanks for the clarification, so there is no way to achieve a similar
> effect using tzinfo.

There is no equivalent of the parse function in TZInfo. You could use
parse and do the following though:

tz = TZInfo::Timezone.get(source_tz)
src_time_as_utc = tz.local_to_utc(Time.parse("09:30" + " UTC", tz.now))

Adding " UTC" to the time forces it to be parsed as a UTC timestamp
and causes any daylight savings rules of the system timezone to be
ignored. TZInfo will still treat the time value as if it were in local
time (it ignores the timezone aspect of the Time).

Passing tz.now as the second argument to Time.parse is required in
order to make sure you are dealing with the correct date.

> OK this is where I got a little confused - apologies if this is
> obvious, so it seems to do what I want I can't use 'parse', but
> instead will have to extract the hrs and minutes, then use:
> tz = TZInfo::Timezone.get(source_tz)
> src_time_as_utc = tz.local_to_utc(Time.local(2005,8,29,src_hrs,src_mins,0))

This would work too. TZInfo will just read the year, month, day, hour,
minute and second of the Time passed to local_to_utc and ignore the
timezone. It doesn't matter if Time.local returns a different timezone
to source_tz.

I would use Time.utc instead of Time.local though. If you specified a
time that fell in a transition between standard and daylight savings
time of the system timezone, then you might get an unexpected result
with Time.local.



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

More information about the TZInfo-users mailing list