DateTime->sub produces different value than on older versions
Description
The following code:
https://3v4l.org/1kqoo
<?php
$today = new DateTime('today');
$delta = new DateInterval('PT0S');
$delta->f = 0.0001;
$expectedLower = (clone $today)
->setTimezone(new DateTimeZone('UTC'))
->sub($delta);
var_dump($expectedLower);
Resulted in this output:
object(DateTime)#3 (3) {
["date"]=>
string(26) "2022-02-24 23:00:00.999900"
["timezone_type"]=>
int(3)
["timezone"]=>
string(3) "UTC"
}
But I expected this output instead:
object(DateTime)#3 (3) {
["date"]=>
string(26) "2022-02-24 22:59:59.999900"
["timezone_type"]=>
int(3)
["timezone"]=>
string(3) "UTC"
}
PHP Version
PHP 8.1.3
Operating System
Docker image php:8.1.3-cli-buster
This is actually a timelib issue. Relevant code excerpt https://github.com/php/php-src/blob/33cd61c9045ae8675448456cdda6fb244b2efa01/ext/date/lib/interval.c#L326-L334
This can't work, since the changes after the first timelib_update_from_sse() are reset due to the second call to that function. I'm not sure whether that second call can be removed altogether, or whether it needs to be moved to the then-clause of the if-statement. @derickr certainly knows. :)
Encountered this problem trying to update a project for the php8.1 and all unit tests with delta less than 1 are failing now. Should I rewrite all unit tests, all this issue will be fixed soon?
I've just submitted https://github.com/derickr/timelib/pull/125.
@cmb69 If through diff I create DateInterval or from PHP >= 7.1.7 clone DateInterval DateTime::sub works as expected. Maybe from php 8.1 DateInterval gets the reference from some cache or oblivious even if it is setter $delta->f = float_value a fterwards. Only Microsecond.
https://3v4l.org/kREZN
<?php
//PHP >= 7.1.7 clone DateInterval https://bugs.php.net/bug.php?id=70975
$date1 = new \DateTime('2022-03-30 00:00:00.000100', new \DateTimeZone('Europe/Rome'));
$delta = new \DateInterval('PT0S');
$clone = (clone $delta);
$clone->f = 0.0001;
$date2 = new \DateTime('2022-03-30 00:00:00.000000', new \DateTimeZone('Europe/Rome'));
//See more complex example for real date with diff
$diff = $date2->diff($date1);
$date2->setTimezone(new \DateTimeZone('UTC'));
$date2->sub($clone);
$date1->setTimezone(new \DateTimeZone('UTC'));
$date1->sub($diff);
$date1->sub($diff);
var_dump($date2, $date1, $clone, $diff);
More complex example
<?php
//PHP >= 7.1.7 clone DateInterval https://bugs.php.net/bug.php?id=70975
$tz = new \DateTimeZone('Europe/Rome');
$tz_utc = new \DateTimeZone('UTC');
$date1 = new \DateTime('2022-03-30 00:00:00.000100', $tz);
$delta = new \DateInterval('PT0S');
$clone = (clone $delta);
$clone->f = 0.0001;
$date2 = new \DateTime('2022-03-30 00:00:00.000000', $tz);
$arr = array(0 => (array) $date1);
$td1 = $arr[0]['date'];
$date3 = new \DateTime($td1, $tz);
$arr = array(0 => $arr[0], 1 => (array) $date2);
$td2 = $arr[1]['date'];
$date4 = new \DateTime($td2, $tz);
//Convert from local to local (change offset: add or sub for transition) UTC
$date2 = new \DateTime('2022-03-30 00:00:00.000000', $tz_utc);
$date1 = new \DateTime('2022-03-30 00:00:00.000100', $tz_utc);
$without_transition = TRUE;
$date2->sub($clone);
$arr = array(0 => $arr[0], 1 => $arr[1], 2 => (array) $date2);
$td3 = $arr[2]['date'];
$date5 = new \DateTime($td3, $tz_utc);
$date2->add($clone);
$diff = $date2->diff($date1);
$date1->sub($diff);
$date1->sub($diff);
$arr = array(0 => $arr[0], 1=> $arr[1], 2=> $arr[2], 3 => (array) $date1);
$td4 = $arr[3]['date'];
$date6 = new \DateTime($td4, $tz_utc);
$date1->add($clone);
$delta->f = 0.0001;
$date1->sub($delta);
$arr = array(0 => $arr[0], 1 => $arr[1], 2 => $arr[2], 3 => $arr[3], 4 => (array) $date1);
$td5 = $arr[4]['date'];
$date7 = new \DateTime($td5, $tz_utc); /*From Local to Local UTC object(DateTime)#8 (3) {
["date"]=>
string(26) "2022-03-29 23:59:59.999900"
["timezone_type"]=>
int(3)
["timezone"]=>
string(3) "UTC"
}*/
var_dump($date1, $date2, $date3, $date4, $date5, $date6, $date7);
From PHP 8.1.0 $delta->f = 0.0001; $date7 don't work $date5, $date6, $date7 create new object with DateInterval.
I guess the fix will also fix this ?
<?php
$today = new DateTimeImmutable('today'); // '2022-04-01 00:00:00.0'
$todayLast = new DateTimeImmutable('today last microsecond'); // actually '2022-03-31 23:59:59.999999' instead of '2022-04-01 23:59:59.999999'
With "last" or "previous" "space" "word two" decrement the entire date to the new value example: "last microsecond" or "last day" with "last day of" you get the last day of this month, there is no "last microsecond of". Current today Europe/Rome 2022-04-02 01:27:00
$tz = new DateTimeZone('Europe/Rome');
$today = new DateTimeImmutable('today', $tz); // '2022-04-02 00:00:00.000000'
$todayLast = $today->modify('+999999 usec');
$todaylast2 = new DateTimeImmutable('today +999999 usec', $tz);
var_dump($today, $todayLast, $todaylast2);
PHP >= 7.1 It's work.
The maintainer @derickr will decide whether to fix, but I am more in favor of setting the microseconds to 000000 and then addition to a positive float of less than 1.000000 Note* I hope that there will be no microsecond transition period
today means start of day set to 00:00:00.000000 (hour, minute, second, microsecond to zero) and the "user last usec" or "user last usec of" today is .000000 only when the previous one is not locked (example: today or midnight) "user last usec of" can be force to .999999 "today user last usec" or "today user last usec of" // only 00:00:00.000000 "user last usec of" or "this day user last usec of"// only this period time and .999999 today since [PHP 4.3.1] 00:00:00 , for DateTime PHP >= 5.2.2 00:00:00.000000 DateTime::format U add since 5.2.2 i think it's not a bug but a design choice i disagree. you can manage with multiple syntax via user code if needed
<?php
$tz = new DateTimeZone('Europe/Rome');
$today = new DateTime('today', $tz);
$this_day_last_usec_of = $today;
$fractions = $today->format('u');
if($fractions) {
$arr = preg_split('//', $fractions);
$count = count($arr);
$check = NULL;
$i = 0;
$result = NULL;
foreach ($arr as $key => $value) {
$i = $i + 1;
if(is_string($value)) {
if('u' != $value) {
if($value) {
$check = TRUE;
$i = $count;
}
} else {
$check = FALSE;
$i = $count;
}
} else {
$i = $count;
}
if($i == $count)
break;
}
if(is_null($check)) {
$test = new DateTime('1970-01-01 00:00:00.000100', new DateTimeZone('UTC'));
$test_fra = $test->format('u');
$result = ((float) 0 . '.' . $test_fra) * 1.000000 - 0.0001;
}
if($check || $result === 0.0) {
$date = $today->format('Y-m-d H:i:s');
$last_fractions = '999999'; //support microsecond
$this_day_last_usec_of = new DateTime($date . '.' . $last_fractions, $tz);
}
}
var_dump($this_day_last_usec_of->format('Y-m-d H:i:s') . ' => ' . $this_day_last_usec_of->format('u'));
Regarding fix for this issue. In which PHP version it is planned to be included?
@egorbwork the bug has existed since php 8.1.0 and later, while previous versions do not have this type of bug. The bug exists via the 3v4l.org site
https://3v4l.org/1kqoo
Resulted in this output:
object(DateTime)#3 (3) {
["date"]=>
string(26) "2022-02-24 23:00:00.999900"
["timezone_type"]=>
int(3)
["timezone"]=>
string(3) "UTC"
}
PHP 8.1.6 and the fix still not included.
The issue is still open, but the fix for this should come in PHP 8.1.7, now that timelib has been merged: https://github.com/php/php-src/commit/3d53e59b22818b07cc974047599186dbaec4bbde#diff-e445525fa9728dabc610a2737328dd0d9bcfaf6e9045f7c87d3913bb7376aa4eL310
@derickr thanks a lot!