-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b00dfe3
commit 8669e0d
Showing
2 changed files
with
215 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace BrandEmbassy\DateTime; | ||
|
||
use DateTimeImmutable; | ||
use DateTimeZone; | ||
use Nette\StaticClass; | ||
|
||
final class DateTimeModifier | ||
{ | ||
use StaticClass; | ||
|
||
|
||
public static function getBeginningOfTheDay(DateTimeImmutable $originDateTime): DateTimeImmutable | ||
{ | ||
return $originDateTime->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)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,175 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace BrandEmbassy\DateTime; | ||
|
||
use DateTimeZone; | ||
use PHPUnit\Framework\Assert; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
final class DateTimeModifierTest extends TestCase | ||
{ | ||
/** | ||
* @dataProvider getBeginningOfTheDayDataProvider | ||
*/ | ||
public function testGetBeginningOfTheDay(string $expectedAtom, string $originAtom): void | ||
{ | ||
$originDateTime = DateTimeFromString::createFromAtom($originAtom); | ||
$expectedDateTime = DateTimeFromString::createFromAtom($expectedAtom); | ||
|
||
Assert::assertSame( | ||
DateTimeFormatter::formatAsAtom($expectedDateTime), | ||
DateTimeFormatter::formatAsAtom(DateTimeModifier::getBeginningOfTheDay($originDateTime)) | ||
); | ||
} | ||
|
||
|
||
/** | ||
* @return string[][] | ||
*/ | ||
public function getBeginningOfTheDayDataProvider(): array | ||
{ | ||
return [ | ||
['expectedAtom' => '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', | ||
], | ||
]; | ||
} | ||
} |