Skip to content

Commit

Permalink
LocalDateRange::toInterval
Browse files Browse the repository at this point in the history
  • Loading branch information
solodkiy committed Sep 30, 2023
1 parent f013742 commit 4b22406
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
19 changes: 19 additions & 0 deletions src/LocalDateRange.php
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,25 @@ public function jsonSerialize(): string
return (string) $this;
}

/**
* Converts this LocalDateRange to Interval instance.
*
* The result is Interval from 00:00 start date and 00:00 end date + one day (because end in Interval is exclude)
* in the given time-zone.
*/
public function toInterval(TimeZone $timeZone): Interval
{
$startZonedDateTime = $this->getStart()
->atTime(LocalTime::min())
->atTimeZone($timeZone);
$endZonedDateTime = $this->getEnd()
->plusDays(1)
->atTime(LocalTime::min())
->atTimeZone($timeZone);

return $startZonedDateTime->getIntervalTo($endZonedDateTime);
}

/**
* Converts this LocalDateRange to a native DatePeriod object.
*
Expand Down
23 changes: 23 additions & 0 deletions tests/LocalDateRangeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Brick\DateTime\LocalDate;
use Brick\DateTime\LocalDateRange;
use Brick\DateTime\Parser\DateTimeParseException;
use Brick\DateTime\TimeZone;

use function array_map;
use function iterator_count;
Expand Down Expand Up @@ -240,6 +241,28 @@ public function providerToNativeDatePeriod(): array
];
}

/**
* @dataProvider providerToInterval
*/
public function testToInterval(string $range, string $timeZone, string $expectedInterval): void
{
$actualResult = LocalDateRange::parse($range)->toInterval(TimeZone::parse($timeZone));
self::assertSame($expectedInterval, (string) $actualResult);
}

public function providerToInterval(): array
{
return [
['2010-01-01/2010-01-01', 'UTC', '2010-01-01T00:00Z/2010-01-02T00:00Z'],
['2010-01-01/2020-12-31', 'UTC', '2010-01-01T00:00Z/2021-01-01T00:00Z'],
['2022-03-20/2022-03-26', 'Europe/London', '2022-03-20T00:00Z/2022-03-27T00:00Z'],
['2022-03-20/2022-03-27', 'Europe/London', '2022-03-20T00:00Z/2022-03-27T23:00Z'],
['2022-03-20/2022-03-26', 'Europe/Berlin', '2022-03-19T23:00Z/2022-03-26T23:00Z'],
['2022-03-20/2022-03-27', 'Europe/Berlin', '2022-03-19T23:00Z/2022-03-27T22:00Z'],
['2022-01-01/2022-12-31', 'Europe/Berlin', '2021-12-31T23:00Z/2022-12-31T23:00Z'],
];
}

/**
* @dataProvider providerIntersectsWith
*/
Expand Down

0 comments on commit 4b22406

Please sign in to comment.