#106
May 14, 2008

Time Zones in Rails 2.1

In the past, time zones have been very difficult to work with, but no longer! Rails 2.1 brings with it great time zone support as you can see in this episode.
Tags: rails-2.1
Download (8.3 MB, 6:07)
alternative download for iPod & Apple TV (7.1 MB, 6:07)

Resources

rake time:zones:local
# config/environment.rb
config.time_zone = "Pacific Time (US & Canada)"

# controllers/application.rb
before_filter :set_user_time_zone

private

def set_user_time_zone
  Time.zone = current_user.time_zone if logged_in?
end

# in migration file
add_column :users, :time_zone, :string
<!-- users/new.html.erb -->
<%= f.time_zone_select :time_zone, ActiveSupport::TimeZone.us_zones %>

RSS Feed for Episode Comments 48 comments

1. Bryce May 14, 2008 at 00:42

What happens if in time_zone_select you want a different set of Zones to be up the top (i.e a non US application)


2. Pablo Formoso May 14, 2008 at 01:12

This's what I waiting for :) Thanks


3. Eric May 14, 2008 at 01:15

I feel very bad when I see such shortcut : #us_zones. US is not the center of the world.

Do we still think that US is more important than other countries ? Why not #russia_zones or #china_zones ? or at least #euro_zones ? or if we really want to be america centric why not #canada_zones ?

We shouldn't have such shortcut in the core, we should *never* have US be a prioritized value or even a default value. This is very bad practice.

Worse, we have a #us_zones and we don't even have a #utc_timezone or #gmt_timezone shortcut.


4. dude May 14, 2008 at 01:50

lolz. then contribute one if you like. :)


5. Keith May 14, 2008 at 05:00

Why exactly are you so worked up over this issue? There are rake tasks for all timezones and one that guesses your local timezone based on your system time.

While I agree having some degree of internationalization would be nice Rails has many other instances where specific internationalization support is not offered. The timezone Internationalization issue isn't really out of line with the existing core development philosophy.


6. jpemberthy May 14, 2008 at 05:18

exactly what i was waiting for :P thx


7. David May 14, 2008 at 06:04

When you use the time_zone_select in Rails 2.1 it also displays the time zones as an offset from UTC rather than GMT.

e.g. "(UTC-10:00) Hawaii" instead of "(GMT-10:00) Hawaii"

Its only a minor point, but UTC wont mean much to end users whereas GMT would.

Does anyone know if you can change this?


8. Ryan Bates May 14, 2008 at 08:14

@Eric, I would like to see more support for international users as well. Please do consider submitting a ticket/patch on this.

@David, this is actually generated by the TimeZone#to_s method, so you can override that if you want something different. That feels a bit hackish, so alternatively you can subclass TimeZone and pass that to the select field. I'm not sure if this will work though:
http://pastie.caboo.se/196823

You may want to submit ticket/patch for this.


9. Morten May 14, 2008 at 08:55

No need for the tzinfo gem?


10. Morten May 14, 2008 at 09:00

In reply to myself, it's bundled in Rails 2.1: http://mad.ly/2008/04/09/rails-21-time-zone-support-an-overview/#more-26

Bliss.


11. Carl May 14, 2008 at 13:06

I'm still having the sound issues with this one. Does anyone else have a problem right about 37 seconds into the cast?


12. Matt May 14, 2008 at 15:56

Thanks again Ryan. I've turned to your site a primary source of learning rails ideas (and rails in general).

Have you (would you) consider posting the projects you use in the screen casts on your site?
I know there are many times, I would love to be able to peer through the code itself (in the event I missed you explaining something previously).


13. Ryan Bates May 14, 2008 at 21:53

@Matt, I'm just starting to do this actually. You can find the code here:

http://github.com/ryanb/railscasts-episodes/


14. Matt May 15, 2008 at 17:54

Quoting one of my favorite simpsons episodes:
"Dear die hard...you rock! P.S. Do you know mad max?"

Seriously...you rock.


15. Chris Hobbs May 16, 2008 at 16:40

I hope time zone support is good in Active Resource for rails 2.1. A lot of bugs existed in 2.0.2.


