Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added diff method #173

Merged
merged 2 commits into from
May 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
---

Expand Down
31 changes: 31 additions & 0 deletions src/Jalalian.php
Original file line number Diff line number Diff line change
Expand Up @@ -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];
}
}
56 changes: 56 additions & 0 deletions tests/JalalianTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
}
}