Correlate time zone abbreviations with time zone names

by john on November 7, 2011

There has got to be any easier way.


require 'rubygems'
require 'tzinfo'
require 'active_support'
require 'active_support/values/time_zone'

time_zone_names = [ 'Eastern Time (US & Canada)', 'Central Time (US & Canada)', 'Paris' ]
abbrev_to_zone = time_zone_names.inject({}) do |map, name|
  map[ActiveSupport::TimeZone.find_tzinfo(name).current_period.abbreviation.to_s] = name; map
end

p Time.now.zone
p abbrev_to_zone[Time.now.zone]
p abbrev_to_zone['CET']

Leave a Comment

Previous post: Rails 3.0 to 3.1 gotcha: Changed ActiveSupport::JSON.backend

Next post: ASUS AiGuru SV1T Skype VideoPhone – Worst consumer electronics product of 2011 (and 2010 and maybe 2009)