From 3c8dd00c96510a21d9bd9f82a4df5cafe7b9feeb Mon Sep 17 00:00:00 2001 From: Jeremy Evans Date: Fri, 21 Jun 2019 10:33:06 -0700 Subject: [PATCH] Support %U/%u/%W/%w formats in Time.strptime These formats were documented as supported, but were not actually supported. If %U/%W is specified without yday and mon/mday are not specified, then Date.strptime is used to get the appropriate yday. Fixes [Bug #14241] --- lib/time.rb | 6 +++++- test/test_time.rb | 7 +++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/lib/time.rb b/lib/time.rb index 81c0ebd7ba..efe9df4bc7 100644 --- a/lib/time.rb +++ b/lib/time.rb @@ -457,7 +457,11 @@ def strptime(date, format, now=self.now) else year = d[:year] year = yield(year) if year && block_given? - t = make_time(date, year, d[:yday], d[:mon], d[:mday], d[:hour], d[:min], d[:sec], d[:sec_fraction], d[:zone], now) + yday = d[:yday] + if (d[:wnum0] || d[:wnum1]) && !yday && !(d[:mon] && d[:mday]) + yday = Date.strptime(date, format).yday + end + t = make_time(date, year, yday, d[:mon], d[:mday], d[:hour], d[:min], d[:sec], d[:sec_fraction], d[:zone], now) end t end diff --git a/test/test_time.rb b/test/test_time.rb index e4fea31945..4f48a0b188 100644 --- a/test/test_time.rb +++ b/test/test_time.rb @@ -528,6 +528,13 @@ def test_strptime_p assert_equal(15, t.hour) end + def test_strptime_wu + assert_equal(Time.local(2019, 1, 30), Time.strptime("3 4 2019", "%w %W %Y")) + assert_equal(Time.local(2019, 2, 7), Time.strptime("4 5 2019", "%u %U %Y")) + assert_equal(Time.local(2019, 1, 28), Time.strptime("4 2019", "%W %Y")) + assert_equal(Time.local(2019, 2, 3), Time.strptime("5 2019", "%U %Y")) + end + def test_nsec assert_equal(123456789, Time.parse("2000-01-01T00:00:00.123456789+00:00").tv_nsec) end -- 2.21.0