Skip to content

Commit 2ce7185

Browse files
committed
Merge pull request rails#25793 from jmondo/strptime
Raise ArgumentError for bad strptime arguments
1 parent d43d8b9 commit 2ce7185

File tree

3 files changed

+18
-0
lines changed

3 files changed

+18
-0
lines changed

activesupport/CHANGELOG.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1+
* Fix `ActiveSupport::TimeZone#strptime`. Now raises `ArgumentError` when the
2+
given time doesn't match the format. The error is the same as the one given
3+
by Ruby's `Date.strptime`. Previously it raised
4+
`NoMethodError: undefined method empty? for nil:NilClass.` due to a bug.
5+
6+
Fixes #25701.
7+
8+
*John Gesimondo*
9+
10+
111
## Rails 5.0.0 (June 30, 2016) ##
212

313
* Support parsing JSON time in ISO8601 local time strings in

activesupport/lib/active_support/values/time_zone.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -447,6 +447,7 @@ def encode_with(coder) #:nodoc:
447447

448448
private
449449
def parts_to_time(parts, now)
450+
raise ArgumentError, "invalid date" if parts.nil?
450451
return if parts.empty?
451452

452453
time = Time.new(

activesupport/test/time_zone_test.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,13 @@ def test_strptime_with_day_omitted
388388
end
389389
end
390390

391+
def test_strptime_with_malformed_string
392+
with_env_tz 'US/Eastern' do
393+
zone = ActiveSupport::TimeZone['Eastern Time (US & Canada)']
394+
assert_raise(ArgumentError) { zone.strptime('1999-12-31', '%Y/%m/%d') }
395+
end
396+
end
397+
391398
def test_utc_offset_lazy_loaded_from_tzinfo_when_not_passed_in_to_initialize
392399
tzinfo = TZInfo::Timezone.get('America/New_York')
393400
zone = ActiveSupport::TimeZone.create(tzinfo.name, nil, tzinfo)

0 commit comments

Comments
 (0)