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
19 changes: 11 additions & 8 deletions lib/time.rb
Original file line number Diff line number Diff line change
Expand Up @@ -213,14 +213,17 @@ def make_time(date, year, yday, mon, day, hour, min, sec, sec_fraction, zone, no
unless (1..366) === yday
raise ArgumentError, "yday #{yday} out of range"
end
mon, day = (yday-1).divmod(31)
mon += 1
day += 1
t = make_time(date, year, nil, mon, day, hour, min, sec, sec_fraction, zone, now)
diff = yday - t.yday
return t if diff.zero?
day += diff
if day > 28 and day > (mday = month_days(off_year, mon))
# Find the month and day directly. A guessed date like Feb 31
# isn't valid, and apply_offset can move it to the wrong day.
unless year
if now and now.respond_to?(:getlocal)
year = (off ? now.getlocal(off) : now.getlocal).year

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I'm not sure this logic is what we want:

Time.strptime("001 -12:00", "%j %z", Time.utc(2000))
# => 1999-01-01 00:00:00 -1200

To be fair, you get the same result with or without this PR, so it doesn't seem like a bug in the PR, more like an additional bug not fixed.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Thanks for approving. I looked at that case: the year is taken from now moved to the parsed offset, and Time.utc(2000) is 1999-12-31 12:00:00 -1200 there. Other formats without a year do the same today:

Time.strptime("01-01 -12:00", "%m-%d %z", Time.utc(2000)) # => 1999-01-01 00:00:00 -1200
Time.strptime("10:00 -12:00", "%H:%M %z", Time.utc(2000)) # => 1999-12-31 10:00:00 -1200

So it is not specific to %j. If you think the year should come from now as given (UTC here), I can send that as a separate PR.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Agreed that is a different issue. If you would like to work on a PR for that, it would be appreciated.

else
year = now ? now.year : 1970
end
end
mon, day = 1, yday
while day > (mday = month_days(year, mon))
if (mon += 1) > 12
raise ArgumentError, "yday #{yday} out of range"
end
Expand Down
13 changes: 13 additions & 0 deletions test/test_time.rb
Original file line number Diff line number Diff line change
Expand Up @@ -557,6 +557,19 @@ def test_strptime_j
assert_equal(1, t.day)
end

def test_strptime_j_with_offset
t = Time.strptime("1985-062 14:58:14 -1130", "%Y-%j %H:%M:%S %z")
assert_equal(Time.new(1985, 3, 3, 14, 58, 14, "-11:30"), t)
assert_equal(-41400, t.utc_offset)
t = Time.strptime("2030-060 20:18:28 -0345", "%Y-%j %H:%M:%S %z")
assert_equal(Time.new(2030, 3, 1, 20, 18, 28, "-03:45"), t)
t = Time.strptime("2024-366 23:00:00 -1200", "%Y-%j %H:%M:%S %z")
assert_equal(Time.new(2024, 12, 31, 23, 0, 0, "-12:00"), t)
t = Time.strptime("2023-001 00:30:00 +1400", "%Y-%j %H:%M:%S %z")
assert_equal(Time.new(2023, 1, 1, 0, 30, 0, "+14:00"), t)
assert_raise(ArgumentError) { Time.strptime("2023-366", "%Y-%j") }
end

def test_strptime_p
t = Time.strptime("3am", "%I%p")
assert_equal(3, t.hour)
Expand Down