Skip to content

Commit

Permalink
Added DateTime::isDateNotAfterInTimeZone
Browse files Browse the repository at this point in the history
  • Loading branch information
Christian Kolb committed Sep 20, 2023
1 parent 7e71a93 commit d4c2633
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/DateTime.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,13 @@ public function isDateAfterInTimeZone(self $dateTime, \DateTimeZone $timeZone):
);
}

public function isDateNotAfterInTimeZone(self $dateTime, \DateTimeZone $timeZone): bool
{
return $this->dateInTimeZone($timeZone)->isNotAfter(
$dateTime->dateInTimeZone($timeZone),
);
}

public function isDateAfterOrEqualToInTimeZone(self $dateTime, \DateTimeZone $timeZone): bool
{
return $this->dateInTimeZone($timeZone)->isAfterOrEqualTo(
Expand Down
80 changes: 80 additions & 0 deletions tests/DateTime/IsDateNotAfterInTimeZoneTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php

declare(strict_types=1);

namespace DigitalCraftsman\DateTimeParts\DateTime;

use DigitalCraftsman\DateTimeParts\DateTime;
use PHPUnit\Framework\TestCase;

/** @coversDefaultClass \DigitalCraftsman\DateTimeParts\DateTime */
final class IsDateNotAfterInTimeZoneTest extends TestCase
{
/**
* @test
*
* @dataProvider dataProvider
*
* @covers ::isDateNotAfterInTimeZone
*/
public function is_date_not_after_in_time_zone_works(
bool $expectedResult,
DateTime $dateTime,
DateTime $comparator,
\DateTimeZone $timeZone,
): void {
// -- Act & Assert
self::assertSame($expectedResult, $dateTime->isDateNotAfterInTimeZone($comparator, $timeZone));
}

/**
* @return array<string, array{
* 0: boolean,
* 1: DateTime,
* 2: DateTime,
* 3: \DateTimeZone
* }>
*/
public function dataProvider(): array
{
return [
'previous day in UTC' => [
true,
DateTime::fromString('2022-10-07 00:00:00'),
DateTime::fromString('2022-10-08 15:00:00'),
new \DateTimeZone('UTC'),
],
'previous day in Europe/Berlin' => [
true,
DateTime::fromStringInTimeZone('2022-10-07 00:00:00', new \DateTimeZone('Europe/Berlin')),
DateTime::fromStringInTimeZone('2022-10-08 15:00:00', new \DateTimeZone('Europe/Berlin')),
new \DateTimeZone('Europe/Berlin'),
],
'same day in Europe/Berlin' => [
true,
DateTime::fromStringInTimeZone('2022-10-08 00:00:00', new \DateTimeZone('Europe/Berlin')),
DateTime::fromStringInTimeZone('2022-10-08 15:00:00', new \DateTimeZone('Europe/Berlin')),
new \DateTimeZone('Europe/Berlin'),
],
'next day in UTC' => [
false,
DateTime::fromString('2022-10-08 00:00:00'),
DateTime::fromString('2022-10-07 15:00:00'),
new \DateTimeZone('UTC'),
],
'next day in Europe/Berlin' => [
false,
DateTime::fromStringInTimeZone('2022-10-08 00:00:00', new \DateTimeZone('Europe/Berlin')),
DateTime::fromStringInTimeZone('2022-10-07 15:00:00', new \DateTimeZone('Europe/Berlin')),
new \DateTimeZone('Europe/Berlin'),
],
'next day in Europe/Berlin when partially in Europe/Berlin' => [
false,
DateTime::fromStringInTimeZone('2022-10-08 00:00:00', new \DateTimeZone('Europe/Berlin')),
DateTime::fromStringInTimeZone('2022-10-07 15:00:00', new \DateTimeZone('Europe/Berlin'))
->toTimeZone(new \DateTimeZone('Europe/Berlin')),
new \DateTimeZone('Europe/Berlin'),
],
];
}
}

0 comments on commit d4c2633

Please sign in to comment.