Raise ArgumentError for bad strptime arguments

This commit is contained in:
John Gesimondo 2016-07-11 20:48:56 -07:00
parent 108a407b7d
commit 424e961be7
3 changed files with 17 additions and 0 deletions

View File

@ -1,3 +1,12 @@
* Fix `ActiveSupport::TimeZone#strptime`. Now raises `ArgumentError` when the
given time doesn't match the format. The error is the same as the one given
by Ruby's `Date.strptime`. Previously it raised
`NoMethodError: undefined method empty? for nil:NilClass.` due to a bug.
Fixes #25701.
*John Gesimondo*
* `travel/travel_to` travel time helpers, now raise on nested calls,
as this can lead to confusing time stubbing.

View File

@ -447,6 +447,7 @@ module ActiveSupport
private
def parts_to_time(parts, now)
raise ArgumentError, "invalid date" if parts.nil?
return if parts.empty?
time = Time.new(

View File

@ -388,6 +388,13 @@ class TimeZoneTest < ActiveSupport::TestCase
end
end
def test_strptime_with_malformed_string
with_env_tz 'US/Eastern' do
zone = ActiveSupport::TimeZone['Eastern Time (US & Canada)']
assert_raise(ArgumentError) { zone.strptime('1999-12-31', '%Y/%m/%d') }
end
end
def test_utc_offset_lazy_loaded_from_tzinfo_when_not_passed_in_to_initialize
tzinfo = TZInfo::Timezone.get('America/New_York')
zone = ActiveSupport::TimeZone.create(tzinfo.name, nil, tzinfo)