Quick Tip: Rails 2.1 Time Zones
My last quick tip involved setting your time zone in Ubuntu Hardy, so now, how do you set your time zone in a Ruby on Rails application? Rails 2.1 makes it much easier to manage time zone settings than it was previously.
Add the following to your environment configuration file:
config/environment.rb
config.time_zone = 'Arizona'
Replace Arizona with your own time zone. You can find a list of valid values by running any of the following rake tasks:
rake time:zones:all
rake time:zones:local
rake time:zones:us
Your data will still be stored in UTC time, but it will be converted into the specified time zone when it is type cast on retrieval.
Update: If you set both your server’s time zone and your applications time zone then you may see some incorrect times. I believe this is because you’ll be storing local times and the Rails app will be trying to convert them to local – a double conversion. So be careful.
References
- What’s New in Edge Rails: Easier Timezones
- Rails 2.1 Time Zone Support: An Overview
- Updating MySQL DATETIMEs for Rails 2.1 Time Zones
More of my rantings
- Quick Tip: Override Rails Generated URLs
- Quick Tip: Form Partials
- Quick Tip: Rails Singularization of -ess
These might also interest you
- 10 Excel Tips to Make Your Life Easier – Part 2 (Tomas Carrillo)
- 10 Excel Tips to Make Your Life Easier – Part 1 (Tomas Carrillo)
- 100 free websites worth $100,000 (No Pun Intended)