Skip to content

fix: time2str stores wrong epoch when timezone is specified#141

Draft
Koan-Bot wants to merge 1 commit into
cpan-authors:mainfrom
Koan-Bot:koan.atoomic/fix-time2str-epoch-storage
Draft

fix: time2str stores wrong epoch when timezone is specified#141
Koan-Bot wants to merge 1 commit into
cpan-authors:mainfrom
Koan-Bot:koan.atoomic/fix-time2str-epoch-storage

Conversation

@Koan-Bot
Copy link
Copy Markdown

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

What

time2str stored the timezone-adjusted epoch in $me->[9] instead of the real epoch, causing format codes that read $me->[9] to compute wrong values.

Why

format_G (GPS week number) and format_z (numeric timezone offset) both read $me->[9] expecting the real Unix epoch. When time2str is called with a timezone, $time is modified by tz_offset() before gmtime(), but the modified value was stored in $me->[9]. This causes GPS week numbers to be off by 1 at week boundaries with non-UTC timezones, and potentially wrong DST detection in format_z.

strftime already stored $epoch (real epoch) correctly — this was a time2str-only bug.

How

One-line fix: $me->[9] = $epoch; instead of $me->[9] = $time; (line 95 of Generic.pm). $epoch is set to the original time on line 79, before $time is modified.

Testing

New t/time2str-tz-epoch.t with 8 tests:

  • GPS week at exact boundary with UTC vs EST (fails without fix)
  • Edge cases: one second before boundary, mid-week with various timezones
  • Verifies %s (format_s) still returns real epoch (uses $epoch directly, unaffected)

Full test suite passes (all 38 test files).

🤖 Generated with Claude Code


Quality Report

Changes: 2 files changed, 44 insertions(+), 1 deletion(-)

Code scan: clean

Tests: skipped

Branch hygiene: clean

Generated by Kōan post-mission quality pipeline

When time2str is called with a timezone, $time is modified by tz_offset()
before gmtime(), but then the modified value was stored in $me->[9].
Format codes that read $me->[9] (format_G for GPS week, format_z for
numeric offset DST detection) got the wrong timestamp.

strftime already stored $epoch (real epoch) correctly — this aligns
time2str with the same behavior.

The bug manifests at GPS week boundaries: time2str("%G", $t, "EST")
returned week N-1 instead of N when the timezone offset pushed the
adjusted time across the 604800-second boundary.

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