Skip to content

Commit

Permalink
jewish months bug fix
Browse files Browse the repository at this point in the history
  • Loading branch information
zachweix committed Dec 25, 2023
1 parent 65ffbf1 commit b546690
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 3 deletions.
46 changes: 43 additions & 3 deletions src/HebrewCalendar/JewishDate.php
Original file line number Diff line number Diff line change
Expand Up @@ -605,7 +605,7 @@ public function addMonthsJewish($amount) {
}

// If we're moving enough, we'll move to Tishrei of next year
if ($diffToEndOfYear > $amount) {
if ($diffToEndOfYear < $amount) {
$this->jewishMonth = self::TISHREI;
$this->jewishYear++;
$amount = $amount - $diffToEndOfYear - 1;
Expand Down Expand Up @@ -763,7 +763,7 @@ public function subMonthsJewish($amount) {
}

// If we're moving enough, we'll move to Elul of last year
if ($monthOfYear > $amount) {
if ($monthOfYear < $amount) {
$this->jewishMonth = self::ELUL;
$this->jewishYear--;
$amount = $amount - $monthOfYear;
Expand Down Expand Up @@ -887,12 +887,52 @@ public function setJewishDayOfMonth($dayOfMonth) {

/*
|--------------------------------------------------------------------------
| CLONEABLE
| UTILITIES
|--------------------------------------------------------------------------
*/

public function copy() {
return clone $this;
}

public function formatGregorian() {
$year = $this->gregorianYear;
$month = $this->gregorianMonth;
$day = $this->gregorianDayOfMonth;

if ($month < 10) {
$month = "0" . $month;
}

if ($day < 10) {
$day = "0" . $day;
}

return $year . "-" . $month . "-" . $day;
}

public function formatJewish() {
$year = $this->jewishYear;
$month = $this->jewishMonth;
$day = $this->jewishDay;

if ($month < 10) {
$month = "0" . $month;
}

if ($day < 10) {
$day = "0" . $day;
}

return $year . "-" . $month . "-" . $day;
}

public function __debugInfo() {
return [
'jewishDate' => $this->formatJewish(),
'gregorianDate' => $this->formatGregorian(),
'dayOfWeek' => $this->dayOfWeek,
];
}

}
32 changes: 32 additions & 0 deletions tests/HebrewCalendar/JewishDateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,38 @@ public function gregorianForwardMonthToMonth() {
$this->assertEquals(5774, $hebrewDate->getJewishYear());
$this->assertEquals(2, $hebrewDate->getJewishMonth());
$this->assertEquals(18, $hebrewDate->getJewishDayOfMonth());

$hebrewDate->addMonthsJewish(1);
$this->assertEquals(2014, $hebrewDate->getGregorianYear());
$this->assertEquals(6, $hebrewDate->getGregorianMonth());
$this->assertEquals(16, $hebrewDate->getGregorianDayOfMonth());
$this->assertEquals(5774, $hebrewDate->getJewishYear());
$this->assertEquals(3, $hebrewDate->getJewishMonth());
$this->assertEquals(18, $hebrewDate->getJewishDayOfMonth());

$hebrewDate->addMonthsJewish(5);
$this->assertEquals(2014, $hebrewDate->getGregorianYear());
$this->assertEquals(11, $hebrewDate->getGregorianMonth());
$this->assertEquals(11, $hebrewDate->getGregorianDayOfMonth());
$this->assertEquals(5775, $hebrewDate->getJewishYear());
$this->assertEquals(8, $hebrewDate->getJewishMonth());
$this->assertEquals(18, $hebrewDate->getJewishDayOfMonth());

$hebrewDate->subMonthsJewish(5);
$this->assertEquals(2014, $hebrewDate->getGregorianYear());
$this->assertEquals(6, $hebrewDate->getGregorianMonth());
$this->assertEquals(16, $hebrewDate->getGregorianDayOfMonth());
$this->assertEquals(5774, $hebrewDate->getJewishYear());
$this->assertEquals(3, $hebrewDate->getJewishMonth());
$this->assertEquals(18, $hebrewDate->getJewishDayOfMonth());

$hebrewDate->subMonthsJewish(1);
$this->assertEquals(2014, $hebrewDate->getGregorianYear());
$this->assertEquals(5, $hebrewDate->getGregorianMonth());
$this->assertEquals(18, $hebrewDate->getGregorianDayOfMonth());
$this->assertEquals(5774, $hebrewDate->getJewishYear());
$this->assertEquals(2, $hebrewDate->getJewishMonth());
$this->assertEquals(18, $hebrewDate->getJewishDayOfMonth());
}


Expand Down

0 comments on commit b546690

Please sign in to comment.