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 1 commit
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
9 changes: 9 additions & 0 deletions src/Instant.php
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,15 @@ public function atTimeZone(TimeZone $timeZone): ZonedDateTime
return ZonedDateTime::ofInstant($this, $timeZone);
}

/**
* Returns Interval from this Instant and the given one (exclusive).
* @throws DateTimeException If given Instant is before that this Instant
* */
solodkiy marked this conversation as resolved.
Show resolved Hide resolved
public function getIntervalTo(Instant $that): Interval
{
return Interval::of($this, $that);
}

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

/**
* Returns Interval from this ZonedDateTime and the given one (exclusive).
* @throws DateTimeException If given date is before this date
* */
solodkiy marked this conversation as resolved.
Show resolved Hide resolved
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 $firstSecond, int $firstNano, int $secondSecond, int $secondNano, string $expectedInterval): void
BenMorel marked this conversation as resolved.
Show resolved Hide resolved
{
$actualResult = Instant::of($firstSecond, $firstNano)->getIntervalTo(Instant::of($secondSecond, $secondNano));

$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