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