views:

53

answers:

2

When I use return the time that the record created, it show this :

2010-01-20 15:04:40 UTC

but I want the time in my specify time zone, for example, China. Is there any convenient method in RoR?

+2  A: 

Configure your time zone in config/environment.rb to have Rails cast all timestamps to this time zone.

config.time_zone = 'Berlin'

As an alternative you can always use something like

Time.utc(2000).in_time_zone('Alaska') 

See the documentation here.

mikezter
does it supports ruby 1.8.7 , Rails 2.3.2 ? because it seems not working for me.
Ted Wong
A: 

Take a look at the TimeZone and TimeWithZone classes. They add time zone support. There's also been some additions to the Time and DateTime classes that also help deal with time zones. The documentation is given here: http://api.rubyonrails.org/classes/ActiveSupport/TimeWithZone.html.

There's also an excellent post here giving some extra details: http://ryandaigle.com/articles/2008/1/25/what-s-new-in-edge-rails-easier-timezones

tobyclemson