From e9f571811e74eed442bb9e6f12223e63ea21ed2a Mon Sep 17 00:00:00 2001 From: Lenny ROUANET Date: Tue, 23 Jul 2024 11:07:11 +0200 Subject: [PATCH] Fix date interval duration --- component/Time/DateInterval.php | 2 +- test/Time/DateIntervalTest.php | 6 +++++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/component/Time/DateInterval.php b/component/Time/DateInterval.php index 8a64c5a..6a1c441 100644 --- a/component/Time/DateInterval.php +++ b/component/Time/DateInterval.php @@ -85,7 +85,7 @@ public function isAfter( private function calculateDuration(): void { - $this->duration = ($this->from && $this->to) ? new Duration($this->to->time - $this->from->time) : null; + $this->duration = ($this->from && $this->to) ? new Duration(($this->to->time + Duration::DAY - 1) - $this->from->time) : null; } public static function make( diff --git a/test/Time/DateIntervalTest.php b/test/Time/DateIntervalTest.php index 284b60f..4551b18 100644 --- a/test/Time/DateIntervalTest.php +++ b/test/Time/DateIntervalTest.php @@ -5,6 +5,7 @@ namespace Test\Time; use Phant\DataStructure\Time\DateInterval; +use Phant\DataStructure\Time\Duration; use Phant\Error\NotCompliant; @@ -39,7 +40,10 @@ public function testInterface(): void $this->assertEquals('1970-01-01', (string)$this->fixture->to); $this->assertIsObject($this->fixture->duration); - $this->assertEquals('15 years, 6 months, 29 days, 12 h', (string)$this->fixture->duration); + $this->assertInstanceOf(Duration::class, $this->fixture->duration); + $this->assertEquals(new Duration( + strtotime('1970-01-01 23:59:59') - strtotime('1954-06-07 00:00:00') + ), $this->fixture->duration); } public function testIsBefore(): void