Skip to content

Commit b91f885

Browse files
committed
Rename methods to addElapsed*/subElapsed* based on feedback
Renamed from *WithTimestamp to *Elapsed for clearer semantics: - addElapsedHours() / subElapsedHours() - addElapsedMinutes() / subElapsedMinutes() - addElapsedSeconds() / subElapsedSeconds() Also fixed "Daylight Saving Time" spelling (was "Savings").
1 parent 29c247b commit b91f885

3 files changed

Lines changed: 71 additions & 71 deletions

File tree

docs/en/modifying.md

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,9 @@ Available add/sub methods:
4444

4545
For DST-safe operations that add actual elapsed time (see [DST Considerations](#dst-considerations)):
4646

47-
- `addHoursWithTimestamp()` / `subHoursWithTimestamp()`
48-
- `addMinutesWithTimestamp()` / `subMinutesWithTimestamp()`
49-
- `addSecondsWithTimestamp()` / `subSecondsWithTimestamp()`
47+
- `addElapsedHours()` / `subElapsedHours()`
48+
- `addElapsedMinutes()` / `subElapsedMinutes()`
49+
- `addElapsedSeconds()` / `subElapsedSeconds()`
5050

5151
### Month Overflow Handling
5252

@@ -173,20 +173,20 @@ information and you need to assign the correct timezone.
173173

174174
## DST Considerations
175175

176-
When modifying dates/times across DST (Daylight Savings Time) transitions,
176+
When modifying dates/times across DST (Daylight Saving Time) transitions,
177177
your operations may gain/lose an additional hour resulting in values that
178178
don't add up. Methods like `addHours()`, `addMinutes()`, and `addSeconds()`
179179
add "wall clock" time, which can produce unexpected results during DST
180180
transitions.
181181

182-
### Timestamp-Based Methods
182+
### Elapsed Time Methods
183183

184184
For operations that need to add actual elapsed time (not wall clock time),
185-
use the timestamp-based variants:
185+
use the elapsed time variants:
186186

187-
- `addHoursWithTimestamp()` / `subHoursWithTimestamp()`
188-
- `addMinutesWithTimestamp()` / `subMinutesWithTimestamp()`
189-
- `addSecondsWithTimestamp()` / `subSecondsWithTimestamp()`
187+
- `addElapsedHours()` / `subElapsedHours()`
188+
- `addElapsedMinutes()` / `subElapsedMinutes()`
189+
- `addElapsedSeconds()` / `subElapsedSeconds()`
190190

191191
These methods manipulate the Unix timestamp directly, ensuring that adding
192192
600 minutes always means exactly 36000 seconds of elapsed time:
@@ -200,13 +200,13 @@ $startOfDay = Chronos::parse('2026-04-05 00:00:00', 'Australia/Melbourne');
200200
$wallClock = $startOfDay->addMinutes(600);
201201
// Result: 2026-04-05T10:00:00+10:00
202202

203-
// Timestamp addition - adds 10 hours of elapsed time
204-
$elapsed = $startOfDay->addMinutesWithTimestamp(600);
203+
// Elapsed time addition - adds 10 hours of elapsed time
204+
$elapsed = $startOfDay->addElapsedMinutes(600);
205205
// Result: 2026-04-05T09:00:00+10:00
206206
```
207207

208-
The timestamp-based methods ensure that `diffInMinutes()` and
209-
`addMinutesWithTimestamp()` are true inverses of each other:
208+
The elapsed time methods ensure that `diffInMinutes()` and
209+
`addElapsedMinutes()` are true inverses of each other:
210210

211211
```php
212212
$time = Chronos::parse('2026-04-05 09:00:00', 'Australia/Melbourne');
@@ -215,7 +215,7 @@ $startOfDay = $time->startOfDay();
215215
$diff = $time->diffInMinutes($startOfDay); // 600
216216

217217
// Reconstructing the original time works correctly
218-
$reconstructed = $startOfDay->addMinutesWithTimestamp($diff);
218+
$reconstructed = $startOfDay->addElapsedMinutes($diff);
219219
// $reconstructed equals $time
220220
```
221221

src/Chronos.php

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1471,7 +1471,7 @@ public function subSeconds(int $value): static
14711471
}
14721472

14731473
/**
1474-
* Add hours to the instance using timestamp arithmetic.
1474+
* Add hours to the instance using elapsed time.
14751475
*
14761476
* Unlike `addHours()` which uses wall clock time, this method
14771477
* adds actual elapsed time by manipulating the Unix timestamp.
@@ -1481,25 +1481,25 @@ public function subSeconds(int $value): static
14811481
* @param int $value The number of hours to add.
14821482
* @return static
14831483
*/
1484-
public function addHoursWithTimestamp(int $value): static
1484+
public function addElapsedHours(int $value): static
14851485
{
14861486
return $this->setTimestamp($this->getTimestamp() + ($value * 3600));
14871487
}
14881488

14891489
/**
1490-
* Remove hours from the instance using timestamp arithmetic.
1490+
* Remove hours from the instance using elapsed time.
14911491
*
14921492
* @param int $value The number of hours to remove.
14931493
* @return static
1494-
* @see addHoursWithTimestamp()
1494+
* @see addElapsedHours()
14951495
*/
1496-
public function subHoursWithTimestamp(int $value): static
1496+
public function subElapsedHours(int $value): static
14971497
{
1498-
return $this->addHoursWithTimestamp(-$value);
1498+
return $this->addElapsedHours(-$value);
14991499
}
15001500

15011501
/**
1502-
* Add minutes to the instance using timestamp arithmetic.
1502+
* Add minutes to the instance using elapsed time.
15031503
*
15041504
* Unlike `addMinutes()` which uses wall clock time, this method
15051505
* adds actual elapsed time by manipulating the Unix timestamp.
@@ -1509,25 +1509,25 @@ public function subHoursWithTimestamp(int $value): static
15091509
* @param int $value The number of minutes to add.
15101510
* @return static
15111511
*/
1512-
public function addMinutesWithTimestamp(int $value): static
1512+
public function addElapsedMinutes(int $value): static
15131513
{
15141514
return $this->setTimestamp($this->getTimestamp() + ($value * 60));
15151515
}
15161516

15171517
/**
1518-
* Remove minutes from the instance using timestamp arithmetic.
1518+
* Remove minutes from the instance using elapsed time.
15191519
*
15201520
* @param int $value The number of minutes to remove.
15211521
* @return static
1522-
* @see addMinutesWithTimestamp()
1522+
* @see addElapsedMinutes()
15231523
*/
1524-
public function subMinutesWithTimestamp(int $value): static
1524+
public function subElapsedMinutes(int $value): static
15251525
{
1526-
return $this->addMinutesWithTimestamp(-$value);
1526+
return $this->addElapsedMinutes(-$value);
15271527
}
15281528

15291529
/**
1530-
* Add seconds to the instance using timestamp arithmetic.
1530+
* Add seconds to the instance using elapsed time.
15311531
*
15321532
* Unlike `addSeconds()` which uses wall clock time, this method
15331533
* adds actual elapsed time by manipulating the Unix timestamp.
@@ -1537,21 +1537,21 @@ public function subMinutesWithTimestamp(int $value): static
15371537
* @param int $value The number of seconds to add.
15381538
* @return static
15391539
*/
1540-
public function addSecondsWithTimestamp(int $value): static
1540+
public function addElapsedSeconds(int $value): static
15411541
{
15421542
return $this->setTimestamp($this->getTimestamp() + $value);
15431543
}
15441544

15451545
/**
1546-
* Remove seconds from the instance using timestamp arithmetic.
1546+
* Remove seconds from the instance using elapsed time.
15471547
*
15481548
* @param int $value The number of seconds to remove.
15491549
* @return static
1550-
* @see addSecondsWithTimestamp()
1550+
* @see addElapsedSeconds()
15511551
*/
1552-
public function subSecondsWithTimestamp(int $value): static
1552+
public function subElapsedSeconds(int $value): static
15531553
{
1554-
return $this->addSecondsWithTimestamp(-$value);
1554+
return $this->addElapsedSeconds(-$value);
15551555
}
15561556

15571557
/**

tests/TestCase/DateTime/TimestampAddTest.php renamed to tests/TestCase/DateTime/ElapsedTimeAddTest.php

Lines changed: 39 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -18,77 +18,77 @@
1818
use Cake\Chronos\Chronos;
1919
use Cake\Chronos\Test\TestCase\TestCase;
2020

21-
class TimestampAddTest extends TestCase
21+
class ElapsedTimeAddTest extends TestCase
2222
{
23-
public function testAddSecondsWithTimestamp(): void
23+
public function testAddElapsedSeconds(): void
2424
{
2525
$time = Chronos::parse('2024-01-15 12:00:00', 'UTC');
26-
$result = $time->addSecondsWithTimestamp(30);
26+
$result = $time->addElapsedSeconds(30);
2727
$this->assertSame('2024-01-15 12:00:30', $result->format('Y-m-d H:i:s'));
2828
}
2929

30-
public function testAddSecondsWithTimestampNegative(): void
30+
public function testAddElapsedSecondsNegative(): void
3131
{
3232
$time = Chronos::parse('2024-01-15 12:00:30', 'UTC');
33-
$result = $time->addSecondsWithTimestamp(-30);
33+
$result = $time->addElapsedSeconds(-30);
3434
$this->assertSame('2024-01-15 12:00:00', $result->format('Y-m-d H:i:s'));
3535
}
3636

37-
public function testSubSecondsWithTimestamp(): void
37+
public function testSubElapsedSeconds(): void
3838
{
3939
$time = Chronos::parse('2024-01-15 12:00:30', 'UTC');
40-
$result = $time->subSecondsWithTimestamp(30);
40+
$result = $time->subElapsedSeconds(30);
4141
$this->assertSame('2024-01-15 12:00:00', $result->format('Y-m-d H:i:s'));
4242
}
4343

44-
public function testAddMinutesWithTimestamp(): void
44+
public function testAddElapsedMinutes(): void
4545
{
4646
$time = Chronos::parse('2024-01-15 12:00:00', 'UTC');
47-
$result = $time->addMinutesWithTimestamp(30);
47+
$result = $time->addElapsedMinutes(30);
4848
$this->assertSame('2024-01-15 12:30:00', $result->format('Y-m-d H:i:s'));
4949
}
5050

51-
public function testAddMinutesWithTimestampNegative(): void
51+
public function testAddElapsedMinutesNegative(): void
5252
{
5353
$time = Chronos::parse('2024-01-15 12:30:00', 'UTC');
54-
$result = $time->addMinutesWithTimestamp(-30);
54+
$result = $time->addElapsedMinutes(-30);
5555
$this->assertSame('2024-01-15 12:00:00', $result->format('Y-m-d H:i:s'));
5656
}
5757

58-
public function testSubMinutesWithTimestamp(): void
58+
public function testSubElapsedMinutes(): void
5959
{
6060
$time = Chronos::parse('2024-01-15 12:30:00', 'UTC');
61-
$result = $time->subMinutesWithTimestamp(30);
61+
$result = $time->subElapsedMinutes(30);
6262
$this->assertSame('2024-01-15 12:00:00', $result->format('Y-m-d H:i:s'));
6363
}
6464

65-
public function testAddHoursWithTimestamp(): void
65+
public function testAddElapsedHours(): void
6666
{
6767
$time = Chronos::parse('2024-01-15 12:00:00', 'UTC');
68-
$result = $time->addHoursWithTimestamp(2);
68+
$result = $time->addElapsedHours(2);
6969
$this->assertSame('2024-01-15 14:00:00', $result->format('Y-m-d H:i:s'));
7070
}
7171

72-
public function testAddHoursWithTimestampNegative(): void
72+
public function testAddElapsedHoursNegative(): void
7373
{
7474
$time = Chronos::parse('2024-01-15 14:00:00', 'UTC');
75-
$result = $time->addHoursWithTimestamp(-2);
75+
$result = $time->addElapsedHours(-2);
7676
$this->assertSame('2024-01-15 12:00:00', $result->format('Y-m-d H:i:s'));
7777
}
7878

79-
public function testSubHoursWithTimestamp(): void
79+
public function testSubElapsedHours(): void
8080
{
8181
$time = Chronos::parse('2024-01-15 14:00:00', 'UTC');
82-
$result = $time->subHoursWithTimestamp(2);
82+
$result = $time->subElapsedHours(2);
8383
$this->assertSame('2024-01-15 12:00:00', $result->format('Y-m-d H:i:s'));
8484
}
8585

8686
/**
8787
* Test DST transition when clocks go BACK (fall back).
88-
* Australia/Melbourne changes out of daylight savings on 5th April 2026
88+
* Australia/Melbourne changes out of daylight saving on 5th April 2026
8989
* at 3:00 AM AEDT (+11) -> 2:00 AM AEST (+10)
9090
*/
91-
public function testAddMinutesWithTimestampAcrossDstFallBack(): void
91+
public function testAddElapsedMinutesAcrossDstFallBack(): void
9292
{
9393
$time = Chronos::parse('2026-04-05 09:00:00', 'Australia/Melbourne');
9494

@@ -98,8 +98,8 @@ public function testAddMinutesWithTimestampAcrossDstFallBack(): void
9898
$diff = $time->diffInMinutes($time->startOfDay());
9999
$this->assertSame(600, $diff);
100100

101-
// Using timestamp arithmetic should correctly account for DST
102-
$result = $time->startOfDay()->addMinutesWithTimestamp(600);
101+
// Using elapsed time should correctly account for DST
102+
$result = $time->startOfDay()->addElapsedMinutes(600);
103103
$this->assertSame('2026-04-05T09:00:00+10:00', $result->toIso8601String());
104104
}
105105

@@ -108,76 +108,76 @@ public function testAddMinutesWithTimestampAcrossDstFallBack(): void
108108
* America/New_York springs forward on 2nd Sunday of March 2025
109109
* at 2:00 AM EST (-05) -> 3:00 AM EDT (-04)
110110
*/
111-
public function testAddMinutesWithTimestampAcrossDstSpringForward(): void
111+
public function testAddElapsedMinutesAcrossDstSpringForward(): void
112112
{
113113
// March 9, 2025 is the 2nd Sunday of March (DST starts)
114114
$beforeDst = Chronos::parse('2025-03-09 01:00:00', 'America/New_York');
115115
$this->assertSame('-05:00', $beforeDst->format('P'));
116116

117-
// Add 2 hours (120 minutes) using timestamp arithmetic
117+
// Add 2 hours (120 minutes) using elapsed time
118118
// Wall clock would show 3:00 AM (skipping 2:00-3:00)
119-
$result = $beforeDst->addMinutesWithTimestamp(120);
119+
$result = $beforeDst->addElapsedMinutes(120);
120120

121121
// Should be 04:00 AM EDT (not 03:00 AM)
122122
$this->assertSame('2025-03-09T04:00:00-04:00', $result->toIso8601String());
123123
}
124124

125125
/**
126-
* Test that addMinutes and addMinutesWithTimestamp differ during DST
126+
* Test that addMinutes and addElapsedMinutes differ during DST
127127
*/
128-
public function testAddMinutesVsAddMinutesWithTimestampDuringDst(): void
128+
public function testAddMinutesVsAddElapsedMinutesDuringDst(): void
129129
{
130130
// Australia/Melbourne DST ends April 5, 2026 at 3am
131131
$startOfDay = Chronos::parse('2026-04-05 00:00:00', 'Australia/Melbourne');
132132

133133
// Wall clock addition (regular addMinutes)
134134
$wallClock = $startOfDay->addMinutes(600);
135135

136-
// Timestamp addition
137-
$elapsed = $startOfDay->addMinutesWithTimestamp(600);
136+
// Elapsed time addition
137+
$elapsed = $startOfDay->addElapsedMinutes(600);
138138

139139
// These should differ by 1 hour due to DST transition
140140
$this->assertSame('2026-04-05T10:00:00+10:00', $wallClock->toIso8601String());
141141
$this->assertSame('2026-04-05T09:00:00+10:00', $elapsed->toIso8601String());
142142
}
143143

144144
/**
145-
* Test addHoursWithTimestamp across DST
145+
* Test addElapsedHours across DST
146146
*/
147-
public function testAddHoursWithTimestampAcrossDst(): void
147+
public function testAddElapsedHoursAcrossDst(): void
148148
{
149149
$startOfDay = Chronos::parse('2026-04-05 00:00:00', 'Australia/Melbourne');
150150

151-
$result = $startOfDay->addHoursWithTimestamp(10);
151+
$result = $startOfDay->addElapsedHours(10);
152152

153153
// 10 actual hours from midnight should be 09:00 (since we gain an hour at 3am)
154154
$this->assertSame('2026-04-05T09:00:00+10:00', $result->toIso8601String());
155155
}
156156

157157
/**
158-
* Test addSecondsWithTimestamp across DST
158+
* Test addElapsedSeconds across DST
159159
*/
160-
public function testAddSecondsWithTimestampAcrossDst(): void
160+
public function testAddElapsedSecondsAcrossDst(): void
161161
{
162162
$startOfDay = Chronos::parse('2026-04-05 00:00:00', 'Australia/Melbourne');
163163

164164
// 10 hours in seconds = 36000
165-
$result = $startOfDay->addSecondsWithTimestamp(36000);
165+
$result = $startOfDay->addElapsedSeconds(36000);
166166

167167
$this->assertSame('2026-04-05T09:00:00+10:00', $result->toIso8601String());
168168
}
169169

170170
/**
171-
* Test that diffInMinutes and addMinutesWithTimestamp are inverses
171+
* Test that diffInMinutes and addElapsedMinutes are inverses
172172
*/
173-
public function testDiffInMinutesIsInverseOfAddMinutesWithTimestamp(): void
173+
public function testDiffInMinutesIsInverseOfAddElapsedMinutes(): void
174174
{
175175
$time = Chronos::parse('2026-04-05 09:00:00', 'Australia/Melbourne');
176176
$startOfDay = $time->startOfDay();
177177

178178
$diff = $time->diffInMinutes($startOfDay);
179179

180-
$reconstructed = $startOfDay->addMinutesWithTimestamp($diff);
180+
$reconstructed = $startOfDay->addElapsedMinutes($diff);
181181

182182
$this->assertSame($time->toIso8601String(), $reconstructed->toIso8601String());
183183
}

0 commit comments

Comments
 (0)