From 3a3eb8f3eac32c4db4d127a79cd71099b2a28409 Mon Sep 17 00:00:00 2001 From: Alexey Solodkiy Date: Sun, 17 Sep 2023 02:28:21 +0300 Subject: [PATCH 1/3] ZonedDateTime::getIntervalTo and Instant::getIntervalTo --- src/Instant.php | 9 +++++++++ src/ZonedDateTime.php | 9 +++++++++ tests/InstantTest.php | 21 +++++++++++++++++++++ tests/ZonedDateTimeTest.php | 23 +++++++++++++++++++++++ 4 files changed, 62 insertions(+) diff --git a/src/Instant.php b/src/Instant.php index 46db3d6..4d9ad11 100644 --- a/src/Instant.php +++ b/src/Instant.php @@ -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 + * */ + public function getIntervalTo(Instant $that): Interval + { + return Interval::of($this, $that); + } + /** * Returns a decimal representation of the timestamp represented by this instant. * diff --git a/src/ZonedDateTime.php b/src/ZonedDateTime.php index 18c2eeb..0326f3e 100644 --- a/src/ZonedDateTime.php +++ b/src/ZonedDateTime.php @@ -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 + * */ + 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. diff --git a/tests/InstantTest.php b/tests/InstantTest.php index f8be2e1..0f6d560 100644 --- a/tests/InstantTest.php +++ b/tests/InstantTest.php @@ -583,6 +583,27 @@ public function providerIsBetweenInclusive(): array ]; } + /** + * @dataProvider providerGetIntervalTo + */ + public function testGetIntervalTo(int $firstSecond, int $firstNano, int $secondSecond, int $secondNano, string $expectedInterval): void + { + $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 * diff --git a/tests/ZonedDateTimeTest.php b/tests/ZonedDateTimeTest.php index 2be7f06..90216f4 100644 --- a/tests/ZonedDateTimeTest.php +++ b/tests/ZonedDateTimeTest.php @@ -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'); From 6dcc2a4b90803e8853be3a51b5a0c3546bde6b67 Mon Sep 17 00:00:00 2001 From: Alexey Solodkiy Date: Sun, 24 Sep 2023 17:34:40 +0300 Subject: [PATCH 2/3] Apply suggestions from code review Co-authored-by: Benjamin Morel --- src/Instant.php | 7 ++++--- src/ZonedDateTime.php | 7 ++++--- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/src/Instant.php b/src/Instant.php index 4d9ad11..bc04c66 100644 --- a/src/Instant.php +++ b/src/Instant.php @@ -334,9 +334,10 @@ public function atTimeZone(TimeZone $timeZone): ZonedDateTime } /** - * Returns Interval from this Instant and the given one (exclusive). - * @throws DateTimeException If given Instant is before that this Instant - * */ + * 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); diff --git a/src/ZonedDateTime.php b/src/ZonedDateTime.php index 0326f3e..2ced2b0 100644 --- a/src/ZonedDateTime.php +++ b/src/ZonedDateTime.php @@ -436,9 +436,10 @@ public function plusDuration(Duration $duration): ZonedDateTime } /** - * Returns Interval from this ZonedDateTime and the given one (exclusive). - * @throws DateTimeException If given date is before this date - * */ + * 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()); From af94a3ddb602e704f94e6f4afe99e0028e02a5a4 Mon Sep 17 00:00:00 2001 From: Alexey Solodkiy Date: Sun, 24 Sep 2023 17:37:26 +0300 Subject: [PATCH 3/3] review fixes --- tests/InstantTest.php | 6 +++--- tests/ZonedDateTimeTest.php | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/InstantTest.php b/tests/InstantTest.php index 0f6d560..a4065c8 100644 --- a/tests/InstantTest.php +++ b/tests/InstantTest.php @@ -586,11 +586,11 @@ public function providerIsBetweenInclusive(): array /** * @dataProvider providerGetIntervalTo */ - public function testGetIntervalTo(int $firstSecond, int $firstNano, int $secondSecond, int $secondNano, string $expectedInterval): void + public function testGetIntervalTo(int $second1, int $nano1, int $second2, int $nano2, string $expectedInterval): void { - $actualResult = Instant::of($firstSecond, $firstNano)->getIntervalTo(Instant::of($secondSecond, $secondNano)); + $actualResult = Instant::of($second1, $nano1)->getIntervalTo(Instant::of($second2, $nano2)); - $this->assertSame($expectedInterval, (string)$actualResult); + $this->assertSame($expectedInterval, (string) $actualResult); } public function providerGetIntervalTo(): array diff --git a/tests/ZonedDateTimeTest.php b/tests/ZonedDateTimeTest.php index 90216f4..0dd1504 100644 --- a/tests/ZonedDateTimeTest.php +++ b/tests/ZonedDateTimeTest.php @@ -864,7 +864,7 @@ public function testGetIntervalTo(string $firstDate, string $secondDate, string { $actualResult = ZonedDateTime::parse($firstDate)->getIntervalTo(ZonedDateTime::parse($secondDate)); - $this->assertSame($expectedInterval, (string)$actualResult); + $this->assertSame($expectedInterval, (string) $actualResult); } public function providerGetIntervalTo(): array