Timezone support in Ruby
I spent some time at the weekend looking for a library in Ruby that would support converting times in UTC to particular named timezones, such as Europe/London or America/New_York, taking care of whether daylight savings was in force.
Unfortunately, I couldn’t find one, so I had to write my own instead. The result, TZInfo, is now available at RubyForge.
October 19th, 2005 at 11:44 pm
Phil
Great job… we are developing a site where we heavily rely on UTC ->user’s timezone calculations. Too bad that we cannot get a dropdown in Rails with TZInfo that simulates the look-and-feel of the standard Rails TimeZone. I feel that Rails TimeZone dropdown is somehow more user-friendly by indicating the gmt +/- times for locations, rather than an alphabetical sort.
December 1st, 2005 at 1:07 pm
Hey dude, long time no communicat-i-o. I finally noticed the ‘linking to’ list in Wordpress
Um….. I got nothin, hope all is well in English Land, say hi to the other guys from me!
-
Australian Ash
December 5th, 2005 at 1:44 am
Phil - Great TimeZone library!
What would be your approach on displaying just the U.S. timezones when using time_zone_select ?
The code below returns all counties:
TZInfo::Timezone %>
December 5th, 2005 at 10:36 am
To use time_zone_select to display just US timezones, you will have to create a class with an all method that time_zone_options_for_select can use:
class USTimezone def initialize(timezone) @timezone = timezone end def name @timezone.name end def to_s @timezone.to_s end def self.all TZInfo::Country.get('US').zones.sort.map {|tz| USTimezone.new(tz)} end endYou can then use this class with:
June 14th, 2006 at 5:00 pm
I’m a little confused as to how to implement this, and the how-to by Scott seemed to skip over some of the initial set up. After I require the gem, do I create a TZInfo model?
I desperately need to add this functionality to a little web app that is all done, minus timezone offset support!
Totally stoked that this TZ Library exists, thanks for your hard work!
June 14th, 2006 at 7:37 pm
TZInfo is the name of the module that contains all the timezone code.
Basic use of TZInfo involves code like:
Scott’s how-to describes how you can add an attribute to an existing User model that returns the timezone. This is done using the Rails composed_of method. In the example, the field time_zone in the users table contains the timezone identifier for the user (e.g. America/New_York). composed_of exposes a TZInfo::Timezone object as an attribute called tz. You can then call utc_to_local, local_to_utc, etc on this timezone to convert the time.
May 15th, 2008 at 4:47 pm
Hi Philip ,
Is there anyway I can display time in “US-Central†or “US-Eastern†format instead of “ America-New york†or “ Europe-Athens†format at the top of drop down list using Tzinfo .
I am presently using “time_zone_select†which displays US zones on top of drop down in “ America-New york†format .
TZInfo::Timezone, :default => “America/Chicago” )%>
I can display US zones in “ (GMT - 06:00 ) CentralTime (US & Canada)†by using standard rails TimeZone ( ) . But , I will have problem when I use “utc_to_local†method .
Thanks,
Madhu Nallamani
May 15th, 2008 at 10:35 pm
Madhu,
If you are using Rails 2.0 or earlier, you might want to take a look at the TzinfoTimezone plugin (http://agilewebdevelopment.com/plugins/tzinfo_timezone). This replaces the built in Rails TimeZone class with one that uses TZinfo. With this plugin installed, you’ll still get Rails’ standard list of timezones with the GMT offset when you use time_zone_options_for_select or time_zone_select. However, the TimeZone object has additional utc_to_local, local_to_utc and tzinfo methods to allow you to access the TZInfo functionality.
Edge Rails (2.1) now bundles a cut-down version of TZInfo and includes the functionality of TzinfoTimezone. See http://mad.ly/2008/04/09/rails-21-time-zone-support-an-overview/ for more information.
Regards,
Phil