Skip to content

ext/calendar: Allow easter_date to process years after 2037 on 64bit systems #11862

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

Merged
merged 1 commit into from
Aug 4, 2023
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
4 changes: 4 additions & 0 deletions UPGRADING
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,10 @@ PHP 8.3 UPGRADE NOTES
significant digits before the decimal point. Previously negative $decimals
got silently ignored and the number got rounded to zero decimal places.

- Calendar
. easter_date() now supports years from 1970 to 2,000,000,000 on 64-bit systems,
previously it only supported years in the range from 1970 to 2037.

Comment on lines +306 to +309
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We list these in alphabetical order, but that can be fixed as a follow-up for now

========================================
6. New Functions
========================================
Expand Down
24 changes: 23 additions & 1 deletion ext/calendar/easter.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
| Authors: Shane Caraveo <[email protected]> |
| Colin Viebrock <[email protected]> |
| Hartmut Holzgraefe <[email protected]> |
| Arne Perschke <[email protected]> |
+----------------------------------------------------------------------+
*/

Expand All @@ -21,6 +22,10 @@
#include "sdncal.h"
#include <time.h>

/**
* If `gm` is true this will return the timestamp at midnight on Easter of the given year. If it is false this
* will return the number of days Easter is after March 21st.
*/
static void _cal_easter(INTERNAL_FUNCTION_PARAMETERS, bool gm)
{
/* based on code by Simon Kershaw, <[email protected]> */
Expand Down Expand Up @@ -48,10 +53,27 @@ static void _cal_easter(INTERNAL_FUNCTION_PARAMETERS, bool gm)
}
}

if (gm && (year<1970 || year>2037)) { /* out of range for timestamps */
#ifdef ZEND_ENABLE_ZVAL_LONG64
/* Compiling for 64bit, allow years between 1970 and 2.000.000.000 */
if (gm && year < 1970) {
/* timestamps only start after 1970 */
zend_argument_value_error(1, "must be a year after 1970 (inclusive)");
RETURN_THROWS();
}

if (gm && year > 2000000000) {
/* timestamps only go up to the year 2.000.000.000 */
zend_argument_value_error(1, "must be a year before 2.000.000.000 (inclusive)");
RETURN_THROWS();
}
#else
/* Compiling for 32bit, allow years between 1970 and 2037 */
if (gm && (year < 1970 || year > 2037)) {
zend_argument_value_error(1, "must be between 1970 and 2037 (inclusive)");
RETURN_THROWS();
}
#endif


golden = (year % 19) + 1; /* the Golden number */

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
--TEST--
easter_date()
Test easter_date() on 32bit systems
--SKIPIF--
<?php if (PHP_INT_SIZE != 4) die("skip 32-bit only"); ?>
--INI--
date.timezone=UTC
--ENV--
Expand Down
33 changes: 33 additions & 0 deletions ext/calendar/tests/easter_date_64bit.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
--TEST--
Test easter_date() on 64bit systems
--SKIPIF--
<?php if (PHP_INT_SIZE != 8) die("skip 64-bit only"); ?>
--INI--
date.timezone=UTC
--ENV--
TZ=UTC
--EXTENSIONS--
calendar
--FILE--
<?php
putenv('TZ=UTC');
echo date("Y-m-d", easter_date(2000))."\n";
echo date("Y-m-d", easter_date(2001))."\n";
echo date("Y-m-d", easter_date(2002))."\n";
echo date("Y-m-d", easter_date(2045))."\n";
echo date("Y-m-d", easter_date(2046))."\n";
echo date("Y-m-d", easter_date(2047))."\n";
try {
easter_date(1492);
} catch (ValueError $ex) {
echo "{$ex->getMessage()}\n";
}
?>
--EXPECT--
2000-04-23
2001-04-15
2002-03-31
2045-04-09
2046-03-25
2047-04-14
easter_date(): Argument #1 ($year) must be a year after 1970 (inclusive)
21 changes: 21 additions & 0 deletions ext/calendar/tests/easter_date_checks_upper_bound_32bit.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
--TEST--
Test easter_date() on 32bit systems checks the upper year limit
--SKIPIF--
<?php if (PHP_INT_SIZE != 4) die("skip 32-bit only"); ?>
--INI--
date.timezone=UTC
--ENV--
TZ=UTC
--EXTENSIONS--
calendar
--FILE--
<?php
putenv('TZ=UTC');
try {
easter_date(2040);
} catch (ValueError $ex) {
echo "{$ex->getMessage()}\n";
}
?>
--EXPECT--
easter_date(): Argument #1 ($year) must be between 1970 and 2037 (inclusive)
21 changes: 21 additions & 0 deletions ext/calendar/tests/easter_date_checks_upper_bound_64bit.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
--TEST--
Test easter_date() on 64bit systems checks the upper year limit
--SKIPIF--
<?php if (PHP_INT_SIZE != 8) die("skip 64-bit only"); ?>
--INI--
date.timezone=UTC
--ENV--
TZ=UTC
--EXTENSIONS--
calendar
--FILE--
<?php
putenv('TZ=UTC');
try {
easter_date(293274701009);
} catch (ValueError $ex) {
echo "{$ex->getMessage()}\n";
}
?>
--EXPECT--
easter_date(): Argument #1 ($year) must be a year before 2.000.000.000 (inclusive)