Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion lib/time.rb
Original file line number Diff line number Diff line change
Expand Up @@ -482,7 +482,9 @@ def strptime(date, format, now=self.now)
yday = d[:yday]
if (d[:cwyear] && !year) || ((d[:cwday] || d[:cweek]) && !(d[:mon] && d[:mday]))
# make_time doesn't deal with cwyear/cwday/cweek
return Date.strptime(date, format).to_time
d2 = Date.strptime(date, format)
year = d2.year
yday = d2.yday
Comment on lines +486 to +487

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Passing yday sends these through make_time's yday branch, which gets the day wrong for late times with a negative offset. It guesses mon, day = (yday-1).divmod(31) (e.g. Feb 29/31), and apply_offset on that invalid date moves it by a day or more:

Time.strptime("2030-W09-5 20:18:28 -0345", "%G-W%V-%u %H:%M:%S %z")
# => 2030-03-02 20:18:28 -0345   (2030-W09-5 is Friday 2030-03-01)
Time.strptime("1985-W09-7 14:58:14 -1130", "%G-W%V-%u %H:%M:%S %z")
# => 1985-03-06 14:58:14 -1130   (should be 1985-03-03)

Before this PR the date part of these was right (only the time was lost), so it's a regression for them. In a 40k random check against DateTime.strptime (%G-W%V-%u, %g %V %u, %u %U %Y with times and offsets) 55 came out on the wrong day, same count with TZ=UTC, America/New_York and Australia/Lord_Howe.

Since d2 already has the month and day, passing those avoids the yday guess. With this, the same check has 0 mismatches and the 54 tests pass:

Suggested change
year = d2.year
yday = d2.yday
year, yday = d2.year, nil
d[:mon], d[:mday] = d2.mon, d2.mday

(The %j path has the same problem on master, e.g. Time.strptime("1985-062 14:58:14 -1130", "%Y-%j %H:%M:%S %z") gives Mar 6. That's separate from this PR; I'll send a fix for make_time on its own.)

end
if (d[:wnum0] || d[:wnum1]) && !yday && !(d[:mon] && d[:mday])
yday = Date.strptime(date, format).yday
Expand Down
8 changes: 8 additions & 0 deletions test/test_time.rb
Original file line number Diff line number Diff line change
Expand Up @@ -587,6 +587,14 @@ def test_strptime_wuvg
assert_equal(Time.local(2015, 12, 28), Time.strptime("53 15", "%V %g"))
end

def test_strptime_wuvg_with_time
assert_equal(Time.local(2019, 1, 1, 10, 30, 15), Time.strptime("2019-W01-2 10:30:15", "%G-W%V-%u %H:%M:%S"))
assert_equal(Time.local(2019, 2, 7, 12, 34), Time.strptime("4 5 2019 12:34", "%u %U %Y %H:%M"))
t = Time.strptime("2004-W53-7 12:00:00.5 +0900", "%G-W%V-%u %H:%M:%S.%N %z")
assert_equal(Time.new(2005, 1, 2, 12, 0, 1/2r, "+09:00"), t)
assert_equal(9 * 3600, t.utc_offset)
end

def test_nsec
assert_equal(123456789, Time.parse("2000-01-01T00:00:00.123456789+00:00").tv_nsec)
end
Expand Down
Loading