php-src icon indicating copy to clipboard operation
php-src copied to clipboard

Datetime created from negative float timestamp is not monotonic (fraction part is added instead of substracted)

Open mvorisek opened this issue 4 years ago • 4 comments

Description

The following code:

foreach ([0.4, 0, -0.4, -1, -1.4] as $ts) {
    $date = new DateTime('@' . $ts);
    print_r($date);
}

https://3v4l.org/EWpSj

Diff of current output vs expected:

 DateTime Object
 (
     [date] => 1970-01-01 00:00:00.400000
     [timezone_type] => 1
     [timezone] => +00:00
 )
 DateTime Object
 (
     [date] => 1970-01-01 00:00:00.000000
     [timezone_type] => 1
     [timezone] => +00:00
 )
 DateTime Object
 (
-    [date] => 1970-01-01 00:00:00.400000
+    [date] => 1969-12-31 23:59:59.600000
     [timezone_type] => 1
     [timezone] => +00:00
 )
 DateTime Object
 (
     [date] => 1969-12-31 23:59:59.000000
     [timezone_type] => 1
     [timezone] => +00:00
 )
 DateTime Object
 (
-    [date] => 1969-12-31 23:59:59.400000
+    [date] => 1969-12-31 23:59:58.600000
     [timezone_type] => 1
     [timezone] => +00:00
 )

PHP Version

present since PHP 8.0, should be fixed starting this version

it seems the issue is present only when TS is parsed, getTimestamp / format('u') seems correct across all PHP versions - https://3v4l.org/CjJLf

mvorisek avatar Dec 11 '21 13:12 mvorisek

For the explanation: fractional timestamps are currently not supported when parsing, which means PHP handles inputs like @-1.4 as an absolute "@-1" (1970 minus 1s) followed by a relative ".4" (plus 0.4s).

damianwadley avatar Dec 11 '21 22:12 damianwadley

For the explanation: fractional timestamps are currently not supported when parsing, which means PHP handles inputs like @-1.4 as an absolute "@-1" (1970 minus 1s) followed by a relative ".4" (plus 0.4s).

this is how it is parsed currently probably, however if unix timestamp is negative, the fractional part must be decremented as if the whole TS was a standard floating point number

https://3v4l.org/rKOAb shows the fractional part cannot be even used alone (eg. parsed with @ts token and the parsing must be fixed)

mvorisek avatar Dec 13 '21 14:12 mvorisek

I think it's fair to say that this a feature request as the docs do state that only whole numbers are supported.

iluuu1994 avatar Dec 13 '21 14:12 iluuu1994

Confusing is a documentation problem, Unix time is a number sequence * for negative values ​​the sign exists otherwise for positive values ​​it is omitted. the DateTime class supports the theoretical limit from 2 ^ 64-1 to -2 ^ 64 regardless of your operating system support, old php versions had a block (internal functions limited to 32bit) even on a 64bit system. From php 7.4.0 the support for milliseconds has been extended through the v format, while from php 8.0.0 through the unix time format preceded by the character @ seconds with decimal point and microseconds. Every 1000 microseconds is a millisecond and since the constructor does not accept arbitrary fractions it is with the limit of 6 digits from php 5.0.0 in strtotime but visible with DateTime::format('u') (created by php 5.2.2) since strtotime returns a signed integer, otherwise called Unix Time. Since there cannot exist format ('U') and format('u') with different results (ex .1969-12-31 23:59:59.100000 format('U') -1 and format('u') 100000) , ie the unix time operator '@ -1 .100000' the desired behavior is to subtract 1 second and set 100000 otherwise it creates confusion. Also a float -0.0 or -0.1 converted to integer is 0 seconds while 000000 microseconds or 100000. Without considering that the arithmetic in float loses precision, well explain that it is recommended to insert a string in the constructor rather than the value of type int or float, since Via user code often a simple conversion of 6 digits after the decimal point can be converted into integer with the exception if preceded by 0 and carry out the relative subtractions or additions. Ie there is no subtracting microseconds with '@ -1.100000' is a positive value by definition with the exception of seconds. Other languages ​​adopt Unix time as a float but this is not the case with php as it is integer. Please correct me if I'm wrong, thanks.

<?php

error_reporting(PHP_INT_MAX);
//PHP >= 7.1.0
ini_set('precision', -1);
ini_set('serialize_precision', -1);
$string = microtime(); //"msec sec"
//msec 10.0E-8 if last two digit is significant
$typefloat = microtime(TRUE);
$positive_int = PHP_INT_MAX;
$negative_int = -1 + -$positive_int;
$mymsec = '000010';
$mymsec = '';
$check = NULL;
if(is_string($mymsec)) {
$arr = preg_split('//', $mymsec); // support for empty string ''
if(is_array($arr)) {
$count = count($arr);
$stop1 = 0;
$stop2 = $count - 1;
$stop3 = $count - 2;
$y = 0;
if($count) {
foreach($arr as $key => $value) {
if($key != $stop1) {
$ordv = ord($value);
if($ordv < 58 && $ordv > 47) {
$y = $y + 1;
if(!isset($val)) {
if($ordv > 48) {
$arr2[] = chr($ordv);
$val = $key;
}
} else {
$arr2[] = chr($ordv);
}
if($stop3 == $y && empty($error)) {
$check = TRUE;
}
} else {
$error = 1;
}
} else {
if(0 == $key) {
$stop1 = $stop2;
} else {
$stop1 = $stop3;
}
}
if($key == $stop3) {
break;
}
}
}
}
}
var_dump($string, $typefloat, (float) ($negative_int . '.100500'));
if($check) {
if(empty($arr2)) {
$arr2 = array('0', '0', '0', '0', '0', '0');
$val = FALSE;
}
var_dump(implode('', $arr2), 'first not zero key ' . $val);
}
elseif(!isset($error) && isset($count)) {
var_dump('000000', 'first zero'); //$mymsec is empty string ''
}

(float) ($negative_int . '.100500') as greater than 17 digits the float loses precision. length 6 for usec of DateTime.

Note* I call it number sequence because with class DateTime and format('U) it can return a float in an OS limited to 32bit, but Unix Time is an optional signed integer for DateTime::__construct('now', $timezone) or string into float/int for 32bit DateTime::format('U'). On the latter issue, I guessed please @derickr your input is appreciated, thank you. 32bit PHP_INT_MAX 2147483647 support DateTime 64bit, example '@100000000000' https://bugs.php.net/bug.php?id=52062

hormus avatar Apr 24 '22 20:04 hormus