php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #63435 Datetime::format('u') sometimes wrong by 1 microsecond
Submitted: 2012-11-04 15:50 UTC Modified: 2012-12-01 09:20 UTC
From: miau dot jp at gmail dot com Assigned: remi (profile)
Status: Closed Package: Date/time related
PHP Version: 5.3.18 OS: Any
Private report: No CVE-ID: None
 [2012-11-04 15:50 UTC] miau dot jp at gmail dot com
Description:
------------
When storing '015700' microseconds in a Datetime object,
Datetime::format('u') returns '015699'. This behavior is critical when
I use optimistic locking.

----
$datetime = Datetime::createFromFormat('u', '015700');
$datetime->format('u'); // => '015699'
----

Datetime objects hold microseconds as "double" type in C.
'015700' is held as 0.015699999999999998679. Datetime::format('u')
calls floor() and it returns .015699.
(see https://github.com/php/php-src/blob/PHP-5.3.18/ext/date/php_date.c#L1098 )


Test script:
---------------
$datetime = Datetime::createFromFormat('u', '015700');
$datetime->format('u');


Expected result:
----------------
015700

Actual result:
--------------
015699

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2012-12-01 09:20 UTC] remi@php.net
-Status: Open +Status: Assigned -Assigned To: +Assigned To: remi
 [2012-12-01 09:26 UTC] remi@php.net
Automatic comment on behalf of remi
Revision: http://git.php.net/?p=php-src.git;a=commit;h=c2554b4bb43700ae16db34d9706db1db85ec78a4
Log: Fixed Bug #63435 Datetime::format('u') sometimes wrong by 1 microsecond
 [2012-12-01 09:26 UTC] remi@php.net
-Status: Assigned +Status: Closed
 [2012-12-19 17:55 UTC] derick@php.net
Automatic comment on behalf of remi
Revision: http://git.php.net/?p=php-src.git;a=commit;h=c2554b4bb43700ae16db34d9706db1db85ec78a4
Log: Fixed Bug #63435 Datetime::format('u') sometimes wrong by 1 microsecond
 [2014-10-07 23:21 UTC] stas@php.net
Automatic comment on behalf of remi
Revision: http://git.php.net/?p=php-src-security.git;a=commit;h=c2554b4bb43700ae16db34d9706db1db85ec78a4
Log: Fixed Bug #63435 Datetime::format('u') sometimes wrong by 1 microsecond
 [2014-10-07 23:31 UTC] stas@php.net
Automatic comment on behalf of remi
Revision: http://git.php.net/?p=php-src-security.git;a=commit;h=c2554b4bb43700ae16db34d9706db1db85ec78a4
Log: Fixed Bug #63435 Datetime::format('u') sometimes wrong by 1 microsecond
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Wed Nov 13 03:01:33 2024 UTC