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.

8 Responses to “Timezone support in Ruby”

  1. Philippe Creytens Says:

    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.

  2. Ash Says:

    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

  3. John Rochford Says:

    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 %>

  4. Phil Ross Says:

    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
    end
    

    You can then use this class with:

      <%= time_zone_select 'user', 'timezone', nil, :model => USTimezone %>
    
  5. stirman Says:

    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!

  6. Phil Ross Says:

    TZInfo is the name of the module that contains all the timezone code.

    Basic use of TZInfo involves code like:

    tz = TZInfo::Timezone.get("America/New_York")
    local_time = tz.utc_to_local(my_time_in_utc)
    utc_time = tz.local_to_utc(my_local_time)

    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.

  7. Madhu Nallamani Says:

    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

  8. Phil Ross Says:

    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

Leave a Reply