Skip to content

Commit

Permalink
+ DateTimeModifier (#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
dominikvoda authored Aug 19, 2020
1 parent b00dfe3 commit 8669e0d
Show file tree
Hide file tree
Showing 2 changed files with 215 additions and 0 deletions.
40 changes: 40 additions & 0 deletions src/DateTimeModifier.php
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));
}
}
175 changes: 175 additions & 0 deletions src/DateTimeModifierTest.php
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',
],
];
}
}

0 comments on commit 8669e0d

Please sign in to comment.