Skip to content

Commit

Permalink
Fix: #14 - string parameters give incorrect results.
Browse files Browse the repository at this point in the history
  • Loading branch information
fisharebest committed May 31, 2022
1 parent 77801e9 commit a1442ee
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 8 deletions.
2 changes: 1 addition & 1 deletion 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.

Expand Down
58 changes: 51 additions & 7 deletions src/Shim.php
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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)');
Expand Down Expand Up @@ -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);
}
Expand All @@ -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;
}
Expand All @@ -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;
}

Expand All @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand All @@ -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);
}
Expand Down Expand Up @@ -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);
}
Expand All @@ -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) {
Expand Down Expand Up @@ -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;
}
Expand All @@ -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;
}

Expand All @@ -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;
Expand Down
40 changes: 40 additions & 0 deletions test/ShimTest.php
Expand Up @@ -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)
);
}
}

0 comments on commit a1442ee

Please sign in to comment.