From df58ce1ff8a6a10f4728cfe6ffc6af4435a6d2e2 Mon Sep 17 00:00:00 2001 From: Mark Scherer Date: Wed, 16 Sep 2026 14:47:25 +0200 Subject: [PATCH 1/4] Fix ChronosDate relative modifier validation --- src/ChronosDate.php | 20 ++++++-- tests/TestCase/Date/AddTest.php | 88 +++++++++++++++++++++++++++++++++ 2 files changed, 105 insertions(+), 3 deletions(-) diff --git a/src/ChronosDate.php b/src/ChronosDate.php index b51d911..ab63951 100644 --- a/src/ChronosDate.php +++ b/src/ChronosDate.php @@ -300,7 +300,7 @@ public static function diffFormatter(?DifferenceFormatterInterface $formatter = */ public function add(DateInterval $interval): static { - if ($interval->f > 0 || $interval->s > 0 || $interval->i > 0 || $interval->h > 0) { + if ($interval->f != 0 || $interval->s !== 0 || $interval->i !== 0 || $interval->h !== 0) { throw new InvalidArgumentException('Cannot add intervals with time components'); } $new = clone $this; @@ -319,7 +319,7 @@ public function add(DateInterval $interval): static */ public function sub(DateInterval $interval): static { - if ($interval->f > 0 || $interval->s > 0 || $interval->i > 0 || $interval->h > 0) { + if ($interval->f != 0 || $interval->s !== 0 || $interval->i !== 0 || $interval->h !== 0) { throw new InvalidArgumentException('Cannot subtract intervals with time components'); } $new = clone $this; @@ -338,7 +338,21 @@ public function sub(DateInterval $interval): static */ public function modify(string $modifier): static { - if (preg_match('/hour|minute|second/', $modifier)) { + $parsed = date_parse($modifier); + $relative = $parsed['relative'] ?? []; + $timestampSeconds = 0; + if (preg_match('/(?:^|\s)@(-?\d+)(?:\.\d+)?(?:\s|$)/', $modifier, $matches)) { + $timestampSeconds = (int)$matches[1]; + } + if ( + ($relative['hour'] ?? 0) !== 0 || + ($relative['minute'] ?? 0) !== 0 || + ($relative['second'] ?? 0) !== $timestampSeconds || + preg_match( + '/(?add($interval); } + public function testAddRaiseErrorOnNegativeTime(): void + { + $interval = DateInterval::createFromDateString('-1 hour'); + $date = ChronosDate::create(2001, 1, 1); + $this->expectException(InvalidArgumentException::class); + $date->add($interval); + } + public function testSubFullDay(): void { $interval = DateInterval::createFromDateString('1 day'); @@ -53,6 +61,14 @@ public function testSubIgnoreTime(): void $date->sub($interval); } + public function testSubRaiseErrorOnNegativeTime(): void + { + $interval = DateInterval::createFromDateString('-1 hour'); + $date = ChronosDate::create(2001, 1, 1); + $this->expectException(InvalidArgumentException::class); + $date->sub($interval); + } + public function testAddDay(): void { $this->assertSame(1, ChronosDate::create(1975, 5, 31)->addDays(1)->day); @@ -90,4 +106,76 @@ public function testModifyTimeComponentError(): void $this->expectException(InvalidArgumentException::class); $date->modify('10 seconds'); } + + public function testModifyOrdinalWeekday(): void + { + $date = ChronosDate::create(2026, 9, 1); + $new = $date->modify('second monday of this month'); + $this->assertSame('2026-09-14', $new->toDateString()); + } + + public function testModifyUppercaseTimeComponentError(): void + { + $date = ChronosDate::create(2001, 1, 1); + $this->expectException(InvalidArgumentException::class); + $date->modify('10 SECONDS'); + } + + public function testModifyClockTimeIgnored(): void + { + $date = ChronosDate::create(2001, 1, 1); + $new = $date->modify('12:00'); + $this->assertSame('2001-01-01', $new->toDateString()); + } + + public function testModifyTimeComponentWrappingDayError(): void + { + $date = ChronosDate::create(2001, 1, 1); + $this->expectException(InvalidArgumentException::class); + $date->modify('+24 hours'); + } + + public function testModifyFractionalTimeComponentError(): void + { + $date = ChronosDate::create(2001, 1, 1); + $this->expectException(InvalidArgumentException::class); + $date->modify('1 millisecond'); + } + + public function testModifyAbbreviatedFractionalTimeComponentError(): void + { + $date = ChronosDate::create(2001, 1, 1); + $this->expectException(InvalidArgumentException::class); + $date->modify('-1 msec'); + } + + public function testModifyTimestamp(): void + { + $date = ChronosDate::create(2001, 1, 1); + $new = $date->modify('@86400'); + $this->assertSame('1970-01-02', $new->toDateString()); + } + + public function testModifyTimestampWithTimezone(): void + { + $date = ChronosDate::create(2001, 1, 1); + $new = $date->modify('@1 UTC'); + $this->assertSame('1970-01-01', $new->toDateString()); + } + + public function testModifyTimestampWithDateComponent(): void + { + $date = ChronosDate::create(2001, 1, 1); + $new = $date->modify('@86400 +1 day'); + $this->assertSame('1970-01-03', $new->toDateString()); + } + + public function testModifyAcrossSkippedMidnight(): void + { + $this->withTimezone('America/Havana', function (): void { + $date = new ChronosDate('2023-03-11'); + $new = $date->modify('+1 day'); + $this->assertSame('2023-03-12', $new->toDateString()); + }); + } } From fcfcf00acdb9331f0272a5c4f07f243f0ddb40f9 Mon Sep 17 00:00:00 2001 From: Mark Scherer Date: Mon, 9 Nov 2026 20:59:09 +0100 Subject: [PATCH 2/4] Explain why modify() validates before modifying A post-modify time check cannot see '+24 hours' or '1000 ms', which land back on midnight or whole seconds, and would reject clock times and dates in time zones that skip midnight. --- src/ChronosDate.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/ChronosDate.php b/src/ChronosDate.php index ab63951..bb56d85 100644 --- a/src/ChronosDate.php +++ b/src/ChronosDate.php @@ -338,6 +338,7 @@ public function sub(DateInterval $interval): static */ public function modify(string $modifier): static { + // Validate before modifying: '+24 hours' or '1000 ms' leave no time-of-day trace afterwards. $parsed = date_parse($modifier); $relative = $parsed['relative'] ?? []; $timestampSeconds = 0; From 5f7313c15f9380d39e1342cb27a498b4bff7b12b Mon Sep 17 00:00:00 2001 From: Mark Scherer Date: Mon, 9 Nov 2026 21:04:09 +0100 Subject: [PATCH 3/4] Accept timestamp modifiers without surrounding whitespace PHP accepts @86400+1 day, @86400UTC, @86400+00:00 and +1 day@86400. The anchored regex missed them, so the timestamp's seconds were treated as a time adjustment and the modifier was rejected. --- src/ChronosDate.php | 2 +- tests/TestCase/Date/AddTest.php | 29 +++++++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/src/ChronosDate.php b/src/ChronosDate.php index bb56d85..40c559b 100644 --- a/src/ChronosDate.php +++ b/src/ChronosDate.php @@ -342,7 +342,7 @@ public function modify(string $modifier): static $parsed = date_parse($modifier); $relative = $parsed['relative'] ?? []; $timestampSeconds = 0; - if (preg_match('/(?:^|\s)@(-?\d+)(?:\.\d+)?(?:\s|$)/', $modifier, $matches)) { + if (preg_match('/@(-?\d+)/', $modifier, $matches)) { $timestampSeconds = (int)$matches[1]; } if ( diff --git a/tests/TestCase/Date/AddTest.php b/tests/TestCase/Date/AddTest.php index dba25f7..41d584d 100644 --- a/tests/TestCase/Date/AddTest.php +++ b/tests/TestCase/Date/AddTest.php @@ -18,6 +18,7 @@ use Cake\Chronos\Test\TestCase\TestCase; use DateInterval; use InvalidArgumentException; +use PHPUnit\Framework\Attributes\DataProvider; class AddTest extends TestCase { @@ -170,6 +171,34 @@ public function testModifyTimestampWithDateComponent(): void $this->assertSame('1970-01-03', $new->toDateString()); } + /** + * @return array + */ + public static function timestampWithoutWhitespaceProvider(): array + { + return [ + 'relative suffix' => ['@86400+1 day', '1970-01-03'], + 'zone abbreviation' => ['@86400UTC', '1970-01-02'], + 'zone offset' => ['@86400+00:00', '1970-01-02'], + 'negative timestamp' => ['@-86400+1 day', '1970-01-01'], + 'relative prefix' => ['+1 day@86400', '1970-01-03'], + ]; + } + + #[DataProvider('timestampWithoutWhitespaceProvider')] + public function testModifyTimestampWithoutWhitespace(string $modifier, string $expected): void + { + $date = ChronosDate::create(2001, 1, 1); + $this->assertSame($expected, $date->modify($modifier)->toDateString()); + } + + public function testModifyTimestampWithTimeComponentError(): void + { + $date = ChronosDate::create(2001, 1, 1); + $this->expectException(InvalidArgumentException::class); + $date->modify('@86400+10 seconds'); + } + public function testModifyAcrossSkippedMidnight(): void { $this->withTimezone('America/Havana', function (): void { From 40be6050ed4b8994f86821d05ca2387a9321a9ae Mon Sep 17 00:00:00 2001 From: Mark Scherer Date: Mon, 9 Nov 2026 21:24:12 +0100 Subject: [PATCH 4/4] Strip timestamps before checking modifiers for time components date_parse() counts an @timestamp as relative seconds and negates them for 'ago', so '@86400 1 day ago' was rejected as a time adjustment. Removing the timestamp before parsing drops the bookkeeping that tried to cancel those seconds out. --- src/ChronosDate.php | 9 +++------ tests/TestCase/Date/AddTest.php | 26 ++++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 6 deletions(-) diff --git a/src/ChronosDate.php b/src/ChronosDate.php index 40c559b..12e70c8 100644 --- a/src/ChronosDate.php +++ b/src/ChronosDate.php @@ -339,16 +339,13 @@ public function sub(DateInterval $interval): static public function modify(string $modifier): static { // Validate before modifying: '+24 hours' or '1000 ms' leave no time-of-day trace afterwards. - $parsed = date_parse($modifier); + // date_parse() counts an @timestamp as relative seconds (negated by 'ago'), so strip it first. + $parsed = date_parse((string)preg_replace('/@-?\d+(?:\.\d+)?/', ' ', $modifier)); $relative = $parsed['relative'] ?? []; - $timestampSeconds = 0; - if (preg_match('/@(-?\d+)/', $modifier, $matches)) { - $timestampSeconds = (int)$matches[1]; - } if ( ($relative['hour'] ?? 0) !== 0 || ($relative['minute'] ?? 0) !== 0 || - ($relative['second'] ?? 0) !== $timestampSeconds || + ($relative['second'] ?? 0) !== 0 || preg_match( '/(? + */ + public static function timestampWithAgoProvider(): array + { + return [ + 'date component ago' => ['@86400 1 day ago', '1969-12-30'], + 'bare ago' => ['@86400 ago', '1969-12-31'], + 'ago before timestamp' => ['1 day ago @86400', '1970-01-01'], + ]; + } + + #[DataProvider('timestampWithAgoProvider')] + public function testModifyTimestampWithAgo(string $modifier, string $expected): void + { + $date = ChronosDate::create(2001, 1, 1); + $this->assertSame($expected, $date->modify($modifier)->toDateString()); + } + + public function testModifyTimestampWithTimeComponentAgoError(): void + { + $date = ChronosDate::create(2001, 1, 1); + $this->expectException(InvalidArgumentException::class); + $date->modify('@86400 10 seconds ago'); + } + #[DataProvider('timestampWithoutWhitespaceProvider')] public function testModifyTimestampWithoutWhitespace(string $modifier, string $expected): void {