Skip to content

Commit 4b22406

Browse files
committed
LocalDateRange::toInterval
1 parent f013742 commit 4b22406

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

src/LocalDateRange.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,25 @@ public function jsonSerialize(): string
244244
return (string) $this;
245245
}
246246

247+
/**
248+
* Converts this LocalDateRange to Interval instance.
249+
*
250+
* The result is Interval from 00:00 start date and 00:00 end date + one day (because end in Interval is exclude)
251+
* in the given time-zone.
252+
*/
253+
public function toInterval(TimeZone $timeZone): Interval
254+
{
255+
$startZonedDateTime = $this->getStart()
256+
->atTime(LocalTime::min())
257+
->atTimeZone($timeZone);
258+
$endZonedDateTime = $this->getEnd()
259+
->plusDays(1)
260+
->atTime(LocalTime::min())
261+
->atTimeZone($timeZone);
262+
263+
return $startZonedDateTime->getIntervalTo($endZonedDateTime);
264+
}
265+
247266
/**
248267
* Converts this LocalDateRange to a native DatePeriod object.
249268
*

tests/LocalDateRangeTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Brick\DateTime\LocalDate;
99
use Brick\DateTime\LocalDateRange;
1010
use Brick\DateTime\Parser\DateTimeParseException;
11+
use Brick\DateTime\TimeZone;
1112

1213
use function array_map;
1314
use function iterator_count;
@@ -240,6 +241,28 @@ public function providerToNativeDatePeriod(): array
240241
];
241242
}
242243

244+
/**
245+
* @dataProvider providerToInterval
246+
*/
247+
public function testToInterval(string $range, string $timeZone, string $expectedInterval): void
248+
{
249+
$actualResult = LocalDateRange::parse($range)->toInterval(TimeZone::parse($timeZone));
250+
self::assertSame($expectedInterval, (string) $actualResult);
251+
}
252+
253+
public function providerToInterval(): array
254+
{
255+
return [
256+
['2010-01-01/2010-01-01', 'UTC', '2010-01-01T00:00Z/2010-01-02T00:00Z'],
257+
['2010-01-01/2020-12-31', 'UTC', '2010-01-01T00:00Z/2021-01-01T00:00Z'],
258+
['2022-03-20/2022-03-26', 'Europe/London', '2022-03-20T00:00Z/2022-03-27T00:00Z'],
259+
['2022-03-20/2022-03-27', 'Europe/London', '2022-03-20T00:00Z/2022-03-27T23:00Z'],
260+
['2022-03-20/2022-03-26', 'Europe/Berlin', '2022-03-19T23:00Z/2022-03-26T23:00Z'],
261+
['2022-03-20/2022-03-27', 'Europe/Berlin', '2022-03-19T23:00Z/2022-03-27T22:00Z'],
262+
['2022-01-01/2022-12-31', 'Europe/Berlin', '2021-12-31T23:00Z/2022-12-31T23:00Z'],
263+
];
264+
}
265+
243266
/**
244267
* @dataProvider providerIntersectsWith
245268
*/

0 commit comments

Comments
 (0)