16. Arya A. May 19, 2008 at 11:21

In the before filter, you change the value of Time.zone, does it automatically get changed back at the end of the request? So if a logged in user with their time zone set to a Russian TZ logs in and then an non-logged in user goes to the site immediately after that, do they see the previous user's time zone?


17. Ryan Bates May 19, 2008 at 14:31

@Arya, good question. I believe Time.zone does reset when the request ends. I have read this elsewhere. However I honestly have not fully tested it.


18. Geoff Buesing May 20, 2008 at 10:29

@Ryan: excellent screencast!

@Arya: when you set Time.zone, this zone is stored in the Thread.current hash, so there's no possibility that the zone set for one request will leak into another.

@David: regarding TimeZone#to_s, I'm actually inclined to change this back to showing GMT -- I've started a thread about this on the Rails Core list.


19. Walter Horstman May 28, 2008 at 00:58

I recently implemented time zone support using Rails 2.0.2 and TzInfo and I think it uses different time zone names or am I incorrect? If so, is there then a way of converting my existing user accounts to use the other, default time zone names?


20. Nico Jul 09, 2008 at 22:14

The new timezone support is really good, however so far I prefer to write out times in UTC and then to use javascript to substitute the correct time using the timezone set on the client. This of cause requires the clients timezone to be set correctly, but that is usually the case I think.

I prefer this solution for two reasons:

1. No need for the user to set his timezone in the app.

2. People are travelling a lot nowadays. When I'm opening a website while in a different country, I like to see times in local time, not my "home" time.

Maybe it is also possible to first render the page without the times and then using a remote call to pass in the user timezone to the rails app which than renders the correct times using ajax. However, the javascript client site solution is probably faster as it saves the second server request.


21. chris Jul 13, 2008 at 01:52

Anyone know why the following code defaults to selecting the UTC time zone (instead of the config.time_zone or Time.zone setting):
<code>
<%= f.time_zone_select :time_zone, TimeZone.us_zones %>
</code>

Many thanks,
Chris


22. joahking Jul 30, 2008 at 01:28

some ActiveRecord caveat to care about regarding UTC conversions:
>> time_str = "2008-07-30T09:44:28+02:00"
>> time = Time.parse time_str
if used then like this:
>> XXX.find_all_by_YYY('yyy', :conditions => ["created_at >= ?", time_str])
you see this query in the log:
SELECT * FROM "XXX" WHERE (created_at >= '2008-07-30T09:44:28+02:00')
same happens with:
>> XXX.find_all_by_YYY('yyy', :conditions => ["created_at >= ?", time])
in the mean time, USE this:
>> XXX.find_all_by_YYY('yyy', :conditions => ["created_at >= ?", time.utc])
then you get this perfectly in the log:
SELECT * FROM "messages" WHERE (created_at >= '2008-07-30 07:44:28')


23. Ilia Nov 15, 2008 at 11:08

Is there any way to get the name of the zone DateTime object is in that is in the format used by 'in_time_zone'?

I want to do datetime1.in_time_zone(datetime2.zone) but zone method returns the name in a different format.

I would have thought this to be trivial, but I've been banging my head for hours and can't find a clean way to convert one DateTime to another DateTime's zone.

Ultimately, I want to compare two DateTimes after taking the time zone into account. A normal comparison doesn't seem to do it: start_date_time < 1.hours.from_now doesn't work if the user is in a different time zone from the server.


24. daniel Nov 26, 2008 at 05:21

Placing link rel no follow will make the spam go away.


25. Marko Dec 30, 2008 at 12:19

I'm getting the following error on my view:
"uninitialized constant ActionView::Base::CompiledTemplates::TimeZone"

If I remove the TimeZone.us_zones parameter from the selection list the error goes away -- but then I get all time zones in the list. Any idea what could be wrong?

I'm using Rails 2.2.2 so maybe there's an issue with this version.


26. Marko Dec 30, 2008 at 12:27

Found the answer to my own question.

In Rails 2.2.2 TimeZone lives in ActiveSupport, so I had to change the parameter to:

ActiveSupport::TimeZone.us_zones


