Skip to content

Commit

Permalink
Fix %Y date format %Y
Browse files Browse the repository at this point in the history
  • Loading branch information
joshuacronemeyer committed Feb 28, 2022
1 parent 8ca1018 commit 138bc93
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
9 changes: 7 additions & 2 deletions lib/timecop/time_extensions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,16 @@ def strptime_with_mock_date(str = '-4712-01-01', fmt = '%F', start = Date::ITALY
"supports Date::ITALY for the start argument."
end

d = Date._strptime(str, fmt) || Date.strptime_without_mock_date(str, fmt)
#If date is not valid the following line raises
Date.strptime_without_mock_date(str, fmt)

d = Date._strptime(str, fmt)
now = Time.now.to_date
year = d[:year] || now.year
mon = d[:mon] || now.mon
if d[:mday]
if d.keys == [:year]
Date.new(year)
elsif d[:mday]
Date.new(year, mon, d[:mday])
elsif d[:wday]
Date.new(year, mon, now.mday) + (d[:wday] - now.wday)
Expand Down
5 changes: 5 additions & 0 deletions test/date_strptime_scenarios.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
module DateStrptimeScenarios

#calling freeze and travel tests are making the date Time.local(1984,2,28)

def test_date_strptime_with_year
assert_equal Date.strptime('1999', '%Y'), Date.new(1999, 1, 1)
end

def test_date_strptime_without_year
assert_equal Date.strptime('04-14', '%m-%d'), Date.new(1984, 4, 14)
end
Expand Down

0 comments on commit 138bc93

Please sign in to comment.