Skip to content

Commit

Permalink
bug fix add month algorithm and add a test to check it
Browse files Browse the repository at this point in the history
  • Loading branch information
mbpcoder committed Feb 5, 2021
1 parent 5062531 commit bf14b0e
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
16 changes: 10 additions & 6 deletions src/Traits/AdditionAndSubstraction.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,23 @@ public function addYear($count)
$date = $this->currentCalendar->timestampToDate($this->timestamp + $this->timezoneOffset);
$timestamp = $this->currentCalendar->dateToTimestamp($date->year + $count, $date->month, $date->day, $date->hour, $date->minute, $date->second);
$this->timestamp = $timestamp - $this->timezoneOffset;

return $this;
}

public function addMonth($count)
{
$date = $this->currentCalendar->timestampToDate($this->timestamp + $this->timezoneOffset);
$totalMonth = $date->month + $count;
$year = $date->year + floor($totalMonth / 12);
$month = $totalMonth % 12;
$timestamp = $this->currentCalendar->dateToTimestamp($year, $month, $date->day, $date->hour, $date->minute, $date->second);
$year = $date->year + floor($totalMonth / 12) - (($totalMonth % 12) == 0 ? 1 : 0);
$month = ($totalMonth % 12) == 0 ? 12 : ($totalMonth % 12);

$daysInMonth = $this->currentCalendar->daysInMonth($year, $month);
$day = $date->day <= $daysInMonth ? $date->day : $daysInMonth;

$timestamp = $this->currentCalendar->dateToTimestamp($year, $month, $day, $date->hour, $date->minute, $date->second);
$this->timestamp = $timestamp - $this->timezoneOffset;

return $this;
}

Expand Down Expand Up @@ -107,7 +111,7 @@ public function subMinute($count)
public function subSecond($count)
{
$this->timestamp = $this->timestamp - $count;

return $this;
}
}
3 changes: 2 additions & 1 deletion tests/CalendarTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@ public function testJalaliDateTime()
$this->assertTrue($pasoonate->jalali()->setDate(1399,12,11)->daysInMonth() === 30);

$julianDayNumber = $pasoonate->jalali()->setDate(1399,12,11)->getJulianDayNumber();

$this->assertTrue($pasoonate->jalali()->setJulianDayNumber($julianDayNumber)->gregorian()->getJulianDayNumber() == $julianDayNumber);

$this->assertEquals($pasoonate->jalali()->setDate(1399,1,31)->addMonth(11)->format('yyyy/MM/dd'), '1399/12/30');
}

public function testGregorianDateTime()
Expand Down

0 comments on commit bf14b0e

Please sign in to comment.