Back to results · bugfix-hard/ergo-max-run-1/REPORT.md
# Investigation report
All checks were run with `PYTHONPATH=src` against the code in this folder. The project's own test suite was not present, so I wrote standalone reproduction scripts for each complaint, plus regression scripts that compare the implementation against Python's standard `datetime` ISO-calendar arithmetic and against direct calendar calculations.
## Summary
| # | Complaint | Real bug? | Action |
|---|-----------|-----------|--------|
| 1 | Month-end billing dates a day out | Yes | Fixed leap-year clamp in `relativedelta` |
| 2 | Just-after-midnight bookings shown mid-day | Yes | Fixed `12 AM` parsing |
| 3 | "Last Friday" meeting a week early | Yes | Fixed negative weekday offsets in `rrule` |
| 4 | `+1 month` from 31 January should be early March | No | Left unchanged |
| 5 | Recurrences with an end date stop one early | Yes | Made `until` inclusive |
| 6 | Orthodox Easter one day off | Yes | Corrected Julian-to-Gregorian shift |
| 7 | ISO week dates a week early in some years | Yes | Fixed ISO week-1 calculation and last-week mask |
| 8 | Wrong zone at the exact DST changeover | Yes | Fixed transition boundary lookup in `tzfile` |
| 9 | UK `01/02/2026` parsed as 2 January | No | Left unchanged; documented `dayfirst` behaviour |
| 10 | Easter 2026 should be 12 April | No | Left unchanged; default is Western Easter |
## 1. Month-end billing dates are sometimes a day out — real bug
**What the customer was doing:** adding a whole number of months to a month-end date, e.g. `date(2023, 12, 31) + relativedelta(months=2)`, expecting the last valid day of the target month. It only fails when the target is February of a leap year.
**Cause:** in `relativedelta.__add__`, the target year is adjusted before the day is clamped, but the clamp used `other.year` (the original year) instead of the adjusted `year`:
```python
day = min(calendar.monthrange(other.year, month)[1], self.day or other.day)
```
So for 31 December 2023 + 2 months, the target was February 2024 but the day was clamped to February 2023's 28 days, producing `2024-02-28` instead of `2024-02-29`.
**Fix:** `src/dateutil/relativedelta.py` now clamps with the target `year`:
```python
day = min(calendar.monthrange(year, month)[1], self.day or other.day)
```
Verified: 31 Dec 2023 + 2 months -> 29 Feb 2024; 31 Dec 2022 + 2 months -> 28 Feb 2023; 31 Jan 2026 + 1 month still -> 28 Feb 2026.
## 2. Bookings just after midnight show up in the middle of the day — real bug
**What the customer was doing:** parsing strings such as `"12:15 AM"`. `parse("12:15 AM")` returned `12:15` instead of `00:15`.
**Cause:** `_adjust_ampm` only handled PM (`hour < 12 and ampm == 1`). It never converted the 12-hour-clock value `12 AM` to hour zero, so midnight stayed as noon.
**Fix:** `src/dateutil/parser/_parser.py` now maps `12 AM` to hour 0, while leaving `12 PM` at hour 12 and adding 12 for other PM hours:
```python
if hour == 12 and ampm == 0:
hour = 0
elif hour < 12 and ampm == 1:
hour += 12
```
Verified: `12:15 AM` -> 00:15, `12:00 AM` -> 00:00, `12:15 PM` -> 12:15, `1:15 AM` -> 01:15, `11:59 PM` -> 23:59, and the same with `HH:MM:SS`.
## 3. Monthly "last Friday" meeting is a week early — real bug
**What the customer was doing:** `rrule(MONTHLY, byweekday=FR(-1))` (equivalently `byweekday=FR, bysetpos=-1`). It produced 23 Jan, 20 Feb and 20 Mar 2026 instead of 30 Jan, 27 Feb and 27 Mar.
**Cause:** the negative `bynweekday` branch in `_iterinfo.rebuild` started the search at `last + n*7`. For `n = -1` this begins a full week before the final day of the month, then walks forward to the requested weekday; when that weekday falls in the last week, it selects the previous week's occurrence. The final day itself must be included as a candidate.
**Fix:** `src/dateutil/rrule.py` starts the backward search at `last + (n + 1)*7` before adjusting to the requested weekday. Positive `n` logic was already correct.
Verified against direct calendar calculations for every month from 1990 through 2050, for all seven weekdays and `n = -1` through `-5`; also checked `YEARLY` last-weekday and the documented `bysetpos=-2` example. No mismatches.
## 4. Adding a month to 31 January should give early March — not a bug
`relativedelta` documents its month-end rule explicitly: "If the result falls on a day after the last one of the month, the last day of the month is used instead." Thus `2026-01-31 + relativedelta(months=1)` is `2026-02-28` by design, not `2026-03-03`. Changing this would break the library's established, documented arithmetic and the "adding one month will never cross the month boundary" contract. No code changed. Customers who want the same day in the next month or a day count should use `days=`/explicit dates instead.
## 5. Repeating reminders with an end date stop one occurrence too soon — real bug
**What the customer was doing:** `rrule(DAILY, dtstart=..., until=...)` with `until` exactly on an occurrence. The final occurrence equal to `until` was omitted.
**Cause:** the non-`bysetpos` output loop in `rrule._iter` used:
```python
if until and res >= until:
return
```
The constructor documentation and RFC 5545 specify that the last recurrence is the greatest datetime **less than or equal to** `until`. The `bysetpos` branch already used `res > until`; the normal branch was inconsistent.
**Fix:** `src/dateutil/rrule.py` now rejects only `res > until` in the normal output loop.
Verified `until` exactly on occurrences (daily and hourly), just before/after occurrences, and compared many daily/weekly/monthly/yearly rules against filtering a `count`-based rule by `res <= until`. All matched. The documented `until=parse("19971224T000000")` example still produces the expected final 23 December occurrence.
## 6. Orthodox Easter dates are one day off — real bug
**What the customer was doing:** calling `easter(year, EASTER_ORTHODOX)`, expecting the Orthodox (Julian paschalion converted to the Gregorian calendar) date, e.g. 20 Apr 2025 and 12 Apr 2026.
**Cause:** the Julian-to-Gregorian correction for years after 1600 subtracted `(y//100 - 16)//5`. The Julian/Gregorian drift is three days per 400 years, i.e. one day per century except every fourth century, which corresponds to division by 4, not division by 5. The wrong expression was usually right only near the correction boundaries, which is why the result was one day off for these years.
**Fix:** `src/dateutil/easter.py` now uses `(y//100 - 16)//4`.
Verified against an independent Julian computus implementation plus the published 13-day offset for the current century: 2025 -> 20 Apr, 2026 -> 12 Apr, 2024 -> 5 May, 2023 -> 16 Apr, 2022 -> 24 Apr. The `EASTER_JULIAN` and `EASTER_WESTERN` algorithms were not changed (Western Easter 2026 remains 5 April).
## 7. ISO week numbers give dates in the wrong week — real bug
**What the customer was doing:** using ISO week dates such as `isoparse("2021-W01-1")`, which returned 28 Dec 2020 instead of 4 Jan 2021. `"2026-W10-3"` happened to work.
**Cause:** `isoparser._calculate_weekdate` derived the start of week 1 from January 1:
```python
week_1 = jan_1 - timedelta(days=jan_1.isocalendar()[2] - 1)
```
That finds the Monday of the week containing 1 January. If 1 January belongs to the *previous* ISO year (e.g. 1 Jan 2021 is in 2020-W53), this yields the Monday of the previous year's last week, shifting every result by one week. ISO week 1 is the week containing 4 January, which is always in the requested ISO year.
There was a second, related bug in `rrule`'s `byweekno` mask for the last week(s) of the previous year. `lnumweeks` was computed from the current year's `self.yearlen`/`no1wkst` instead of the previous year's `lyearlen`/`lno1wkst`; the expression also had wrong operator precedence (`52 + X % 7 // 4` rather than a weeks-plus-remainder calculation). This made `byweekno=53` and `byweekno=52` mark incorrect dates in years such as 1994, 2011, 2022, 2039 and 2050.
**Fix:**
- `src/dateutil/parser/isoparser.py` now bases week 1 on 4 January.
- `src/dateutil/rrule.py` now computes the previous ISO year's week count as `div + mod//4`, where `div, mod = divmod(lyearlen - lno1wkst, 7)`.
Verified `_calculate_weekdate` against `date.fromisocalendar` for every valid ISO week/day from year 1 through 9999 (no mismatches), and verified the `rrule` `wnomask` for every week/year from 1990 through 2050 against the ISO week number of every date (no mismatches after the fix). The documented `byweekno` examples still produce the same output.
## 8. Wrong time zone at the exact DST changeover — real bug
**What the customer was doing:** attaching a `tzfile` zone (e.g. `gettz("America/New_York")`) to local times exactly at a transition, such as 03:00 on the spring-forward day or 02:00 on the autumn changeover. At exactly the transition the previous offset/abbreviation was returned; a minute later it was correct. `astimezone`/`fromutc` was also wrong at the exact instant.
**Cause:** `_find_trans` in `tzfile` used `bisect_left(lt, ts) - 1`. For a local timestamp exactly equal to a transition entry, `bisect_left` returns that entry's index, and subtracting one selects the *previous* ttinfo. At exactly 03:00 after the spring-forward transition, the code returned EST. At exactly 02:00 after the autumn transition, it returned EDT. The correct choice is the transition that has just taken effect, so the lookup must be `bisect_right(...) - 1`.
**Fix:** `src/dateutil/tz/_tzfile.py` now uses `bisect_right` in `_find_trans`.
Verified for New York at both 2026 transitions: 03:00 -> EDT, 02:00 autumn -> EST; 01:59/03:01 and 01:59/02:01 remain correct. Also checked UTC-to-local `astimezone` at the exact transition instants (03:00 EDT, fold/EST behaviour at 01:00), ambiguous-time `fold` handling, and round-trip conversions across transition windows in several zones (New York, London, Lord Howe, Santiago, Tehran) without regressions.
## 9. UK `01/02/2026` parsed as 2 January — not a bug
`parse()` defaults to month-first for ambiguous numeric dates. This is documented in the parser module: day-first and year-first are inferred only from values that disambiguate them (e.g. a first component greater than 12), with explicit `dayfirst`/`yearfirst` options available. In the UK the caller must pass `dayfirst=True`; `parse('01/02/2026', dayfirst=True)` returns `2026-02-01`. Changing the default would silently change behaviour for all existing users and is not a defect. No code changed.
## 10. Easter 2026 should be 12 April — not a bug
The default `easter(year)` is Western/Gregorian Easter, documented as method 3. For 2026 the Western date is indeed 5 April. The Greek Orthodox date is 12 April and is obtained with `easter(2026, method=EASTER_ORTHODOX)`. The report #6 fix makes that Orthodox result correct; the Western default is intentionally unchanged. No further change needed.
## Files changed
- `src/dateutil/relativedelta.py`
- `src/dateutil/parser/_parser.py`
- `src/dateutil/rrule.py`
- `src/dateutil/easter.py`
- `src/dateutil/parser/isoparser.py`
- `src/dateutil/tz/_tzfile.py`