aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport/lib/active_support/core_ext/time/zones.rb
diff options
context:
space:
mode:
authorMarc-Andre Lafortune <github@marc-andre.ca>2011-03-16 09:43:14 -0400
committerSantiago Pastorino <santiago@wyeworks.com>2011-04-05 17:11:13 -0300
commit1c4db4d7c34a1062e998bbdee03e992c4c5bff6d (patch)
tree0cc36b6637483274b1a510df21c8e227bbd56ef6 /activesupport/lib/active_support/core_ext/time/zones.rb
parent18dde7bf4ffffc93c9b32df65c168df29a898fbb (diff)
downloadrails-1c4db4d7c34a1062e998bbdee03e992c4c5bff6d.tar.gz
rails-1c4db4d7c34a1062e998bbdee03e992c4c5bff6d.tar.bz2
rails-1c4db4d7c34a1062e998bbdee03e992c4c5bff6d.zip
Raise on invalid timezone
Signed-off-by: Santiago Pastorino <santiago@wyeworks.com>
Diffstat (limited to 'activesupport/lib/active_support/core_ext/time/zones.rb')
-rw-r--r--activesupport/lib/active_support/core_ext/time/zones.rb20
1 files changed, 12 insertions, 8 deletions
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 <tt>Time.zone</tt> locally inside supplied block; resets <tt>Time.zone</tt> 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