5.10. Setting a Default Time Zone

Every time a model's datetime attribute is saved in the database, this becomes stored as a UTC time. The problem is that the dates and times presented to the visitor and the blog author are UTC times, rather than a predefined local time zone. When you click New Article you'll see that the default date and time selected are based on UTC, not the local time zone. This implies that if you want to schedule a post in the future for a certain "local time," you'll have to offset it (in your head) to obtain the UTC time. Likewise, having chosen the "long ordinal" presentation of the date and time, when showing an article the readers of your blog will just see an absolute numeric representation of the time and won't assume that it's a UTC time.

Figure 5.16. Figure 5-16

Depending on your application's requirements, this may or may not be a big deal. Luckily for you, Rails 2.2.2 provides excellent support for time zones and setting a predefined time zone for your application is extremely easy. I live in Toronto, so my time zone is Eastern Time (US & Canada). You can see a list of local time zones by running the rake task time:zones:local:

C:\projects\blog> rake time:zones:local
(in C:/projects/blog)

* UTC −05:00 *
Bogota
Eastern Time (US & Canada)
Indiana (East)
Lima
Quito

You can use rake time:zones:us to obtain only the US time zones matching the local ...

Get Ruby on Rails® for Microsoft Developers now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.