# Comparing Time Zones

**URL:** <https://www.ruby-forum.com/t/comparing-time-zones/139583>\
**Category:** Rails\
**Created:** [June 5, 2008, 6:55am UTC](https://www.ruby-forum.com/t/comparing-time-zones/139583 "2008-06-05T06:55:35Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![blaine](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/51bf81/32.png) [@blaine](https://www.ruby-forum.com/u/blaine)\
**Post date:** [June 5, 2008, 6:55am UTC](https://www.ruby-forum.com/t/comparing-time-zones/139583/1 "2008-06-05T06:55:35Z")

</div>

Is there a way in 2.1 to compare Time Zones? Basically, my  
application’s time zone is set to EST. I have a Client model with a  
time zone and I have a drop down using TimeZone.us\_zones which only  
stores the string of the time zone in the database.

Is there a way however to compare (not \<=\>) the Time.zone.name with  
Client.timezone to get the offset difference?

Greatly appreciated,

Tim K.

---

<div class="post-metadata">

**Author:** ![blaine](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/51bf81/32.png) [@blaine](https://www.ruby-forum.com/u/blaine)\
**Post date:** [June 5, 2008, 7:00am UTC](https://www.ruby-forum.com/t/comparing-time-zones/139583/2 "2008-06-05T07:00:18Z")

</div>

To follow up, I don’t think it’s an offset that I’m looking for. I  
want to be able to display the current datetime of a Client without  
altering the application’s time zone and basically show a “Time where  
they are” function on a client’s profile.

Thanks,

Tim K.

---

<div class="post-metadata">

**Author:** ![blaine](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/51bf81/32.png) [@blaine](https://www.ruby-forum.com/u/blaine)\
**Post date:** [June 5, 2008, 8:00am UTC](https://www.ruby-forum.com/t/comparing-time-zones/139583/3 "2008-06-05T08:00:09Z")

</div>

Why not just get the current time with the UTC, find the users timezone  
(stored in the database?) and then work it out from there?

On Thu, Jun 5, 2008 at 2:29 PM, Tim K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> On Jun 5, 12:54 am, blaine [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > Tim K.

–  
Appreciated my help?  
Recommend me on Working With Rails  
[http://workingwithrails.com/person/11030-ryan-bigg](http://workingwithrails.com/person/11030-ryan-bigg)

---

<div class="post-metadata">

**Author:** ![blaine](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/51bf81/32.png) [@blaine](https://www.ruby-forum.com/u/blaine)\
**Post date:** [June 5, 2008, 8:11am UTC](https://www.ruby-forum.com/t/comparing-time-zones/139583/4 "2008-06-05T08:11:19Z")

</div>

Because I’m not sure how I can set the users time zone as an instance  
variable and have it behave like a time zone. All that is stored in  
the time zone name, for example, “Mountain Time (US & Canada)”.  
Whether or not I’m getting the current time in UTC or not is  
irrelevant isn’t it? I mean essentially what I’m wanting to do (in  
poorly written 2am pseudo code) is something like:

c = Client.find(1)  
c.timezone.now.to\_s(:long)

There c.timezone is returning the timezone name.

I’m not really sure how to pull this off, that’s why I’m hoping  
someone can provide some guidance. I just want to know if it’s  
possible to now the current time of a timezone without have to reset  
the application’s timezone.

On Jun 5, 1:59 am, “Ryan B. (Radar)” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![blaine](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/51bf81/32.png) [@blaine](https://www.ruby-forum.com/u/blaine)\
**Post date:** [June 5, 2008, 11:33am UTC](https://www.ruby-forum.com/t/comparing-time-zones/139583/5 "2008-06-05T11:33:14Z")

</div>

On Jun 5, 7:10 am, “Tim K.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> There c.timezone is returning the timezone name.
> 
> I’m not really sure how to pull this off, that’s why I’m hoping  
> someone can provide some guidance. I just want to know if it’s  
> possible to now the current time of a timezone without have to reset  
> the application’s timezone.

In rails 2.1 this is easy

> **[Mad Mimi Email Marketing](https://madmimi.com)**
>
> Mad Mimi makes it simple to send HTML email newsletters, grow your subscribers and manage your email list. Sign up today!

Otherwise use something like the tzinfo\_timezone, which lets you do  
something like  
tz = TzinfoTimezone.new(‘London’)  
tz.utc\_to\_local(some\_utc\_time)

Fred

---

<div class="post-metadata">

**Author:** ![blaine](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/51bf81/32.png) [@blaine](https://www.ruby-forum.com/u/blaine)\
**Post date:** [June 5, 2008, 3:32pm UTC](https://www.ruby-forum.com/t/comparing-time-zones/139583/6 "2008-06-05T15:32:25Z")

</div>

Ok, I got it… I wasn’t looking at TimeZones as just another object.  
So basically to do what I was wanting it’s just something like this.

c = Client.find(1)  
tz = TimeZone.new(c.timezone)  
tz.now

Thanks 🙂

On Jun 5, 5:32 am, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![blaine](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/51bf81/32.png) [@blaine](https://www.ruby-forum.com/u/blaine)\
**Post date:** [June 5, 2008, 4:25pm UTC](https://www.ruby-forum.com/t/comparing-time-zones/139583/7 "2008-06-05T16:25:13Z")

</div>

Ryan B. has a good Railscast on timezones

> **[\#106 Time Zones in Rails 2.1 - RailsCasts](http://railscasts.com/episodes/106-time-zones-in-rails-2-1)**
>
> Short Ruby on Rails screencasts containing tips, tricks and tutorials. Great for both novice and experienced web developers.
