Improve compatibility with `date` versions `3.2.1`, `3.1.2`, `3.0.2` and `2.0.1`

Historically `Date._iso8601(nil)` returns `{}`.

But in these versions it raises a `TypeError` changing the exception type
of `ActiveSupport::TimeZone#iso8601(nil)`, which may break some applications.
This commit is contained in:
Jean Boussier 2021-11-15 12:02:05 +01:00
parent 3e2f74c186
commit 1b0dca131b
2 changed files with 15 additions and 0 deletions

View File

@ -385,6 +385,11 @@ module ActiveSupport
# If the string is invalid then an +ArgumentError+ will be raised unlike +parse+
# which usually returns +nil+ when given an invalid date string.
def iso8601(str)
# Historically `Date._iso8601(nil)` returns `{}`, but in the `date` gem versions `3.2.1`, `3.1.2`, `3.0.2`,
# and `2.0.1`, `Date._iso8601(nil)` raises `TypeError` https://github.com/ruby/date/issues/39
# Future `date` releases are expected to revert back to the original behavior.
raise ArgumentError, "invalid date" if str.nil?
parts = Date._iso8601(str)
year = parts.fetch(:year)

View File

@ -306,6 +306,16 @@ class TimeZoneTest < ActiveSupport::TestCase
assert_equal "invalid date", exception.message
end
def test_iso8601_with_nil
zone = ActiveSupport::TimeZone["Eastern Time (US & Canada)"]
exception = assert_raises(ArgumentError) do
zone.iso8601(nil)
end
assert_equal "invalid date", exception.message
end
def test_iso8601_with_missing_time_components
zone = ActiveSupport::TimeZone["Eastern Time (US & Canada)"]
twz = zone.iso8601("1999-12-31")