From 3d06bd9d43502a120a2d3e9c4e9c58a2b554be21 Mon Sep 17 00:00:00 2001 From: Corey Taylor Date: Tue, 17 Oct 2023 02:32:51 -0500 Subject: [PATCH] Add microseconds to Chronos::endOfDay() --- src/Chronos.php | 32 +++++++++++++--------- tests/TestCase/DateTime/StartEndOfTest.php | 6 +++- 2 files changed, 24 insertions(+), 14 deletions(-) diff --git a/src/Chronos.php b/src/Chronos.php index 80d50b7d..436abe20 100644 --- a/src/Chronos.php +++ b/src/Chronos.php @@ -1425,7 +1425,7 @@ public function subSeconds(int $value): static } /** - * Resets the time to 00:00:00 + * Sets the time to 00:00:00 * * @return static */ @@ -1435,17 +1435,23 @@ public function startOfDay(): static } /** - * Resets the time to 23:59:59 + * Sets the time to 23:59:59 or 23:59:59.999999 + * if `$microseconds` is true. * + * @param bool $microseconds Whether to set microseconds * @return static */ - public function endOfDay(): static + public function endOfDay(bool $microseconds = false): static { + if ($microseconds) { + return $this->modify('23:59:59.999999'); + } + return $this->modify('23:59:59'); } /** - * Resets the date to the first day of the month and the time to 00:00:00 + * Sets the date to the first day of the month and the time to 00:00:00 * * @return static */ @@ -1455,7 +1461,7 @@ public function startOfMonth(): static } /** - * Resets the date to end of the month and time to 23:59:59 + * Sets the date to end of the month and time to 23:59:59 * * @return static */ @@ -1465,7 +1471,7 @@ public function endOfMonth(): static } /** - * Resets the date to the first day of the year and the time to 00:00:00 + * Sets the date to the first day of the year and the time to 00:00:00 * * @return static */ @@ -1475,7 +1481,7 @@ public function startOfYear(): static } /** - * Resets the date to end of the year and time to 23:59:59 + * Sets the date to end of the year and time to 23:59:59 * * @return static */ @@ -1485,7 +1491,7 @@ public function endOfYear(): static } /** - * Resets the date to the first day of the decade and the time to 00:00:00 + * Sets the date to the first day of the decade and the time to 00:00:00 * * @return static */ @@ -1497,7 +1503,7 @@ public function startOfDecade(): static } /** - * Resets the date to end of the decade and time to 23:59:59 + * Sets the date to end of the decade and time to 23:59:59 * * @return static */ @@ -1509,7 +1515,7 @@ public function endOfDecade(): static } /** - * Resets the date to the first day of the century and the time to 00:00:00 + * Sets the date to the first day of the century and the time to 00:00:00 * * @return static */ @@ -1523,7 +1529,7 @@ public function startOfCentury(): static } /** - * Resets the date to end of the century and time to 23:59:59 + * Sets the date to end of the century and time to 23:59:59 * * @return static */ @@ -1542,7 +1548,7 @@ public function endOfCentury(): static } /** - * Resets the date to the first day of week (defined in $weekStartsAt) and the time to 00:00:00 + * Sets the date to the first day of week (defined in $weekStartsAt) and the time to 00:00:00 * * @return static */ @@ -1557,7 +1563,7 @@ public function startOfWeek(): static } /** - * Resets the date to end of week (defined in $weekEndsAt) and time to 23:59:59 + * Sets the date to end of week (defined in $weekEndsAt) and time to 23:59:59 * * @return static */ diff --git a/tests/TestCase/DateTime/StartEndOfTest.php b/tests/TestCase/DateTime/StartEndOfTest.php index a9511366..c67caa3b 100644 --- a/tests/TestCase/DateTime/StartEndOfTest.php +++ b/tests/TestCase/DateTime/StartEndOfTest.php @@ -33,7 +33,11 @@ public function testEndOfDay() $now = Chronos::now(); $dt = $now->endOfDay(); $this->assertTrue($dt instanceof Chronos); - $this->assertDateTime($dt, $dt->year, $dt->month, $dt->day, 23, 59, 59); + $this->assertDateTime($dt, $dt->year, $dt->month, $dt->day, 23, 59, 59, 0); + + $dt = $now->endOfDay(true); + $this->assertTrue($dt instanceof Chronos); + $this->assertDateTime($dt, $dt->year, $dt->month, $dt->day, 23, 59, 59, 999999); } public function testStartOfMonthIsFluid()