From 1b742ea9b100278dd818879a4162dab1a2c0e6d4 Mon Sep 17 00:00:00 2001 From: Santiago Pastorino Date: Fri, 2 Apr 2010 16:44:38 -0300 Subject: delegate unknown timezones to TZInfo --- activesupport/lib/active_support/values/time_zone.rb | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) (limited to 'activesupport/lib/active_support/values') diff --git a/activesupport/lib/active_support/values/time_zone.rb b/activesupport/lib/active_support/values/time_zone.rb index b9766a236a..03b324764b 100644 --- a/activesupport/lib/active_support/values/time_zone.rb +++ b/activesupport/lib/active_support/values/time_zone.rb @@ -310,13 +310,15 @@ module ActiveSupport end def tzinfo - @tzinfo ||= find_tzinfo + @tzinfo ||= TimeZone.find_tzinfo(name) end # TODO: Preload instead of lazy load for thread safety - def find_tzinfo + def self.find_tzinfo(name) require 'tzinfo' unless defined?(::TZInfo) - ::TZInfo::Timezone.get(MAPPING[name]) + ::TZInfo::Timezone.get(MAPPING[name] || name) + rescue TZInfo::InvalidTimezoneIdentifier + nil end unless const_defined?(:ZONES) @@ -330,7 +332,6 @@ module ActiveSupport end ZONES.sort! ZONES.freeze - ZONES_MAP.freeze US_ZONES = ZONES.find_all { |z| z.name =~ /US|Arizona|Indiana|Hawaii|Alaska/ } US_ZONES.freeze @@ -361,7 +362,7 @@ module ActiveSupport def [](arg) case arg when String - ZONES_MAP[arg] + ZONES_MAP[arg] ||= lookup(arg) when Numeric, ActiveSupport::Duration arg *= 3600 if arg.abs <= 13 all.find { |z| z.utc_offset == arg.to_i } @@ -375,6 +376,12 @@ module ActiveSupport def us_zones US_ZONES end + + private + + def lookup(name) + (tzinfo = find_tzinfo(name)) && create(tzinfo.name.freeze) + end end end end -- cgit v1.2.3