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

ZonedDateTime::getIntervalTo and Instant::getIntervalTo #81

Merged
merged 3 commits into from
Sep 26, 2023
Merged
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
10 changes: 10 additions & 0 deletions src/Instant.php
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,16 @@ public function atTimeZone(TimeZone $timeZone): ZonedDateTime
return ZonedDateTime::ofInstant($this, $timeZone);
}

/**
* Returns an Interval from this Instant (inclusive) to the given one (exclusive).
*
* @throws DateTimeException If the given Instant is before this Instant.
*/
public function getIntervalTo(Instant $that): Interval
{
return Interval::of($this, $that);
}

/**
* Returns a decimal representation of the timestamp represented by this instant.
*
Expand Down
10 changes: 10 additions & 0 deletions src/ZonedDateTime.php
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,16 @@ public function plusDuration(Duration $duration): ZonedDateTime
return ZonedDateTime::ofInstant($this->instant->plus($duration), $this->timeZone);
}

/**
* Returns an Interval from this ZonedDateTime (inclusive) to the given one (exclusive).
*
* @throws DateTimeException If the given ZonedDateTime is before this ZonedDateTime.
*/
public function getIntervalTo(ZonedDateTime $that): Interval
{
return $this->getInstant()->getIntervalTo($that->getInstant());
}

/**
* Returns a Duration representing the time elapsed between this ZonedDateTime and the given one.
* This method will return a negative duration if the given ZonedDateTime is before the current one.
Expand Down
21 changes: 21 additions & 0 deletions tests/InstantTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -583,6 +583,27 @@ public function providerIsBetweenInclusive(): array
];
}

/**
* @dataProvider providerGetIntervalTo
*/
public function testGetIntervalTo(int $second1, int $nano1, int $second2, int $nano2, string $expectedInterval): void
{
$actualResult = Instant::of($second1, $nano1)->getIntervalTo(Instant::of($second2, $nano2));

$this->assertSame($expectedInterval, (string) $actualResult);
}

public function providerGetIntervalTo(): array
{
return [
[1672567200, 0, 1672567200, 0, '2023-01-01T10:00Z/2023-01-01T10:00Z'],
[1672567200, 0, 1672567210, 0, '2023-01-01T10:00Z/2023-01-01T10:00:10Z'],
[1672567200, 1000000, 1672567210, 2000000, '2023-01-01T10:00:00.001Z/2023-01-01T10:00:10.002Z'],
[1672567200, 1, 1672567200, 9, '2023-01-01T10:00:00.000000001Z/2023-01-01T10:00:00.000000009Z'],
[1672567200, 0, 1672653600, 0, '2023-01-01T10:00Z/2023-01-02T10:00Z'],
];
}

/**
* @dataProvider providerToDecimal
*
Expand Down
23 changes: 23 additions & 0 deletions tests/ZonedDateTimeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -857,6 +857,29 @@ public function providerGetDurationTo(): array
];
}

/**
* @dataProvider providerGetIntervalTo
*/
public function testGetIntervalTo(string $firstDate, string $secondDate, string $expectedInterval): void
{
$actualResult = ZonedDateTime::parse($firstDate)->getIntervalTo(ZonedDateTime::parse($secondDate));

$this->assertSame($expectedInterval, (string) $actualResult);
}

public function providerGetIntervalTo(): array
{
return [
['2023-01-01T10:00:00Z', '2023-01-01T10:00:00Z', '2023-01-01T10:00Z/2023-01-01T10:00Z'],
['2023-01-01T10:00:00Z', '2023-01-01T10:00:10Z', '2023-01-01T10:00Z/2023-01-01T10:00:10Z'],
['2023-01-01T10:00:00.001Z', '2023-01-01T10:00:10.002Z', '2023-01-01T10:00:00.001Z/2023-01-01T10:00:10.002Z'],
['2023-01-01T10:00:00.001Z', '2023-01-01T13:00:10.002+03:00', '2023-01-01T10:00:00.001Z/2023-01-01T10:00:10.002Z'],
['2023-01-01T10:00:00.001+03:00', '2023-01-01T13:00:10.002+03:00', '2023-01-01T07:00:00.001Z/2023-01-01T10:00:10.002Z'],
['2023-01-01T10:00:00.000000001Z', '2023-01-01T10:00:00.000000009Z', '2023-01-01T10:00:00.000000001Z/2023-01-01T10:00:00.000000009Z'],
['2023-01-01T10:00:00Z', '2023-01-02T10:00:00Z', '2023-01-01T10:00Z/2023-01-02T10:00Z'],
];
}

private function getTestZonedDateTime(): ZonedDateTime
{
$timeZone = TimeZone::parse('America/Los_Angeles');
Expand Down
Loading