From 0a78abe0886be3530bb8354eeef56a4b00d4bd32 Mon Sep 17 00:00:00 2001 From: mscherer Date: Sat, 14 Mar 2026 18:46:05 +0100 Subject: [PATCH] Add time comparison methods to ChronosTime Add isStartOfDay(), isEndOfDay(), isMidnight(), and isMidday() methods to ChronosTime for checking if a time instance represents common boundary times. - isStartOfDay(): Checks if time is 00:00:00.000000 - isEndOfDay(): Checks if time is 23:59:59 (ignores microseconds) - isMidnight(): Alias for isStartOfDay() - isMidday(): Checks if time is 12:00:00.000000 --- src/ChronosTime.php | 48 ++++++++++++++++++++++++++++++ tests/TestCase/ChronosTimeTest.php | 36 ++++++++++++++++++++++ 2 files changed, 84 insertions(+) diff --git a/src/ChronosTime.php b/src/ChronosTime.php index 7d65dd2..22600cd 100644 --- a/src/ChronosTime.php +++ b/src/ChronosTime.php @@ -460,6 +460,54 @@ public function between(ChronosTime $start, ChronosTime $end, bool $equals = tru return $this->greaterThan($start) && $this->lessThan($end); } + /** + * Returns whether time is start of day. + * + * @return bool + */ + public function isStartOfDay(): bool + { + return $this->ticks === 0; + } + + /** + * Returns whether time is end of day. + * + * Compares against 23:59:59, ignoring microseconds. + * + * @return bool + */ + public function isEndOfDay(): bool + { + $endOfDayTicks = 23 * self::TICKS_PER_HOUR + + 59 * self::TICKS_PER_MINUTE + + 59 * self::TICKS_PER_SECOND; + + $ticksWithoutMicroseconds = $this->ticks - $this->ticks % self::TICKS_PER_SECOND; + + return $ticksWithoutMicroseconds === $endOfDayTicks; + } + + /** + * Returns whether time is midnight. + * + * @return bool + */ + public function isMidnight(): bool + { + return $this->isStartOfDay(); + } + + /** + * Returns whether time is midday. + * + * @return bool + */ + public function isMidday(): bool + { + return $this->ticks === 12 * self::TICKS_PER_HOUR; + } + /** * Returns an `DateTimeImmutable` instance set to this clock time. * diff --git a/tests/TestCase/ChronosTimeTest.php b/tests/TestCase/ChronosTimeTest.php index 92b6786..5dd3c00 100644 --- a/tests/TestCase/ChronosTimeTest.php +++ b/tests/TestCase/ChronosTimeTest.php @@ -269,6 +269,42 @@ public function testComparisons(): void $this->assertFalse($t3->between($t1, $t2)); } + public function testIsStartOfDay(): void + { + $this->assertTrue(ChronosTime::parse('00:00:00')->isStartOfDay()); + $this->assertTrue(ChronosTime::midnight()->isStartOfDay()); + $this->assertFalse(ChronosTime::parse('00:00:00.000001')->isStartOfDay()); + $this->assertFalse(ChronosTime::parse('00:00:01')->isStartOfDay()); + $this->assertFalse(ChronosTime::noon()->isStartOfDay()); + } + + public function testIsEndOfDay(): void + { + $this->assertTrue(ChronosTime::parse('23:59:59')->isEndOfDay()); + $this->assertTrue(ChronosTime::endOfDay()->isEndOfDay()); + $this->assertTrue(ChronosTime::parse('23:59:59.999999')->isEndOfDay()); + $this->assertFalse(ChronosTime::parse('23:59:58')->isEndOfDay()); + $this->assertFalse(ChronosTime::midnight()->isEndOfDay()); + $this->assertFalse(ChronosTime::noon()->isEndOfDay()); + } + + public function testIsMidnight(): void + { + $this->assertTrue(ChronosTime::midnight()->isMidnight()); + $this->assertTrue(ChronosTime::parse('00:00:00')->isMidnight()); + $this->assertFalse(ChronosTime::parse('00:00:00.000001')->isMidnight()); + $this->assertFalse(ChronosTime::noon()->isMidnight()); + } + + public function testIsMidday(): void + { + $this->assertTrue(ChronosTime::noon()->isMidday()); + $this->assertTrue(ChronosTime::parse('12:00:00')->isMidday()); + $this->assertFalse(ChronosTime::parse('12:00:00.000001')->isMidday()); + $this->assertFalse(ChronosTime::parse('12:00:01')->isMidday()); + $this->assertFalse(ChronosTime::midnight()->isMidday()); + } + public function testToDateTimeImmutable(): void { $time = ChronosTime::parse('23:59:59.999999');