Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LocalDateRange::toInterval #83

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
*/
Comment on lines +247 to +252
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/**
* 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.
*/
/**
* Converts this LocalDateRange to an Interval in the given TimeZone.
*
* The result is an Interval from 00:00 on the start date to 00:00 on the end date + one day
* (the end Instant of Interval being exclusive).
*/

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, should we add a warning relative to this?

As to what happens if 00:00 falls within a DST transition (I'm not even sure if this is possible in any timezone).

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
Loading