Bug #79420 [NEW]: Time off by a microsecond
From: danack
Operating system: MacOS High Sierra 10.13.6
PHP version: 7.4.4
Package: Date/time related
Bug Type: Bug
Bug description:Time off by a microsecond
Description:
------------
Somehow, creating two DateTime objects from the same date input string,
and then setting all of the time values to be 0 manages to create one of
them with the wrong microseconds.
I have a testsuite that includes a test for creating a datetime object
from user input and comparing them against a DateTime object created
from set parameters. This test has worked for months, but just now
failed twice in a row, and the DateTimes were different.
-2002-10-02T00:00:00.000001+0000
+2002-10-02T00:00:00.000000+0000
The code below is _not_ a reproduce case, but shows what PHPUnit was
doing to compare the DateTimes.
Not expecting this to be fixed as high chance it was something specific
to my system, more just creating the report for posterity
Test script:
---------------
<?php
declare(strict_types = 1);
$max = 1000000;
$formatString = \DateTimeInterface::RFC3339_EXTENDED;
$absDelta = 0.0;
for ($i = 0; $i < $max; $i += 1) {
$actual = \DateTimeImmutable::createFromFormat('Y-m-d',
'2002-10-02');
if ($actual instanceof \DateTimeImmutable) {
$actual = $actual->setTime(0, 0, 0, 0);
}
usleep(rand(0, 1000));
$expected = \DateTime::createFromFormat('Y-m-d',
'2002-10-02')->setTime(0, 0, 0, 0);
$delta = new \DateInterval(\sprintf('PT%dS', $absDelta));
$actualClone = (clone $actual)
->setTimezone(new \DateTimeZone('UTC'));
usleep(rand(0, 1000));
$expectedLower = (clone $expected)
->setTimezone(new \DateTimeZone('UTC'))
->sub($delta);
usleep(rand(0, 1000));
$expectedUpper = (clone $expected)
->setTimezone(new \DateTimeZone('UTC'))
->add($delta);
if ($actualClone < $expectedLower || $actualClone > $expectedUpper)
{
$string1 = $expected->format($formatString);
$string2 = $actual->format($formatString);
echo "Odd\n";
echo $string1 . "\n";
echo $string2 . "\n";
exit(0);
}
if ($i % ($max / 100) === 0) {
echo ".";
}
if ($i % ($max / 10) === 0) {
echo "\n";
}
}
echo "fin";
--
Edit bug report at https://bugs.php.net/bug.php?id=79420&edit=1
--
Fix committed: https://bugs.php.net/fix.php?id=79420&r=fixed
Fixed in release: https://bugs.php.net/fix.php?id=79420&r=alreadyfixed
Need backtrace: https://bugs.php.net/fix.php?id=79420&r=needtrace
Need Reproduce Script: https://bugs.php.net/fix.php?id=79420&r=needscript
Try newer version: https://bugs.php.net/fix.php?id=79420&r=oldversion
Not developer issue: https://bugs.php.net/fix.php?id=79420&r=support
Expected behavior: https://bugs.php.net/fix.php?id=79420&r=notwrong
Not enough info: https://bugs.php.net/fix.php?id=79420&r=notenoughinfo
Submitted twice: https://bugs.php.net/fix.php?id=79420&r=submittedtwice
register_globals: https://bugs.php.net/fix.php?id=79420&r=globals
PHP version support discontinued: https://bugs.php.net/fix.php?id=79420&r=phptooold
Daylight Savings: https://bugs.php.net/fix.php?id=79420&r=dst
IIS Stability: https://bugs.php.net/fix.php?id=79420&r=isapi
Install GNU Sed: https://bugs.php.net/fix.php?id=79420&r=gnused
Floating point limitations: https://bugs.php.net/fix.php?id=79420&r=float
No Zend Extensions: https://bugs.php.net/fix.php?id=79420&r=nozend
MySQL Configuration Error: https://bugs.php.net/fix.php?id=79420&r=mysqlcfg
Thread (2 messages)
- danack@php.net