Back to results  ·  bugfix/ergo-medium-run-1/REPORT.md

# Bug report investigation

All checks were run with `PYTHONPATH=src` against the code in this folder. No
external copy of dateutil was consulted. Reports 1, 2, 3, 5, 6, 7 and 8 were
real bugs; report 4 was not a bug.

## Report 1: Month-end payment date is a day early — real bug

**Reproduction:** `date(2023, 12, 31) + relativedelta(months=2)` returned
`2024-02-28` instead of `2024-02-29`.

**Cause:** In `relativedelta.__add__` (`src/dateutil/relativedelta.py`), the
day was clamped using:

```python
calendar.monthrange(other.year, month)[1]
```

`other.year` is the *source* date's year, not the year of the result. When
adding months crosses into a different year, the code used the wrong year's
February length. For 2023-12-31 + 2 months, the target is February 2024 (29
days), but the code checked February 2023 (28 days) and clamped to 28. The
bug could also raise `ValueError: day is out of range for month` when moving
into a shorter February from a leap year (e.g. 2024-12-31 + 2 months).

**Fix:** Use the already-computed target `year`:

```python
day = min(calendar.monthrange(year, month)[1], self.day or other.day)
```

Verified the documented `relativedelta` examples (including
`2000-02-29 + relativedelta(years=1) == 2001-02-28`) and a full
1995–2030 sweep of month additions against independently calculated
clamped dates.

## Report 2: Midnight bookings show up at lunchtime — real bug

**Reproduction:** `parse("12:15 AM")` returned `12:15:00` instead of
`00:15:00`.

**Cause:** `parser._adjust_ampm()` (`src/dateutil/parser/_parser.py`) only
handled the PM case (`hour < 12` and PM → add 12). It never converted 12 AM
to hour 0, so 12 AM was left at noon. 12 PM worked because 12 < 12 is false,
leaving 12.

**Fix:** Add the AM case:

```python
elif hour == 12 and ampm == 0:
    hour = 0
```

Verified `12:00 AM → 00:00`, `12:59 AM → 00:59`, `12 AM → 00:00`, while
`12:15 PM`, `1:15 AM`, `1 PM`, `11:59 PM`, and 24-hour times are unchanged.

## Report 3: "Last Friday of the month" is a week early — real bug

**Reproduction:** `rrule(MONTHLY, byweekday=FR(-1), ...)` produced
23 January, 20 February and 20 March 2026 instead of 30 January, 27 February
and 27 March.

**Cause:** In `_iterinfo.rebuild()` (`src/dateutil/rrule.py`), the negative
weekday branch computed:

```python
i = last + n * 7
```

For `n = -1`, this starts one full week before the last day of the month,
then walks backward to the requested weekday. That finds the *second-to-last*
occurrence (and can even land in the previous month). For negative `n`, the
search must start `n + 1` weeks back; `-1` must start in the final week.

**Fix:**

```python
i = last + (n + 1) * 7
```

This also fixes the documented examples, which were failing before:
`byweekday=(SU(1), SU(-1))` for first/last Sunday and
`byweekday=MO(-2)` for the second-to-last Monday. Verified against
independently calculated nth-weekday dates for every month from 1995–2030
and for yearly/by-month nth-weekday rules.

## Report 4: Adding one month to 31 January gives the wrong date — not a bug

**Observed:** `date(2026, 1, 31) + relativedelta(months=1)` returns
`2026-02-28`.

**Why this is correct:** `relativedelta` deliberately clamps to the last day
of the target month. The documentation explicitly states: "Notice that
adding one month will never cross the month boundary" and shows
`date(2003, 1, 31) + relativedelta(months=+1) == 2003-02-28`. A month is not
a fixed number of days, so there is no "3 March" result; `months=1` means
February, and 31 February does not exist. The caller's expectation that it
should be 3 March contradicts documented, intentional behavior.

**Change:** None.

## Report 5: Daily reminder stops one day early — real bug

**Reproduction:** `rrule(DAILY, dtstart=2026-03-08, until=2026-03-10)`
yielded only 8 and 9 March.

**Cause:** The non-`bysetpos` branch of `rrule._iter()` (`src/dateutil/rrule.py`)
used:

```python
if until and res >= until:
    return
```

