Skip to content

Keep time of day and offset in Time.strptime with ISO week dates - #91

Open
0z5a wants to merge 1 commit into
ruby:masterfrom
0z5a:fix/strptime-week-date-time
Open

0z5a wants to merge 1 commit into
ruby:masterfrom
0z5a:fix/strptime-week-date-time

Conversation

@0z5a

@0z5a 0z5a commented Nov 15, 2026

Copy link
Copy Markdown

When the format uses week-based fields (%G/%g, %V, %u) without a month and day, Time.strptime returns Date.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.

require "time"

# TZ=America/New_York
Time.strptime("2004-W53-7 12:00 +0900", "%G-W%V-%u %H:%M %z")
# => 2005-01-02 00:00:00 -0500

DateTime.strptime("2004-W53-7 12:00 +0900", "%G-W%V-%u %H:%M %z")
# => #<DateTime: 2005-01-02T12:00:00+09:00 ...>

Python's datetime.strptime also returns 2005-01-02T12:00:00+09:00 for the same input and format. The same thing happens when %u is combined with %U or %W, for example Time.strptime("4 5 2019 12:34", "%u %U %Y %H:%M") returns 2019-02-07 00:00:00 -0500 in the same time zone.

This change still lets Date.strptime resolve the week date, but only takes the year and day of year from it and passes them to make_time together with the other parsed fields, the same way the %U/%W branch 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 with TZ set 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.

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.
Comment thread lib/time.rb
Comment on lines +486 to +487
year = d2.year
yday = d2.yday

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.)

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants

⌂ Proxy