Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
mvorisek opened this issue Dec 11, 2021 · 4 comments

Comments

@mvorisek
Copy link
Contributor

mvorisek commented Dec 11, 2021

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

@damianwadley
Copy link
Member

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).

@mvorisek
Copy link
Contributor Author

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)

@iluuu1994 iluuu1994 added Feature and removed Bug labels Dec 13, 2021
@iluuu1994
Copy link
Member

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

@hormus

This comment was marked as off-topic.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

7 participants
@derickr @iluuu1994 @mvorisek @cmb69 @damianwadley @hormus and others