Skip to content

Commit

Permalink
Merge branch 'dockwa-commercial_date_strptime'
Browse files Browse the repository at this point in the history
* dockwa-commercial_date_strptime:
  support iso 8601 week dates
  add basic support for week date parsing
  • Loading branch information
joshuacronemeyer committed Feb 5, 2021
2 parents f0f4420 + 44b1cbd commit 589e515
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
6 changes: 6 additions & 0 deletions lib/timecop/time_extensions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,12 @@ def strptime_with_mock_date(str = '-4712-01-01', fmt = '%F', start = Date::ITALY
Date.new(year, mon, now.mday) + (d[:wday] - now.wday)
elsif d[:yday]
Date.new(year).next_day(d[:yday] - 1)
elsif d[:cwyear] && d[:cweek]
if d[:cwday]
Date.commercial(d[:cwyear], d[:cweek], d[:cwday])
else
Date.commercial(d[:cwyear], d[:cweek])
end
else
Date.new(year, mon)
end
Expand Down
8 changes: 8 additions & 0 deletions test/date_strptime_scenarios.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@ def test_date_strptime_with_day_of_week
assert_equal Date.strptime('Monday', '%A'), Date.new(1984, 2, 27)
end

def test_date_strptime_with_commercial_week_date
assert_equal Date.strptime('1984-09', '%G-%V'), Date.new(1984, 2, 27)
end

def test_date_strptime_with_iso_8601_week_date
assert_equal Date.strptime('1984-W09-1', '%G-W%V-%u'), Date.new(1984, 2, 27)
end

def test_date_strptime_with_invalid_date
assert_raises(ArgumentError) { Date.strptime('', '%Y-%m-%d') }
end
Expand Down

0 comments on commit 589e515

Please sign in to comment.