Bug #81263 [Asn->Csd]: Wrong result from DateTimeImmutable::diff
| From: | git@php.net | Date: | Thu, 21 Jul 2022 11:32:51 +0000 |
| Subject: | Bug #81263 [Asn->Csd]: Wrong result from DateTimeImmutable::diff | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-241967@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=81263&edit=1
ID: 81263
Updated by: git@php.net
Reported by: mail at gh01 dot de
Summary: Wrong result from DateTimeImmutable::diff
-Status: Assigned
+Status: Closed
Type: Bug
Package: Date/time related
PHP Version: master-Git-2021-07-15 (Git)
Assigned To: derick
Block user comment: N
Private report: N
New Comment:
Automatic comment on behalf of derickr
Revision: https://github.com/php/php-src/commit/37d460b64a07888d45a72440f63880421f80d61e
Log: Fixed #81263: Wrong result from DateTimeImmutable::diff
Previous Comments:
------------------------------------------------------------------------
[2022-06-18 11:22:17] derick@php.net
Fixed in third party library: https://github.com/derickr/timelib/compare/php-81263?expand=1
â which means it will make it into PHP once this library gets merged again.
------------------------------------------------------------------------
[2022-03-22 21:52:48] matthew at komputerwiz dot net
I pulled the latest source, compiled it, and ran the following example while in GDB:
<?php
$dFmt = \DateTimeInterface::ISO8601;
$iFmt = '%R[%yy %mM %dd %hh %im %ss]';
// creates new DateTime for the current timezone (UTC-5 in my case)
$exp = new \DateTime('+3600 seconds', new \DateTimeZone('-5'));
echo 'expires: '.$exp->format($dFmt)."\n";
$gen = $exp->getTimestamp() - 3600;
// creates new DateTime in UTC
$genTime = \DateTime::createFromFormat('U', (string) $gen);
echo 'created: '.$genTime->format($dFmt)."\n";
$ival = $exp->diff($genTime);
echo 'interval: '.$ival->format($iFmt)."\n";
The expected interval output should be '-[0y 0M 0d 1h 0m 0s]', but is instead '-[-1y
11M 31d 1h 0m 0s]'.
The problem seems to start around timelib.c:90, during the early relative normalization step. Just
prior to this point, the
rt struct contains:
{y:0, m:0, d:0, h:-4, i:0, s:0, ...}
The normalization step eliminates out-of-range negative values by "borrowing" from larger
date units. After this point, rt contains:
{y:-1, m:11, d:30, h:20, i:0, s:0, ...}
When this is corrected for timezone offset (h += 5) and normalized again, it leads to the unexpected
output above.
Perhaps the issue stems from normalizing prior to correcting for time zone offset. If the timezone
offset had been added prior to normalization, the "hours" field would have been in range.
------------------------------------------------------------------------
[2021-10-21 13:23:19] cmb@php.net
This appears to be related to bug #81458.
------------------------------------------------------------------------
[2021-10-21 13:16:03] sjon@php.net
while the related https://3v4l.org/8g4NB seems fixed, this
issue remains
------------------------------------------------------------------------
[2021-10-07 22:36:34] mail at gh01 dot de
This issue still exists in current master: https://3v4l.org/f1fai/rfc#vgitmaster
------------------------------------------------------------------------
The remainder of the comments for this report are too long. To view
the rest of the comments, please view the bug report online at
https://bugs.php.net/bug.php?id=81263
--
Edit this bug report at https://bugs.php.net/bug.php?id=81263&edit=1