Search code examples
ruby-on-railsrubyruby-on-rails-3

How to translate timezone full names to tz abbreviations?


In a Rails 3.x app I need to display time zone abbreviations (EST, PST, CST, etc.) rather than the full time zone name. I've seen a number of discussions that seem to address the issue, but in overly verbose manners.

Is there a gem or a very concise method to handle this that could be used to map them properly?


Solution

  • For the current time zone:

    Time.zone.now.strftime('%Z')
    

    Or for a list of time zones:

    ActiveSupport::TimeZone.us_zones.map do |zone|
      Time.now.in_time_zone(zone).strftime('%Z')
    end
    

    It works, but it still seems like there should be a better way...