Skip to content

Commit d4186a7

Browse files
committed
Raise ArgumentError from TimeZone.iso8601 when invalid value can be parsed by Date._iso8601
Date._iso8601 will return a hash for some values eg. '12936' but this will not contain the expected :mon and :mday keys. Check for these keys and raise an ArgumentError if they aren't present as this is consistent with other invalid input behaviour.
1 parent 6daa2d8 commit d4186a7

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

activesupport/lib/active_support/values/time_zone.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -383,8 +383,6 @@ def at(*args)
383383
def iso8601(str)
384384
parts = Date._iso8601(str)
385385

386-
raise ArgumentError, "invalid date" if parts.empty?
387-
388386
time = Time.new(
389387
parts.fetch(:year),
390388
parts.fetch(:mon),
@@ -400,6 +398,9 @@ def iso8601(str)
400398
else
401399
TimeWithZone.new(nil, self, time)
402400
end
401+
402+
rescue KeyError
403+
raise ArgumentError, "invalid date"
403404
end
404405

405406
# Method for creating new ActiveSupport::TimeWithZone instance in time zone

activesupport/test/time_zone_test.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,16 @@ def test_iso8601_with_ambiguous_time
343343
assert_equal Time.utc(2014, 10, 25, 22, 0, 0), zone.parse("2014-10-26T01:00:00")
344344
end
345345

346+
def test_iso8601_with_invalid_value_parseable_by_date__iso8601
347+
zone = ActiveSupport::TimeZone["Eastern Time (US & Canada)"]
348+
349+
exception = assert_raises(ArgumentError) do
350+
zone.iso8601("12936")
351+
end
352+
353+
assert_equal "invalid date", exception.message
354+
end
355+
346356
def test_parse
347357
zone = ActiveSupport::TimeZone["Eastern Time (US & Canada)"]
348358
twz = zone.parse("1999-12-31 19:00:00")

0 commit comments

Comments
 (0)