# Use a Time Zone without setting Time.zone?

**URL:** <https://www.ruby-forum.com/t/use-a-time-zone-without-setting-time-zone/164995>\
**Category:** Rails\
**Created:** [April 8, 2009, 4:13am UTC](https://www.ruby-forum.com/t/use-a-time-zone-without-setting-time-zone/164995 "2009-04-08T04:13:39Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Danimal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/bb73d2/32.png) [@Danimal](https://www.ruby-forum.com/u/Danimal)\
**Post date:** [April 8, 2009, 4:13am UTC](https://www.ruby-forum.com/t/use-a-time-zone-without-setting-time-zone/164995/1 "2009-04-08T04:13:39Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Danimal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/bb73d2/32.png) [@Danimal](https://www.ruby-forum.com/u/Danimal)\
**Post date:** [April 8, 2009, 5:51am UTC](https://www.ruby-forum.com/t/use-a-time-zone-without-setting-time-zone/164995/2 "2009-04-08T05:51:15Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Danimal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/bb73d2/32.png) [@Danimal](https://www.ruby-forum.com/u/Danimal)\
**Post date:** [April 8, 2009, 6:03am UTC](https://www.ruby-forum.com/t/use-a-time-zone-without-setting-time-zone/164995/3 "2009-04-08T06:03:32Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Danimal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/bb73d2/32.png) [@Danimal](https://www.ruby-forum.com/u/Danimal)\
**Post date:** [April 27, 2009, 9:30am UTC](https://www.ruby-forum.com/t/use-a-time-zone-without-setting-time-zone/164995/4 "2009-04-27T09:30:54Z")

</div>

Thanks a lot i got it…
