Skip to content

Fix Instant::toDecimal() for negative epoch seconds with nanos - #145

Open
gnutix wants to merge 3 commits into
brick:mainfrom
gnutix:fix/instant-to-decimal-negative
Open

gnutix wants to merge 3 commits into
brick:mainfrom
gnutix:fix/instant-to-decimal-negative

Conversation

@gnutix

@gnutix gnutix commented Nov 7, 2026 •

Copy link
Copy Markdown
Contributor

Item 5 of #137.

Problem

An Instant stores a floored epoch second and a positive nano adjustment, so -0.5 s is stored as -1 s and 500000000 ns. toDecimal() concatenated the two, which gave the wrong value whenever the epoch second was negative and the nanos were not zero:

Instant::of(-1, 500_000_000)->toDecimal(); // "-1.5"         (expected "-0.5")
Instant::of(0, -1)->toDecimal();           // "-1.999999999" (expected "-0.000000001")
Instant::of(-2, 250_000_000)->toDecimal(); // "-2.25"        (expected "-1.75")

Change

For a negative epoch second with nanos, the integer part is -(epochSecond + 1) and the fraction is 1e9 - nano. toISOString() was not affected: it goes through ZonedDateTime. PHP_INT_MIN does not overflow, because -(PHP_INT_MIN + 1) is PHP_INT_MAX.

New cases in InstantTest::providerToDecimal(), including PHP_INT_MIN.

CI

The last commit is #133's Install tzdata-legacy in CI, cherry-picked: the ubuntu-26.04 runner image no longer ships legacy zone names such as CET and Asia/Rangoon, so PHPUnit fails on every branch based on main (#143 and #144 too). It is the same change as in #133, so it merges cleanly whichever lands first.

🤖 Generated with Claude Code (Opus 5.5)

@codecov

codecov Bot commented Nov 7, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.24%. Comparing base (81915a3) to head (6c5b5cb).

Additional details and impacted files
@@            Coverage Diff            @@
##               main     #145   +/-   ##
=========================================
  Coverage     99.24%   99.24%           
- Complexity     1143     1144    +1     
=========================================
  Files            48       48           
  Lines          2517     2518    +1     
=========================================
+ Hits           2498     2499    +1     
  Misses           19       19           

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Comment thread tests/InstantTest.php Resolved
@gnutix
gnutix force-pushed the fix/instant-to-decimal-negative branch from 4a9e012 to f058548 Compare November 9, 2026 08:45
joelbutcher and others added 3 commits November 9, 2026 09:46
ubuntu-latest now points to Ubuntu 26.04, which no longer installs
tzdata-legacy. Without it, PHP doesn't know legacy time zone names such as
CET and Asia/Rangoon, which the tests use.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01FQhVM8scWxQrMU3djngjpv
(cherry picked from commit 1c48e3b)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@gnutix
gnutix force-pushed the fix/instant-to-decimal-negative branch from f058548 to 6c5b5cb Compare November 9, 2026 08:46
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.

3 participants