Bug #73837 [Asn->Csd]: "new DateTime()" sometimes returns 1 second ago value on PHP 7.1.0

From: Date: Sat, 11 Feb 2017 11:10:35 +0000
Subject: Bug #73837 [Asn->Csd]: "new DateTime()" sometimes returns 1 second ago value on PHP 7.1.0
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-207267@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=73837&edit=1 ID: 73837 Updated by: derick@php.net Reported by: for-bugs at hnw dot jp Summary: "new DateTime()" sometimes returns 1 second ago value on PHP 7.1.0 -Status: Assigned +Status: Closed Type: Bug Package: Date/time related Operating System: macOS PHP Version: 7.1.0 Assigned To: derick Block user comment: N Private report: N New Comment: Automatic comment on behalf of github@derickrethans.nl Revision: http://git.php.net/?p=php-src.git;a=commit;h=5113909259f897c9c69f23cf5f8f7ab8972d5eba Log: Fixed bug #73837 (&quot;new DateTime()&quot; sometimes returns 1 second ago value). Previous Comments: ------------------------------------------------------------------------ [2017-01-10 12:28:27] for-bug at hnw dot jp There's nothing curious. The current code uses different time-source against seconds and microseconds for the current time. They should be unified. In php_date_initialize(), timelib_unixtime2local() is called with time(NULL) as 2nd argument. In this function, unixtime is set to "now". Then php_date_set_current_time_fraction() is executed and the value from gettimeofday() is set as microsecond of "now". Suppose the accurate time is "2016-12-29 18:35:54.999999+00:00" when time(NULL) is called. Then time(NULL) returns 1483036554. 1 microsecond after, gettimeofday() is called and returns {1483036555, 0}. Then the value of "now" gets "148303654.000000" (Of course, it should be "1483036554.999999" or "1483036555.000000"). ------------------------------------------------------------------------ [2017-01-01 12:29:44] derick@php.net That's curious... will investigate. ------------------------------------------------------------------------ [2016-12-29 19:43:04] requinix@php.net Same on Windows. Looks like the problem is in timelib. ------------------------------------------------------------------------ [2016-12-29 19:25:12] for-bugs at hnw dot jp Description: ------------ From PHP 7.1, DateTime constructor fills microseconds. However, setting seconds and setting microseconds are in different function, so sometimes DateTime instance has 1 second ago value from actual time. Test script: --------------- <?php $prev_dt = new DateTime(); while (true) { $dt = new DateTime(); if ($prev_dt > $dt) { var_dump($prev_dt->format("Y-m-d H:i:s.u")); var_dump($dt->format("Y-m-d H:i:s.u")); break; } $prev_dt = $dt; } Expected result: ---------------- infinite loop Actual result: -------------- soon ends and outputs like this: string(26) "2016-12-29 18:35:54.999998" string(26) "2016-12-29 18:35:54.000000" ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=73837&edit=1

« previous php.bugs (#207267) next »