From 033b118d3e9416f3226cddeef5f3bef86596b5ae Mon Sep 17 00:00:00 2001 From: paul Date: Fri, 25 Sep 2020 09:11:01 -0400 Subject: [PATCH 1/2] add basic support for week date parsing --- lib/timecop/time_extensions.rb | 2 ++ test/timecop_test.rb | 6 ++++++ 2 files changed, 8 insertions(+) diff --git a/lib/timecop/time_extensions.rb b/lib/timecop/time_extensions.rb index 986c35fb..e3c9a5ac 100644 --- a/lib/timecop/time_extensions.rb +++ b/lib/timecop/time_extensions.rb @@ -56,6 +56,8 @@ def strptime_with_mock_date(str = '-4712-01-01', fmt = '%F', start = Date::ITALY Date.new(year, mon, d[:mday]) elsif d[:wday] Date.new(year, mon, now.mday) + (d[:wday] - now.wday) + elsif d[:cwyear] && d[:cweek] + Date.commercial(d[:cwyear], d[:cweek]) else Date.new(year, mon, now.mday) end diff --git a/test/timecop_test.rb b/test/timecop_test.rb index c54c0e5d..183b06a8 100644 --- a/test/timecop_test.rb +++ b/test/timecop_test.rb @@ -552,6 +552,12 @@ def test_date_strptime_with_day_of_week end end + def test_date_strptime_with_commercial_week_date + Timecop.freeze(Time.new(1984,2,28)) do + assert_equal Date.strptime('1984-09', '%G-%V'), Date.new(1984, 2, 27) + end + end + def test_date_strptime_with_invalid_date begin Date.strptime('', '%Y-%m-%d') From 7c069398691ac1eb3cc4a9a2b0a91618c69d68f1 Mon Sep 17 00:00:00 2001 From: paul Date: Thu, 7 Jan 2021 17:26:06 -0500 Subject: [PATCH 2/2] support iso 8601 week dates --- lib/timecop/time_extensions.rb | 6 +++++- test/timecop_test.rb | 6 ++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/lib/timecop/time_extensions.rb b/lib/timecop/time_extensions.rb index e3c9a5ac..6fbfb164 100644 --- a/lib/timecop/time_extensions.rb +++ b/lib/timecop/time_extensions.rb @@ -57,7 +57,11 @@ def strptime_with_mock_date(str = '-4712-01-01', fmt = '%F', start = Date::ITALY elsif d[:wday] Date.new(year, mon, now.mday) + (d[:wday] - now.wday) elsif d[:cwyear] && d[:cweek] - Date.commercial(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, now.mday) end diff --git a/test/timecop_test.rb b/test/timecop_test.rb index 183b06a8..10688ffa 100644 --- a/test/timecop_test.rb +++ b/test/timecop_test.rb @@ -558,6 +558,12 @@ def test_date_strptime_with_commercial_week_date end end + def test_date_strptime_with_iso_8601_week_date + Timecop.freeze(Time.new(1984,2,28)) do + assert_equal Date.strptime('1984-W09-1', '%G-W%V-%u'), Date.new(1984, 2, 27) + end + end + def test_date_strptime_with_invalid_date begin Date.strptime('', '%Y-%m-%d')