Skip to content

Commit c1a505a

Browse files
authored
Add time comparison methods to ChronosTime (#505)
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
1 parent 01c9515 commit c1a505a

File tree

2 files changed

+84
-0
lines changed

2 files changed

+84
-0
lines changed

src/ChronosTime.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -491,6 +491,54 @@ public function between(ChronosTime $start, ChronosTime $end, bool $equals = tru
491491
return $this->greaterThan($start) && $this->lessThan($end);
492492
}
493493

494+
/**
495+
* Returns whether time is start of day.
496+
*
497+
* @return bool
498+
*/
499+
public function isStartOfDay(): bool
500+
{
501+
return $this->ticks === 0;
502+
}
503+
504+
/**
505+
* Returns whether time is end of day.
506+
*
507+
* Compares against 23:59:59, ignoring microseconds.
508+
*
509+
* @return bool
510+
*/
511+
public function isEndOfDay(): bool
512+
{
513+
$endOfDayTicks = 23 * self::TICKS_PER_HOUR
514+
+ 59 * self::TICKS_PER_MINUTE
515+
+ 59 * self::TICKS_PER_SECOND;
516+
517+
$ticksWithoutMicroseconds = $this->ticks - $this->ticks % self::TICKS_PER_SECOND;
518+
519+
return $ticksWithoutMicroseconds === $endOfDayTicks;
520+
}
521+
522+
/**
523+
* Returns whether time is midnight.
524+
*
525+
* @return bool
526+
*/
527+
public function isMidnight(): bool
528+
{
529+
return $this->isStartOfDay();
530+
}
531+
532+
/**
533+
* Returns whether time is midday.
534+
*
535+
* @return bool
536+
*/
537+
public function isMidday(): bool
538+
{
539+
return $this->ticks === 12 * self::TICKS_PER_HOUR;
540+
}
541+
494542
/**
495543
* Returns an `DateTimeImmutable` instance set to this clock time.
496544
*

tests/TestCase/ChronosTimeTest.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,42 @@ public function testComparisons(): void
299299
$this->assertFalse($t3->between($t1, $t2));
300300
}
301301

302+
public function testIsStartOfDay(): void
303+
{
304+
$this->assertTrue(ChronosTime::parse('00:00:00')->isStartOfDay());
305+
$this->assertTrue(ChronosTime::midnight()->isStartOfDay());
306+
$this->assertFalse(ChronosTime::parse('00:00:00.000001')->isStartOfDay());
307+
$this->assertFalse(ChronosTime::parse('00:00:01')->isStartOfDay());
308+
$this->assertFalse(ChronosTime::noon()->isStartOfDay());
309+
}
310+
311+
public function testIsEndOfDay(): void
312+
{
313+
$this->assertTrue(ChronosTime::parse('23:59:59')->isEndOfDay());
314+
$this->assertTrue(ChronosTime::endOfDay()->isEndOfDay());
315+
$this->assertTrue(ChronosTime::parse('23:59:59.999999')->isEndOfDay());
316+
$this->assertFalse(ChronosTime::parse('23:59:58')->isEndOfDay());
317+
$this->assertFalse(ChronosTime::midnight()->isEndOfDay());
318+
$this->assertFalse(ChronosTime::noon()->isEndOfDay());
319+
}
320+
321+
public function testIsMidnight(): void
322+
{
323+
$this->assertTrue(ChronosTime::midnight()->isMidnight());
324+
$this->assertTrue(ChronosTime::parse('00:00:00')->isMidnight());
325+
$this->assertFalse(ChronosTime::parse('00:00:00.000001')->isMidnight());
326+
$this->assertFalse(ChronosTime::noon()->isMidnight());
327+
}
328+
329+
public function testIsMidday(): void
330+
{
331+
$this->assertTrue(ChronosTime::noon()->isMidday());
332+
$this->assertTrue(ChronosTime::parse('12:00:00')->isMidday());
333+
$this->assertFalse(ChronosTime::parse('12:00:00.000001')->isMidday());
334+
$this->assertFalse(ChronosTime::parse('12:00:01')->isMidday());
335+
$this->assertFalse(ChronosTime::midnight()->isMidday());
336+
}
337+
302338
public function testToDateTimeImmutable(): void
303339
{
304340
$time = ChronosTime::parse('23:59:59.999999');

0 commit comments

Comments
 (0)