Skip to content

Conversation

AetherUnbound
Copy link
Contributor

Description

Fixes #916

I dug into the rust parsing code a bit and saw that the ordinal calculation might be why 2026W36 was getting the error ValueError: day is out of range for month. 2026W36 should be August 31, 2026, which becomes ordinal day 243.

Looking at the MONTHS_OFFSETS for non-leap year (2026):
[0]==-1, [1]==0, [2]==31, [3]==59, [4]==90, [5]==120, [6]==151, [7]==181, [8]==212, [9]==243, [10]==273, [11]==304, [12]==334, [13]==365

The original loop: for i in 1..14:

  • i=1: ord < MONTHS_OFFSETS[0][1] → 243 < 0? No
  • i=2: ord < MONTHS_OFFSETS[0][2] → 243 < 31? No
  • ...
  • i=8: ord < MONTHS_OFFSETS[0][8] → 243 < 212? No
  • i=9: ord < MONTHS_OFFSETS[0][9] → 243 < 243? No ← This is the problem!

Since 243 < 243 is false, it continues to:

  • i=10: ord < MONTHS_OFFSETS[0][10] → 243 < 273? Yes

At this point, the original code would execute:

let day = ord as u32 - MONTHS_OFFSETS[leap][i - 1] as u32;  // i=10, so i-1=9
let month = (i - 1) as u32;  // month = 9 (September, 1-based)

So: day = 243 - MONTHS_OFFSETS[0][9] = 243 - 243 = 0

This gives us month=9, day=0, which is September 0th - an invalid date, hence the "day is out of range for month" error.

Changing the comparison to be <= means that September (9) is matched appropriately as the month offset, so when it becomes 1-based with let month = (i - 1) as u32;, it becomes August once more.

I added a new test case for this as well in order to verify the functionality! I'm no rust expert though so this may be completely off base 😅

Shoutout to @d3jawu for helping walk me through some of the rust pieces 😊

Pull Request Check List

  • Added tests for changed code.
  • Updated documentation for changed code.

Copy link

@arthurnw arthurnw left a comment

Choose a reason for hiding this comment

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

Great test case!

Copy link

codspeed-hq bot commented Sep 16, 2025

CodSpeed Performance Report

Merging #918 will not alter performance

Comparing AetherUnbound:fix/week-formatting-bug (c0901d6) with master (0e92991)

Summary

✅ 1 untouched

@Secrus Secrus merged commit 3382956 into python-pendulum:master Sep 18, 2025
18 checks passed
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.

Cannot parse 2026-W36-1 specifically
3 participants