Fixed GH-14383: Fixed usec was carry up (#14463)

If round to the fractional part of a timestamp, a carry will occur in cases
such as 999 999 9. In that case, set usec to 0 and add/sub 1 to sec.
This commit is contained in:
Saki Takamachi 2024-06-11 19:02:43 +09:00 committed by GitHub
parent 5433f02e79
commit bfc988552f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 66 additions and 0 deletions

View File

@ -2537,6 +2537,11 @@ PHPAPI bool php_date_initialize_from_ts_double(php_date_obj *dateobj, double ts)
sec = (zend_long)sec_dval;
usec = (int) round(fmod(ts, 1) * 1000000);
if (UNEXPECTED(abs(usec) == 1000000)) {
sec += usec > 0 ? 1 : -1;
usec = 0;
}
if (UNEXPECTED(usec < 0)) {
if (UNEXPECTED(sec == TIMELIB_LONG_MIN)) {
zend_argument_error(

View File

@ -0,0 +1,61 @@
--TEST--
Bug GH-14383 (DateTime::createFromTimestamp overflowed microseconds value)
--INI--
date.timezone=UTC
--FILE--
<?php
$cases = [
[0.999_999_0, '0.999_999_0'],
[0.999_999_1, '0.999_999_1'],
[0.999_999_8, '0.999_999_8'],
[0.999_999_9, '0.999_999_9'],
[1.000_000_0, '1.000_000_0'],
[1.000_000_1, '1.000_000_1'],
[1.000_000_8, '1.000_000_8'],
[1.000_000_9, '1.000_000_9'],
[1.000_001_0, '1.000_001_0'],
[1.000_001_1, '1.000_001_1'],
[1.000_001_8, '1.000_001_8'],
[1.000_001_9, '1.000_001_9'],
];
echo "plus:\n";
foreach ($cases as [$usec, $label]) {
echo "{$label}: ";
echo DateTime::createFromTimestamp($usec)->format('s.u'), "\n";
}
echo "\nminus:\n";
foreach ($cases as [$usec, $label]) {
echo "-{$label}: ";
echo DateTime::createFromTimestamp(-$usec)->format('s.u'), "\n";
}
?>
--EXPECT--
plus:
0.999_999_0: 00.999999
0.999_999_1: 00.999999
0.999_999_8: 01.000000
0.999_999_9: 01.000000
1.000_000_0: 01.000000
1.000_000_1: 01.000000
1.000_000_8: 01.000001
1.000_000_9: 01.000001
1.000_001_0: 01.000001
1.000_001_1: 01.000001
1.000_001_8: 01.000002
1.000_001_9: 01.000002
minus:
-0.999_999_0: 59.000001
-0.999_999_1: 59.000001
-0.999_999_8: 59.000000
-0.999_999_9: 59.000000
-1.000_000_0: 59.000000
-1.000_000_1: 59.000000
-1.000_000_8: 58.999999
-1.000_000_9: 58.999999
-1.000_001_0: 58.999999
-1.000_001_1: 58.999999
-1.000_001_8: 58.999998
-1.000_001_9: 58.999998