From 8669e0db2b94461ff2f0e0112ef7c8dab4daebcc Mon Sep 17 00:00:00 2001 From: Dominik Voda Date: Wed, 19 Aug 2020 14:08:16 +0200 Subject: [PATCH] + DateTimeModifier (#14) --- src/DateTimeModifier.php | 40 ++++++++ src/DateTimeModifierTest.php | 175 +++++++++++++++++++++++++++++++++++ 2 files changed, 215 insertions(+) create mode 100644 src/DateTimeModifier.php create mode 100644 src/DateTimeModifierTest.php diff --git a/src/DateTimeModifier.php b/src/DateTimeModifier.php new file mode 100644 index 0000000..7ee3a69 --- /dev/null +++ b/src/DateTimeModifier.php @@ -0,0 +1,40 @@ +modify('midnight'); + } + + + public static function getEndOfTheDay(DateTimeImmutable $originDateTime): DateTimeImmutable + { + return $originDateTime->modify('tomorrow')->modify('-1 second'); + } + + + public static function getBeginningOfTheDayInTimezone( + DateTimeImmutable $originDateTime, + DateTimeZone $dateTimeZone + ): DateTimeImmutable { + return self::getBeginningOfTheDay($originDateTime->setTimezone($dateTimeZone)); + } + + + public static function getEndOfTheDayInTimezone( + DateTimeImmutable $originDateTime, + DateTimeZone $dateTimeZone + ): DateTimeImmutable { + return self::getEndOfTheDay($originDateTime->setTimezone($dateTimeZone)); + } +} diff --git a/src/DateTimeModifierTest.php b/src/DateTimeModifierTest.php new file mode 100644 index 0000000..faa85e6 --- /dev/null +++ b/src/DateTimeModifierTest.php @@ -0,0 +1,175 @@ + '2020-08-18T00:00:00+02:00', 'originAtom' => '2020-08-18T02:18:18+02:00'], + ['expectedAtom' => '2020-08-18T00:00:00+02:00', 'originAtom' => '2020-08-18T00:00:00+02:00'], + ['expectedAtom' => '2020-08-18T00:00:00+02:00', 'originAtom' => '2020-08-18T23:59:59+02:00'], + ['expectedAtom' => '2020-08-18T00:00:00+01:00', 'originAtom' => '2020-08-18T00:00:01+01:00'], + ['expectedAtom' => '2020-08-18T00:00:00+01:00', 'originAtom' => '2020-08-18T11:59:00+01:00'], + ['expectedAtom' => '2020-08-18T00:00:00-04:00', 'originAtom' => '2020-08-18T11:59:00-04:00'], + ]; + } + + + /** + * @dataProvider getBeginningOfTheDayInTimezoneDataProvider + */ + public function testGetBeginningOfTheDayInTimezone( + string $expectedAtom, + string $originAtom, + string $dateTimeZoneName + ): void { + $originDateTime = DateTimeFromString::createFromAtom($originAtom); + $expectedDateTime = DateTimeFromString::createFromAtom($expectedAtom); + + $dateTimeZone = new DateTimeZone($dateTimeZoneName); + + Assert::assertSame( + DateTimeFormatter::formatAsAtom($expectedDateTime), + DateTimeFormatter::formatAsAtom( + DateTimeModifier::getBeginningOfTheDayInTimezone($originDateTime, $dateTimeZone) + ) + ); + } + + + /** + * @return string[][] + */ + public function getBeginningOfTheDayInTimezoneDataProvider(): array + { + return [ + [ + 'expectedAtom' => '2020-08-18T00:00:00+00:00', + 'originAtom' => '2020-08-18T02:18:18+02:00', + 'dateTimeZoneName' => 'UTC', + ], + [ + 'expectedAtom' => '2020-08-18T00:00:00+02:00', + 'originAtom' => '2020-08-18T02:18:18+02:00', + 'dateTimeZoneName' => 'Europe/Prague', + ], + [ + 'expectedAtom' => '2020-08-18T00:00:00+01:00', + 'originAtom' => '2020-08-18T02:18:18+02:00', + 'dateTimeZoneName' => 'Europe/London', + ], + [ + 'expectedAtom' => '2020-08-17T00:00:00-06:00', + 'originAtom' => '2020-08-18T02:18:18+02:00', + 'dateTimeZoneName' => 'America/Denver', + ], + ]; + } + + + /** + * @dataProvider getEndOfTheDayDataProvider + */ + public function testGetEndOfTheDay(string $expectedAtom, string $originAtom): void + { + $originDateTime = DateTimeFromString::createFromAtom($originAtom); + $expectedDateTime = DateTimeFromString::createFromAtom($expectedAtom); + + Assert::assertSame( + DateTimeFormatter::formatAsAtom($expectedDateTime), + DateTimeFormatter::formatAsAtom(DateTimeModifier::getEndOfTheDay($originDateTime)) + ); + } + + + /** + * @return string[][] + */ + public function getEndOfTheDayDataProvider(): array + { + return [ + ['expectedAtom' => '2020-08-18T23:59:59+02:00', 'originAtom' => '2020-08-18T02:18:18+02:00'], + ['expectedAtom' => '2020-08-18T23:59:59+02:00', 'originAtom' => '2020-08-18T23:59:59+02:00'], + ['expectedAtom' => '2020-08-18T23:59:59+01:00', 'originAtom' => '2020-08-18T00:00:01+01:00'], + ['expectedAtom' => '2020-08-18T23:59:59+01:00', 'originAtom' => '2020-08-18T11:59:00+01:00'], + ['expectedAtom' => '2020-08-18T23:59:59+01:00', 'originAtom' => '2020-08-18T23:59:59+01:00'], + ['expectedAtom' => '2020-08-18T23:59:59-04:00', 'originAtom' => '2020-08-18T11:59:00-04:00'], + ]; + } + + + /** + * @dataProvider getEndOfTheDayInTimezoneDataProvider + */ + public function testGetEndOfTheDayInTimezone( + string $expectedAtom, + string $originAtom, + string $dateTimeZoneName + ): void { + $originDateTime = DateTimeFromString::createFromAtom($originAtom); + $expectedDateTime = DateTimeFromString::createFromAtom($expectedAtom); + + $dateTimeZone = new DateTimeZone($dateTimeZoneName); + + Assert::assertSame( + DateTimeFormatter::formatAsAtom($expectedDateTime), + DateTimeFormatter::formatAsAtom( + DateTimeModifier::getEndOfTheDayInTimezone($originDateTime, $dateTimeZone) + ) + ); + } + + + /** + * @return string[][] + */ + public function getEndOfTheDayInTimezoneDataProvider(): array + { + return [ + [ + 'expectedAtom' => '2020-08-18T23:59:59+00:00', + 'originAtom' => '2020-08-18T02:18:18+02:00', + 'dateTimeZoneName' => 'UTC', + ], + [ + 'expectedAtom' => '2020-08-18T23:59:59+02:00', + 'originAtom' => '2020-08-18T02:18:18+02:00', + 'dateTimeZoneName' => 'Europe/Prague', + ], + [ + 'expectedAtom' => '2020-08-18T23:59:59+01:00', + 'originAtom' => '2020-08-18T02:18:18+02:00', + 'dateTimeZoneName' => 'Europe/London', + ], + [ + 'expectedAtom' => '2020-08-17T23:59:59-06:00', + 'originAtom' => '2020-08-18T02:18:18+02:00', + 'dateTimeZoneName' => 'America/Denver', + ], + ]; + } +}