diff options
author | Josh Kalderimis <josh.kalderimis@gmail.com> | 2011-04-05 00:33:29 +0200 |
---|---|---|
committer | Santiago Pastorino <santiago@wyeworks.com> | 2011-04-05 17:12:09 -0300 |
commit | e9020b4b5dbd4a19e288c613a86c78e32010c361 (patch) | |
tree | 21cfdc61c1db53243fafc69e0afeb06df5f1df62 /activesupport/lib/active_support/core_ext/time/zones.rb | |
parent | 1c4db4d7c34a1062e998bbdee03e992c4c5bff6d (diff) | |
download | rails-e9020b4b5dbd4a19e288c613a86c78e32010c361.tar.gz rails-e9020b4b5dbd4a19e288c613a86c78e32010c361.tar.bz2 rails-e9020b4b5dbd4a19e288c613a86c78e32010c361.zip |
added find_zone and find_zone! to AS timezones and changed the AS Railtie to use find_zone! as well as adding Railtie tests
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.rb | 33 |
1 files changed, 18 insertions, 15 deletions
diff --git a/activesupport/lib/active_support/core_ext/time/zones.rb b/activesupport/lib/active_support/core_ext/time/zones.rb index 3ee6053073..0c5962858e 100644 --- a/activesupport/lib/active_support/core_ext/time/zones.rb +++ b/activesupport/lib/active_support/core_ext/time/zones.rb @@ -34,12 +34,12 @@ class Time # end # end def zone=(time_zone) - Thread.current[:time_zone] = get_zone(time_zone) + Thread.current[:time_zone] = find_zone!(time_zone) end # 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) - new_zone = get_zone(time_zone) + new_zone = find_zone!(time_zone) begin old_zone, ::Time.zone = ::Time.zone, new_zone yield @@ -48,19 +48,22 @@ class Time 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) - end - # Return if a TimeZone instance, or wrap in a TimeZone instance if a TZInfo::Timezone - 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}" + # Returns a TimeZone instance or nil, or raises an ArgumentError for invalid timezones. + def find_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) end + # Return if a TimeZone instance, or wrap in a TimeZone instance if a TZInfo::Timezone + 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 + + def find_zone(time_zone) + find_zone!(time_zone) rescue nil + end end # Returns the simultaneous time in <tt>Time.zone</tt>. @@ -78,6 +81,6 @@ class Time def in_time_zone(zone = ::Time.zone) return self unless zone - ActiveSupport::TimeWithZone.new(utc? ? self : getutc, ::Time.__send__(:get_zone, zone)) + ActiveSupport::TimeWithZone.new(utc? ? self : getutc, ::Time.find_zone!(zone)) end end |