This excludes an occurrence that is exactly equal to `until`. The
`until` parameter is documented as an inclusive upper bound: "The last
recurrence in the rule is the greatest datetime that is less than or equal
to the value specified in the ``until`` parameter." The `bysetpos` branch
already used `>` correctly.

**Fix:** Use `if until and res > until:` in the normal iteration branch.

Verified exact-boundary inclusion, `until` between occurrences, `until`
before `dtstart`, `count` behavior, and the `bysetpos` branch.

## Report 6: Orthodox Easter is on the wrong day — real bug

**Reproduction:** `easter(2025, method=EASTER_ORTHODOX)` returned
`2025-04-21`; `easter(2026, ...)` returned `2026-04-13`.

**Cause:** `easter()` method 2 computes the Julian Easter date and then adds
the Julian-to-Gregorian offset. The offset used was:

```python
e = 10 + y//100 - 16 - (y//100 - 16)//5
```

This formula misses century corrections. The actual Gregorian-minus-Julian
offset is `y//100 - y//400 - 2` days (13 days for 1900–2099, not 14). The
old formula gives 14 for 2000–2099, producing a date one day late.

**Fix:**

```python
e = 10 + y//100 - y//400 - 12
```

which is algebraically `10 + (y//100 - y//400 - 2) - 10`, the correct
offset. Verified 2025-04-20 and 2026-04-12, and all years 1583–2499
against an independent Julian computus plus the Gregorian correction.
`EASTER_JULIAN` and `EASTER_WESTERN` are unaffected.

## Report 7: ISO week numbers give dates in the wrong week — real bug

**Reproduction:** `isoparse("2021-W01-1")` returned `2020-12-28` instead of
`2021-01-04`. `2026-W10-3` happened to work because 2026-01-01 is a Thursday.

**Cause:** `_calculate_weekdate()` (`src/dateutil/parser/isoparser.py`)
calculated "week 1" as the Monday of the week containing January 1:

```python
week_1 = jan_1 - timedelta(days=jan_1.isocalendar()[2] - 1)
```

For years where January 1 falls Friday–Sunday, that Monday belongs to the
previous ISO year. The parser did not apply the ISO rule that week 1 is the
week containing January 4 (equivalently, the week with at least four days
of the new year). 2021-01-01 is a Friday, so the code returned the Monday
of the previous year's last week.

**Fix:** If the computed Monday's ISO year is before the requested year,
advance one week:

```python
if week_1.isocalendar()[0] < year:
    week_1 += timedelta(days=7)
```

Verified round-trips against `datetime.date.fromisocalendar()` for every
valid ISO week date from 1990–2030 (no mismatches), plus the dashed, compact
and week-only forms.

## Report 8: Wrong time zone at the moment the clocks change — real bug

**Reproduction:** `datetime(2026, 3, 8, 3, 0, tzinfo=tz.gettz("America/New_York"))`
reported EST/UTC-5 instead of EDT/UTC-4. Similarly, 2026-11-01 02:00 reported
EDT instead of EST.

**Cause:** `tzfile._find_trans()` (`src/dateutil/tz/_tzfile.py`) looked up
the transition index with:

```python
idx = bisect.bisect_left(lt, ts) - 1
```

When a local wall-clock timestamp is exactly on a transition, `bisect_left`
returns the index of that transition, and subtracting one selects the
*previous* offset. At 3:00 AM on spring-forward day the transition is
03:00 local (post-transition wall time), so the old offset was returned.
The 03:01 case worked because `bisect_left` then points at the following
transition.

**Fix:** Use `bisect_right`, so a timestamp exactly on a transition uses
the new offset:

```python
idx = bisect.bisect_right(lt, ts) - 1
```

Verified both reported boundaries (spring 03:00 → EDT, autumn 02:00 → EST),
fold behavior for the repeated autumn hour (01:30 fold=0 → EDT,
fold=1 → EST), `is_ambiguous()`, UTC round-trips, and a sweep of all 37,257
transitions (1900–2030) across all 598 system zones with no failures.

## Files changed

- `src/dateutil/relativedelta.py` — report 1
- `src/dateutil/parser/_parser.py` — report 2
- `src/dateutil/rrule.py` — reports 3 and 5
- `src/dateutil/easter.py` — report 6
- `src/dateutil/parser/isoparser.py` — report 7
- `src/dateutil/tz/_tzfile.py` — report 8