Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify Time.find_timezone! logic #41957

Merged
merged 1 commit into from Apr 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 2 additions & 17 deletions activesupport/lib/active_support/core_ext/time/zones.rb
Expand Up @@ -80,24 +80,9 @@ def use_zone(time_zone)
# Time.find_zone! false # => false
# Time.find_zone! "NOT-A-TIMEZONE" # => ArgumentError: Invalid Timezone: NOT-A-TIMEZONE
def find_zone!(time_zone)
if !time_zone || time_zone.is_a?(ActiveSupport::TimeZone)
time_zone
else
# Look up the timezone based on the 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 time_zone unless time_zone

# Return if a TimeZone instance, or wrap in a TimeZone instance if a TZInfo::Timezone
if time_zone.is_a?(ActiveSupport::TimeZone)
time_zone
else
ActiveSupport::TimeZone.create(time_zone.name, nil, time_zone)
end
end
rescue TZInfo::InvalidTimezoneIdentifier
raise ArgumentError, "Invalid Timezone: #{time_zone}"
ActiveSupport::TimeZone[time_zone] || raise(ArgumentError, "Invalid Timezone: #{time_zone}")
end

# Returns a TimeZone instance matching the time zone provided.
Expand Down
4 changes: 4 additions & 0 deletions activesupport/lib/active_support/values/time_zone.rb
Expand Up @@ -229,12 +229,16 @@ def all
# Returns +nil+ if no such time zone is known to the system.
def [](arg)
case arg
when self
arg
when String
begin
@lazy_zones_map[arg] ||= create(arg)
rescue TZInfo::InvalidTimezoneIdentifier
nil
end
when TZInfo::Timezone
@lazy_zones_map[arg.name] ||= create(arg.name, nil, arg)
when Numeric, ActiveSupport::Duration
arg *= 3600 if arg.abs <= 13
all.find { |z| z.utc_offset == arg.to_i }
Expand Down
16 changes: 13 additions & 3 deletions activesupport/test/core_ext/time_with_zone_test.rb
Expand Up @@ -1243,9 +1243,19 @@ def test_find_zone_without_bang_returns_nil_if_time_zone_can_not_be_found
end

def test_find_zone_with_bang_raises_if_time_zone_can_not_be_found
assert_raise(ArgumentError) { Time.find_zone!("No such timezone exists") }
assert_raise(ArgumentError) { Time.find_zone!(-15.hours) }
assert_raise(ArgumentError) { Time.find_zone!(Object.new) }
error = assert_raise(ArgumentError) { Time.find_zone!("No such timezone exists") }
assert_equal "Invalid Timezone: No such timezone exists", error.message

error = assert_raise(ArgumentError) { Time.find_zone!(-15.hours) }
assert_equal "Invalid Timezone: -54000", error.message

error = assert_raise(ArgumentError) { Time.find_zone!(Object.new) }
assert_match "invalid argument to TimeZone[]", error.message
end

def test_find_zone_with_bang_doesnt_raises_with_nil_and_false
assert_nil Time.find_zone!(nil)
assert_equal false, Time.find_zone!(false)
end

def test_time_zone_setter_with_find_zone_without_bang
Expand Down
6 changes: 6 additions & 0 deletions activesupport/test/time_zone_test.rb
Expand Up @@ -69,6 +69,12 @@ def test_from_duration_to_map
assert_instance_of ActiveSupport::TimeZone, ActiveSupport::TimeZone[-480.minutes] # PST
end

def test_from_tzinfo_to_map
tzinfo = TZInfo::Timezone.get("Europe/London")
assert_instance_of ActiveSupport::TimeZone, ActiveSupport::TimeZone[tzinfo]
assert_same ActiveSupport::TimeZone[tzinfo], ActiveSupport::TimeZone[tzinfo]
end

ActiveSupport::TimeZone.all.each do |zone|
name = zone.name.downcase.gsub(/[^a-z]/, "_")
define_method("test_from_#{name}_to_map") do
Expand Down