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

[v1.35.0] diffInSeconds() incorrect by 86399 seconds if one date has microseconds #1515

Closed
uphlewis opened this issue Nov 14, 2018 · 12 comments
Assignees
Milestone

Comments

@uphlewis
Copy link

Hello,

I encountered an issue with the following code:

$requestTime = new \Carbon\Carbon('2018-11-14 18:23:12.0 +00:00');
$serverTime = new \Carbon\Carbon('2018-11-14 18:23:12.307628 +00:00');

return  $serverTime->diffInSeconds($requestTime); //should be int 0 or int 1, but actually returns int 86399

Carbon version: 1.35.0
PHP version:7.2.12-1+ubuntu18.04.1+deb.sury.org+1

I expected to get:

0

But I actually get:

86399

Obviously this is very close to being 1 day worth of seconds, so something strange appears to be going on somewhere...

Thanks!

@uphlewis
Copy link
Author

Further to the above if you do the diff in the other direction, it works as expected...

echo $requestTime->diffInSeconds($serverTime, true) // 0 <-- correct
echo $serverTime->diffInSeconds($requestTime, true); // 86399 <-- incorrect

@uphlewis
Copy link
Author

To give some more context, the $requestTime is created out of an RFC-7231 formatted timestamp which doesnt have microseconds. The $serverTime is created with a simple Carbon::now(new \DateTimeZone('GMT');) so obviously does have microseconds.

This bug only happens when the time being diffed doesn't have microseconds, but the time doing the diffing does have microseconds.

@uphlewis
Copy link
Author

The above is on a Homestead Vagrant VirtualBox VM.

Running the same code on my local machine with PHP 7.2.11-4+ubuntu18.04.1+deb.sury.org+1, I can't reproduce the bug.

So it could be specific to PHP 7.2.12-1

@pulkitjalan
Copy link

Having the same issue, specifically on 7.2.12, not happening on 7.2.10

@stueynet
Copy link

Also not happening on 7.2.11.

@kylekatarnls
Copy link
Collaborator

Yes, PHP has a known bug appearing in certains specific versions (PHP create an interval with year = -1, month = 11, days = 29). I thought we found a way, but seems not in all cases. A work-around while we figure it out is to trim microseconds:

$requestTime->startOfSecond()->diffInSeconds($serverTime->startOfSecond(), true)

@uphlewis
Copy link
Author

http://php.net/ChangeLog-7.php#7.2.12

Date:

  • Upgraded timelib to 2017.08.
  • Fixed bug #75851 (Year component overflow with date formats "c", "o", "r" and "y").
  • Fixed bug #77007 (fractions in diff() are not correctly normalized).

@kylekatarnls
Copy link
Collaborator

kylekatarnls commented Nov 14, 2018

Or simplest work-around (often more relevant too), use diffInRealSeconds that uses internally timestamps rather than date intervals. (So no call to diff() which has changed on PHP 7.2.12)

@kylekatarnls
Copy link
Collaborator

This should fix this case: #1517 waiting for tests to pass in every PHP versions to be sure.

@kylekatarnls
Copy link
Collaborator

Hi guys, please retry with 1.35.1.

Note: diff() results still uses native PHP DateTime with no filtering, so this fix catch the PHP bug introduced in PHP 7.1.12 in diffAsCarbonInterval and diffInXxx methods but raw methods from DateTime class will still produce unexpected outputs for differences less than 1 second.

@kylekatarnls kylekatarnls self-assigned this Nov 14, 2018
@uphlewis
Copy link
Author

Yep 1.35.1 fixes it :) Awesome stuff thanks so much for the rapid turnaround !!

$ artisan tinker
Psy Shell v0.9.9 (PHP 7.2.12-1+ubuntu18.04.1+deb.sury.org+1 — cli) by Justin Hileman
>>> $requestTime = new \Carbon\Carbon('2018-11-14 18:23:12.0 +00:00');
=> Carbon\Carbon @1542219792 {#2980
     date: 2018-11-14 18:23:12.0 +00:00,
   }
>>> $serverTime = new \Carbon\Carbon('2018-11-14 18:23:12.307628 +00:00');
=> Carbon\Carbon @1542219792 {#2981
     date: 2018-11-14 18:23:12.307628 +00:00,
   }
>>> $serverTime->diffInSeconds($requestTime);
=> 0
>>> $requestTime->diffInSeconds($serverTime);
=> 0

@pulkitjalan
Copy link

pulkitjalan commented Nov 15, 2018 via email

@kylekatarnls kylekatarnls added this to the 1.35 milestone Nov 21, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants