Skip to content

Support range of INT64_MIN/MAX on 32bit for DateTime[Immutable]::createFromTimestamp #18857

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
31 changes: 18 additions & 13 deletions ext/date/php_date.c
Original file line number Diff line number Diff line change
Expand Up @@ -2481,35 +2481,40 @@ PHPAPI bool php_date_initialize(php_date_obj *dateobj, const char *time_str, siz
return 1;
} /* }}} */

PHPAPI void php_date_initialize_from_ts_long(php_date_obj *dateobj, zend_long sec, int usec) /* {{{ */
PHPAPI void php_date_initialize_from_ts_sll(php_date_obj *dateobj, timelib_sll sec, int usec) /* {{{ */
{
dateobj->time = timelib_time_ctor();
dateobj->time->zone_type = TIMELIB_ZONETYPE_OFFSET;

timelib_unixtime2gmt(dateobj->time, (timelib_sll)sec);
timelib_unixtime2gmt(dateobj->time, sec);
timelib_update_ts(dateobj->time, NULL);
php_date_set_time_fraction(dateobj->time, usec);
} /* }}} */

PHPAPI void php_date_initialize_from_ts_long(php_date_obj *dateobj, zend_long sec, int usec) /* {{{ */
{
php_date_initialize_from_ts_sll(dateobj, (timelib_sll)sec, usec);
} /* }}} */

PHPAPI bool php_date_initialize_from_ts_double(php_date_obj *dateobj, double ts) /* {{{ */
{
double sec_dval = trunc(ts);
zend_long sec;
timelib_sll sec;
int usec;

if (UNEXPECTED(isnan(sec_dval) || !PHP_DATE_DOUBLE_FITS_LONG(sec_dval))) {
if (UNEXPECTED(isnan(sec_dval) || !PHP_DATE_DOUBLE_FITS_SLL(sec_dval))) {
zend_argument_error(
date_ce_date_range_error,
1,
"must be a finite number between " TIMELIB_LONG_FMT " and " TIMELIB_LONG_FMT ".999999, %g given",
TIMELIB_LONG_MIN,
TIMELIB_LONG_MAX,
"must be a finite number between " PHP_DATE_SLL_FMT " and " PHP_DATE_SLL_FMT ".999999, %g given",
INT64_MIN,
INT64_MAX,
ts
);
return false;
}

sec = (zend_long)sec_dval;
sec = (timelib_sll)sec_dval;
usec = (int) round(fmod(ts, 1) * 1000000);

if (UNEXPECTED(abs(usec) == 1000000)) {
Expand All @@ -2518,13 +2523,13 @@ PHPAPI bool php_date_initialize_from_ts_double(php_date_obj *dateobj, double ts)
}

if (UNEXPECTED(usec < 0)) {
if (UNEXPECTED(sec == TIMELIB_LONG_MIN)) {
if (UNEXPECTED(sec == INT64_MAX)) {
zend_argument_error(
date_ce_date_range_error,
1,
"must be a finite number between " TIMELIB_LONG_FMT " and " TIMELIB_LONG_FMT ".999999, %g given",
TIMELIB_LONG_MIN,
TIMELIB_LONG_MAX,
"must be a finite number between " PHP_DATE_SLL_FMT " and " PHP_DATE_SLL_FMT ".999999, %g given",
INT64_MIN,
INT64_MAX,
ts
);
return false;
Expand All @@ -2534,7 +2539,7 @@ PHPAPI bool php_date_initialize_from_ts_double(php_date_obj *dateobj, double ts)
usec = 1000000 + usec;
}

php_date_initialize_from_ts_long(dateobj, sec, usec);
php_date_initialize_from_ts_sll(dateobj, sec, usec);

return true;
} /* }}} */
Expand Down
7 changes: 7 additions & 0 deletions ext/date/php_date.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@
# define PHP_DATE_DOUBLE_FITS_LONG(d) (!((d) >= (double)TIMELIB_LONG_MAX || (d) < (double)TIMELIB_LONG_MIN))
#endif

/* Same as PHP_DATE_SIZEOF_LONG but for timelib_sll (int64_t) */
#define PHP_DATE_DOUBLE_FITS_SLL(d) (!((d) >= (double)INT64_MAX || (d) < (double)INT64_MIN))

/* Same as TIMELIB_LONG_FMT but for timelib_sll (int64_t) */
#define PHP_DATE_SLL_FMT "%" PRId64

#include "php_version.h"
#define PHP_DATE_VERSION PHP_VERSION

Expand Down Expand Up @@ -160,6 +166,7 @@ PHPAPI zend_class_entry *php_date_get_period_ce(void);

PHPAPI zval *php_date_instantiate(zend_class_entry *pce, zval *object);
PHPAPI bool php_date_initialize(php_date_obj *dateobj, const char *time_str, size_t time_str_len, const char *format, zval *timezone_object, int flags);
PHPAPI void php_date_initialize_from_ts_sll(php_date_obj *dateobj, timelib_sll sec, int usec);
PHPAPI void php_date_initialize_from_ts_long(php_date_obj *dateobj, zend_long sec, int usec);
PHPAPI bool php_date_initialize_from_ts_double(php_date_obj *dateobj, double ts);

Expand Down
38 changes: 36 additions & 2 deletions ext/date/tests/createFromTimestamp.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,15 @@ $timestamps = array(
0,
0.0,
-0.0,
PHP_INT_MAX + 1024.0,
PHP_INT_MIN - 1025.0,
9223372036854775808.0, // INT64_MAX + 1
-9223372036854777856.0, // INT64_MIN - 2048
NAN,
+INF,
-INF,
1599828571.235628,
-1599828571.235628,
2147483648.0, // INT32_MAX + 1
-2147483649.0, // INT32_MIN - 1
);

foreach ($timestamps as $ts) {
Expand Down Expand Up @@ -245,6 +247,38 @@ DateTimeImmutable::createFromTimestamp(-1599828571.235628): object(DateTimeImmut
["timezone"]=>
string(6) "+00:00"
}
DateTime::createFromTimestamp(2147483648.0): object(DateTime)#%d (3) {
["date"]=>
string(26) "2038-01-19 03:14:08.000000"
["timezone_type"]=>
int(1)
["timezone"]=>
string(6) "+00:00"
}
DateTimeImmutable::createFromTimestamp(2147483648.0): object(DateTimeImmutable)#%d (3) {
["date"]=>
string(26) "2038-01-19 03:14:08.000000"
["timezone_type"]=>
int(1)
["timezone"]=>
string(6) "+00:00"
}
DateTime::createFromTimestamp(-2147483649.0): object(DateTime)#%d (3) {
["date"]=>
string(26) "1901-12-13 20:45:51.000000"
["timezone_type"]=>
int(1)
["timezone"]=>
string(6) "+00:00"
}
DateTimeImmutable::createFromTimestamp(-2147483649.0): object(DateTimeImmutable)#%d (3) {
["date"]=>
string(26) "1901-12-13 20:45:51.000000"
["timezone_type"]=>
int(1)
["timezone"]=>
string(6) "+00:00"
}
MyDateTime::createFromTimestamp(0): object(MyDateTime)#%d (3) {
["date"]=>
string(26) "1970-01-01 00:00:00.000000"
Expand Down
126 changes: 0 additions & 126 deletions ext/date/tests/createFromTimestamp_32bit.phpt

This file was deleted.