From a1442ee2e6d0e28da16608be80a20642f52b791b Mon Sep 17 00:00:00 2001 From: Greg Roach Date: Tue, 31 May 2022 16:56:53 +0100 Subject: [PATCH] Fix: #14 - string parameters give incorrect results. --- CHANGELOG.md | 2 +- src/Shim.php | 58 +++++++++++++++++++++++++++++++++++++++++------ test/ShimTest.php | 40 ++++++++++++++++++++++++++++++++ 3 files changed, 92 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 66e38a5..ce05e79 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,7 +1,7 @@ CHANGE LOG ========== -## 2.6.0 (????-??-??) +## 2.6.0 (2022-05-31) - Update error handling to match latest versions of PHP. - Add PHP 8 to the test matrix, and remove PHP 5.3 from it. diff --git a/src/Shim.php b/src/Shim.php index ffdd6cd..ac9154a 100644 --- a/src/Shim.php +++ b/src/Shim.php @@ -174,6 +174,9 @@ public static function shouldEmulateBug67976() */ public static function calDaysInMonth($calendar_id, $month, $year) { + $month = (int) $month; + $year = (int) $year; + switch ($calendar_id) { case CAL_FRENCH: return self::calDaysInMonthFrench($year, $month); @@ -265,6 +268,8 @@ private static function calDaysInMonthFrench($year, $month) */ public static function calFromJd($julian_day, $calendar_id) { + $julian_day = (int) $julian_day; + switch ($calendar_id) { case CAL_FRENCH: return self::calFromJdCalendar($julian_day, self::jdToFrench($julian_day), self::$MONTH_NAMES_FRENCH, self::$MONTH_NAMES_FRENCH); @@ -407,6 +412,10 @@ private static function calInfoCalendar($month_names, $month_names_short, $max_d */ public static function calToJd($calendar_id, $month, $day, $year) { + $day = (int) $day; + $month = (int) $month; + $year = (int) $year; + switch ($calendar_id) { case CAL_FRENCH: return self::frenchToJd($month, $day, $year); @@ -442,6 +451,8 @@ public static function calToJd($calendar_id, $month, $day, $year) */ public static function easterDate($year) { + $year = (int) $year; + if ($year < 1970 || $year > 2037) { if (PHP_VERSION_ID >= 80000) { throw new ValueError('easter_date(): Argument #1 ($year) must be between 1970 and 2037 (inclusive)'); @@ -476,10 +487,13 @@ public static function easterDate($year) */ public static function easterDays($year, $method) { + $year = (int) $year; + $method = (int) $method; + if ( - $method == CAL_EASTER_ALWAYS_JULIAN || - $method == CAL_EASTER_ROMAN && $year <= 1582 || - $year <= 1752 && $method != CAL_EASTER_ROMAN && $method != CAL_EASTER_ALWAYS_GREGORIAN + $method === CAL_EASTER_ALWAYS_JULIAN || + $method === CAL_EASTER_ROMAN && $year <= 1582 || + $year <= 1752 && $method !== CAL_EASTER_ROMAN && $method !== CAL_EASTER_ALWAYS_GREGORIAN ) { return self::$julian_calendar->easterDays($year); } @@ -502,6 +516,10 @@ public static function easterDays($year, $method) */ public static function frenchToJd($month, $day, $year) { + $day = (int) $day; + $month = (int) $month; + $year = (int) $year; + if ($year <= 0) { return 0; } @@ -524,7 +542,11 @@ public static function frenchToJd($month, $day, $year) */ public static function gregorianToJd($month, $day, $year) { - if ($year == 0) { + $day = (int) $day; + $month = (int) $month; + $year = (int) $year; + + if ($year === 0) { return 0; } @@ -546,6 +568,8 @@ public static function gregorianToJd($month, $day, $year) */ public static function jdDayOfWeek($julian_day, $mode) { + $julian_day = (int) $julian_day; + $day_of_week = ($julian_day + 1) % 7; if ($day_of_week < 0) { $day_of_week += 7; @@ -577,6 +601,8 @@ public static function jdDayOfWeek($julian_day, $mode) */ public static function jdMonthName($julian_day, $mode) { + $julian_day = (int) $julian_day; + switch ($mode) { case CAL_MONTH_GREGORIAN_LONG: return self::jdMonthNameCalendar(self::$gregorian_calendar, $julian_day, self::$MONTH_NAMES); @@ -670,6 +696,8 @@ private static function jdToCalendar(CalendarInterface $calendar, $julian_day, $ */ public static function jdToFrench($julian_day) { + $julian_day = (int) $julian_day; + // JDToFrench() converts years 1 to 14 inclusive, even though the calendar // officially ended on 10 NivĂ´se 14 (JD 2380687, 31st December 1805 Gregorian). return self::jdToCalendar(self::$french_calendar, $julian_day, 2375840, 2380952); @@ -688,8 +716,10 @@ public static function jdToFrench($julian_day) */ public static function jdToGregorian($julian_day) { + $julian_day = (int) $julian_day; + // PHP has different limits on 32 and 64 bit systems. - $MAX_JD = PHP_INT_SIZE == 4 ? 536838866 : 2305843009213661906; + $MAX_JD = PHP_INT_SIZE === 4 ? 536838866 : 2305843009213661906; return self::jdToCalendar(self::$gregorian_calendar, $julian_day, 1, $MAX_JD); } @@ -741,8 +771,10 @@ public static function jdToJewish($julian_day, $hebrew, $fl) */ public static function jdToJulian($julian_day) { + $julian_day = (int) $julian_day; + // PHP has different limits on 32 and 64 bit systems. - $MAX_JD = PHP_INT_SIZE == 4 ? 536838829 : 784368370349; + $MAX_JD = PHP_INT_SIZE === 4 ? 536838829 : 784368370349; return self::jdToCalendar(self::$julian_calendar, $julian_day, 1, $MAX_JD); } @@ -761,6 +793,8 @@ public static function jdToJulian($julian_day) */ public static function jdToUnix($julian_day) { + $julian_day = (int) $julian_day; + $upper_limit = self::jdToUnixUpperLimit(); if ($julian_day >= 2440588 && $julian_day <= $upper_limit) { @@ -805,6 +839,10 @@ public static function jdToUnixUpperLimit() */ public static function jewishToJd($month, $day, $year) { + $day = (int) $day; + $month = (int) $month; + $year = (int) $year; + if ($year <= 0) { return 0; } @@ -827,7 +865,11 @@ public static function jewishToJd($month, $day, $year) */ public static function julianToJd($month, $day, $year) { - if ($year == 0) { + $day = (int) $day; + $month = (int) $month; + $year = (int) $year; + + if ($year === 0) { return 0; } @@ -847,6 +889,8 @@ public static function julianToJd($month, $day, $year) */ public static function unixToJd($timestamp) { + $timestamp = (int) $timestamp; + if ($timestamp < 0) { if (PHP_VERSION_ID < 80000) { return false; diff --git a/test/ShimTest.php b/test/ShimTest.php index 820ab18..22cfbd1 100644 --- a/test/ShimTest.php +++ b/test/ShimTest.php @@ -1826,4 +1826,44 @@ public function testUnixToJdEdgeCases() Shim::unixToJd(-1); } } + + /** + * Tests for issue #14 + * + * @covers \Fisharebest\ExtCalendar\Shim::calDaysInMonth + * + * @return void + */ + public function testNonIntegerParameters() + { + $this->assertSame( + Shim::calDaysInMonth(CAL_GREGORIAN, '04', '2022'), + Shim::calDaysInMonth(CAL_GREGORIAN, 4, 2022) + ); + + $this->assertSame( + Shim::calToJd(CAL_GREGORIAN, '04', '03', '2022'), + Shim::calToJd(CAL_GREGORIAN, 4, 3, 2022) + ); + + $this->assertSame( + Shim::frenchToJd('04', '03', '13'), + Shim::frenchToJd(4, 3, 13) + ); + + $this->assertSame( + Shim::gregorianToJd('04', '03', '2022'), + Shim::gregorianToJd(4, 3, 2022) + ); + + $this->assertSame( + Shim::jewishToJd('04', '03', '4321'), + Shim::jewishToJd(4, 3, 4321) + ); + + $this->assertSame( + Shim::julianToJd('04', '03', '2022'), + Shim::julianToJd(4, 3, 2022) + ); + } }