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
♻️ Cleanup diffIn* diffInReal* floatDiffIn* floatDiffInReal* #2119
Comments
diffIn*
/ diffInReal*
/ floatDiffIn*
/ floatDiffInReal*
…dff-methods-set #2119 Dedupe the dff methods set
…e-diff-flag Fix #2119 absolute diff flag
Currently migrating to Carbon v3. Thank you for such a great explanation. Very easy to understand what and why was changed and what I need to change in my codebase :) |
At least make the default abs behavior globally modifiable |
Changing behavior globally is a bad habit we want to move away from. Here is why, let's say you include in your project another library that is also using Here is a RegExp you can use to easily find all the calls to those functions so you can add
It might also be a good occasion to review those calls and wonder "what if date in parameter is before the date on which the method is called?" and re-assert your code is correctly handling the case. For instance if you have |
Can the breaking change somehow be flagged more noticable in https://github.com/briannesbitt/Carbon/releases/tag/3.0.0? The thing is that in versions < 3.0 the <?php declare(strict_types=1);
require __DIR__.'/vendor/autoload.php';
use Carbon\Carbon;
use Composer\InstalledVersions;
var_dump(
InstalledVersions::getVersion('nesbot/carbon'),
Carbon::now()->addDays(1)->diffInSeconds(),
);
|
Your case (caching) is precisely a case where sign matters, with default behavior in Carbon 2, both date in past and future would cause cache storing, now if you use the correct order (or reverse order but put minus in front), then expiration in the past will no longer trigger caching (which is obviously bugged). But emphasis the change is a valid request, I promoted it to first change in the list with a warning. |
I understand why, I just don't agree with it. Thankfully Carbon 2 will work for 99.99% use cases until the end of mankind. Cheers |
Jusst downgrade to 2.0 or overload the method in your own class. Easiest way |
📅 Historically, Carbon had
diffIn*
methods returning integer values relying on->diff()
PHP native which is sensitive to DST.Then it appears that a floating result was more convenient. And despite the integer result can easily be get from the float result, the existing methods (for each unit) has been kept for backward compatibility and an other set has been implemented for "float" result.
Then 2 other sets have been added to work around the native PHP DST bugs and use the timestamp instead. So the "Real" methods appeared. Using GMT for calculation (which is what we promote), it makes no difference. Old methods have just been kept for backward compatibility for those who run calculations with a timezone setting having DST.
🚀 For the version 3.0 (branch: 3.x) we need to get rid of the old methods. We will just provide 1 set of
diffIn*
method (only 1 for each unit) which will return afloat
using timestamps (ignoring DST).Users wanting the old DST-behavior would have to code their own as we discourage this approach.
Users wanting integer result will have to explicitly use
(int)
,floor()
,ceil()
orround()
which is actually a good thing. We actually use(int)
which truncate the result (equivalent tofloor()
for positive numbers, but toceil()
for negative numbers) and this is actually arbitrary. I think it will give a better control to users to always give a result as precise as possible and let him decide how to reduce precision on need.If someone want to take this task, pull-requests on 3.x branch are welcome. :D
The text was updated successfully, but these errors were encountered: