Skip to content

fix: correct %V ISO week at leap year boundaries#133

Draft
Koan-Bot wants to merge 1 commit intocpan-authors:mainfrom
Koan-Bot:koan.atoomic/fix-format-V-year-boundary
Draft

fix: correct %V ISO week at leap year boundaries#133
Koan-Bot wants to merge 1 commit intocpan-authors:mainfrom
Koan-Bot:koan.atoomic/fix-format-V-year-boundary

Conversation

@Koan-Bot
Copy link
Copy Markdown

@Koan-Bot Koan-Bot commented May 3, 2026

What

Fixes %V (ISO 8601 week number) returning 01 instead of the correct week number for early-January dates when the previous year is a leap year.

Why

When a date like 2021-01-01 (Friday) belongs to ISO week 53 of 2020, format_V correctly identifies that Thursday falls in the previous year and adjusts by adding 2020's 366 days. But the adjusted value (365) then matches the current year's day count (365), incorrectly triggering the year-end check and returning week 01.

The year-end check ($thu >= $ylen) is meant for late-December dates whose Thursday is in the next year — it should never apply when Thursday was already adjusted to the previous year.

How

Move the year-end boundary check into an else branch so it only applies when no previous-year adjustment was made. The $thu < 0 path (previous year) and $thu >= $ylen path (next year) are mutually exclusive by definition.

Testing

  • Added 5 regression tests for the leap-year boundary case (2021-01-01 through 2021-01-04, 2017-01-01)
  • Verified against POSIX strftime("%V") for 14 year-boundary dates — all match
  • Full test suite passes (all existing tests unaffected)

🤖 Generated with Claude Code

When a date falls in early January and its ISO Thursday belongs to the
previous year, format_V adjusts $thu by adding the previous year's day
count. If that previous year is a leap year (366 days), the adjusted
$thu equals the current year's $ylen (365), incorrectly triggering the
year-end check and returning week 01 instead of the correct week 53.

Fix: only apply the year-end check ($thu >= $ylen) when Thursday was
NOT already adjusted to the previous year. The year-end check is meant
for late-December dates whose Thursday falls in the next year, not for
early-January dates already handled by the previous-year adjustment.

Affected dates: Jan 1-3 of any year where:
- January 1 is Thursday, Friday, Saturday, or Sunday, AND
- the previous year is a leap year

Example: 2021-01-01 (Fri) returned %V=01 instead of %V=53.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
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.

1 participant