From 1c4db4d7c34a1062e998bbdee03e992c4c5bff6d Mon Sep 17 00:00:00 2001 From: Marc-Andre Lafortune Date: Wed, 16 Mar 2011 09:43:14 -0400 Subject: Raise on invalid timezone Signed-off-by: Santiago Pastorino --- .../lib/active_support/core_ext/time/zones.rb | 20 ++++++++++++-------- activesupport/lib/active_support/railtie.rb | 2 +- 2 files changed, 13 insertions(+), 9 deletions(-) (limited to 'activesupport/lib/active_support') diff --git a/activesupport/lib/active_support/core_ext/time/zones.rb b/activesupport/lib/active_support/core_ext/time/zones.rb index ff90d7ca58..3ee6053073 100644 --- a/activesupport/lib/active_support/core_ext/time/zones.rb +++ b/activesupport/lib/active_support/core_ext/time/zones.rb @@ -39,23 +39,27 @@ class Time # Allows override of Time.zone locally inside supplied block; resets Time.zone to existing value when done. def use_zone(time_zone) - old_zone, ::Time.zone = ::Time.zone, get_zone(time_zone) - yield - ensure - ::Time.zone = old_zone + new_zone = get_zone(time_zone) + begin + old_zone, ::Time.zone = ::Time.zone, new_zone + yield + ensure + ::Time.zone = old_zone + end end private + # Returns a TimeZone instance or nil, or raises an ArgumentError for invalid timezones. def get_zone(time_zone) return time_zone if time_zone.nil? || time_zone.is_a?(ActiveSupport::TimeZone) # lookup timezone based on identifier (unless we've been passed a TZInfo::Timezone) unless time_zone.respond_to?(:period_for_local) - time_zone = ActiveSupport::TimeZone[time_zone] || TZInfo::Timezone.get(time_zone) rescue nil + time_zone = ActiveSupport::TimeZone[time_zone] || TZInfo::Timezone.get(time_zone) end # Return if a TimeZone instance, or wrap in a TimeZone instance if a TZInfo::Timezone - if time_zone - time_zone.is_a?(ActiveSupport::TimeZone) ? time_zone : ActiveSupport::TimeZone.create(time_zone.name, nil, time_zone) - end + time_zone.is_a?(ActiveSupport::TimeZone) ? time_zone : ActiveSupport::TimeZone.create(time_zone.name, nil, time_zone) + rescue TZInfo::InvalidTimezoneIdentifier + raise ArgumentError, "Invalid Timezone: #{time_zone}" end end diff --git a/activesupport/lib/active_support/railtie.rb b/activesupport/lib/active_support/railtie.rb index c2deba3b1b..910e9640d1 100644 --- a/activesupport/lib/active_support/railtie.rb +++ b/activesupport/lib/active_support/railtie.rb @@ -46,7 +46,7 @@ module ActiveSupport # If assigned value cannot be matched to a TimeZone, an exception will be raised. initializer "active_support.initialize_time_zone" do |app| require 'active_support/core_ext/time/zones' - zone_default = Time.__send__(:get_zone, app.config.time_zone) + zone_default = Time.__send__(:get_zone, app.config.time_zone) rescue nil unless zone_default raise \ -- cgit v1.2.3