Skip to content
Closed
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
22 changes: 7 additions & 15 deletions ext/date/php_date.c
Original file line number Diff line number Diff line change
Expand Up @@ -2521,10 +2521,15 @@ PHPAPI void php_date_initialize_from_ts_long(php_date_obj *dateobj, zend_long se

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

if (fabs(ts_next - ts) <= 0.5 / 1000000) {
ts = ts_next;
}
sec_dval = floor(ts);
if (UNEXPECTED(isnan(sec_dval) || !PHP_DATE_DOUBLE_FITS_LONG(sec_dval))) {
zend_argument_error(
date_ce_date_range_error,
Expand All @@ -2538,22 +2543,9 @@ PHPAPI bool php_date_initialize_from_ts_double(php_date_obj *dateobj, double ts)
}

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

if (UNEXPECTED(usec < 0)) {
if (UNEXPECTED(sec == TIMELIB_LONG_MIN)) {
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,
ts
);
return false;
}

sec = sec - 1;
usec = 1000000 + usec;
}

Expand Down
85 changes: 85 additions & 0 deletions ext/date/tests/createFromTimestamp.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ $timestamps = array(
-1696883232.013981,
0.123456,
-0.123456,
1.000_001,
-1.000_001,
1.000_000_1,
-1.000_000_1,
1599828571.235_612,
0,
0.0,
-0.0,
Expand Down Expand Up @@ -153,6 +158,86 @@ DateTimeImmutable::createFromTimestamp(-0.123456): object(DateTimeImmutable)#%d
["timezone"]=>
string(6) "+00:00"
}
DateTime::createFromTimestamp(1.000001): object(DateTime)#1 (3) {
["date"]=>
string(26) "1970-01-01 00:00:01.000001"
["timezone_type"]=>
int(1)
["timezone"]=>
string(6) "+00:00"
}
DateTimeImmutable::createFromTimestamp(1.000001): object(DateTimeImmutable)#1 (3) {
["date"]=>
string(26) "1970-01-01 00:00:01.000001"
["timezone_type"]=>
int(1)
["timezone"]=>
string(6) "+00:00"
}
DateTime::createFromTimestamp(-1.000001): object(DateTime)#1 (3) {
["date"]=>
string(26) "1969-12-31 23:59:58.999999"
["timezone_type"]=>
int(1)
["timezone"]=>
string(6) "+00:00"
}
DateTimeImmutable::createFromTimestamp(-1.000001): object(DateTimeImmutable)#1 (3) {
["date"]=>
string(26) "1969-12-31 23:59:58.999999"
["timezone_type"]=>
int(1)
["timezone"]=>
string(6) "+00:00"
}
DateTime::createFromTimestamp(1.0000001): object(DateTime)#1 (3) {
["date"]=>
string(26) "1970-01-01 00:00:01.000000"
["timezone_type"]=>
int(1)
["timezone"]=>
string(6) "+00:00"
}
DateTimeImmutable::createFromTimestamp(1.0000001): object(DateTimeImmutable)#1 (3) {
["date"]=>
string(26) "1970-01-01 00:00:01.000000"
["timezone_type"]=>
int(1)
["timezone"]=>
string(6) "+00:00"
}
DateTime::createFromTimestamp(-1.0000001): object(DateTime)#1 (3) {
["date"]=>
string(26) "1969-12-31 23:59:58.999999"
["timezone_type"]=>
int(1)
["timezone"]=>
string(6) "+00:00"
}
DateTimeImmutable::createFromTimestamp(-1.0000001): object(DateTimeImmutable)#1 (3) {
["date"]=>
string(26) "1969-12-31 23:59:58.999999"
["timezone_type"]=>
int(1)
["timezone"]=>
string(6) "+00:00"
}
DateTime::createFromTimestamp(1599828571.235612): object(DateTime)#1 (3) {
["date"]=>
string(26) "2020-09-11 12:49:31.235612"
["timezone_type"]=>
int(1)
["timezone"]=>
string(6) "+00:00"
}
DateTimeImmutable::createFromTimestamp(1599828571.235612): object(DateTimeImmutable)#1 (3) {
["date"]=>
string(26) "2020-09-11 12:49:31.235612"
["timezone_type"]=>
int(1)
["timezone"]=>
string(6) "+00:00"
}
DateTime::createFromTimestamp(0): object(DateTime)#%d (3) {
["date"]=>
string(26) "1970-01-01 00:00:00.000000"
Expand Down