From 02b6941acd386745aa5ee64574b1170fb858ae18 Mon Sep 17 00:00:00 2001 From: Omid Reza Vasheghani Farahani Date: Fri, 16 Sep 2022 17:38:23 +0430 Subject: [PATCH 1/2] Added diff method --- src/Jalalian.php | 31 +++++++++++++++++++++++ tests/JalalianTest.php | 56 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 87 insertions(+) diff --git a/src/Jalalian.php b/src/Jalalian.php index e57e86c..e859a63 100644 --- a/src/Jalalian.php +++ b/src/Jalalian.php @@ -754,4 +754,35 @@ public function getWeekOfMonth(): int { return floor(($this->day + 5 - $this->getDayOfWeek()) / 7) + 1; } + + public function diff(Jalalian $ref): array + { + if ($this->equalsTo($ref)) { + return [0, 0, 0]; + } + + $biggerDate = $this->greaterThan($ref) ? $this : $ref; + $biggerYear = $biggerDate->getYear(); + $biggerMonth = $biggerDate->getMonth(); + $biggerDay = $biggerDate->getDay(); + $smallerDate = $this->greaterThan($ref) ? $ref : $this; + $smallerYear = $smallerDate->getYear(); + $smallerMonth = $smallerDate->getMonth(); + $smallerDay = $smallerDate->getDay(); + + $yearDiff = $biggerYear - $smallerYear; + $monthDiff = $biggerMonth - $smallerMonth; + $dayDiff = $biggerDay - $smallerDay; + if ($dayDiff < 0) { + $dayDiff = $biggerDay + + $smallerDate->getEndDayOfMonth()->getDay() - $smallerDate->getDay(); + $monthDiff--; + } + if ($monthDiff < 0) { + $monthDiff += 12; + $yearDiff--; + } + + return [$yearDiff, $monthDiff, $dayDiff]; + } } diff --git a/tests/JalalianTest.php b/tests/JalalianTest.php index b22ca09..3e308ab 100644 --- a/tests/JalalianTest.php +++ b/tests/JalalianTest.php @@ -370,4 +370,60 @@ public function testGetQuarter() $jDate = new Jalalian(1402, 7, 23); $this->assertEquals(3, $jDate->getQuarter()); } + + public function testdiff() + { + $jDate = new Jalalian(1401, 6, 26); + + //same day + $this->assertEquals($jDate->diff(new Jalalian(1401, 6, 26)), [0, 0, 0]); + + //year before + $this->assertEquals($jDate->diff(new Jalalian(1401, 6, 25)), [0, 0, 1]); + + //day after + $this->assertEquals($jDate->diff(new Jalalian(1401, 6, 27)), [0, 0, 1]); + + //same montt, same year, before + $this->assertEquals($jDate->diff(new Jalalian(1401, 6, 24)), [0, 0, 2]); + + //same month, same year, after + $this->assertEquals($jDate->diff(new Jalalian(1401, 6, 28)), [0, 0, 2]); + + //same year, before, smaller day + $this->assertEquals($jDate->diff(new Jalalian(1401, 4, 12)), [0, 2, 14]); + + //same year, before, greater day + $this->assertEquals($jDate->diff(new Jalalian(1401, 4, 27)), [0, 1, 30]); + + //same year, after, smaller day + $this->assertEquals($jDate->diff(new Jalalian(1401, 9, 10)), [0, 2, 15]); + + //same year, after, greater day + $this->assertEquals($jDate->diff(new Jalalian(1401, 10, 28)), [0, 4, 2]); + + //previous year, smaller month, smaller day + $this->assertEquals($jDate->diff(new Jalalian(1389, 4, 12)), [12, 2, 14]); + + //previous year, smaller month, greater day + $this->assertEquals($jDate->diff(new Jalalian(1395, 4, 29)), [6, 1, 28]); + + //previous year, greater month, smaller day + $this->assertEquals($jDate->diff(new Jalalian(1395, 9, 10)), [5, 9, 16]); + + //previous year, greater month, greater day + $this->assertEquals($jDate->diff(new Jalalian(1395, 9, 30)), [5, 8, 26]); + + //greater year, smaller month, smaller day + $this->assertEquals($jDate->diff(new Jalalian(1402, 4, 12)), [0, 9, 17]); + + //greater year, smaller month, greater day + $this->assertEquals($jDate->diff(new Jalalian(1403, 4, 29)), [1, 10, 3]); + + //greater year, greater month, smaller day + $this->assertEquals($jDate->diff(new Jalalian(1405, 9, 10)), [4, 2, 15]); + + //greater year, greater month, greater day + $this->assertEquals($jDate->diff(new Jalalian(1405, 9, 30)), [4, 3, 4]); + } } From c972a69934deb6251ef124705e553a4e4bd45ebe Mon Sep 17 00:00:00 2001 From: Omid Reza Vasheghani Farahani Date: Fri, 16 Sep 2022 17:38:58 +0430 Subject: [PATCH 2/2] Updated readme and added getLastDayOfMonth, getLastDayOfYear and diff methods --- README.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/README.md b/README.md index 99a2b18..7f80316 100644 --- a/README.md +++ b/README.md @@ -639,7 +639,12 @@ public function getNextMonth(): Jalalian ``` --- +```php +public function diff(Jalalian $ref): array +$diff = (new Jalalian(1397, 5, 24))->diff(new Jalalian(1398, 6, 30)); +// output: [1, 1, 6] +``` ### CalendarUtils ---