Skip to content

Commit 784066a

Browse files
committed
feat: add start and end of day
1 parent 28943ea commit 784066a

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

packages/datetime/src/DateTime.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use IntlCalendar;
99
use Tempest\Clock\Clock;
1010
use Tempest\Container\GenericContainer;
11+
use Tempest\DateTime\Exception\UnexpectedValueException;
1112
use Tempest\Support\Language\Locale;
1213

1314
/**
@@ -506,6 +507,22 @@ public function withTime(int $hours, int $minutes, int $seconds = 0, int $nanose
506507
);
507508
}
508509

510+
/**
511+
* Returns a new instance set to midnight of the same day.
512+
*/
513+
public function startOfDay(): static
514+
{
515+
return $this->withTime(0, 0, 0, 0);
516+
}
517+
518+
/**
519+
* Returns a new instance set to the end of the day.
520+
*/
521+
public function endOfDay(): static
522+
{
523+
return $this->withTime(23, 59, 59, 999999999);
524+
}
525+
509526
#[\Override]
510527
public function jsonSerialize(): array
511528
{

packages/datetime/tests/DateTimeTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -611,6 +611,28 @@ public function test_with_time(): void
611611
$this->assertSame(0, $new->getNanoseconds());
612612
}
613613

614+
public function test_start_of_day(): void
615+
{
616+
$date = DateTime::todayAt(14, 0);
617+
$new = $date->startOfDay();
618+
619+
$this->assertSame(0, $new->getHours());
620+
$this->assertSame(0, $new->getMinutes());
621+
$this->assertSame(0, $new->getSeconds());
622+
$this->assertSame(0, $new->getNanoseconds());
623+
}
624+
625+
public function test_end_of_day(): void
626+
{
627+
$date = DateTime::todayAt(14, 0);
628+
$new = $date->endOfDay();
629+
630+
$this->assertSame(23, $new->getHours());
631+
$this->assertSame(59, $new->getMinutes());
632+
$this->assertSame(59, $new->getSeconds());
633+
$this->assertSame(999999999, $new->getNanoseconds());
634+
}
635+
614636
public function test_timezone_info(): void
615637
{
616638
$timeZone = Timezone::EUROPE_BRUSSELS;

0 commit comments

Comments
 (0)