Add timestamp-based add/sub methods for DST-safe time arithmetic#520
Open
dereuromark wants to merge 2 commits into3.xfrom
Open
Add timestamp-based add/sub methods for DST-safe time arithmetic#520dereuromark wants to merge 2 commits into3.xfrom
dereuromark wants to merge 2 commits into3.xfrom
Conversation
Adds new methods that use Unix timestamp arithmetic instead of wall clock time, ensuring correct behavior across DST transitions: - addHoursWithTimestamp() / subHoursWithTimestamp() - addMinutesWithTimestamp() / subMinutesWithTimestamp() - addSecondsWithTimestamp() / subSecondsWithTimestamp() These methods are the true inverse of diffInMinutes/diffInSeconds/diffInHours, which return elapsed time based on timestamps.
There was a problem hiding this comment.
Pull request overview
This PR adds DST-safe, elapsed-time variants of Chronos’ second/minute/hour arithmetic by implementing timestamp-based add/sub methods and documenting/testing their behavior across DST transitions.
Changes:
- Added
add*WithTimestamp()/sub*WithTimestamp()methods for hours, minutes, and seconds inChronos. - Added new test coverage for these methods, including DST “fall back” and “spring forward” scenarios.
- Updated modifying-time documentation to explain wall-clock vs elapsed-time semantics and point users to the new methods.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| src/Chronos.php | Introduces timestamp-based add/sub methods to perform elapsed-time arithmetic across DST. |
| tests/TestCase/DateTime/TimestampAddTest.php | Adds unit tests validating timestamp-based behavior and DST transition correctness. |
| docs/en/modifying.md | Documents DST considerations and the new timestamp-based APIs. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Renamed from *WithTimestamp to *Elapsed for clearer semantics: - addElapsedHours() / subElapsedHours() - addElapsedMinutes() / subElapsedMinutes() - addElapsedSeconds() / subElapsedSeconds() Also fixed "Daylight Saving Time" spelling (was "Savings").
LordSimal
approved these changes
Apr 3, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This PR adds elapsed-time variants of
addSeconds(),addMinutes(), andaddHours()that correctly handle DST transitions by manipulating Unix timestamps directly instead of using wall clock time.Related to #519 and #518 which demonstrate this issue.
The Problem
When adding time across DST transitions, the existing methods (
addMinutes(),addHours(),addSeconds()) add "wall clock" time, which can produce unexpected results:How Carbon Handles This
Carbon provides two sets of methods:
addMinutes()addUTCMinutes()This allows developers to choose the semantics they need.
Solution
This PR adds similar elapsed-time methods to Chronos:
addElapsedHours()/subElapsedHours()addElapsedMinutes()/subElapsedMinutes()addElapsedSeconds()/subElapsedSeconds()These methods ensure that
diffInMinutes()andaddElapsedMinutes()are true inverses:Changes
Chronos.phpdocs/en/modifying.md