#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, TimeZone.us_zones %>

RSS Feed for Episode Comments 33 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. Sebastian May 14, 2008 at 11:36

Usefull post!


12. 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?


13. 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).


14. 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/


15. 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.


16. 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.


17. 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?


18. 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.


19. 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.


20. kino May 23, 2008 at 01:52

Separated modes of consciousness become adjusted to multiplicities of the repeatable act of grounding of noematic descriptions.


21. 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?


22. 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.


23. 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


24. 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')


25. wotlk cd key Nov 13, 2008 at 01:27

you always made me cry!


26. 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.


28. daniel Nov 26, 2008 at 05:21

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


30. diablo 2 CD Key Dec 15, 2008 at 22:26

waiting so long


31. Starcraft CD Keys Dec 15, 2008 at 22:27

waiting


31. Warcraft 3 CD Key Dec 15, 2008 at 22:28

soso....


32. 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.


33. 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


34. jordan shoes Jan 06, 2009 at 01:16

good, thanks for your infos

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