27. Brian Feb 23, 2009 at 07:57

I've got a strange problem: I followed these steps, and everything works perfectly when I save to the db and pull records out of the db: that nice Pacific Time translation happens automatically.

The problem is that when I use Time.now I get Pacific Time -0800, which is a double offset! It should either be UTC -0800 or simply Pacific Time. Any thoughts?


28. Jonathan Sep 01, 2009 at 16:17

Ryan,
As usual great screencast. I borrowed from this teaching some time ago, however, w/Rails 2.3.2 time_zone is no longer a defined method (e.g. NoMethodError (undefined method `time_zone')
...and, thus far, I have wasted 30 minutes trying to find it's equivalent.

Thanks in advance for any pointers,
Jonathan


29. Jonathan Sep 01, 2009 at 16:30

oops; nevermind, my mistake. Just forget I mentioned it. (my issue was actually the user.time_zone field in the database)


30. discount ugg Nov 19, 2009 at 18:18

Real nice article!Love them very much.This blog so wonderful.thanks for sharing!


31. ellen Nov 25, 2009 at 21:40

http://www.trade9shoes.com
http://www.tradeshoes9.com
http://www.alltopshoes.com
http://www.mynikeairmax.com


32. huangyu Nov 30, 2009 at 00:46

I just want to say,the article you write is great,today is my first visit here,and i'll come again to read your new blog.
Come on, meteor! You can make it! Do you want to know some information of tibia gold on sale! share my site with you.
http://www.10minget.com
http://www.10minget.com/tibia.htm
[url=http://www.10minget.com/] tibia gold [/url]
[url=http://www.10minget.com/tibia.htm] buy tibia gold [/url]
[url=http://www.10minget.com/tibia-gold.htm] cheap tibia gold [/url]
buy Tibia Gold cheap on http://www.10minget.com
<a href=" http://www.tibiaitem.com/Tibia-gold.htm"> tibia gold</a>
<a href=" http://www.gamezmoney.com/tibia-gold.htm"> tibia gold</a>


33. konya chat Dec 18, 2009 at 03:44

It is very useful of your post.Good for it best.Thank you.


34. freerealmscoins Jan 12, 2010 at 16:38

It is very good.I like to see it.Thank you.have a good time.


35. wholesale scarves Jan 13, 2010 at 22:47

Such a good article, caught my sympathy!
-


36. wholesale nike shoes Jan 13, 2010 at 22:47

A very good article, I will always come in.


37. Tibia gold Jan 17, 2010 at 17:08

It is very good of this post:Time Zones in Rails 2.1.I will try it.Thank you for the share.Have a good time.


38. Nike Air Max 90 Jan 24, 2010 at 18:39

Thank you for sharing.Nice post.


39. Nike Air Max 90 Jan 27, 2010 at 05:08

Thank you for sharing.Nice post


40. MBT Fanaka GTX Jan 27, 2010 at 05:11

A very good article, I will always come in.


41. Nike ACG Air Max Goadome Jan 27, 2010 at 05:12

very useful of your post.Good for it best.Thank you.


42. Nike Air Max Tailwind 2010 Jan 27, 2010 at 05:15

Just forget I mentioned it. (my issue was actually the user.


43. Nike Air Max TN Plus Jan 27, 2010 at 05:18

In the before filter, you change the value of Time.zone


44. Air Jordan 2010 Jan 27, 2010 at 05:19

Thanks in advance for any pointers,
Jonathan


45. Affliction Denim Jeans Jan 27, 2010 at 05:29

I just want to say,the article you write is great!


46. Ed Hardy Boots Jan 27, 2010 at 05:31

It is very good.I like to see it.Thank you.have a good time.


47. Bailey Button 5803 Jan 27, 2010 at 05:34

While I agree having some degree of internationalization would be nice Rails has many other instances where specific internationalization support is not offered


48. Nike Air Max 90 Jan 28, 2010 at 18:13

Nice post,nice sharing.Thanks!

Add your comment:

(SKIP THIS ONE)

(required)

(not shown)


(use pastie or gist for code)

sponsored by:
if you want to help:
required:
Get Quicktime Player
Give Back to Open Source