Skip to content

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

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 2 commits into from
Jun 11, 2024
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
5 changes: 5 additions & 0 deletions ext/date/php_date.c
Original file line number Diff line number Diff line change
Expand Up @@ -2540,6 +2540,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(
Expand Down
61 changes: 61 additions & 0 deletions ext/date/tests/gh14383.phpt
Original file line number Diff line number Diff line change
@@ -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
Loading