Skip to content

Commit

Permalink
Merge pull request #81 from solodkiy/getIntervalTo
Browse files Browse the repository at this point in the history
ZonedDateTime::getIntervalTo and Instant::getIntervalTo
  • Loading branch information
BenMorel authored Sep 26, 2023
2 parents 8988982 + af94a3d commit 8fdf91c
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 0 deletions.
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

0 comments on commit 8fdf91c

Please sign in to comment.