From 1bd3993c2120e04fb4588f95b81cb398e75bc6fc Mon Sep 17 00:00:00 2001 From: Alexey Solodkiy Date: Sun, 2 Jul 2023 14:05:21 +0300 Subject: [PATCH 1/5] add ZonedDateTime::getDurationTo method --- src/ZonedDateTime.php | 9 +++++++++ tests/ZonedDateTimeTest.php | 23 +++++++++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/src/ZonedDateTime.php b/src/ZonedDateTime.php index 9d1ae56..f618714 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 Duration between current and second ZonedDataTime + * Duration will be negative if second ZonedDateTime before current + */ + public function getDurationTo(ZonedDateTime $secondZonedDateTime): Duration + { + return Duration::between($this->getInstant(), $secondZonedDateTime->getInstant()); + } + /** * Returns a copy of this ZonedDateTime with the specified period in years added. */ diff --git a/tests/ZonedDateTimeTest.php b/tests/ZonedDateTimeTest.php index 42ec7fe..71995e0 100644 --- a/tests/ZonedDateTimeTest.php +++ b/tests/ZonedDateTimeTest.php @@ -841,4 +841,27 @@ private function getTestZonedDateTime(): ZonedDateTime return ZonedDateTime::of($localDateTime, $timeZone); } + + /** + * @dataProvider providerGetDurationTo + */ + public function testGetDurationTo(string $firstDate, string $secondDate, Duration $expectedResult): void + { + $actualResult = ZonedDateTime::parse($firstDate)->getDurationTo(ZonedDateTime::parse($secondDate)); + + $this->assertEquals($expectedResult, $actualResult); + } + + public function providerGetDurationTo(): array + { + return [ + ['2023-01-01T10:00:00Z', '2023-01-01T10:00:00Z', Duration::ofSeconds(0)], + ['2023-01-01T10:00:00Z', '2023-01-01T10:00:10Z', Duration::ofSeconds(10)], + ['2023-01-01T10:00:00.001Z', '2023-01-01T10:00:10.002Z', Duration::ofSeconds(10, 1000000)], + ['2023-01-01T10:00:00.001Z', '2023-01-01T13:00:10.002+03:00', Duration::ofSeconds(10, 1000000)], + ['2023-01-01T10:00:00.000000001Z', '2023-01-01T10:00:00.000000009Z', Duration::ofSeconds(0, 8)], + ['2023-01-01T10:00:00Z', '2023-01-02T10:00:00Z', Duration::ofSeconds(24*60*60)], + ['2023-01-02T10:00:00Z', '2023-01-01T10:00:00Z', Duration::ofSeconds(-24*60*60)], + ]; + } } From ddea6f7c67dbb3551f22d123ba88f37b6c4ae5a4 Mon Sep 17 00:00:00 2001 From: Alexey Solodkiy Date: Sat, 22 Jul 2023 23:14:19 +0300 Subject: [PATCH 2/5] Apply suggestions from code review Co-authored-by: Benjamin Morel --- src/ZonedDateTime.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/ZonedDateTime.php b/src/ZonedDateTime.php index f618714..ab1010e 100644 --- a/src/ZonedDateTime.php +++ b/src/ZonedDateTime.php @@ -436,8 +436,8 @@ public function plusDuration(Duration $duration): ZonedDateTime } /** - * Returns Duration between current and second ZonedDataTime - * Duration will be negative if second ZonedDateTime before current + * 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. */ public function getDurationTo(ZonedDateTime $secondZonedDateTime): Duration { From 3a401a297b0604d54569096584d9e5828b488eab Mon Sep 17 00:00:00 2001 From: Alexey Solodkiy Date: Sat, 22 Jul 2023 23:20:40 +0300 Subject: [PATCH 3/5] code review fixes --- src/ZonedDateTime.php | 4 ++-- tests/ZonedDateTimeTest.php | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/ZonedDateTime.php b/src/ZonedDateTime.php index ab1010e..18c2eeb 100644 --- a/src/ZonedDateTime.php +++ b/src/ZonedDateTime.php @@ -439,9 +439,9 @@ public function plusDuration(Duration $duration): ZonedDateTime * 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. */ - public function getDurationTo(ZonedDateTime $secondZonedDateTime): Duration + public function getDurationTo(ZonedDateTime $that): Duration { - return Duration::between($this->getInstant(), $secondZonedDateTime->getInstant()); + return Duration::between($this->getInstant(), $that->getInstant()); } /** diff --git a/tests/ZonedDateTimeTest.php b/tests/ZonedDateTimeTest.php index 71995e0..5a12c44 100644 --- a/tests/ZonedDateTimeTest.php +++ b/tests/ZonedDateTimeTest.php @@ -849,7 +849,7 @@ public function testGetDurationTo(string $firstDate, string $secondDate, Duratio { $actualResult = ZonedDateTime::parse($firstDate)->getDurationTo(ZonedDateTime::parse($secondDate)); - $this->assertEquals($expectedResult, $actualResult); + $this->assertDurationIs($expectedResult->getSeconds(), $expectedResult->getNanos(), $actualResult); } public function providerGetDurationTo(): array @@ -860,8 +860,8 @@ public function providerGetDurationTo(): array ['2023-01-01T10:00:00.001Z', '2023-01-01T10:00:10.002Z', Duration::ofSeconds(10, 1000000)], ['2023-01-01T10:00:00.001Z', '2023-01-01T13:00:10.002+03:00', Duration::ofSeconds(10, 1000000)], ['2023-01-01T10:00:00.000000001Z', '2023-01-01T10:00:00.000000009Z', Duration::ofSeconds(0, 8)], - ['2023-01-01T10:00:00Z', '2023-01-02T10:00:00Z', Duration::ofSeconds(24*60*60)], - ['2023-01-02T10:00:00Z', '2023-01-01T10:00:00Z', Duration::ofSeconds(-24*60*60)], + ['2023-01-01T10:00:00Z', '2023-01-02T10:00:00Z', Duration::ofSeconds(24 * 60 * 60)], + ['2023-01-02T10:00:00Z', '2023-01-01T10:00:00Z', Duration::ofSeconds(-24 * 60 * 60)], ]; } } From 93904aaa3f20a44d257371425eb81e8bb68df349 Mon Sep 17 00:00:00 2001 From: Alexey Solodkiy Date: Sat, 22 Jul 2023 23:21:44 +0300 Subject: [PATCH 4/5] fix cs --- tests/ZonedDateTimeTest.php | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/tests/ZonedDateTimeTest.php b/tests/ZonedDateTimeTest.php index 5a12c44..931e441 100644 --- a/tests/ZonedDateTimeTest.php +++ b/tests/ZonedDateTimeTest.php @@ -834,14 +834,6 @@ public function testToString(): void $this->assertSame('2000-01-20T12:34:56.123456789-08:00[America/Los_Angeles]', (string) $zonedDateTime); } - private function getTestZonedDateTime(): ZonedDateTime - { - $timeZone = TimeZone::parse('America/Los_Angeles'); - $localDateTime = LocalDateTime::parse('2000-01-20T12:34:56.123456789'); - - return ZonedDateTime::of($localDateTime, $timeZone); - } - /** * @dataProvider providerGetDurationTo */ @@ -864,4 +856,12 @@ public function providerGetDurationTo(): array ['2023-01-02T10:00:00Z', '2023-01-01T10:00:00Z', Duration::ofSeconds(-24 * 60 * 60)], ]; } + + private function getTestZonedDateTime(): ZonedDateTime + { + $timeZone = TimeZone::parse('America/Los_Angeles'); + $localDateTime = LocalDateTime::parse('2000-01-20T12:34:56.123456789'); + + return ZonedDateTime::of($localDateTime, $timeZone); + } } From 6d9d427b76f5aeb6bef3e5988c24fa2b88454a48 Mon Sep 17 00:00:00 2001 From: Benjamin Morel Date: Sun, 23 Jul 2023 20:00:04 +0200 Subject: [PATCH 5/5] Change tests --- tests/ZonedDateTimeTest.php | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/tests/ZonedDateTimeTest.php b/tests/ZonedDateTimeTest.php index 931e441..d0db3ed 100644 --- a/tests/ZonedDateTimeTest.php +++ b/tests/ZonedDateTimeTest.php @@ -837,23 +837,23 @@ public function testToString(): void /** * @dataProvider providerGetDurationTo */ - public function testGetDurationTo(string $firstDate, string $secondDate, Duration $expectedResult): void + public function testGetDurationTo(string $firstDate, string $secondDate, int $expectedSeconds, int $expectedNanos): void { $actualResult = ZonedDateTime::parse($firstDate)->getDurationTo(ZonedDateTime::parse($secondDate)); - $this->assertDurationIs($expectedResult->getSeconds(), $expectedResult->getNanos(), $actualResult); + $this->assertDurationIs($expectedSeconds, $expectedNanos, $actualResult); } public function providerGetDurationTo(): array { return [ - ['2023-01-01T10:00:00Z', '2023-01-01T10:00:00Z', Duration::ofSeconds(0)], - ['2023-01-01T10:00:00Z', '2023-01-01T10:00:10Z', Duration::ofSeconds(10)], - ['2023-01-01T10:00:00.001Z', '2023-01-01T10:00:10.002Z', Duration::ofSeconds(10, 1000000)], - ['2023-01-01T10:00:00.001Z', '2023-01-01T13:00:10.002+03:00', Duration::ofSeconds(10, 1000000)], - ['2023-01-01T10:00:00.000000001Z', '2023-01-01T10:00:00.000000009Z', Duration::ofSeconds(0, 8)], - ['2023-01-01T10:00:00Z', '2023-01-02T10:00:00Z', Duration::ofSeconds(24 * 60 * 60)], - ['2023-01-02T10:00:00Z', '2023-01-01T10:00:00Z', Duration::ofSeconds(-24 * 60 * 60)], + ['2023-01-01T10:00:00Z', '2023-01-01T10:00:00Z', 0, 0], + ['2023-01-01T10:00:00Z', '2023-01-01T10:00:10Z', 10, 0], + ['2023-01-01T10:00:00.001Z', '2023-01-01T10:00:10.002Z', 10, 1000000], + ['2023-01-01T10:00:00.001Z', '2023-01-01T13:00:10.002+03:00', 10, 1000000], + ['2023-01-01T10:00:00.000000001Z', '2023-01-01T10:00:00.000000009Z', 0, 8], + ['2023-01-01T10:00:00Z', '2023-01-02T10:00:00Z', 24 * 60 * 60, 0], + ['2023-01-02T10:00:00Z', '2023-01-01T10:00:00Z', -24 * 60 * 60, 0], ]; }