Forum: Ruby on Rails Use a Time Zone without setting Time.zone?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
E00fa5dff361e0bed4b488d874960889?d=identicon&s=25 Danimal (Guest)
on 2009-04-08 04:13
(Received via mailing list)
Hello!

Is there an easy way to get a time in a particular time zone without
calling Time.zone = some_zone ?

I store events in UTC in the DB, and I also store a string
representation of the time zone as an attribute on the event.

What works is to do:

Time.zone = event.time_zone
start_time = event.start.in_time_zone
# and now I have the start time in the appropriate time zone

The problem, however, is that Time.zone = event.time_zone sets it
across the whole application, as best as I can tell. And I don't want
that!!

I could grab the Time.zone value, change it as above, then change it
back, but that's so ugly. What would be wonderful is if there was
something like:

start_time = event.start.in_time_zone(event.time_zone)

*sigh*

Anything like that? Any advice? This isn't an issue with a user's
local time zone, but rather just trying to show a PST time as 8am PST
and an EST time as 11am EST even if both are stored as 15:00:00 (in
UTC)

Thoughts?

-Danimal
0c51049fb747cd60088e4878458e05d1?d=identicon&s=25 Rick Schumeyer (Guest)
on 2009-04-08 05:51
(Received via mailing list)
Either I'm not understanding what you want, or this is real easy.

Let's say I have a class Event with a field edate, which is stored in
UTC:

>> e = Event.find(1)
=> #<Event id: 1, edate: "2009-03-30 12:00:00">
>> e.edate
=> Mon, 30 Mar 2009 12:00:00 UTC +00:00
>> e.edate.class
=> ActiveSupport::TimeWithZone

Now Let's say you want the time in "America New York" zone.  First,
create a new TimeZone object:

>> ze = ActiveSupport::TimeZone.new("Eastern Time (US & Canada)")
=> #<ActiveSupport::TimeZone:0xb780fb80 @utc_offset=-18000,
@name="Eastern Time (US & Canada)", @tzinfo=#<TZInfo::DataTimezone:
America/New_York>>

Then:
>> e.edate.in_time_zone(ze)
=> Mon, 30 Mar 2009 08:00:00 EDT -04:00

So in summary, I think you just need this:

local_zone = ActiveSupport::TimeZone.new("name_of_time_zone")
local_time = time_in_utc.in_time_zone(local_zone)

You will need to see the documentation for TimeZone for the names of
all the zones.
E00fa5dff361e0bed4b488d874960889?d=identicon&s=25 Danimal (Guest)
on 2009-04-08 06:03
(Received via mailing list)
Rick,

You are _THE MAN!_. Thank you!

For some reason, I was getting all confused with all the various Time,
Date, TimeZone, TimeWithZone, etc. objects.

I hacked together something that worked, but it was a bit more complex
than what you had. I didn't realized that you could pass a TimeZone
object to in_time_zone. I'm gonna clean it up according to what you
suggested.

Whew!

Thanks,

-Danimal
12fd072974b808af755dd3a5b3c3d84f?d=identicon&s=25 M@d (Guest)
on 2009-04-27 09:30
(Received via mailing list)
Thanks a lot i got it..
This topic is locked and can not be replied to.