Repository navigation
Conversation
With week-based fields (%G/%g, %V, %u) and no month and day, Time.strptime returned Date.strptime(date, format).to_time, which drops the hour, minute, second, fraction and offset parsed from the same string. Take only the year and day of year from Date.strptime and pass them to make_time with the other fields, as the %U/%W branch does.
| year = d2.year | ||
| yday = d2.yday |
There was a problem hiding this comment.
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:
| 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.)
When the format uses week-based fields (
%G/%g,%V,%u) without a month and day,Time.strptimereturnsDate.strptime(date, format).to_time. That conversion keeps only the calendar date, so the hour, minute, second, fraction and UTC offset parsed from the same string are silently dropped, and the result is midnight in the local time zone.Python's
datetime.strptimealso returns2005-01-02T12:00:00+09:00for the same input and format. The same thing happens when%uis combined with%Uor%W, for exampleTime.strptime("4 5 2019 12:34", "%u %U %Y %H:%M")returns2019-02-07 00:00:00 -0500in the same time zone.This change still lets
Date.strptimeresolve the week date, but only takes the year and day of year from it and passes them tomake_timetogether with the other parsed fields, the same way the%U/%Wbranch right below it already does. Results for formats that have no time-of-day or offset fields are unchanged.I added
test_strptime_wuvg_with_time, which fails without this change. With the change, the full test suite (54 tests) passes withTZset to UTC, Europe/Berlin, America/New_York, Asia/Tokyo, Australia/Lord_Howe and Pacific/Chatham.This change was drafted with Claude Code (claude-opus-5-5); the test results above are from the runs described.