From 7ba40db2e53bb08e0989023aebff2cc0ec12b6d2 Mon Sep 17 00:00:00 2001 From: KyleKatarn Date: Wed, 6 May 2020 23:04:31 +0200 Subject: [PATCH 01/38] #2070 Drop Symfony < 5 and PHP < 7.2.5 support --- composer.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/composer.json b/composer.json index 821e676b5c..4ec80662af 100644 --- a/composer.json +++ b/composer.json @@ -28,10 +28,10 @@ "minimum-stability": "dev", "bin": ["bin/carbon"], "require": { - "php": "^7.1.8 || ^8.0", + "php": "^7.2.5 || ^8.0", "ext-json": "*", "symfony/polyfill-mbstring": "^1.0", - "symfony/translation": "^3.4 || ^4.0 || ^5.0" + "symfony/translation": "^5.0" }, "require-dev": { "doctrine/orm": "^2.7", From 8106250ae581fa89f922dcb653a34e9ed34b2485 Mon Sep 17 00:00:00 2001 From: KyleKatarn Date: Wed, 6 May 2020 23:27:42 +0200 Subject: [PATCH 02/38] #2070 Drop Symfony\Component\Translation\TranslatorInterface --- src/Carbon/CarbonInterface.php | 88 ++-- src/Carbon/Factory.php | 378 +++++++++--------- src/Carbon/FactoryImmutable.php | 378 +++++++++--------- src/Carbon/Traits/Creator.php | 11 +- src/Carbon/Traits/Localization.php | 66 ++- tests/Carbon/LocalizationTest.php | 34 +- tests/CarbonImmutable/LocalizationTest.php | 31 +- .../Translation/TranslatorInterface.php | 21 - 8 files changed, 461 insertions(+), 546 deletions(-) delete mode 100644 tests/Symfony/Component/Translation/TranslatorInterface.php diff --git a/src/Carbon/CarbonInterface.php b/src/Carbon/CarbonInterface.php index a9b70be3e7..6e3589a011 100644 --- a/src/Carbon/CarbonInterface.php +++ b/src/Carbon/CarbonInterface.php @@ -841,9 +841,9 @@ public function betweenExcluded($date1, $date2): bool; * * @example * ``` - * Carbon::parse('2018-07-25')->betweenExcluded('2018-07-14', '2018-08-01'); // true - * Carbon::parse('2018-07-25')->betweenExcluded('2018-08-01', '2018-08-20'); // false - * Carbon::parse('2018-07-25')->betweenExcluded('2018-07-25', '2018-08-01'); // true + * Carbon::parse('2018-07-25')->betweenIncluded('2018-07-14', '2018-08-01'); // true + * Carbon::parse('2018-07-25')->betweenIncluded('2018-08-01', '2018-08-20'); // false + * Carbon::parse('2018-07-25')->betweenIncluded('2018-07-25', '2018-08-01'); // true * ``` * * @param \Carbon\Carbon|\DateTimeInterface|mixed $date1 @@ -1017,11 +1017,11 @@ public static function createFromFormat($format, $time, $tz = null); /** * Create a Carbon instance from a specific ISO format (same replacements as ->isoFormat()). * - * @param string $format Datetime format - * @param string $time - * @param DateTimeZone|string|false|null $tz optional timezone - * @param string|null $locale locale to be used for LTS, LT, LL, LLL, etc. macro-formats (en by fault, unneeded if no such macro-format in use) - * @param \Symfony\Component\Translation\TranslatorInterface $translator optional custom translator to use for macro-formats + * @param string $format Datetime format + * @param string $time + * @param DateTimeZone|string|false|null $tz optional timezone + * @param string|null $locale locale to be used for LTS, LT, LL, LLL, etc. macro-formats (en by fault, unneeded if no such macro-format in use) + * @param TranslatorInterface $translator optional custom translator to use for macro-formats * * @throws InvalidArgumentException * @@ -1310,6 +1310,16 @@ public function diffInMinutes($date = null, $absolute = true); */ public function diffInMonths($date = null, $absolute = true); + /** + * Get the difference in quarters rounded down. + * + * @param \Carbon\CarbonInterface|\DateTimeInterface|string|null $date + * @param bool $absolute Get the absolute of the difference + * + * @return int + */ + public function diffInQuarters($date = null, $absolute = true); + /** * Get the difference in hours rounded down using timestamps. * @@ -1400,16 +1410,6 @@ public function diffInWeekendDays($date = null, $absolute = true); */ public function diffInWeeks($date = null, $absolute = true); - /** - * Get the difference in quarters rounded down. - * - * @param \Carbon\CarbonInterface|\DateTimeInterface|string|null $date - * @param bool $absolute Get the absolute of the difference - * - * @return int - */ - public function diffInQuarters($date = null, $absolute = true); - /** * Get the difference in years * @@ -2037,7 +2037,7 @@ public function getLocalMacro($name); /** * Get the translator of the current instance or the default if none set. * - * @return \Symfony\Component\Translation\TranslatorInterface + * @return TranslatorInterface */ public function getLocalTranslator(); @@ -2184,10 +2184,10 @@ public function getTranslatedShortMonthName($context = null); /** * Returns raw translation message for a given key. * - * @param string $key key to find - * @param string|null $locale current locale used if null - * @param string|null $default default value if translation returns the key - * @param \Symfony\Component\Translation\TranslatorInterface $translator an optional translator to use + * @param string $key key to find + * @param string|null $locale current locale used if null + * @param string|null $default default value if translation returns the key + * @param TranslatorInterface $translator an optional translator to use * * @return string */ @@ -2196,10 +2196,10 @@ public function getTranslationMessage(string $key, string $locale = null, string /** * Returns raw translation message for a given key. * - * @param \Symfony\Component\Translation\TranslatorInterface $translator the translator to use - * @param string $key key to find - * @param string|null $locale current locale used if null - * @param string|null $default default value if translation returns the key + * @param TranslatorInterface $translator the translator to use + * @param string $key key to find + * @param string|null $locale current locale used if null + * @param string|null $default default value if translation returns the key * * @return string */ @@ -2208,7 +2208,7 @@ public static function getTranslationMessageWith($translator, string $key, strin /** * Get the default translator instance in use. * - * @return \Symfony\Component\Translation\TranslatorInterface + * @return TranslatorInterface */ public static function getTranslator(); @@ -2439,8 +2439,8 @@ public function isBefore($date): bool; * ``` * Carbon::parse('2018-07-25')->isBetween('2018-07-14', '2018-08-01'); // true * Carbon::parse('2018-07-25')->isBetween('2018-08-01', '2018-08-20'); // false - * Carbon::parse('2018-07-25')->isBetween('2018-07-25', '2018-08-01'); // false - * Carbon::parse('2018-07-25')->isBetween('2018-07-25', '2018-08-01', true); // true + * Carbon::parse('2018-07-25')->isBetween('2018-07-25', '2018-08-01'); // true + * Carbon::parse('2018-07-25')->isBetween('2018-07-25', '2018-08-01', false); // false * ``` * * @param \Carbon\Carbon|\DateTimeInterface|mixed $date1 @@ -3604,11 +3604,11 @@ public function setISODate($year, $week, $day = 1); /** * Set the translator for the current instance. * - * @param \Symfony\Component\Translation\TranslatorInterface $translator + * @param TranslatorInterface $translator * * @return $this */ - public function setLocalTranslator(\Symfony\Component\Translation\TranslatorInterface $translator); + public function setLocalTranslator(\Symfony\Contracts\Translation\TranslatorInterface $translator); /** * Set the current translator locale and indicate if the source locale file exists. @@ -3726,11 +3726,11 @@ public static function setToStringFormat($format); /** * Set the default translator instance to use. * - * @param \Symfony\Component\Translation\TranslatorInterface $translator + * @param TranslatorInterface $translator * * @return void */ - public static function setTranslator(\Symfony\Component\Translation\TranslatorInterface $translator); + public static function setTranslator(\Symfony\Contracts\Translation\TranslatorInterface $translator); /** * Set specified unit to new given value. @@ -4578,14 +4578,14 @@ public static function tomorrow($tz = null); /** * Translate using translation string or callback available. * - * @param string $key - * @param array $parameters - * @param null $number - * @param \Symfony\Component\Translation\TranslatorInterface $translator + * @param string $key + * @param array $parameters + * @param null $number + * @param TranslatorInterface $translator * * @return string */ - public function translate(string $key, array $parameters = [], $number = null, \Symfony\Component\Translation\TranslatorInterface $translator = null, bool $altNumbers = false): string; + public function translate(string $key, array $parameters = [], $number = null, \Symfony\Contracts\Translation\TranslatorInterface $translator = null, bool $altNumbers = false): string; /** * Returns the alternative number for a given integer if available in the current locale. @@ -4627,14 +4627,14 @@ public function translateTimeStringTo($timeString, $to = null); /** * Translate using translation string or callback available. * - * @param \Symfony\Component\Translation\TranslatorInterface $translator - * @param string $key - * @param array $parameters - * @param null $number + * @param TranslatorInterface $translator + * @param string $key + * @param array $parameters + * @param null $number * * @return string */ - public static function translateWith(\Symfony\Component\Translation\TranslatorInterface $translator, string $key, array $parameters = [], $number = null): string; + public static function translateWith(\Symfony\Contracts\Translation\TranslatorInterface $translator, string $key, array $parameters = [], $number = null): string; /** * Format as ->format() do (using date replacements patterns from http://php.net/manual/fr/function.date.php) diff --git a/src/Carbon/Factory.php b/src/Carbon/Factory.php index 355983648c..f1e74a8b35 100644 --- a/src/Carbon/Factory.php +++ b/src/Carbon/Factory.php @@ -17,195 +17,195 @@ * * * - * @method Carbon create($year = 0, $month = 1, $day = 1, $hour = 0, $minute = 0, $second = 0, $tz = null) Create a new Carbon instance from a specific date and time. - * If any of $year, $month or $day are set to null their now() values will - * be used. - * If $hour is null it will be set to its now() value and the default - * values for $minute and $second will be their now() values. - * If $hour is not null then the default values for $minute and $second - * will be 0. - * @method Carbon createFromDate($year = null, $month = null, $day = null, $tz = null) Create a Carbon instance from just a date. The time portion is set to now. - * @method Carbon|false createFromFormat($format, $time, $tz = null) Create a Carbon instance from a specific format. - * @method Carbon|false createFromIsoFormat($format, $time, $tz = null, $locale = 'en', $translator = null) Create a Carbon instance from a specific ISO format (same replacements as ->isoFormat()). - * @method Carbon|false createFromLocaleFormat($format, $locale, $time, $tz = null) Create a Carbon instance from a specific format and a string in a given language. - * @method Carbon|false createFromLocaleIsoFormat($format, $locale, $time, $tz = null) Create a Carbon instance from a specific ISO format and a string in a given language. - * @method Carbon createFromTime($hour = 0, $minute = 0, $second = 0, $tz = null) Create a Carbon instance from just a time. The date portion is set to today. - * @method Carbon createFromTimeString($time, $tz = null) Create a Carbon instance from a time string. The date portion is set to today. - * @method Carbon createFromTimestamp($timestamp, $tz = null) Create a Carbon instance from a timestamp. - * @method Carbon createFromTimestampMs($timestamp, $tz = null) Create a Carbon instance from a timestamp in milliseconds. - * @method Carbon createFromTimestampUTC($timestamp) Create a Carbon instance from an UTC timestamp. - * @method Carbon createMidnightDate($year = null, $month = null, $day = null, $tz = null) Create a Carbon instance from just a date. The time portion is set to midnight. - * @method Carbon|false createSafe($year = null, $month = null, $day = null, $hour = null, $minute = null, $second = null, $tz = null) Create a new safe Carbon instance from a specific date and time. - * If any of $year, $month or $day are set to null their now() values will - * be used. - * If $hour is null it will be set to its now() value and the default - * values for $minute and $second will be their now() values. - * If $hour is not null then the default values for $minute and $second - * will be 0. - * If one of the set values is not valid, an \InvalidArgumentException - * will be thrown. - * @method Carbon disableHumanDiffOption($humanDiffOption) @deprecated To avoid conflict between different third-party libraries, static setters should not be used. - * You should rather use the ->settings() method. - * @method Carbon enableHumanDiffOption($humanDiffOption) @deprecated To avoid conflict between different third-party libraries, static setters should not be used. - * You should rather use the ->settings() method. - * @method mixed executeWithLocale($locale, $func) Set the current locale to the given, execute the passed function, reset the locale to previous one, - * then return the result of the closure (or null if the closure was void). - * @method Carbon fromSerialized($value) Create an instance from a serialized string. - * @method void genericMacro($macro, $priority = 0) Register a custom macro. - * @method array getAvailableLocales() Returns the list of internally available locales and already loaded custom locales. - * (It will ignore custom translator dynamic loading.) - * @method Language[] getAvailableLocalesInfo() Returns list of Language object for each available locale. This object allow you to get the ISO name, native - * name, region and variant of the locale. - * @method array getDays() Get the days of the week - * @method string|null getFallbackLocale() Get the fallback locale. - * @method array getFormatsToIsoReplacements() List of replacements from date() format to isoFormat(). - * @method int getHumanDiffOptions() Return default humanDiff() options (merged flags as integer). - * @method array getIsoUnits() Returns list of locale units for ISO formatting. - * @method Carbon getLastErrors() {@inheritdoc} - * @method string getLocale() Get the current translator locale. - * @method callable|null getMacro($name) Get the raw callable macro registered globally for a given name. - * @method int getMidDayAt() get midday/noon hour - * @method Closure|Carbon getTestNow() Get the Carbon instance (real or mock) to be returned when a "now" - * instance is created. - * @method string getTimeFormatByPrecision($unitPrecision) Return a format from H:i to H:i:s.u according to given unit precision. - * @method string getTranslationMessageWith($translator, string $key, string $locale = null, string $default = null) Returns raw translation message for a given key. - * @method \Symfony\Component\Translation\TranslatorInterface getTranslator() Get the default translator instance in use. - * @method int getWeekEndsAt() Get the last day of week - * @method int getWeekStartsAt() Get the first day of week - * @method array getWeekendDays() Get weekend days - * @method bool hasFormat($date, $format) Checks if the (date)time string is in a given format. - * @method bool hasMacro($name) Checks if macro is registered globally. - * @method bool hasRelativeKeywords($time) Determine if a time string will produce a relative date. - * @method bool hasTestNow() Determine if there is a valid test instance set. A valid test instance - * is anything that is not null. - * @method Carbon instance($date) Create a Carbon instance from a DateTime one. - * @method bool isImmutable() Returns true if the current class/instance is immutable. - * @method bool isModifiableUnit($unit) Returns true if a property can be changed via setter. - * @method bool isMutable() Returns true if the current class/instance is mutable. - * @method bool isStrictModeEnabled() Returns true if the strict mode is globally in use, false else. - * (It can be overridden in specific instances.) - * @method bool localeHasDiffOneDayWords($locale) Returns true if the given locale is internally supported and has words for 1-day diff (just now, yesterday, tomorrow). - * Support is considered enabled if the 3 words are translated in the given locale. - * @method bool localeHasDiffSyntax($locale) Returns true if the given locale is internally supported and has diff syntax support (ago, from now, before, after). - * Support is considered enabled if the 4 sentences are translated in the given locale. - * @method bool localeHasDiffTwoDayWords($locale) Returns true if the given locale is internally supported and has words for 2-days diff (before yesterday, after tomorrow). - * Support is considered enabled if the 2 words are translated in the given locale. - * @method bool localeHasPeriodSyntax($locale) Returns true if the given locale is internally supported and has period syntax support (X times, every X, from X, to X). - * Support is considered enabled if the 4 sentences are translated in the given locale. - * @method bool localeHasShortUnits($locale) Returns true if the given locale is internally supported and has short-units support. - * Support is considered enabled if either year, day or hour has a short variant translated. - * @method void macro($name, $macro) Register a custom macro. - * @method Carbon|null make($var) Make a Carbon instance from given variable if possible. - * Always return a new instance. Parse only strings and only these likely to be dates (skip intervals - * and recurrences). Throw an exception for invalid format, but otherwise return null. - * @method Carbon maxValue() Create a Carbon instance for the greatest supported date. - * @method Carbon minValue() Create a Carbon instance for the lowest supported date. - * @method void mixin($mixin) Mix another object into the class. - * @method Carbon now($tz = null) Get a Carbon instance for the current date and time. - * @method Carbon parse($time = null, $tz = null) Create a carbon instance from a string. - * This is an alias for the constructor that allows better fluent syntax - * as it allows you to do Carbon::parse('Monday next week')->fn() rather - * than (new Carbon('Monday next week'))->fn(). - * @method Carbon parseFromLocale($time, $locale, $tz = null) Create a carbon instance from a localized string (in French, Japanese, Arabic, etc.). - * @method string pluralUnit(string $unit) Returns standardized plural of a given singular/plural unit name (in English). - * @method Carbon|false rawCreateFromFormat($format, $time, $tz = null) Create a Carbon instance from a specific format. - * @method Carbon rawParse($time = null, $tz = null) Create a carbon instance from a string. - * This is an alias for the constructor that allows better fluent syntax - * as it allows you to do Carbon::parse('Monday next week')->fn() rather - * than (new Carbon('Monday next week'))->fn(). - * @method Carbon resetMacros() Remove all macros and generic macros. - * @method void resetMonthsOverflow() @deprecated To avoid conflict between different third-party libraries, static setters should not be used. - * You should rather use the ->settings() method. - * Or you can use method variants: addMonthsWithOverflow/addMonthsNoOverflow, same variants - * are available for quarters, years, decade, centuries, millennia (singular and plural forms). - * @method void resetToStringFormat() Reset the format used to the default when type juggling a Carbon instance to a string - * @method void resetYearsOverflow() @deprecated To avoid conflict between different third-party libraries, static setters should not be used. - * You should rather use the ->settings() method. - * Or you can use method variants: addYearsWithOverflow/addYearsNoOverflow, same variants - * are available for quarters, years, decade, centuries, millennia (singular and plural forms). - * @method void serializeUsing($callback) @deprecated To avoid conflict between different third-party libraries, static setters should not be used. - * You should rather transform Carbon object before the serialization. - * JSON serialize all Carbon instances using the given callback. - * @method Carbon setFallbackLocale($locale) Set the fallback locale. - * @method Carbon setHumanDiffOptions($humanDiffOptions) @deprecated To avoid conflict between different third-party libraries, static setters should not be used. - * You should rather use the ->settings() method. - * @method bool setLocale($locale) Set the current translator locale and indicate if the source locale file exists. - * Pass 'auto' as locale to use closest language from the current LC_TIME locale. - * @method void setMidDayAt($hour) @deprecated To avoid conflict between different third-party libraries, static setters should not be used. - * You should rather consider mid-day is always 12pm, then if you need to test if it's an other - * hour, test it explicitly: - * $date->format('G') == 13 - * or to set explicitly to a given hour: - * $date->setTime(13, 0, 0, 0) - * Set midday/noon hour - * @method Carbon setTestNow($testNow = null) Set a Carbon instance (real or mock) to be returned when a "now" - * instance is created. The provided instance will be returned - * specifically under the following conditions: - * - A call to the static now() method, ex. Carbon::now() - * - When a null (or blank string) is passed to the constructor or parse(), ex. new Carbon(null) - * - When the string "now" is passed to the constructor or parse(), ex. new Carbon('now') - * - When a string containing the desired time is passed to Carbon::parse(). - * Note the timezone parameter was left out of the examples above and - * has no affect as the mock value will be returned regardless of its value. - * To clear the test instance call this method using the default - * parameter of null. - * /!\ Use this method for unit tests only. - * @method void setToStringFormat($format) @deprecated To avoid conflict between different third-party libraries, static setters should not be used. - * You should rather let Carbon object being casted to string with DEFAULT_TO_STRING_FORMAT, and - * use other method or custom format passed to format() method if you need to dump an other string - * format. - * Set the default format used when type juggling a Carbon instance to a string - * @method void setTranslator(\Symfony\Component\Translation\TranslatorInterface $translator) Set the default translator instance to use. - * @method Carbon setUtf8($utf8) @deprecated To avoid conflict between different third-party libraries, static setters should not be used. - * You should rather use UTF-8 language packages on every machine. - * Set if UTF8 will be used for localized date/time. - * @method void setWeekEndsAt($day) @deprecated To avoid conflict between different third-party libraries, static setters should not be used. - * Use $weekStartsAt optional parameter instead when using startOfWeek, floorWeek, ceilWeek - * or roundWeek method. You can also use the 'first_day_of_week' locale setting to change the - * start of week according to current locale selected and implicitly the end of week. - * Set the last day of week - * @method void setWeekStartsAt($day) @deprecated To avoid conflict between different third-party libraries, static setters should not be used. - * Use $weekEndsAt optional parameter instead when using endOfWeek method. You can also use the - * 'first_day_of_week' locale setting to change the start of week according to current locale - * selected and implicitly the end of week. - * Set the first day of week - * @method void setWeekendDays($days) @deprecated To avoid conflict between different third-party libraries, static setters should not be used. - * You should rather consider week-end is always saturday and sunday, and if you have some custom - * week-end days to handle, give to those days an other name and create a macro for them: - * ``` - * Carbon::macro('isDayOff', function ($date) { - * return $date->isSunday() || $date->isMonday(); - * }); - * Carbon::macro('isNotDayOff', function ($date) { - * return !$date->isDayOff(); - * }); - * if ($someDate->isDayOff()) ... - * if ($someDate->isNotDayOff()) ... - * // Add 5 not-off days - * $count = 5; - * while ($someDate->isDayOff() || ($count-- > 0)) { - * $someDate->addDay(); - * } - * ``` - * Set weekend days - * @method bool shouldOverflowMonths() Get the month overflow global behavior (can be overridden in specific instances). - * @method bool shouldOverflowYears() Get the month overflow global behavior (can be overridden in specific instances). - * @method string singularUnit(string $unit) Returns standardized singular of a given singular/plural unit name (in English). - * @method Carbon today($tz = null) Create a Carbon instance for today. - * @method Carbon tomorrow($tz = null) Create a Carbon instance for tomorrow. - * @method string translateTimeString($timeString, $from = null, $to = null, $mode = 15) Translate a time string from a locale to an other. - * @method string translateWith(\Symfony\Component\Translation\TranslatorInterface $translator, string $key, array $parameters = [], $number = null) Translate using translation string or callback available. - * @method void useMonthsOverflow($monthsOverflow = true) @deprecated To avoid conflict between different third-party libraries, static setters should not be used. - * You should rather use the ->settings() method. - * Or you can use method variants: addMonthsWithOverflow/addMonthsNoOverflow, same variants - * are available for quarters, years, decade, centuries, millennia (singular and plural forms). - * @method Carbon useStrictMode($strictModeEnabled = true) @deprecated To avoid conflict between different third-party libraries, static setters should not be used. - * You should rather use the ->settings() method. - * @method void useYearsOverflow($yearsOverflow = true) @deprecated To avoid conflict between different third-party libraries, static setters should not be used. - * You should rather use the ->settings() method. - * Or you can use method variants: addYearsWithOverflow/addYearsNoOverflow, same variants - * are available for quarters, years, decade, centuries, millennia (singular and plural forms). - * @method Carbon yesterday($tz = null) Create a Carbon instance for yesterday. + * @method Carbon create($year = 0, $month = 1, $day = 1, $hour = 0, $minute = 0, $second = 0, $tz = null) Create a new Carbon instance from a specific date and time. + * If any of $year, $month or $day are set to null their now() values will + * be used. + * If $hour is null it will be set to its now() value and the default + * values for $minute and $second will be their now() values. + * If $hour is not null then the default values for $minute and $second + * will be 0. + * @method Carbon createFromDate($year = null, $month = null, $day = null, $tz = null) Create a Carbon instance from just a date. The time portion is set to now. + * @method Carbon|false createFromFormat($format, $time, $tz = null) Create a Carbon instance from a specific format. + * @method Carbon|false createFromIsoFormat($format, $time, $tz = null, $locale = 'en', $translator = null) Create a Carbon instance from a specific ISO format (same replacements as ->isoFormat()). + * @method Carbon|false createFromLocaleFormat($format, $locale, $time, $tz = null) Create a Carbon instance from a specific format and a string in a given language. + * @method Carbon|false createFromLocaleIsoFormat($format, $locale, $time, $tz = null) Create a Carbon instance from a specific ISO format and a string in a given language. + * @method Carbon createFromTime($hour = 0, $minute = 0, $second = 0, $tz = null) Create a Carbon instance from just a time. The date portion is set to today. + * @method Carbon createFromTimeString($time, $tz = null) Create a Carbon instance from a time string. The date portion is set to today. + * @method Carbon createFromTimestamp($timestamp, $tz = null) Create a Carbon instance from a timestamp. + * @method Carbon createFromTimestampMs($timestamp, $tz = null) Create a Carbon instance from a timestamp in milliseconds. + * @method Carbon createFromTimestampUTC($timestamp) Create a Carbon instance from an UTC timestamp. + * @method Carbon createMidnightDate($year = null, $month = null, $day = null, $tz = null) Create a Carbon instance from just a date. The time portion is set to midnight. + * @method Carbon|false createSafe($year = null, $month = null, $day = null, $hour = null, $minute = null, $second = null, $tz = null) Create a new safe Carbon instance from a specific date and time. + * If any of $year, $month or $day are set to null their now() values will + * be used. + * If $hour is null it will be set to its now() value and the default + * values for $minute and $second will be their now() values. + * If $hour is not null then the default values for $minute and $second + * will be 0. + * If one of the set values is not valid, an \InvalidArgumentException + * will be thrown. + * @method Carbon disableHumanDiffOption($humanDiffOption) @deprecated To avoid conflict between different third-party libraries, static setters should not be used. + * You should rather use the ->settings() method. + * @method Carbon enableHumanDiffOption($humanDiffOption) @deprecated To avoid conflict between different third-party libraries, static setters should not be used. + * You should rather use the ->settings() method. + * @method mixed executeWithLocale($locale, $func) Set the current locale to the given, execute the passed function, reset the locale to previous one, + * then return the result of the closure (or null if the closure was void). + * @method Carbon fromSerialized($value) Create an instance from a serialized string. + * @method void genericMacro($macro, $priority = 0) Register a custom macro. + * @method array getAvailableLocales() Returns the list of internally available locales and already loaded custom locales. + * (It will ignore custom translator dynamic loading.) + * @method Language[] getAvailableLocalesInfo() Returns list of Language object for each available locale. This object allow you to get the ISO name, native + * name, region and variant of the locale. + * @method array getDays() Get the days of the week + * @method string|null getFallbackLocale() Get the fallback locale. + * @method array getFormatsToIsoReplacements() List of replacements from date() format to isoFormat(). + * @method int getHumanDiffOptions() Return default humanDiff() options (merged flags as integer). + * @method array getIsoUnits() Returns list of locale units for ISO formatting. + * @method Carbon getLastErrors() {@inheritdoc} + * @method string getLocale() Get the current translator locale. + * @method callable|null getMacro($name) Get the raw callable macro registered globally for a given name. + * @method int getMidDayAt() get midday/noon hour + * @method Closure|Carbon getTestNow() Get the Carbon instance (real or mock) to be returned when a "now" + * instance is created. + * @method string getTimeFormatByPrecision($unitPrecision) Return a format from H:i to H:i:s.u according to given unit precision. + * @method string getTranslationMessageWith($translator, string $key, string $locale = null, string $default = null) Returns raw translation message for a given key. + * @method TranslatorInterface getTranslator() Get the default translator instance in use. + * @method int getWeekEndsAt() Get the last day of week + * @method int getWeekStartsAt() Get the first day of week + * @method array getWeekendDays() Get weekend days + * @method bool hasFormat($date, $format) Checks if the (date)time string is in a given format. + * @method bool hasMacro($name) Checks if macro is registered globally. + * @method bool hasRelativeKeywords($time) Determine if a time string will produce a relative date. + * @method bool hasTestNow() Determine if there is a valid test instance set. A valid test instance + * is anything that is not null. + * @method Carbon instance($date) Create a Carbon instance from a DateTime one. + * @method bool isImmutable() Returns true if the current class/instance is immutable. + * @method bool isModifiableUnit($unit) Returns true if a property can be changed via setter. + * @method bool isMutable() Returns true if the current class/instance is mutable. + * @method bool isStrictModeEnabled() Returns true if the strict mode is globally in use, false else. + * (It can be overridden in specific instances.) + * @method bool localeHasDiffOneDayWords($locale) Returns true if the given locale is internally supported and has words for 1-day diff (just now, yesterday, tomorrow). + * Support is considered enabled if the 3 words are translated in the given locale. + * @method bool localeHasDiffSyntax($locale) Returns true if the given locale is internally supported and has diff syntax support (ago, from now, before, after). + * Support is considered enabled if the 4 sentences are translated in the given locale. + * @method bool localeHasDiffTwoDayWords($locale) Returns true if the given locale is internally supported and has words for 2-days diff (before yesterday, after tomorrow). + * Support is considered enabled if the 2 words are translated in the given locale. + * @method bool localeHasPeriodSyntax($locale) Returns true if the given locale is internally supported and has period syntax support (X times, every X, from X, to X). + * Support is considered enabled if the 4 sentences are translated in the given locale. + * @method bool localeHasShortUnits($locale) Returns true if the given locale is internally supported and has short-units support. + * Support is considered enabled if either year, day or hour has a short variant translated. + * @method void macro($name, $macro) Register a custom macro. + * @method Carbon|null make($var) Make a Carbon instance from given variable if possible. + * Always return a new instance. Parse only strings and only these likely to be dates (skip intervals + * and recurrences). Throw an exception for invalid format, but otherwise return null. + * @method Carbon maxValue() Create a Carbon instance for the greatest supported date. + * @method Carbon minValue() Create a Carbon instance for the lowest supported date. + * @method void mixin($mixin) Mix another object into the class. + * @method Carbon now($tz = null) Get a Carbon instance for the current date and time. + * @method Carbon parse($time = null, $tz = null) Create a carbon instance from a string. + * This is an alias for the constructor that allows better fluent syntax + * as it allows you to do Carbon::parse('Monday next week')->fn() rather + * than (new Carbon('Monday next week'))->fn(). + * @method Carbon parseFromLocale($time, $locale, $tz = null) Create a carbon instance from a localized string (in French, Japanese, Arabic, etc.). + * @method string pluralUnit(string $unit) Returns standardized plural of a given singular/plural unit name (in English). + * @method Carbon|false rawCreateFromFormat($format, $time, $tz = null) Create a Carbon instance from a specific format. + * @method Carbon rawParse($time = null, $tz = null) Create a carbon instance from a string. + * This is an alias for the constructor that allows better fluent syntax + * as it allows you to do Carbon::parse('Monday next week')->fn() rather + * than (new Carbon('Monday next week'))->fn(). + * @method Carbon resetMacros() Remove all macros and generic macros. + * @method void resetMonthsOverflow() @deprecated To avoid conflict between different third-party libraries, static setters should not be used. + * You should rather use the ->settings() method. + * Or you can use method variants: addMonthsWithOverflow/addMonthsNoOverflow, same variants + * are available for quarters, years, decade, centuries, millennia (singular and plural forms). + * @method void resetToStringFormat() Reset the format used to the default when type juggling a Carbon instance to a string + * @method void resetYearsOverflow() @deprecated To avoid conflict between different third-party libraries, static setters should not be used. + * You should rather use the ->settings() method. + * Or you can use method variants: addYearsWithOverflow/addYearsNoOverflow, same variants + * are available for quarters, years, decade, centuries, millennia (singular and plural forms). + * @method void serializeUsing($callback) @deprecated To avoid conflict between different third-party libraries, static setters should not be used. + * You should rather transform Carbon object before the serialization. + * JSON serialize all Carbon instances using the given callback. + * @method Carbon setFallbackLocale($locale) Set the fallback locale. + * @method Carbon setHumanDiffOptions($humanDiffOptions) @deprecated To avoid conflict between different third-party libraries, static setters should not be used. + * You should rather use the ->settings() method. + * @method bool setLocale($locale) Set the current translator locale and indicate if the source locale file exists. + * Pass 'auto' as locale to use closest language from the current LC_TIME locale. + * @method void setMidDayAt($hour) @deprecated To avoid conflict between different third-party libraries, static setters should not be used. + * You should rather consider mid-day is always 12pm, then if you need to test if it's an other + * hour, test it explicitly: + * $date->format('G') == 13 + * or to set explicitly to a given hour: + * $date->setTime(13, 0, 0, 0) + * Set midday/noon hour + * @method Carbon setTestNow($testNow = null) Set a Carbon instance (real or mock) to be returned when a "now" + * instance is created. The provided instance will be returned + * specifically under the following conditions: + * - A call to the static now() method, ex. Carbon::now() + * - When a null (or blank string) is passed to the constructor or parse(), ex. new Carbon(null) + * - When the string "now" is passed to the constructor or parse(), ex. new Carbon('now') + * - When a string containing the desired time is passed to Carbon::parse(). + * Note the timezone parameter was left out of the examples above and + * has no affect as the mock value will be returned regardless of its value. + * To clear the test instance call this method using the default + * parameter of null. + * /!\ Use this method for unit tests only. + * @method void setToStringFormat($format) @deprecated To avoid conflict between different third-party libraries, static setters should not be used. + * You should rather let Carbon object being casted to string with DEFAULT_TO_STRING_FORMAT, and + * use other method or custom format passed to format() method if you need to dump an other string + * format. + * Set the default format used when type juggling a Carbon instance to a string + * @method void setTranslator(\Symfony\Contracts\Translation\TranslatorInterface $translator) Set the default translator instance to use. + * @method Carbon setUtf8($utf8) @deprecated To avoid conflict between different third-party libraries, static setters should not be used. + * You should rather use UTF-8 language packages on every machine. + * Set if UTF8 will be used for localized date/time. + * @method void setWeekEndsAt($day) @deprecated To avoid conflict between different third-party libraries, static setters should not be used. + * Use $weekStartsAt optional parameter instead when using startOfWeek, floorWeek, ceilWeek + * or roundWeek method. You can also use the 'first_day_of_week' locale setting to change the + * start of week according to current locale selected and implicitly the end of week. + * Set the last day of week + * @method void setWeekStartsAt($day) @deprecated To avoid conflict between different third-party libraries, static setters should not be used. + * Use $weekEndsAt optional parameter instead when using endOfWeek method. You can also use the + * 'first_day_of_week' locale setting to change the start of week according to current locale + * selected and implicitly the end of week. + * Set the first day of week + * @method void setWeekendDays($days) @deprecated To avoid conflict between different third-party libraries, static setters should not be used. + * You should rather consider week-end is always saturday and sunday, and if you have some custom + * week-end days to handle, give to those days an other name and create a macro for them: + * ``` + * Carbon::macro('isDayOff', function ($date) { + * return $date->isSunday() || $date->isMonday(); + * }); + * Carbon::macro('isNotDayOff', function ($date) { + * return !$date->isDayOff(); + * }); + * if ($someDate->isDayOff()) ... + * if ($someDate->isNotDayOff()) ... + * // Add 5 not-off days + * $count = 5; + * while ($someDate->isDayOff() || ($count-- > 0)) { + * $someDate->addDay(); + * } + * ``` + * Set weekend days + * @method bool shouldOverflowMonths() Get the month overflow global behavior (can be overridden in specific instances). + * @method bool shouldOverflowYears() Get the month overflow global behavior (can be overridden in specific instances). + * @method string singularUnit(string $unit) Returns standardized singular of a given singular/plural unit name (in English). + * @method Carbon today($tz = null) Create a Carbon instance for today. + * @method Carbon tomorrow($tz = null) Create a Carbon instance for tomorrow. + * @method string translateTimeString($timeString, $from = null, $to = null, $mode = 15) Translate a time string from a locale to an other. + * @method string translateWith(\Symfony\Contracts\Translation\TranslatorInterface $translator, string $key, array $parameters = [], $number = null) Translate using translation string or callback available. + * @method void useMonthsOverflow($monthsOverflow = true) @deprecated To avoid conflict between different third-party libraries, static setters should not be used. + * You should rather use the ->settings() method. + * Or you can use method variants: addMonthsWithOverflow/addMonthsNoOverflow, same variants + * are available for quarters, years, decade, centuries, millennia (singular and plural forms). + * @method Carbon useStrictMode($strictModeEnabled = true) @deprecated To avoid conflict between different third-party libraries, static setters should not be used. + * You should rather use the ->settings() method. + * @method void useYearsOverflow($yearsOverflow = true) @deprecated To avoid conflict between different third-party libraries, static setters should not be used. + * You should rather use the ->settings() method. + * Or you can use method variants: addYearsWithOverflow/addYearsNoOverflow, same variants + * are available for quarters, years, decade, centuries, millennia (singular and plural forms). + * @method Carbon yesterday($tz = null) Create a Carbon instance for yesterday. * * */ diff --git a/src/Carbon/FactoryImmutable.php b/src/Carbon/FactoryImmutable.php index a30d3d4a37..31318387b7 100644 --- a/src/Carbon/FactoryImmutable.php +++ b/src/Carbon/FactoryImmutable.php @@ -17,195 +17,195 @@ * * * - * @method CarbonImmutable create($year = 0, $month = 1, $day = 1, $hour = 0, $minute = 0, $second = 0, $tz = null) Create a new Carbon instance from a specific date and time. - * If any of $year, $month or $day are set to null their now() values will - * be used. - * If $hour is null it will be set to its now() value and the default - * values for $minute and $second will be their now() values. - * If $hour is not null then the default values for $minute and $second - * will be 0. - * @method CarbonImmutable createFromDate($year = null, $month = null, $day = null, $tz = null) Create a Carbon instance from just a date. The time portion is set to now. - * @method CarbonImmutable|false createFromFormat($format, $time, $tz = null) Create a Carbon instance from a specific format. - * @method CarbonImmutable|false createFromIsoFormat($format, $time, $tz = null, $locale = 'en', $translator = null) Create a Carbon instance from a specific ISO format (same replacements as ->isoFormat()). - * @method CarbonImmutable|false createFromLocaleFormat($format, $locale, $time, $tz = null) Create a Carbon instance from a specific format and a string in a given language. - * @method CarbonImmutable|false createFromLocaleIsoFormat($format, $locale, $time, $tz = null) Create a Carbon instance from a specific ISO format and a string in a given language. - * @method CarbonImmutable createFromTime($hour = 0, $minute = 0, $second = 0, $tz = null) Create a Carbon instance from just a time. The date portion is set to today. - * @method CarbonImmutable createFromTimeString($time, $tz = null) Create a Carbon instance from a time string. The date portion is set to today. - * @method CarbonImmutable createFromTimestamp($timestamp, $tz = null) Create a Carbon instance from a timestamp. - * @method CarbonImmutable createFromTimestampMs($timestamp, $tz = null) Create a Carbon instance from a timestamp in milliseconds. - * @method CarbonImmutable createFromTimestampUTC($timestamp) Create a Carbon instance from an UTC timestamp. - * @method CarbonImmutable createMidnightDate($year = null, $month = null, $day = null, $tz = null) Create a Carbon instance from just a date. The time portion is set to midnight. - * @method CarbonImmutable|false createSafe($year = null, $month = null, $day = null, $hour = null, $minute = null, $second = null, $tz = null) Create a new safe Carbon instance from a specific date and time. - * If any of $year, $month or $day are set to null their now() values will - * be used. - * If $hour is null it will be set to its now() value and the default - * values for $minute and $second will be their now() values. - * If $hour is not null then the default values for $minute and $second - * will be 0. - * If one of the set values is not valid, an \InvalidArgumentException - * will be thrown. - * @method CarbonImmutable disableHumanDiffOption($humanDiffOption) @deprecated To avoid conflict between different third-party libraries, static setters should not be used. - * You should rather use the ->settings() method. - * @method CarbonImmutable enableHumanDiffOption($humanDiffOption) @deprecated To avoid conflict between different third-party libraries, static setters should not be used. - * You should rather use the ->settings() method. - * @method mixed executeWithLocale($locale, $func) Set the current locale to the given, execute the passed function, reset the locale to previous one, - * then return the result of the closure (or null if the closure was void). - * @method CarbonImmutable fromSerialized($value) Create an instance from a serialized string. - * @method void genericMacro($macro, $priority = 0) Register a custom macro. - * @method array getAvailableLocales() Returns the list of internally available locales and already loaded custom locales. - * (It will ignore custom translator dynamic loading.) - * @method Language[] getAvailableLocalesInfo() Returns list of Language object for each available locale. This object allow you to get the ISO name, native - * name, region and variant of the locale. - * @method array getDays() Get the days of the week - * @method string|null getFallbackLocale() Get the fallback locale. - * @method array getFormatsToIsoReplacements() List of replacements from date() format to isoFormat(). - * @method int getHumanDiffOptions() Return default humanDiff() options (merged flags as integer). - * @method array getIsoUnits() Returns list of locale units for ISO formatting. - * @method CarbonImmutable getLastErrors() {@inheritdoc} - * @method string getLocale() Get the current translator locale. - * @method callable|null getMacro($name) Get the raw callable macro registered globally for a given name. - * @method int getMidDayAt() get midday/noon hour - * @method Closure|CarbonImmutable getTestNow() Get the Carbon instance (real or mock) to be returned when a "now" - * instance is created. - * @method string getTimeFormatByPrecision($unitPrecision) Return a format from H:i to H:i:s.u according to given unit precision. - * @method string getTranslationMessageWith($translator, string $key, string $locale = null, string $default = null) Returns raw translation message for a given key. - * @method \Symfony\Component\Translation\TranslatorInterface getTranslator() Get the default translator instance in use. - * @method int getWeekEndsAt() Get the last day of week - * @method int getWeekStartsAt() Get the first day of week - * @method array getWeekendDays() Get weekend days - * @method bool hasFormat($date, $format) Checks if the (date)time string is in a given format. - * @method bool hasMacro($name) Checks if macro is registered globally. - * @method bool hasRelativeKeywords($time) Determine if a time string will produce a relative date. - * @method bool hasTestNow() Determine if there is a valid test instance set. A valid test instance - * is anything that is not null. - * @method CarbonImmutable instance($date) Create a Carbon instance from a DateTime one. - * @method bool isImmutable() Returns true if the current class/instance is immutable. - * @method bool isModifiableUnit($unit) Returns true if a property can be changed via setter. - * @method bool isMutable() Returns true if the current class/instance is mutable. - * @method bool isStrictModeEnabled() Returns true if the strict mode is globally in use, false else. - * (It can be overridden in specific instances.) - * @method bool localeHasDiffOneDayWords($locale) Returns true if the given locale is internally supported and has words for 1-day diff (just now, yesterday, tomorrow). - * Support is considered enabled if the 3 words are translated in the given locale. - * @method bool localeHasDiffSyntax($locale) Returns true if the given locale is internally supported and has diff syntax support (ago, from now, before, after). - * Support is considered enabled if the 4 sentences are translated in the given locale. - * @method bool localeHasDiffTwoDayWords($locale) Returns true if the given locale is internally supported and has words for 2-days diff (before yesterday, after tomorrow). - * Support is considered enabled if the 2 words are translated in the given locale. - * @method bool localeHasPeriodSyntax($locale) Returns true if the given locale is internally supported and has period syntax support (X times, every X, from X, to X). - * Support is considered enabled if the 4 sentences are translated in the given locale. - * @method bool localeHasShortUnits($locale) Returns true if the given locale is internally supported and has short-units support. - * Support is considered enabled if either year, day or hour has a short variant translated. - * @method void macro($name, $macro) Register a custom macro. - * @method CarbonImmutable|null make($var) Make a Carbon instance from given variable if possible. - * Always return a new instance. Parse only strings and only these likely to be dates (skip intervals - * and recurrences). Throw an exception for invalid format, but otherwise return null. - * @method CarbonImmutable maxValue() Create a Carbon instance for the greatest supported date. - * @method CarbonImmutable minValue() Create a Carbon instance for the lowest supported date. - * @method void mixin($mixin) Mix another object into the class. - * @method CarbonImmutable now($tz = null) Get a Carbon instance for the current date and time. - * @method CarbonImmutable parse($time = null, $tz = null) Create a carbon instance from a string. - * This is an alias for the constructor that allows better fluent syntax - * as it allows you to do Carbon::parse('Monday next week')->fn() rather - * than (new Carbon('Monday next week'))->fn(). - * @method CarbonImmutable parseFromLocale($time, $locale, $tz = null) Create a carbon instance from a localized string (in French, Japanese, Arabic, etc.). - * @method string pluralUnit(string $unit) Returns standardized plural of a given singular/plural unit name (in English). - * @method CarbonImmutable|false rawCreateFromFormat($format, $time, $tz = null) Create a Carbon instance from a specific format. - * @method CarbonImmutable rawParse($time = null, $tz = null) Create a carbon instance from a string. - * This is an alias for the constructor that allows better fluent syntax - * as it allows you to do Carbon::parse('Monday next week')->fn() rather - * than (new Carbon('Monday next week'))->fn(). - * @method CarbonImmutable resetMacros() Remove all macros and generic macros. - * @method void resetMonthsOverflow() @deprecated To avoid conflict between different third-party libraries, static setters should not be used. - * You should rather use the ->settings() method. - * Or you can use method variants: addMonthsWithOverflow/addMonthsNoOverflow, same variants - * are available for quarters, years, decade, centuries, millennia (singular and plural forms). - * @method void resetToStringFormat() Reset the format used to the default when type juggling a Carbon instance to a string - * @method void resetYearsOverflow() @deprecated To avoid conflict between different third-party libraries, static setters should not be used. - * You should rather use the ->settings() method. - * Or you can use method variants: addYearsWithOverflow/addYearsNoOverflow, same variants - * are available for quarters, years, decade, centuries, millennia (singular and plural forms). - * @method void serializeUsing($callback) @deprecated To avoid conflict between different third-party libraries, static setters should not be used. - * You should rather transform Carbon object before the serialization. - * JSON serialize all Carbon instances using the given callback. - * @method CarbonImmutable setFallbackLocale($locale) Set the fallback locale. - * @method CarbonImmutable setHumanDiffOptions($humanDiffOptions) @deprecated To avoid conflict between different third-party libraries, static setters should not be used. - * You should rather use the ->settings() method. - * @method bool setLocale($locale) Set the current translator locale and indicate if the source locale file exists. - * Pass 'auto' as locale to use closest language from the current LC_TIME locale. - * @method void setMidDayAt($hour) @deprecated To avoid conflict between different third-party libraries, static setters should not be used. - * You should rather consider mid-day is always 12pm, then if you need to test if it's an other - * hour, test it explicitly: - * $date->format('G') == 13 - * or to set explicitly to a given hour: - * $date->setTime(13, 0, 0, 0) - * Set midday/noon hour - * @method CarbonImmutable setTestNow($testNow = null) Set a Carbon instance (real or mock) to be returned when a "now" - * instance is created. The provided instance will be returned - * specifically under the following conditions: - * - A call to the static now() method, ex. Carbon::now() - * - When a null (or blank string) is passed to the constructor or parse(), ex. new Carbon(null) - * - When the string "now" is passed to the constructor or parse(), ex. new Carbon('now') - * - When a string containing the desired time is passed to Carbon::parse(). - * Note the timezone parameter was left out of the examples above and - * has no affect as the mock value will be returned regardless of its value. - * To clear the test instance call this method using the default - * parameter of null. - * /!\ Use this method for unit tests only. - * @method void setToStringFormat($format) @deprecated To avoid conflict between different third-party libraries, static setters should not be used. - * You should rather let Carbon object being casted to string with DEFAULT_TO_STRING_FORMAT, and - * use other method or custom format passed to format() method if you need to dump an other string - * format. - * Set the default format used when type juggling a Carbon instance to a string - * @method void setTranslator(\Symfony\Component\Translation\TranslatorInterface $translator) Set the default translator instance to use. - * @method CarbonImmutable setUtf8($utf8) @deprecated To avoid conflict between different third-party libraries, static setters should not be used. - * You should rather use UTF-8 language packages on every machine. - * Set if UTF8 will be used for localized date/time. - * @method void setWeekEndsAt($day) @deprecated To avoid conflict between different third-party libraries, static setters should not be used. - * Use $weekStartsAt optional parameter instead when using startOfWeek, floorWeek, ceilWeek - * or roundWeek method. You can also use the 'first_day_of_week' locale setting to change the - * start of week according to current locale selected and implicitly the end of week. - * Set the last day of week - * @method void setWeekStartsAt($day) @deprecated To avoid conflict between different third-party libraries, static setters should not be used. - * Use $weekEndsAt optional parameter instead when using endOfWeek method. You can also use the - * 'first_day_of_week' locale setting to change the start of week according to current locale - * selected and implicitly the end of week. - * Set the first day of week - * @method void setWeekendDays($days) @deprecated To avoid conflict between different third-party libraries, static setters should not be used. - * You should rather consider week-end is always saturday and sunday, and if you have some custom - * week-end days to handle, give to those days an other name and create a macro for them: - * ``` - * Carbon::macro('isDayOff', function ($date) { - * return $date->isSunday() || $date->isMonday(); - * }); - * Carbon::macro('isNotDayOff', function ($date) { - * return !$date->isDayOff(); - * }); - * if ($someDate->isDayOff()) ... - * if ($someDate->isNotDayOff()) ... - * // Add 5 not-off days - * $count = 5; - * while ($someDate->isDayOff() || ($count-- > 0)) { - * $someDate->addDay(); - * } - * ``` - * Set weekend days - * @method bool shouldOverflowMonths() Get the month overflow global behavior (can be overridden in specific instances). - * @method bool shouldOverflowYears() Get the month overflow global behavior (can be overridden in specific instances). - * @method string singularUnit(string $unit) Returns standardized singular of a given singular/plural unit name (in English). - * @method CarbonImmutable today($tz = null) Create a Carbon instance for today. - * @method CarbonImmutable tomorrow($tz = null) Create a Carbon instance for tomorrow. - * @method string translateTimeString($timeString, $from = null, $to = null, $mode = 15) Translate a time string from a locale to an other. - * @method string translateWith(\Symfony\Component\Translation\TranslatorInterface $translator, string $key, array $parameters = [], $number = null) Translate using translation string or callback available. - * @method void useMonthsOverflow($monthsOverflow = true) @deprecated To avoid conflict between different third-party libraries, static setters should not be used. - * You should rather use the ->settings() method. - * Or you can use method variants: addMonthsWithOverflow/addMonthsNoOverflow, same variants - * are available for quarters, years, decade, centuries, millennia (singular and plural forms). - * @method CarbonImmutable useStrictMode($strictModeEnabled = true) @deprecated To avoid conflict between different third-party libraries, static setters should not be used. - * You should rather use the ->settings() method. - * @method void useYearsOverflow($yearsOverflow = true) @deprecated To avoid conflict between different third-party libraries, static setters should not be used. - * You should rather use the ->settings() method. - * Or you can use method variants: addYearsWithOverflow/addYearsNoOverflow, same variants - * are available for quarters, years, decade, centuries, millennia (singular and plural forms). - * @method CarbonImmutable yesterday($tz = null) Create a Carbon instance for yesterday. + * @method CarbonImmutable create($year = 0, $month = 1, $day = 1, $hour = 0, $minute = 0, $second = 0, $tz = null) Create a new Carbon instance from a specific date and time. + * If any of $year, $month or $day are set to null their now() values will + * be used. + * If $hour is null it will be set to its now() value and the default + * values for $minute and $second will be their now() values. + * If $hour is not null then the default values for $minute and $second + * will be 0. + * @method CarbonImmutable createFromDate($year = null, $month = null, $day = null, $tz = null) Create a Carbon instance from just a date. The time portion is set to now. + * @method CarbonImmutable|false createFromFormat($format, $time, $tz = null) Create a Carbon instance from a specific format. + * @method CarbonImmutable|false createFromIsoFormat($format, $time, $tz = null, $locale = 'en', $translator = null) Create a Carbon instance from a specific ISO format (same replacements as ->isoFormat()). + * @method CarbonImmutable|false createFromLocaleFormat($format, $locale, $time, $tz = null) Create a Carbon instance from a specific format and a string in a given language. + * @method CarbonImmutable|false createFromLocaleIsoFormat($format, $locale, $time, $tz = null) Create a Carbon instance from a specific ISO format and a string in a given language. + * @method CarbonImmutable createFromTime($hour = 0, $minute = 0, $second = 0, $tz = null) Create a Carbon instance from just a time. The date portion is set to today. + * @method CarbonImmutable createFromTimeString($time, $tz = null) Create a Carbon instance from a time string. The date portion is set to today. + * @method CarbonImmutable createFromTimestamp($timestamp, $tz = null) Create a Carbon instance from a timestamp. + * @method CarbonImmutable createFromTimestampMs($timestamp, $tz = null) Create a Carbon instance from a timestamp in milliseconds. + * @method CarbonImmutable createFromTimestampUTC($timestamp) Create a Carbon instance from an UTC timestamp. + * @method CarbonImmutable createMidnightDate($year = null, $month = null, $day = null, $tz = null) Create a Carbon instance from just a date. The time portion is set to midnight. + * @method CarbonImmutable|false createSafe($year = null, $month = null, $day = null, $hour = null, $minute = null, $second = null, $tz = null) Create a new safe Carbon instance from a specific date and time. + * If any of $year, $month or $day are set to null their now() values will + * be used. + * If $hour is null it will be set to its now() value and the default + * values for $minute and $second will be their now() values. + * If $hour is not null then the default values for $minute and $second + * will be 0. + * If one of the set values is not valid, an \InvalidArgumentException + * will be thrown. + * @method CarbonImmutable disableHumanDiffOption($humanDiffOption) @deprecated To avoid conflict between different third-party libraries, static setters should not be used. + * You should rather use the ->settings() method. + * @method CarbonImmutable enableHumanDiffOption($humanDiffOption) @deprecated To avoid conflict between different third-party libraries, static setters should not be used. + * You should rather use the ->settings() method. + * @method mixed executeWithLocale($locale, $func) Set the current locale to the given, execute the passed function, reset the locale to previous one, + * then return the result of the closure (or null if the closure was void). + * @method CarbonImmutable fromSerialized($value) Create an instance from a serialized string. + * @method void genericMacro($macro, $priority = 0) Register a custom macro. + * @method array getAvailableLocales() Returns the list of internally available locales and already loaded custom locales. + * (It will ignore custom translator dynamic loading.) + * @method Language[] getAvailableLocalesInfo() Returns list of Language object for each available locale. This object allow you to get the ISO name, native + * name, region and variant of the locale. + * @method array getDays() Get the days of the week + * @method string|null getFallbackLocale() Get the fallback locale. + * @method array getFormatsToIsoReplacements() List of replacements from date() format to isoFormat(). + * @method int getHumanDiffOptions() Return default humanDiff() options (merged flags as integer). + * @method array getIsoUnits() Returns list of locale units for ISO formatting. + * @method CarbonImmutable getLastErrors() {@inheritdoc} + * @method string getLocale() Get the current translator locale. + * @method callable|null getMacro($name) Get the raw callable macro registered globally for a given name. + * @method int getMidDayAt() get midday/noon hour + * @method Closure|CarbonImmutable getTestNow() Get the Carbon instance (real or mock) to be returned when a "now" + * instance is created. + * @method string getTimeFormatByPrecision($unitPrecision) Return a format from H:i to H:i:s.u according to given unit precision. + * @method string getTranslationMessageWith($translator, string $key, string $locale = null, string $default = null) Returns raw translation message for a given key. + * @method TranslatorInterface getTranslator() Get the default translator instance in use. + * @method int getWeekEndsAt() Get the last day of week + * @method int getWeekStartsAt() Get the first day of week + * @method array getWeekendDays() Get weekend days + * @method bool hasFormat($date, $format) Checks if the (date)time string is in a given format. + * @method bool hasMacro($name) Checks if macro is registered globally. + * @method bool hasRelativeKeywords($time) Determine if a time string will produce a relative date. + * @method bool hasTestNow() Determine if there is a valid test instance set. A valid test instance + * is anything that is not null. + * @method CarbonImmutable instance($date) Create a Carbon instance from a DateTime one. + * @method bool isImmutable() Returns true if the current class/instance is immutable. + * @method bool isModifiableUnit($unit) Returns true if a property can be changed via setter. + * @method bool isMutable() Returns true if the current class/instance is mutable. + * @method bool isStrictModeEnabled() Returns true if the strict mode is globally in use, false else. + * (It can be overridden in specific instances.) + * @method bool localeHasDiffOneDayWords($locale) Returns true if the given locale is internally supported and has words for 1-day diff (just now, yesterday, tomorrow). + * Support is considered enabled if the 3 words are translated in the given locale. + * @method bool localeHasDiffSyntax($locale) Returns true if the given locale is internally supported and has diff syntax support (ago, from now, before, after). + * Support is considered enabled if the 4 sentences are translated in the given locale. + * @method bool localeHasDiffTwoDayWords($locale) Returns true if the given locale is internally supported and has words for 2-days diff (before yesterday, after tomorrow). + * Support is considered enabled if the 2 words are translated in the given locale. + * @method bool localeHasPeriodSyntax($locale) Returns true if the given locale is internally supported and has period syntax support (X times, every X, from X, to X). + * Support is considered enabled if the 4 sentences are translated in the given locale. + * @method bool localeHasShortUnits($locale) Returns true if the given locale is internally supported and has short-units support. + * Support is considered enabled if either year, day or hour has a short variant translated. + * @method void macro($name, $macro) Register a custom macro. + * @method CarbonImmutable|null make($var) Make a Carbon instance from given variable if possible. + * Always return a new instance. Parse only strings and only these likely to be dates (skip intervals + * and recurrences). Throw an exception for invalid format, but otherwise return null. + * @method CarbonImmutable maxValue() Create a Carbon instance for the greatest supported date. + * @method CarbonImmutable minValue() Create a Carbon instance for the lowest supported date. + * @method void mixin($mixin) Mix another object into the class. + * @method CarbonImmutable now($tz = null) Get a Carbon instance for the current date and time. + * @method CarbonImmutable parse($time = null, $tz = null) Create a carbon instance from a string. + * This is an alias for the constructor that allows better fluent syntax + * as it allows you to do Carbon::parse('Monday next week')->fn() rather + * than (new Carbon('Monday next week'))->fn(). + * @method CarbonImmutable parseFromLocale($time, $locale, $tz = null) Create a carbon instance from a localized string (in French, Japanese, Arabic, etc.). + * @method string pluralUnit(string $unit) Returns standardized plural of a given singular/plural unit name (in English). + * @method CarbonImmutable|false rawCreateFromFormat($format, $time, $tz = null) Create a Carbon instance from a specific format. + * @method CarbonImmutable rawParse($time = null, $tz = null) Create a carbon instance from a string. + * This is an alias for the constructor that allows better fluent syntax + * as it allows you to do Carbon::parse('Monday next week')->fn() rather + * than (new Carbon('Monday next week'))->fn(). + * @method CarbonImmutable resetMacros() Remove all macros and generic macros. + * @method void resetMonthsOverflow() @deprecated To avoid conflict between different third-party libraries, static setters should not be used. + * You should rather use the ->settings() method. + * Or you can use method variants: addMonthsWithOverflow/addMonthsNoOverflow, same variants + * are available for quarters, years, decade, centuries, millennia (singular and plural forms). + * @method void resetToStringFormat() Reset the format used to the default when type juggling a Carbon instance to a string + * @method void resetYearsOverflow() @deprecated To avoid conflict between different third-party libraries, static setters should not be used. + * You should rather use the ->settings() method. + * Or you can use method variants: addYearsWithOverflow/addYearsNoOverflow, same variants + * are available for quarters, years, decade, centuries, millennia (singular and plural forms). + * @method void serializeUsing($callback) @deprecated To avoid conflict between different third-party libraries, static setters should not be used. + * You should rather transform Carbon object before the serialization. + * JSON serialize all Carbon instances using the given callback. + * @method CarbonImmutable setFallbackLocale($locale) Set the fallback locale. + * @method CarbonImmutable setHumanDiffOptions($humanDiffOptions) @deprecated To avoid conflict between different third-party libraries, static setters should not be used. + * You should rather use the ->settings() method. + * @method bool setLocale($locale) Set the current translator locale and indicate if the source locale file exists. + * Pass 'auto' as locale to use closest language from the current LC_TIME locale. + * @method void setMidDayAt($hour) @deprecated To avoid conflict between different third-party libraries, static setters should not be used. + * You should rather consider mid-day is always 12pm, then if you need to test if it's an other + * hour, test it explicitly: + * $date->format('G') == 13 + * or to set explicitly to a given hour: + * $date->setTime(13, 0, 0, 0) + * Set midday/noon hour + * @method CarbonImmutable setTestNow($testNow = null) Set a Carbon instance (real or mock) to be returned when a "now" + * instance is created. The provided instance will be returned + * specifically under the following conditions: + * - A call to the static now() method, ex. Carbon::now() + * - When a null (or blank string) is passed to the constructor or parse(), ex. new Carbon(null) + * - When the string "now" is passed to the constructor or parse(), ex. new Carbon('now') + * - When a string containing the desired time is passed to Carbon::parse(). + * Note the timezone parameter was left out of the examples above and + * has no affect as the mock value will be returned regardless of its value. + * To clear the test instance call this method using the default + * parameter of null. + * /!\ Use this method for unit tests only. + * @method void setToStringFormat($format) @deprecated To avoid conflict between different third-party libraries, static setters should not be used. + * You should rather let Carbon object being casted to string with DEFAULT_TO_STRING_FORMAT, and + * use other method or custom format passed to format() method if you need to dump an other string + * format. + * Set the default format used when type juggling a Carbon instance to a string + * @method void setTranslator(\Symfony\Contracts\Translation\TranslatorInterface $translator) Set the default translator instance to use. + * @method CarbonImmutable setUtf8($utf8) @deprecated To avoid conflict between different third-party libraries, static setters should not be used. + * You should rather use UTF-8 language packages on every machine. + * Set if UTF8 will be used for localized date/time. + * @method void setWeekEndsAt($day) @deprecated To avoid conflict between different third-party libraries, static setters should not be used. + * Use $weekStartsAt optional parameter instead when using startOfWeek, floorWeek, ceilWeek + * or roundWeek method. You can also use the 'first_day_of_week' locale setting to change the + * start of week according to current locale selected and implicitly the end of week. + * Set the last day of week + * @method void setWeekStartsAt($day) @deprecated To avoid conflict between different third-party libraries, static setters should not be used. + * Use $weekEndsAt optional parameter instead when using endOfWeek method. You can also use the + * 'first_day_of_week' locale setting to change the start of week according to current locale + * selected and implicitly the end of week. + * Set the first day of week + * @method void setWeekendDays($days) @deprecated To avoid conflict between different third-party libraries, static setters should not be used. + * You should rather consider week-end is always saturday and sunday, and if you have some custom + * week-end days to handle, give to those days an other name and create a macro for them: + * ``` + * Carbon::macro('isDayOff', function ($date) { + * return $date->isSunday() || $date->isMonday(); + * }); + * Carbon::macro('isNotDayOff', function ($date) { + * return !$date->isDayOff(); + * }); + * if ($someDate->isDayOff()) ... + * if ($someDate->isNotDayOff()) ... + * // Add 5 not-off days + * $count = 5; + * while ($someDate->isDayOff() || ($count-- > 0)) { + * $someDate->addDay(); + * } + * ``` + * Set weekend days + * @method bool shouldOverflowMonths() Get the month overflow global behavior (can be overridden in specific instances). + * @method bool shouldOverflowYears() Get the month overflow global behavior (can be overridden in specific instances). + * @method string singularUnit(string $unit) Returns standardized singular of a given singular/plural unit name (in English). + * @method CarbonImmutable today($tz = null) Create a Carbon instance for today. + * @method CarbonImmutable tomorrow($tz = null) Create a Carbon instance for tomorrow. + * @method string translateTimeString($timeString, $from = null, $to = null, $mode = 15) Translate a time string from a locale to an other. + * @method string translateWith(\Symfony\Contracts\Translation\TranslatorInterface $translator, string $key, array $parameters = [], $number = null) Translate using translation string or callback available. + * @method void useMonthsOverflow($monthsOverflow = true) @deprecated To avoid conflict between different third-party libraries, static setters should not be used. + * You should rather use the ->settings() method. + * Or you can use method variants: addMonthsWithOverflow/addMonthsNoOverflow, same variants + * are available for quarters, years, decade, centuries, millennia (singular and plural forms). + * @method CarbonImmutable useStrictMode($strictModeEnabled = true) @deprecated To avoid conflict between different third-party libraries, static setters should not be used. + * You should rather use the ->settings() method. + * @method void useYearsOverflow($yearsOverflow = true) @deprecated To avoid conflict between different third-party libraries, static setters should not be used. + * You should rather use the ->settings() method. + * Or you can use method variants: addYearsWithOverflow/addYearsNoOverflow, same variants + * are available for quarters, years, decade, centuries, millennia (singular and plural forms). + * @method CarbonImmutable yesterday($tz = null) Create a Carbon instance for yesterday. * * */ diff --git a/src/Carbon/Traits/Creator.php b/src/Carbon/Traits/Creator.php index 2655e2d191..cc732c4852 100644 --- a/src/Carbon/Traits/Creator.php +++ b/src/Carbon/Traits/Creator.php @@ -20,6 +20,7 @@ use DateTimeZone; use Exception; use InvalidArgumentException; +use Symfony\Contracts\Translation\TranslatorInterface; /** * Trait Creator. @@ -655,11 +656,11 @@ public static function createFromFormat($format, $time, $tz = null) /** * Create a Carbon instance from a specific ISO format (same replacements as ->isoFormat()). * - * @param string $format Datetime format - * @param string $time - * @param DateTimeZone|string|false|null $tz optional timezone - * @param string|null $locale locale to be used for LTS, LT, LL, LLL, etc. macro-formats (en by fault, unneeded if no such macro-format in use) - * @param \Symfony\Component\Translation\TranslatorInterface $translator optional custom translator to use for macro-formats + * @param string $format Datetime format + * @param string $time + * @param DateTimeZone|string|false|null $tz optional timezone + * @param string|null $locale locale to be used for LTS, LT, LL, LLL, etc. macro-formats (en by fault, unneeded if no such macro-format in use) + * @param TranslatorInterface $translator optional custom translator to use for macro-formats * * @throws InvalidArgumentException * diff --git a/src/Carbon/Traits/Localization.php b/src/Carbon/Traits/Localization.php index 4eb4f4ae8e..a050847701 100644 --- a/src/Carbon/Traits/Localization.php +++ b/src/Carbon/Traits/Localization.php @@ -17,16 +17,8 @@ use Closure; use InvalidArgumentException; use Symfony\Component\Translation\TranslatorBagInterface; -use Symfony\Component\Translation\TranslatorInterface; use Symfony\Contracts\Translation\LocaleAwareInterface; -use Symfony\Contracts\Translation\TranslatorInterface as ContractsTranslatorInterface; - -if (!interface_exists('Symfony\\Component\\Translation\\TranslatorInterface')) { - class_alias( - 'Symfony\\Contracts\\Translation\\TranslatorInterface', - 'Symfony\\Component\\Translation\\TranslatorInterface' - ); -} +use Symfony\Contracts\Translation\TranslatorInterface; /** * Trait Localization. @@ -38,14 +30,14 @@ trait Localization /** * Default translator. * - * @var \Symfony\Component\Translation\TranslatorInterface + * @var TranslatorInterface */ protected static $translator; /** * Specific translator of the current instance. * - * @var \Symfony\Component\Translation\TranslatorInterface + * @var TranslatorInterface */ protected $localTranslator; @@ -105,7 +97,7 @@ public static function getHumanDiffOptions() /** * Initialize the default translator instance if necessary. * - * @return \Symfony\Component\Translation\TranslatorInterface + * @return TranslatorInterface */ protected static function translator() { @@ -119,7 +111,7 @@ protected static function translator() /** * Get the default translator instance in use. * - * @return \Symfony\Component\Translation\TranslatorInterface + * @return TranslatorInterface */ public static function getTranslator() { @@ -129,7 +121,7 @@ public static function getTranslator() /** * Set the default translator instance to use. * - * @param \Symfony\Component\Translation\TranslatorInterface $translator + * @param TranslatorInterface $translator * * @return void */ @@ -151,7 +143,7 @@ public function hasLocalTranslator() /** * Get the translator of the current instance or the default if none set. * - * @return \Symfony\Component\Translation\TranslatorInterface + * @return TranslatorInterface */ public function getLocalTranslator() { @@ -161,7 +153,7 @@ public function getLocalTranslator() /** * Set the translator for the current instance. * - * @param \Symfony\Component\Translation\TranslatorInterface $translator + * @param TranslatorInterface $translator * * @return $this */ @@ -175,10 +167,10 @@ public function setLocalTranslator(TranslatorInterface $translator) /** * Returns raw translation message for a given key. * - * @param \Symfony\Component\Translation\TranslatorInterface $translator the translator to use - * @param string $key key to find - * @param string|null $locale current locale used if null - * @param string|null $default default value if translation returns the key + * @param TranslatorInterface $translator the translator to use + * @param string $key key to find + * @param string|null $locale current locale used if null + * @param string|null $default default value if translation returns the key * * @return string */ @@ -202,10 +194,10 @@ public static function getTranslationMessageWith($translator, string $key, strin /** * Returns raw translation message for a given key. * - * @param string $key key to find - * @param string|null $locale current locale used if null - * @param string|null $default default value if translation returns the key - * @param \Symfony\Component\Translation\TranslatorInterface $translator an optional translator to use + * @param string $key key to find + * @param string|null $locale current locale used if null + * @param string|null $default default value if translation returns the key + * @param TranslatorInterface $translator an optional translator to use * * @return string */ @@ -217,16 +209,17 @@ public function getTranslationMessage(string $key, string $locale = null, string /** * Translate using translation string or callback available. * - * @param \Symfony\Component\Translation\TranslatorInterface $translator - * @param string $key - * @param array $parameters - * @param null $number + * @param TranslatorInterface $translator + * @param string $key + * @param array $parameters + * @param null $number * * @return string */ public static function translateWith(TranslatorInterface $translator, string $key, array $parameters = [], $number = null): string { $message = static::getTranslationMessageWith($translator, $key, null, $key); + if ($message instanceof Closure) { return (string) $message(...array_values($parameters)); } @@ -234,26 +227,21 @@ public static function translateWith(TranslatorInterface $translator, string $ke if ($number !== null) { $parameters['%count%'] = $number; } + if (isset($parameters['%count%'])) { $parameters[':count'] = $parameters['%count%']; } - // @codeCoverageIgnoreStart - $choice = $translator instanceof ContractsTranslatorInterface - ? $translator->trans($key, $parameters) - : $translator->transChoice($key, $number, $parameters); - // @codeCoverageIgnoreEnd - - return (string) $choice; + return (string) $translator->trans($key, $parameters); } /** * Translate using translation string or callback available. * - * @param string $key - * @param array $parameters - * @param null $number - * @param \Symfony\Component\Translation\TranslatorInterface $translator + * @param string $key + * @param array $parameters + * @param null $number + * @param TranslatorInterface $translator * * @return string */ diff --git a/tests/Carbon/LocalizationTest.php b/tests/Carbon/LocalizationTest.php index e4492c98f0..f1be331678 100644 --- a/tests/Carbon/LocalizationTest.php +++ b/tests/Carbon/LocalizationTest.php @@ -13,14 +13,12 @@ use Carbon\Carbon; use Carbon\CarbonInterval; -use Carbon\Exceptions\NotLocaleAwareException; use Carbon\Language; use Carbon\Translator; +use InvalidArgumentException; use Symfony\Component\Translation\IdentityTranslator; use Symfony\Component\Translation\Loader\ArrayLoader; use Symfony\Component\Translation\MessageCatalogue; -use Symfony\Component\Translation\MessageSelector; -use Symfony\Component\Translation\TranslatorInterface; use Tests\AbstractTestCase; use Tests\Carbon\Fixtures\MyCarbon; @@ -692,27 +690,6 @@ public function testGetAvailableLocales() $this->assertSame(['en'], Carbon::getAvailableLocales()); } - public function testNotLocaleAwareException() - { - if (method_exists(TranslatorInterface::class, 'getLocale')) { - $this->markTestSkipped('In Symfony < 5, NotLocaleAwareException will never been thrown.'); - } - - $translator = new class implements TranslatorInterface { - public function trans(string $id, array $parameters = [], string $domain = null, string $locale = null) - { - return 'x'; - } - }; - - Carbon::setTranslator($translator); - - $this->expectException(NotLocaleAwareException::class); - $this->expectExceptionMessage(get_class($translator).' does neither implements Symfony\\Contracts\\Translation\\LocaleAwareInterface nor getLocale() method.'); - - Carbon::now()->locale(); - } - public function testGetAvailableLocalesInfo() { $infos = Carbon::getAvailableLocalesInfo(); @@ -747,17 +724,14 @@ public function testWeekDayMultipleForms() public function testTranslationCustomWithCustomTranslator() { - $this->expectException(\InvalidArgumentException::class); + $this->expectException(InvalidArgumentException::class); $this->expectExceptionMessage( - 'Translator does not implement Symfony\Component\Translation\TranslatorInterface '. + 'Translator does not implement Symfony\Contracts\Translation\TranslatorInterface '. 'and Symfony\Component\Translation\TranslatorBagInterface.' ); $date = Carbon::create(2018, 1, 1, 0, 0, 0); - $date->setLocalTranslator(class_exists(MessageSelector::class) - ? new IdentityTranslator(new MessageSelector()) - : new IdentityTranslator() - ); + $date->setLocalTranslator(new IdentityTranslator()); $date->getTranslationMessage('foo'); } diff --git a/tests/CarbonImmutable/LocalizationTest.php b/tests/CarbonImmutable/LocalizationTest.php index 299c1a45c2..675b15529f 100644 --- a/tests/CarbonImmutable/LocalizationTest.php +++ b/tests/CarbonImmutable/LocalizationTest.php @@ -13,14 +13,11 @@ use Carbon\CarbonImmutable as Carbon; use Carbon\CarbonInterval; -use Carbon\Exceptions\NotLocaleAwareException; use Carbon\Language; use Carbon\Translator; use Symfony\Component\Translation\IdentityTranslator; use Symfony\Component\Translation\Loader\ArrayLoader; use Symfony\Component\Translation\MessageCatalogue; -use Symfony\Component\Translation\MessageSelector; -use Symfony\Component\Translation\TranslatorInterface; use Tests\AbstractTestCase; use Tests\CarbonImmutable\Fixtures\MyCarbon; @@ -661,27 +658,6 @@ public function testGetAvailableLocales() $this->assertSame(['en'], Carbon::getAvailableLocales()); } - public function testNotLocaleAwareException() - { - if (method_exists(TranslatorInterface::class, 'getLocale')) { - $this->markTestSkipped('In Symfony < 5, NotLocaleAwareException will never been thrown.'); - } - - $translator = new class implements TranslatorInterface { - public function trans(string $id, array $parameters = [], string $domain = null, string $locale = null) - { - return 'x'; - } - }; - - Carbon::setTranslator($translator); - - $this->expectException(NotLocaleAwareException::class); - $this->expectExceptionMessage(get_class($translator).' does neither implements Symfony\\Contracts\\Translation\\LocaleAwareInterface nor getLocale() method.'); - - Carbon::now()->locale(); - } - public function testGetAvailableLocalesInfo() { $infos = Carbon::getAvailableLocalesInfo(); @@ -718,15 +694,12 @@ public function testTranslationCustomWithCustomTranslator() { $this->expectException(\InvalidArgumentException::class); $this->expectExceptionMessage( - 'Translator does not implement Symfony\Component\Translation\TranslatorInterface '. + 'Translator does not implement Symfony\Contracts\Translation\TranslatorInterface '. 'and Symfony\Component\Translation\TranslatorBagInterface.' ); $date = Carbon::create(2018, 1, 1, 0, 0, 0); - $date->setLocalTranslator(class_exists(MessageSelector::class) - ? new IdentityTranslator(new MessageSelector()) - : new IdentityTranslator() - ); + $date->setLocalTranslator(new IdentityTranslator()); $date->getTranslationMessage('foo'); } diff --git a/tests/Symfony/Component/Translation/TranslatorInterface.php b/tests/Symfony/Component/Translation/TranslatorInterface.php deleted file mode 100644 index 393beeed37..0000000000 --- a/tests/Symfony/Component/Translation/TranslatorInterface.php +++ /dev/null @@ -1,21 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ -namespace Symfony\Component\Translation; - -use Symfony\Contracts\Translation\TranslatorInterface as NewTranslatorInterface; - -/** - * @internal Fix the IDE auto-completion. - */ -interface TranslatorInterface extends NewTranslatorInterface -{ - // This is a polyfill for Symfony < 5 compatibility. -} From 4015dac0502371a946ab56ec73b57fbb733f059b Mon Sep 17 00:00:00 2001 From: KyleKatarn Date: Wed, 6 May 2020 23:38:53 +0200 Subject: [PATCH 03/38] #2070 [Travis] Drop PHP < 7.2.5 --- .travis.yml | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/.travis.yml b/.travis.yml index 33eaaed67a..ff9bcd1a0d 100644 --- a/.travis.yml +++ b/.travis.yml @@ -64,15 +64,7 @@ branches: matrix: include: - - php: 7.1.8 - - php: 7.1.28 - - php: 7.1 - - php: 7.1 - env: setup=lowest - - php: 7.1 - env: setup=stable - - - php: 7.2.0 + - php: 7.2.5 - php: 7.2.15 - php: 7.2 - php: 7.2 From 3f9ca81604e57a8642c73f3afb484137d5a61f4f Mon Sep 17 00:00:00 2001 From: KyleKatarn Date: Thu, 7 May 2020 00:02:48 +0200 Subject: [PATCH 04/38] #2070 Fix CarbonInterface and CarbonInterval typing --- phpstan.neon | 3 --- src/Carbon/CarbonInterface.php | 1 + src/Carbon/CarbonInterval.php | 5 +++-- 3 files changed, 4 insertions(+), 5 deletions(-) diff --git a/phpstan.neon b/phpstan.neon index f252dbd314..ae862e1fb5 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -6,9 +6,6 @@ parameters: - '#^Using \$this in static method Carbon\\Carbon(Interface|Immutable|Interval|Period)?::loadMixinTrait\(\)\.$#' - '#^Variable \$this in isset\(\) always exists and is not nullable\.$#' - '#^Call to an undefined method Carbon\\Carbon(Interface|Immutable)?::(super|noThis)\(\)\.$#' - - '#Class Symfony\\Component\\Translation\\MessageSelector not found\.#' - - '#Class Symfony\\Component\\Translation\\IdentityTranslator does not have a constructor and must be instantiated without any parameters\.#' - - '#Instantiated class Symfony\\Component\\Translation\\MessageSelector not found\.#' - '#Call to static method set\(\) on an unknown class Symfony\\Component\\Translation\\PluralizationRules\.#' - '#Call to an undefined method DateTimeInterface::setTimezone\(\)\.#' excludes_analyse: diff --git a/src/Carbon/CarbonInterface.php b/src/Carbon/CarbonInterface.php index 6e3589a011..0333817790 100644 --- a/src/Carbon/CarbonInterface.php +++ b/src/Carbon/CarbonInterface.php @@ -19,6 +19,7 @@ use InvalidArgumentException; use JsonSerializable; use ReflectionException; +use Symfony\Contracts\Translation\TranslatorInterface; /** * Common interface for Carbon and CarbonImmutable. diff --git a/src/Carbon/CarbonInterval.php b/src/Carbon/CarbonInterval.php index 6545810ab7..c6593f7bab 100644 --- a/src/Carbon/CarbonInterval.php +++ b/src/Carbon/CarbonInterval.php @@ -18,6 +18,7 @@ use DateInterval; use Exception; use InvalidArgumentException; +use Symfony\Contracts\Translation\TranslatorInterface; /** * A simple API extension for DateInterval. @@ -185,7 +186,7 @@ class CarbonInterval extends DateInterval /** * A translator to ... er ... translate stuff * - * @var \Symfony\Component\Translation\TranslatorInterface + * @var TranslatorInterface */ protected static $translator; @@ -1522,7 +1523,7 @@ public function forHumans($syntax = null, $short = false, $parts = -1, $options $isFuture = $this->invert === 1; $transId = $relativeToNow ? ($isFuture ? 'from_now' : 'ago') : ($isFuture ? 'after' : 'before'); - /** @var \Symfony\Component\Translation\Translator $translator */ + /** @var Translator $translator */ $translator = $this->getLocalTranslator(); $handleDeclensions = function ($unit, $count) use ($interpolations, $transId, $translator, $altNumbers, $absolute) { From c5ed301afc4c4ee8d13d6ea4c399fd3ea9ec23b1 Mon Sep 17 00:00:00 2001 From: KyleKatarn Date: Thu, 7 May 2020 00:52:48 +0200 Subject: [PATCH 05/38] #2035 Disable NO_ZERO_DIFF option by default --- src/Carbon/Traits/Difference.php | 1 - src/Carbon/Traits/Localization.php | 2 +- tests/Carbon/DiffTest.php | 8 ++++---- tests/CarbonImmutable/DiffTest.php | 8 ++++---- tests/CommonTraits/MacroContextNestingTest.php | 2 +- 5 files changed, 10 insertions(+), 11 deletions(-) diff --git a/src/Carbon/Traits/Difference.php b/src/Carbon/Traits/Difference.php index a3db605141..28a2f35711 100644 --- a/src/Carbon/Traits/Difference.php +++ b/src/Carbon/Traits/Difference.php @@ -1051,7 +1051,6 @@ public function timespan($other = null, $timezone = null) return $this->diffForHumans($other, [ 'join' => ', ', 'syntax' => CarbonInterface::DIFF_ABSOLUTE, - 'options' => CarbonInterface::NO_ZERO_DIFF, 'parts' => -1, ]); } diff --git a/src/Carbon/Traits/Localization.php b/src/Carbon/Traits/Localization.php index a050847701..1080b29d85 100644 --- a/src/Carbon/Traits/Localization.php +++ b/src/Carbon/Traits/Localization.php @@ -46,7 +46,7 @@ trait Localization * * @var int */ - protected static $humanDiffOptions = CarbonInterface::NO_ZERO_DIFF; + protected static $humanDiffOptions = 0; /** * @deprecated To avoid conflict between different third-party libraries, static setters should not be used. diff --git a/tests/Carbon/DiffTest.php b/tests/Carbon/DiffTest.php index 56b8412aa5..09aee94170 100644 --- a/tests/Carbon/DiffTest.php +++ b/tests/Carbon/DiffTest.php @@ -544,7 +544,7 @@ public function testDiffInSecondsWithTimezonesAndVsDefault() public function testDiffForHumansNowAndSecond() { $this->wrapWithTestNow(function () { - $this->assertSame('1 second ago', Carbon::now()->diffForHumans()); + $this->assertSame('0 seconds ago', Carbon::now()->diffForHumans()); }); } @@ -553,7 +553,7 @@ public function testDiffForHumansNowAndSecondWithTimezone() $vanNow = Carbon::now('America/Vancouver'); $hereNow = $vanNow->copy()->setTimezone(Carbon::now()->tz); $this->wrapWithTestNow(function () use ($vanNow) { - $this->assertSame('1 second ago', $vanNow->diffForHumans()); + $this->assertSame('0 seconds ago', $vanNow->diffForHumans()); }, $hereNow); } @@ -1308,10 +1308,10 @@ public function testDiffOptions() $this->assertSame(8, Carbon::TWO_DAY_WORDS); $options = Carbon::getHumanDiffOptions(); - $this->assertSame(1, $options); + $this->assertSame(0, $options); $date = Carbon::create(2018, 3, 12, 2, 5, 6, 'UTC'); - $this->assertSame('1 second before', $date->diffForHumans($date)); + $this->assertSame('0 seconds before', $date->diffForHumans($date)); Carbon::setHumanDiffOptions(0); $this->assertSame(0, Carbon::getHumanDiffOptions()); diff --git a/tests/CarbonImmutable/DiffTest.php b/tests/CarbonImmutable/DiffTest.php index c8ab59ffa1..11c74fb01d 100644 --- a/tests/CarbonImmutable/DiffTest.php +++ b/tests/CarbonImmutable/DiffTest.php @@ -512,7 +512,7 @@ public function testDiffInSecondsWithTimezonesAndVsDefault() public function testDiffForHumansNowAndSecond() { $this->wrapWithTestNow(function () { - $this->assertSame('1 second ago', Carbon::now()->diffForHumans()); + $this->assertSame('0 seconds ago', Carbon::now()->diffForHumans()); }); } @@ -521,7 +521,7 @@ public function testDiffForHumansNowAndSecondWithTimezone() $vanNow = Carbon::now('America/Vancouver'); $hereNow = $vanNow->copy()->setTimezone(Carbon::now()->tz); $this->wrapWithTestNow(function () use ($vanNow) { - $this->assertSame('1 second ago', $vanNow->diffForHumans()); + $this->assertSame('0 seconds ago', $vanNow->diffForHumans()); }, $hereNow); } @@ -1236,10 +1236,10 @@ public function testDiffOptions() $this->assertSame(16, Carbon::SEQUENTIAL_PARTS_ONLY); $options = Carbon::getHumanDiffOptions(); - $this->assertSame(1, $options); + $this->assertSame(0, $options); $date = Carbon::create(2018, 3, 12, 2, 5, 6, 'UTC'); - $this->assertSame('1 second before', $date->diffForHumans($date)); + $this->assertSame('0 seconds before', $date->diffForHumans($date)); Carbon::setHumanDiffOptions(0); $this->assertSame(0, Carbon::getHumanDiffOptions()); diff --git a/tests/CommonTraits/MacroContextNestingTest.php b/tests/CommonTraits/MacroContextNestingTest.php index ae3e6f8ac4..7c47a1a5ac 100644 --- a/tests/CommonTraits/MacroContextNestingTest.php +++ b/tests/CommonTraits/MacroContextNestingTest.php @@ -24,7 +24,7 @@ public function getMacroableClasses() return [ [Carbon::class, Carbon::parse('2010-05-23'), null], [CarbonImmutable::class, CarbonImmutable::parse('2010-05-23'), null], - [CarbonInterval::class, CarbonInterval::make('P1M6D'), strval(CarbonInterval::second())], + [CarbonInterval::class, CarbonInterval::make('P1M6D'), strval(CarbonInterval::seconds(0))], [CarbonPeriod::class, CarbonPeriod::create('2010-08-23', '2010-10-02'), null], ]; } From 145da9f6e7688a91c0f491e918c6c92cdcf2ad78 Mon Sep 17 00:00:00 2001 From: KyleKatarn Date: Thu, 7 May 2020 01:09:07 +0200 Subject: [PATCH 06/38] #2079 Create empty interval by default --- src/Carbon/CarbonInterval.php | 4 ++-- tests/CarbonInterval/ConstructTest.php | 2 +- tests/CarbonInterval/SpecTest.php | 8 +++++++- 3 files changed, 10 insertions(+), 4 deletions(-) diff --git a/src/Carbon/CarbonInterval.php b/src/Carbon/CarbonInterval.php index c6593f7bab..3ffbc61914 100644 --- a/src/Carbon/CarbonInterval.php +++ b/src/Carbon/CarbonInterval.php @@ -320,7 +320,7 @@ public static function setCascadeFactors(array $cascadeFactors) * * @throws Exception when the interval_spec (passed as $years) cannot be parsed as an interval. */ - public function __construct($years = 1, $months = null, $weeks = null, $days = null, $hours = null, $minutes = null, $seconds = null, $microseconds = null) + public function __construct($years = null, $months = null, $weeks = null, $days = null, $hours = null, $minutes = null, $seconds = null, $microseconds = null) { if ($years instanceof DateInterval) { parent::__construct(static::getDateIntervalSpec($years)); @@ -466,7 +466,7 @@ public static function getMicrosecondsPerMillisecond() * * @return static */ - public static function create($years = 1, $months = null, $weeks = null, $days = null, $hours = null, $minutes = null, $seconds = null, $microseconds = null) + public static function create($years = null, $months = null, $weeks = null, $days = null, $hours = null, $minutes = null, $seconds = null, $microseconds = null) { return new static($years, $months, $weeks, $days, $hours, $minutes, $seconds, $microseconds); } diff --git a/tests/CarbonInterval/ConstructTest.php b/tests/CarbonInterval/ConstructTest.php index b2d8de5367..c9c3104e59 100644 --- a/tests/CarbonInterval/ConstructTest.php +++ b/tests/CarbonInterval/ConstructTest.php @@ -48,7 +48,7 @@ public function testDefaults() { $ci = new CarbonInterval(); $this->assertInstanceOfCarbonInterval($ci); - $this->assertCarbonInterval($ci, 1, 0, 0, 0, 0, 0); + $this->assertCarbonInterval($ci, 0, 0, 0, 0, 0, 0); } public function testNulls() diff --git a/tests/CarbonInterval/SpecTest.php b/tests/CarbonInterval/SpecTest.php index 945fc14185..7427ae2a63 100644 --- a/tests/CarbonInterval/SpecTest.php +++ b/tests/CarbonInterval/SpecTest.php @@ -21,11 +21,17 @@ public function testZeroInterval() { $ci = new CarbonInterval(0, 0, 0, 0, 0, 0, 0); $this->assertSame('PT0S', $ci->spec()); + $ci = new CarbonInterval(); + $this->assertSame('PT0S', $ci->spec()); + $ci = CarbonInterval::create(); + $this->assertSame('PT0S', $ci->spec()); } public function testYearInterval() { - $ci = new CarbonInterval(); + $ci = new CarbonInterval(1); + $this->assertSame('P1Y', $ci->spec()); + $ci = CarbonInterval::create(1); $this->assertSame('P1Y', $ci->spec()); } From 06aac08be9bd5fb6d328dfdf8a2eb0a2801c7172 Mon Sep 17 00:00:00 2001 From: KyleKatarn Date: Fri, 8 May 2020 00:13:18 +0200 Subject: [PATCH 07/38] Fix variable name typo --- src/Carbon/CarbonPeriod.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Carbon/CarbonPeriod.php b/src/Carbon/CarbonPeriod.php index f57c668732..6ff51d3e61 100644 --- a/src/Carbon/CarbonPeriod.php +++ b/src/Carbon/CarbonPeriod.php @@ -621,13 +621,13 @@ public function __construct(...$arguments) // Parse and assign arguments one by one. First argument may be an ISO 8601 spec, // which will be first parsed into parts and then processed the same way. - $agumentsCount = count($arguments); + $argumentsCount = count($arguments); - if ($agumentsCount && static::isIso8601($iso = $arguments[0])) { + if ($argumentsCount && static::isIso8601($iso = $arguments[0])) { array_splice($arguments, 0, 1, static::parseIso8601($iso)); } - if ($agumentsCount === 1) { + if ($argumentsCount === 1) { if ($arguments[0] instanceof DatePeriod) { $arguments = [ $arguments[0]->start, From 223d3ba906c31f80676ff9b279167074c7f66300 Mon Sep 17 00:00:00 2001 From: KyleKatarn Date: Wed, 13 May 2020 22:44:31 +0200 Subject: [PATCH 08/38] Fix imports order --- src/Carbon/CarbonInterval.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Carbon/CarbonInterval.php b/src/Carbon/CarbonInterval.php index 589e5bb132..e3a339d716 100644 --- a/src/Carbon/CarbonInterval.php +++ b/src/Carbon/CarbonInterval.php @@ -26,8 +26,8 @@ use DateInterval; use Exception; use ReflectionException; -use Throwable; use Symfony\Contracts\Translation\TranslatorInterface; +use Throwable; /** * A simple API extension for DateInterval. From 75e192fa85479e0852792e18e2359a3777df5598 Mon Sep 17 00:00:00 2001 From: KyleKatarn Date: Sat, 23 May 2020 23:47:25 +0200 Subject: [PATCH 09/38] Regenerate PHPDoc --- src/Carbon/Factory.php | 378 ++++++++++++++++---------------- src/Carbon/FactoryImmutable.php | 378 ++++++++++++++++---------------- 2 files changed, 378 insertions(+), 378 deletions(-) diff --git a/src/Carbon/Factory.php b/src/Carbon/Factory.php index 5cc699cf60..9a22e4eb78 100644 --- a/src/Carbon/Factory.php +++ b/src/Carbon/Factory.php @@ -17,195 +17,195 @@ * * * - * @method Carbon create($year = 0, $month = 1, $day = 1, $hour = 0, $minute = 0, $second = 0, $tz = null) Create a new Carbon instance from a specific date and time. - * If any of $year, $month or $day are set to null their now() values will - * be used. - * If $hour is null it will be set to its now() value and the default - * values for $minute and $second will be their now() values. - * If $hour is not null then the default values for $minute and $second - * will be 0. - * @method Carbon createFromDate($year = null, $month = null, $day = null, $tz = null) Create a Carbon instance from just a date. The time portion is set to now. - * @method Carbon|false createFromFormat($format, $time, $tz = null) Create a Carbon instance from a specific format. - * @method Carbon|false createFromIsoFormat($format, $time, $tz = null, $locale = 'en', $translator = null) Create a Carbon instance from a specific ISO format (same replacements as ->isoFormat()). - * @method Carbon|false createFromLocaleFormat($format, $locale, $time, $tz = null) Create a Carbon instance from a specific format and a string in a given language. - * @method Carbon|false createFromLocaleIsoFormat($format, $locale, $time, $tz = null) Create a Carbon instance from a specific ISO format and a string in a given language. - * @method Carbon createFromTime($hour = 0, $minute = 0, $second = 0, $tz = null) Create a Carbon instance from just a time. The date portion is set to today. - * @method Carbon createFromTimeString($time, $tz = null) Create a Carbon instance from a time string. The date portion is set to today. - * @method Carbon createFromTimestamp($timestamp, $tz = null) Create a Carbon instance from a timestamp. - * @method Carbon createFromTimestampMs($timestamp, $tz = null) Create a Carbon instance from a timestamp in milliseconds. - * @method Carbon createFromTimestampUTC($timestamp) Create a Carbon instance from an UTC timestamp. - * @method Carbon createMidnightDate($year = null, $month = null, $day = null, $tz = null) Create a Carbon instance from just a date. The time portion is set to midnight. - * @method Carbon|false createSafe($year = null, $month = null, $day = null, $hour = null, $minute = null, $second = null, $tz = null) Create a new safe Carbon instance from a specific date and time. - * If any of $year, $month or $day are set to null their now() values will - * be used. - * If $hour is null it will be set to its now() value and the default - * values for $minute and $second will be their now() values. - * If $hour is not null then the default values for $minute and $second - * will be 0. - * If one of the set values is not valid, an InvalidDateException - * will be thrown. - * @method Carbon disableHumanDiffOption($humanDiffOption) @deprecated To avoid conflict between different third-party libraries, static setters should not be used. - * You should rather use the ->settings() method. - * @method Carbon enableHumanDiffOption($humanDiffOption) @deprecated To avoid conflict between different third-party libraries, static setters should not be used. - * You should rather use the ->settings() method. - * @method mixed executeWithLocale($locale, $func) Set the current locale to the given, execute the passed function, reset the locale to previous one, - * then return the result of the closure (or null if the closure was void). - * @method Carbon fromSerialized($value) Create an instance from a serialized string. - * @method void genericMacro($macro, $priority = 0) Register a custom macro. - * @method array getAvailableLocales() Returns the list of internally available locales and already loaded custom locales. - * (It will ignore custom translator dynamic loading.) - * @method Language[] getAvailableLocalesInfo() Returns list of Language object for each available locale. This object allow you to get the ISO name, native - * name, region and variant of the locale. - * @method array getDays() Get the days of the week - * @method string|null getFallbackLocale() Get the fallback locale. - * @method array getFormatsToIsoReplacements() List of replacements from date() format to isoFormat(). - * @method int getHumanDiffOptions() Return default humanDiff() options (merged flags as integer). - * @method array getIsoUnits() Returns list of locale units for ISO formatting. - * @method Carbon getLastErrors() {@inheritdoc} - * @method string getLocale() Get the current translator locale. - * @method callable|null getMacro($name) Get the raw callable macro registered globally for a given name. - * @method int getMidDayAt() get midday/noon hour - * @method Closure|Carbon getTestNow() Get the Carbon instance (real or mock) to be returned when a "now" - * instance is created. - * @method string getTimeFormatByPrecision($unitPrecision) Return a format from H:i to H:i:s.u according to given unit precision. - * @method string getTranslationMessageWith($translator, string $key, string $locale = null, string $default = null) Returns raw translation message for a given key. - * @method \Symfony\Component\Translation\TranslatorInterface getTranslator() Get the default translator instance in use. - * @method int getWeekEndsAt() Get the last day of week - * @method int getWeekStartsAt() Get the first day of week - * @method array getWeekendDays() Get weekend days - * @method bool hasFormat($date, $format) Checks if the (date)time string is in a given format. - * @method bool hasMacro($name) Checks if macro is registered globally. - * @method bool hasRelativeKeywords($time) Determine if a time string will produce a relative date. - * @method bool hasTestNow() Determine if there is a valid test instance set. A valid test instance - * is anything that is not null. - * @method Carbon instance($date) Create a Carbon instance from a DateTime one. - * @method bool isImmutable() Returns true if the current class/instance is immutable. - * @method bool isModifiableUnit($unit) Returns true if a property can be changed via setter. - * @method bool isMutable() Returns true if the current class/instance is mutable. - * @method bool isStrictModeEnabled() Returns true if the strict mode is globally in use, false else. - * (It can be overridden in specific instances.) - * @method bool localeHasDiffOneDayWords($locale) Returns true if the given locale is internally supported and has words for 1-day diff (just now, yesterday, tomorrow). - * Support is considered enabled if the 3 words are translated in the given locale. - * @method bool localeHasDiffSyntax($locale) Returns true if the given locale is internally supported and has diff syntax support (ago, from now, before, after). - * Support is considered enabled if the 4 sentences are translated in the given locale. - * @method bool localeHasDiffTwoDayWords($locale) Returns true if the given locale is internally supported and has words for 2-days diff (before yesterday, after tomorrow). - * Support is considered enabled if the 2 words are translated in the given locale. - * @method bool localeHasPeriodSyntax($locale) Returns true if the given locale is internally supported and has period syntax support (X times, every X, from X, to X). - * Support is considered enabled if the 4 sentences are translated in the given locale. - * @method bool localeHasShortUnits($locale) Returns true if the given locale is internally supported and has short-units support. - * Support is considered enabled if either year, day or hour has a short variant translated. - * @method void macro($name, $macro) Register a custom macro. - * @method Carbon|null make($var) Make a Carbon instance from given variable if possible. - * Always return a new instance. Parse only strings and only these likely to be dates (skip intervals - * and recurrences). Throw an exception for invalid format, but otherwise return null. - * @method Carbon maxValue() Create a Carbon instance for the greatest supported date. - * @method Carbon minValue() Create a Carbon instance for the lowest supported date. - * @method void mixin($mixin) Mix another object into the class. - * @method Carbon now($tz = null) Get a Carbon instance for the current date and time. - * @method Carbon parse($time = null, $tz = null) Create a carbon instance from a string. - * This is an alias for the constructor that allows better fluent syntax - * as it allows you to do Carbon::parse('Monday next week')->fn() rather - * than (new Carbon('Monday next week'))->fn(). - * @method Carbon parseFromLocale($time, $locale, $tz = null) Create a carbon instance from a localized string (in French, Japanese, Arabic, etc.). - * @method string pluralUnit(string $unit) Returns standardized plural of a given singular/plural unit name (in English). - * @method Carbon|false rawCreateFromFormat($format, $time, $tz = null) Create a Carbon instance from a specific format. - * @method Carbon rawParse($time = null, $tz = null) Create a carbon instance from a string. - * This is an alias for the constructor that allows better fluent syntax - * as it allows you to do Carbon::parse('Monday next week')->fn() rather - * than (new Carbon('Monday next week'))->fn(). - * @method Carbon resetMacros() Remove all macros and generic macros. - * @method void resetMonthsOverflow() @deprecated To avoid conflict between different third-party libraries, static setters should not be used. - * You should rather use the ->settings() method. - * Or you can use method variants: addMonthsWithOverflow/addMonthsNoOverflow, same variants - * are available for quarters, years, decade, centuries, millennia (singular and plural forms). - * @method void resetToStringFormat() Reset the format used to the default when type juggling a Carbon instance to a string - * @method void resetYearsOverflow() @deprecated To avoid conflict between different third-party libraries, static setters should not be used. - * You should rather use the ->settings() method. - * Or you can use method variants: addYearsWithOverflow/addYearsNoOverflow, same variants - * are available for quarters, years, decade, centuries, millennia (singular and plural forms). - * @method void serializeUsing($callback) @deprecated To avoid conflict between different third-party libraries, static setters should not be used. - * You should rather transform Carbon object before the serialization. - * JSON serialize all Carbon instances using the given callback. - * @method Carbon setFallbackLocale($locale) Set the fallback locale. - * @method Carbon setHumanDiffOptions($humanDiffOptions) @deprecated To avoid conflict between different third-party libraries, static setters should not be used. - * You should rather use the ->settings() method. - * @method bool setLocale($locale) Set the current translator locale and indicate if the source locale file exists. - * Pass 'auto' as locale to use closest language from the current LC_TIME locale. - * @method void setMidDayAt($hour) @deprecated To avoid conflict between different third-party libraries, static setters should not be used. - * You should rather consider mid-day is always 12pm, then if you need to test if it's an other - * hour, test it explicitly: - * $date->format('G') == 13 - * or to set explicitly to a given hour: - * $date->setTime(13, 0, 0, 0) - * Set midday/noon hour - * @method Carbon setTestNow($testNow = null) Set a Carbon instance (real or mock) to be returned when a "now" - * instance is created. The provided instance will be returned - * specifically under the following conditions: - * - A call to the static now() method, ex. Carbon::now() - * - When a null (or blank string) is passed to the constructor or parse(), ex. new Carbon(null) - * - When the string "now" is passed to the constructor or parse(), ex. new Carbon('now') - * - When a string containing the desired time is passed to Carbon::parse(). - * Note the timezone parameter was left out of the examples above and - * has no affect as the mock value will be returned regardless of its value. - * To clear the test instance call this method using the default - * parameter of null. - * /!\ Use this method for unit tests only. - * @method void setToStringFormat($format) @deprecated To avoid conflict between different third-party libraries, static setters should not be used. - * You should rather let Carbon object being casted to string with DEFAULT_TO_STRING_FORMAT, and - * use other method or custom format passed to format() method if you need to dump an other string - * format. - * Set the default format used when type juggling a Carbon instance to a string - * @method void setTranslator(\Symfony\Component\Translation\TranslatorInterface $translator) Set the default translator instance to use. - * @method Carbon setUtf8($utf8) @deprecated To avoid conflict between different third-party libraries, static setters should not be used. - * You should rather use UTF-8 language packages on every machine. - * Set if UTF8 will be used for localized date/time. - * @method void setWeekEndsAt($day) @deprecated To avoid conflict between different third-party libraries, static setters should not be used. - * Use $weekStartsAt optional parameter instead when using startOfWeek, floorWeek, ceilWeek - * or roundWeek method. You can also use the 'first_day_of_week' locale setting to change the - * start of week according to current locale selected and implicitly the end of week. - * Set the last day of week - * @method void setWeekStartsAt($day) @deprecated To avoid conflict between different third-party libraries, static setters should not be used. - * Use $weekEndsAt optional parameter instead when using endOfWeek method. You can also use the - * 'first_day_of_week' locale setting to change the start of week according to current locale - * selected and implicitly the end of week. - * Set the first day of week - * @method void setWeekendDays($days) @deprecated To avoid conflict between different third-party libraries, static setters should not be used. - * You should rather consider week-end is always saturday and sunday, and if you have some custom - * week-end days to handle, give to those days an other name and create a macro for them: - * ``` - * Carbon::macro('isDayOff', function ($date) { - * return $date->isSunday() || $date->isMonday(); - * }); - * Carbon::macro('isNotDayOff', function ($date) { - * return !$date->isDayOff(); - * }); - * if ($someDate->isDayOff()) ... - * if ($someDate->isNotDayOff()) ... - * // Add 5 not-off days - * $count = 5; - * while ($someDate->isDayOff() || ($count-- > 0)) { - * $someDate->addDay(); - * } - * ``` - * Set weekend days - * @method bool shouldOverflowMonths() Get the month overflow global behavior (can be overridden in specific instances). - * @method bool shouldOverflowYears() Get the month overflow global behavior (can be overridden in specific instances). - * @method string singularUnit(string $unit) Returns standardized singular of a given singular/plural unit name (in English). - * @method Carbon today($tz = null) Create a Carbon instance for today. - * @method Carbon tomorrow($tz = null) Create a Carbon instance for tomorrow. - * @method string translateTimeString($timeString, $from = null, $to = null, $mode = 15) Translate a time string from a locale to an other. - * @method string translateWith(\Symfony\Component\Translation\TranslatorInterface $translator, string $key, array $parameters = [], $number = null) Translate using translation string or callback available. - * @method void useMonthsOverflow($monthsOverflow = true) @deprecated To avoid conflict between different third-party libraries, static setters should not be used. - * You should rather use the ->settings() method. - * Or you can use method variants: addMonthsWithOverflow/addMonthsNoOverflow, same variants - * are available for quarters, years, decade, centuries, millennia (singular and plural forms). - * @method Carbon useStrictMode($strictModeEnabled = true) @deprecated To avoid conflict between different third-party libraries, static setters should not be used. - * You should rather use the ->settings() method. - * @method void useYearsOverflow($yearsOverflow = true) @deprecated To avoid conflict between different third-party libraries, static setters should not be used. - * You should rather use the ->settings() method. - * Or you can use method variants: addYearsWithOverflow/addYearsNoOverflow, same variants - * are available for quarters, years, decade, centuries, millennia (singular and plural forms). - * @method Carbon yesterday($tz = null) Create a Carbon instance for yesterday. + * @method Carbon create($year = 0, $month = 1, $day = 1, $hour = 0, $minute = 0, $second = 0, $tz = null) Create a new Carbon instance from a specific date and time. + * If any of $year, $month or $day are set to null their now() values will + * be used. + * If $hour is null it will be set to its now() value and the default + * values for $minute and $second will be their now() values. + * If $hour is not null then the default values for $minute and $second + * will be 0. + * @method Carbon createFromDate($year = null, $month = null, $day = null, $tz = null) Create a Carbon instance from just a date. The time portion is set to now. + * @method Carbon|false createFromFormat($format, $time, $tz = null) Create a Carbon instance from a specific format. + * @method Carbon|false createFromIsoFormat($format, $time, $tz = null, $locale = 'en', $translator = null) Create a Carbon instance from a specific ISO format (same replacements as ->isoFormat()). + * @method Carbon|false createFromLocaleFormat($format, $locale, $time, $tz = null) Create a Carbon instance from a specific format and a string in a given language. + * @method Carbon|false createFromLocaleIsoFormat($format, $locale, $time, $tz = null) Create a Carbon instance from a specific ISO format and a string in a given language. + * @method Carbon createFromTime($hour = 0, $minute = 0, $second = 0, $tz = null) Create a Carbon instance from just a time. The date portion is set to today. + * @method Carbon createFromTimeString($time, $tz = null) Create a Carbon instance from a time string. The date portion is set to today. + * @method Carbon createFromTimestamp($timestamp, $tz = null) Create a Carbon instance from a timestamp. + * @method Carbon createFromTimestampMs($timestamp, $tz = null) Create a Carbon instance from a timestamp in milliseconds. + * @method Carbon createFromTimestampUTC($timestamp) Create a Carbon instance from an UTC timestamp. + * @method Carbon createMidnightDate($year = null, $month = null, $day = null, $tz = null) Create a Carbon instance from just a date. The time portion is set to midnight. + * @method Carbon|false createSafe($year = null, $month = null, $day = null, $hour = null, $minute = null, $second = null, $tz = null) Create a new safe Carbon instance from a specific date and time. + * If any of $year, $month or $day are set to null their now() values will + * be used. + * If $hour is null it will be set to its now() value and the default + * values for $minute and $second will be their now() values. + * If $hour is not null then the default values for $minute and $second + * will be 0. + * If one of the set values is not valid, an InvalidDateException + * will be thrown. + * @method Carbon disableHumanDiffOption($humanDiffOption) @deprecated To avoid conflict between different third-party libraries, static setters should not be used. + * You should rather use the ->settings() method. + * @method Carbon enableHumanDiffOption($humanDiffOption) @deprecated To avoid conflict between different third-party libraries, static setters should not be used. + * You should rather use the ->settings() method. + * @method mixed executeWithLocale($locale, $func) Set the current locale to the given, execute the passed function, reset the locale to previous one, + * then return the result of the closure (or null if the closure was void). + * @method Carbon fromSerialized($value) Create an instance from a serialized string. + * @method void genericMacro($macro, $priority = 0) Register a custom macro. + * @method array getAvailableLocales() Returns the list of internally available locales and already loaded custom locales. + * (It will ignore custom translator dynamic loading.) + * @method Language[] getAvailableLocalesInfo() Returns list of Language object for each available locale. This object allow you to get the ISO name, native + * name, region and variant of the locale. + * @method array getDays() Get the days of the week + * @method string|null getFallbackLocale() Get the fallback locale. + * @method array getFormatsToIsoReplacements() List of replacements from date() format to isoFormat(). + * @method int getHumanDiffOptions() Return default humanDiff() options (merged flags as integer). + * @method array getIsoUnits() Returns list of locale units for ISO formatting. + * @method Carbon getLastErrors() {@inheritdoc} + * @method string getLocale() Get the current translator locale. + * @method callable|null getMacro($name) Get the raw callable macro registered globally for a given name. + * @method int getMidDayAt() get midday/noon hour + * @method Closure|Carbon getTestNow() Get the Carbon instance (real or mock) to be returned when a "now" + * instance is created. + * @method string getTimeFormatByPrecision($unitPrecision) Return a format from H:i to H:i:s.u according to given unit precision. + * @method string getTranslationMessageWith($translator, string $key, string $locale = null, string $default = null) Returns raw translation message for a given key. + * @method TranslatorInterface getTranslator() Get the default translator instance in use. + * @method int getWeekEndsAt() Get the last day of week + * @method int getWeekStartsAt() Get the first day of week + * @method array getWeekendDays() Get weekend days + * @method bool hasFormat($date, $format) Checks if the (date)time string is in a given format. + * @method bool hasMacro($name) Checks if macro is registered globally. + * @method bool hasRelativeKeywords($time) Determine if a time string will produce a relative date. + * @method bool hasTestNow() Determine if there is a valid test instance set. A valid test instance + * is anything that is not null. + * @method Carbon instance($date) Create a Carbon instance from a DateTime one. + * @method bool isImmutable() Returns true if the current class/instance is immutable. + * @method bool isModifiableUnit($unit) Returns true if a property can be changed via setter. + * @method bool isMutable() Returns true if the current class/instance is mutable. + * @method bool isStrictModeEnabled() Returns true if the strict mode is globally in use, false else. + * (It can be overridden in specific instances.) + * @method bool localeHasDiffOneDayWords($locale) Returns true if the given locale is internally supported and has words for 1-day diff (just now, yesterday, tomorrow). + * Support is considered enabled if the 3 words are translated in the given locale. + * @method bool localeHasDiffSyntax($locale) Returns true if the given locale is internally supported and has diff syntax support (ago, from now, before, after). + * Support is considered enabled if the 4 sentences are translated in the given locale. + * @method bool localeHasDiffTwoDayWords($locale) Returns true if the given locale is internally supported and has words for 2-days diff (before yesterday, after tomorrow). + * Support is considered enabled if the 2 words are translated in the given locale. + * @method bool localeHasPeriodSyntax($locale) Returns true if the given locale is internally supported and has period syntax support (X times, every X, from X, to X). + * Support is considered enabled if the 4 sentences are translated in the given locale. + * @method bool localeHasShortUnits($locale) Returns true if the given locale is internally supported and has short-units support. + * Support is considered enabled if either year, day or hour has a short variant translated. + * @method void macro($name, $macro) Register a custom macro. + * @method Carbon|null make($var) Make a Carbon instance from given variable if possible. + * Always return a new instance. Parse only strings and only these likely to be dates (skip intervals + * and recurrences). Throw an exception for invalid format, but otherwise return null. + * @method Carbon maxValue() Create a Carbon instance for the greatest supported date. + * @method Carbon minValue() Create a Carbon instance for the lowest supported date. + * @method void mixin($mixin) Mix another object into the class. + * @method Carbon now($tz = null) Get a Carbon instance for the current date and time. + * @method Carbon parse($time = null, $tz = null) Create a carbon instance from a string. + * This is an alias for the constructor that allows better fluent syntax + * as it allows you to do Carbon::parse('Monday next week')->fn() rather + * than (new Carbon('Monday next week'))->fn(). + * @method Carbon parseFromLocale($time, $locale, $tz = null) Create a carbon instance from a localized string (in French, Japanese, Arabic, etc.). + * @method string pluralUnit(string $unit) Returns standardized plural of a given singular/plural unit name (in English). + * @method Carbon|false rawCreateFromFormat($format, $time, $tz = null) Create a Carbon instance from a specific format. + * @method Carbon rawParse($time = null, $tz = null) Create a carbon instance from a string. + * This is an alias for the constructor that allows better fluent syntax + * as it allows you to do Carbon::parse('Monday next week')->fn() rather + * than (new Carbon('Monday next week'))->fn(). + * @method Carbon resetMacros() Remove all macros and generic macros. + * @method void resetMonthsOverflow() @deprecated To avoid conflict between different third-party libraries, static setters should not be used. + * You should rather use the ->settings() method. + * Or you can use method variants: addMonthsWithOverflow/addMonthsNoOverflow, same variants + * are available for quarters, years, decade, centuries, millennia (singular and plural forms). + * @method void resetToStringFormat() Reset the format used to the default when type juggling a Carbon instance to a string + * @method void resetYearsOverflow() @deprecated To avoid conflict between different third-party libraries, static setters should not be used. + * You should rather use the ->settings() method. + * Or you can use method variants: addYearsWithOverflow/addYearsNoOverflow, same variants + * are available for quarters, years, decade, centuries, millennia (singular and plural forms). + * @method void serializeUsing($callback) @deprecated To avoid conflict between different third-party libraries, static setters should not be used. + * You should rather transform Carbon object before the serialization. + * JSON serialize all Carbon instances using the given callback. + * @method Carbon setFallbackLocale($locale) Set the fallback locale. + * @method Carbon setHumanDiffOptions($humanDiffOptions) @deprecated To avoid conflict between different third-party libraries, static setters should not be used. + * You should rather use the ->settings() method. + * @method bool setLocale($locale) Set the current translator locale and indicate if the source locale file exists. + * Pass 'auto' as locale to use closest language from the current LC_TIME locale. + * @method void setMidDayAt($hour) @deprecated To avoid conflict between different third-party libraries, static setters should not be used. + * You should rather consider mid-day is always 12pm, then if you need to test if it's an other + * hour, test it explicitly: + * $date->format('G') == 13 + * or to set explicitly to a given hour: + * $date->setTime(13, 0, 0, 0) + * Set midday/noon hour + * @method Carbon setTestNow($testNow = null) Set a Carbon instance (real or mock) to be returned when a "now" + * instance is created. The provided instance will be returned + * specifically under the following conditions: + * - A call to the static now() method, ex. Carbon::now() + * - When a null (or blank string) is passed to the constructor or parse(), ex. new Carbon(null) + * - When the string "now" is passed to the constructor or parse(), ex. new Carbon('now') + * - When a string containing the desired time is passed to Carbon::parse(). + * Note the timezone parameter was left out of the examples above and + * has no affect as the mock value will be returned regardless of its value. + * To clear the test instance call this method using the default + * parameter of null. + * /!\ Use this method for unit tests only. + * @method void setToStringFormat($format) @deprecated To avoid conflict between different third-party libraries, static setters should not be used. + * You should rather let Carbon object being casted to string with DEFAULT_TO_STRING_FORMAT, and + * use other method or custom format passed to format() method if you need to dump an other string + * format. + * Set the default format used when type juggling a Carbon instance to a string + * @method void setTranslator(\Symfony\Contracts\Translation\TranslatorInterface $translator) Set the default translator instance to use. + * @method Carbon setUtf8($utf8) @deprecated To avoid conflict between different third-party libraries, static setters should not be used. + * You should rather use UTF-8 language packages on every machine. + * Set if UTF8 will be used for localized date/time. + * @method void setWeekEndsAt($day) @deprecated To avoid conflict between different third-party libraries, static setters should not be used. + * Use $weekStartsAt optional parameter instead when using startOfWeek, floorWeek, ceilWeek + * or roundWeek method. You can also use the 'first_day_of_week' locale setting to change the + * start of week according to current locale selected and implicitly the end of week. + * Set the last day of week + * @method void setWeekStartsAt($day) @deprecated To avoid conflict between different third-party libraries, static setters should not be used. + * Use $weekEndsAt optional parameter instead when using endOfWeek method. You can also use the + * 'first_day_of_week' locale setting to change the start of week according to current locale + * selected and implicitly the end of week. + * Set the first day of week + * @method void setWeekendDays($days) @deprecated To avoid conflict between different third-party libraries, static setters should not be used. + * You should rather consider week-end is always saturday and sunday, and if you have some custom + * week-end days to handle, give to those days an other name and create a macro for them: + * ``` + * Carbon::macro('isDayOff', function ($date) { + * return $date->isSunday() || $date->isMonday(); + * }); + * Carbon::macro('isNotDayOff', function ($date) { + * return !$date->isDayOff(); + * }); + * if ($someDate->isDayOff()) ... + * if ($someDate->isNotDayOff()) ... + * // Add 5 not-off days + * $count = 5; + * while ($someDate->isDayOff() || ($count-- > 0)) { + * $someDate->addDay(); + * } + * ``` + * Set weekend days + * @method bool shouldOverflowMonths() Get the month overflow global behavior (can be overridden in specific instances). + * @method bool shouldOverflowYears() Get the month overflow global behavior (can be overridden in specific instances). + * @method string singularUnit(string $unit) Returns standardized singular of a given singular/plural unit name (in English). + * @method Carbon today($tz = null) Create a Carbon instance for today. + * @method Carbon tomorrow($tz = null) Create a Carbon instance for tomorrow. + * @method string translateTimeString($timeString, $from = null, $to = null, $mode = 15) Translate a time string from a locale to an other. + * @method string translateWith(\Symfony\Contracts\Translation\TranslatorInterface $translator, string $key, array $parameters = [], $number = null) Translate using translation string or callback available. + * @method void useMonthsOverflow($monthsOverflow = true) @deprecated To avoid conflict between different third-party libraries, static setters should not be used. + * You should rather use the ->settings() method. + * Or you can use method variants: addMonthsWithOverflow/addMonthsNoOverflow, same variants + * are available for quarters, years, decade, centuries, millennia (singular and plural forms). + * @method Carbon useStrictMode($strictModeEnabled = true) @deprecated To avoid conflict between different third-party libraries, static setters should not be used. + * You should rather use the ->settings() method. + * @method void useYearsOverflow($yearsOverflow = true) @deprecated To avoid conflict between different third-party libraries, static setters should not be used. + * You should rather use the ->settings() method. + * Or you can use method variants: addYearsWithOverflow/addYearsNoOverflow, same variants + * are available for quarters, years, decade, centuries, millennia (singular and plural forms). + * @method Carbon yesterday($tz = null) Create a Carbon instance for yesterday. * * */ diff --git a/src/Carbon/FactoryImmutable.php b/src/Carbon/FactoryImmutable.php index 1f4d2e9f76..e97ca831ba 100644 --- a/src/Carbon/FactoryImmutable.php +++ b/src/Carbon/FactoryImmutable.php @@ -17,195 +17,195 @@ * * * - * @method CarbonImmutable create($year = 0, $month = 1, $day = 1, $hour = 0, $minute = 0, $second = 0, $tz = null) Create a new Carbon instance from a specific date and time. - * If any of $year, $month or $day are set to null their now() values will - * be used. - * If $hour is null it will be set to its now() value and the default - * values for $minute and $second will be their now() values. - * If $hour is not null then the default values for $minute and $second - * will be 0. - * @method CarbonImmutable createFromDate($year = null, $month = null, $day = null, $tz = null) Create a Carbon instance from just a date. The time portion is set to now. - * @method CarbonImmutable|false createFromFormat($format, $time, $tz = null) Create a Carbon instance from a specific format. - * @method CarbonImmutable|false createFromIsoFormat($format, $time, $tz = null, $locale = 'en', $translator = null) Create a Carbon instance from a specific ISO format (same replacements as ->isoFormat()). - * @method CarbonImmutable|false createFromLocaleFormat($format, $locale, $time, $tz = null) Create a Carbon instance from a specific format and a string in a given language. - * @method CarbonImmutable|false createFromLocaleIsoFormat($format, $locale, $time, $tz = null) Create a Carbon instance from a specific ISO format and a string in a given language. - * @method CarbonImmutable createFromTime($hour = 0, $minute = 0, $second = 0, $tz = null) Create a Carbon instance from just a time. The date portion is set to today. - * @method CarbonImmutable createFromTimeString($time, $tz = null) Create a Carbon instance from a time string. The date portion is set to today. - * @method CarbonImmutable createFromTimestamp($timestamp, $tz = null) Create a Carbon instance from a timestamp. - * @method CarbonImmutable createFromTimestampMs($timestamp, $tz = null) Create a Carbon instance from a timestamp in milliseconds. - * @method CarbonImmutable createFromTimestampUTC($timestamp) Create a Carbon instance from an UTC timestamp. - * @method CarbonImmutable createMidnightDate($year = null, $month = null, $day = null, $tz = null) Create a Carbon instance from just a date. The time portion is set to midnight. - * @method CarbonImmutable|false createSafe($year = null, $month = null, $day = null, $hour = null, $minute = null, $second = null, $tz = null) Create a new safe Carbon instance from a specific date and time. - * If any of $year, $month or $day are set to null their now() values will - * be used. - * If $hour is null it will be set to its now() value and the default - * values for $minute and $second will be their now() values. - * If $hour is not null then the default values for $minute and $second - * will be 0. - * If one of the set values is not valid, an InvalidDateException - * will be thrown. - * @method CarbonImmutable disableHumanDiffOption($humanDiffOption) @deprecated To avoid conflict between different third-party libraries, static setters should not be used. - * You should rather use the ->settings() method. - * @method CarbonImmutable enableHumanDiffOption($humanDiffOption) @deprecated To avoid conflict between different third-party libraries, static setters should not be used. - * You should rather use the ->settings() method. - * @method mixed executeWithLocale($locale, $func) Set the current locale to the given, execute the passed function, reset the locale to previous one, - * then return the result of the closure (or null if the closure was void). - * @method CarbonImmutable fromSerialized($value) Create an instance from a serialized string. - * @method void genericMacro($macro, $priority = 0) Register a custom macro. - * @method array getAvailableLocales() Returns the list of internally available locales and already loaded custom locales. - * (It will ignore custom translator dynamic loading.) - * @method Language[] getAvailableLocalesInfo() Returns list of Language object for each available locale. This object allow you to get the ISO name, native - * name, region and variant of the locale. - * @method array getDays() Get the days of the week - * @method string|null getFallbackLocale() Get the fallback locale. - * @method array getFormatsToIsoReplacements() List of replacements from date() format to isoFormat(). - * @method int getHumanDiffOptions() Return default humanDiff() options (merged flags as integer). - * @method array getIsoUnits() Returns list of locale units for ISO formatting. - * @method CarbonImmutable getLastErrors() {@inheritdoc} - * @method string getLocale() Get the current translator locale. - * @method callable|null getMacro($name) Get the raw callable macro registered globally for a given name. - * @method int getMidDayAt() get midday/noon hour - * @method Closure|CarbonImmutable getTestNow() Get the Carbon instance (real or mock) to be returned when a "now" - * instance is created. - * @method string getTimeFormatByPrecision($unitPrecision) Return a format from H:i to H:i:s.u according to given unit precision. - * @method string getTranslationMessageWith($translator, string $key, string $locale = null, string $default = null) Returns raw translation message for a given key. - * @method \Symfony\Component\Translation\TranslatorInterface getTranslator() Get the default translator instance in use. - * @method int getWeekEndsAt() Get the last day of week - * @method int getWeekStartsAt() Get the first day of week - * @method array getWeekendDays() Get weekend days - * @method bool hasFormat($date, $format) Checks if the (date)time string is in a given format. - * @method bool hasMacro($name) Checks if macro is registered globally. - * @method bool hasRelativeKeywords($time) Determine if a time string will produce a relative date. - * @method bool hasTestNow() Determine if there is a valid test instance set. A valid test instance - * is anything that is not null. - * @method CarbonImmutable instance($date) Create a Carbon instance from a DateTime one. - * @method bool isImmutable() Returns true if the current class/instance is immutable. - * @method bool isModifiableUnit($unit) Returns true if a property can be changed via setter. - * @method bool isMutable() Returns true if the current class/instance is mutable. - * @method bool isStrictModeEnabled() Returns true if the strict mode is globally in use, false else. - * (It can be overridden in specific instances.) - * @method bool localeHasDiffOneDayWords($locale) Returns true if the given locale is internally supported and has words for 1-day diff (just now, yesterday, tomorrow). - * Support is considered enabled if the 3 words are translated in the given locale. - * @method bool localeHasDiffSyntax($locale) Returns true if the given locale is internally supported and has diff syntax support (ago, from now, before, after). - * Support is considered enabled if the 4 sentences are translated in the given locale. - * @method bool localeHasDiffTwoDayWords($locale) Returns true if the given locale is internally supported and has words for 2-days diff (before yesterday, after tomorrow). - * Support is considered enabled if the 2 words are translated in the given locale. - * @method bool localeHasPeriodSyntax($locale) Returns true if the given locale is internally supported and has period syntax support (X times, every X, from X, to X). - * Support is considered enabled if the 4 sentences are translated in the given locale. - * @method bool localeHasShortUnits($locale) Returns true if the given locale is internally supported and has short-units support. - * Support is considered enabled if either year, day or hour has a short variant translated. - * @method void macro($name, $macro) Register a custom macro. - * @method CarbonImmutable|null make($var) Make a Carbon instance from given variable if possible. - * Always return a new instance. Parse only strings and only these likely to be dates (skip intervals - * and recurrences). Throw an exception for invalid format, but otherwise return null. - * @method CarbonImmutable maxValue() Create a Carbon instance for the greatest supported date. - * @method CarbonImmutable minValue() Create a Carbon instance for the lowest supported date. - * @method void mixin($mixin) Mix another object into the class. - * @method CarbonImmutable now($tz = null) Get a Carbon instance for the current date and time. - * @method CarbonImmutable parse($time = null, $tz = null) Create a carbon instance from a string. - * This is an alias for the constructor that allows better fluent syntax - * as it allows you to do Carbon::parse('Monday next week')->fn() rather - * than (new Carbon('Monday next week'))->fn(). - * @method CarbonImmutable parseFromLocale($time, $locale, $tz = null) Create a carbon instance from a localized string (in French, Japanese, Arabic, etc.). - * @method string pluralUnit(string $unit) Returns standardized plural of a given singular/plural unit name (in English). - * @method CarbonImmutable|false rawCreateFromFormat($format, $time, $tz = null) Create a Carbon instance from a specific format. - * @method CarbonImmutable rawParse($time = null, $tz = null) Create a carbon instance from a string. - * This is an alias for the constructor that allows better fluent syntax - * as it allows you to do Carbon::parse('Monday next week')->fn() rather - * than (new Carbon('Monday next week'))->fn(). - * @method CarbonImmutable resetMacros() Remove all macros and generic macros. - * @method void resetMonthsOverflow() @deprecated To avoid conflict between different third-party libraries, static setters should not be used. - * You should rather use the ->settings() method. - * Or you can use method variants: addMonthsWithOverflow/addMonthsNoOverflow, same variants - * are available for quarters, years, decade, centuries, millennia (singular and plural forms). - * @method void resetToStringFormat() Reset the format used to the default when type juggling a Carbon instance to a string - * @method void resetYearsOverflow() @deprecated To avoid conflict between different third-party libraries, static setters should not be used. - * You should rather use the ->settings() method. - * Or you can use method variants: addYearsWithOverflow/addYearsNoOverflow, same variants - * are available for quarters, years, decade, centuries, millennia (singular and plural forms). - * @method void serializeUsing($callback) @deprecated To avoid conflict between different third-party libraries, static setters should not be used. - * You should rather transform Carbon object before the serialization. - * JSON serialize all Carbon instances using the given callback. - * @method CarbonImmutable setFallbackLocale($locale) Set the fallback locale. - * @method CarbonImmutable setHumanDiffOptions($humanDiffOptions) @deprecated To avoid conflict between different third-party libraries, static setters should not be used. - * You should rather use the ->settings() method. - * @method bool setLocale($locale) Set the current translator locale and indicate if the source locale file exists. - * Pass 'auto' as locale to use closest language from the current LC_TIME locale. - * @method void setMidDayAt($hour) @deprecated To avoid conflict between different third-party libraries, static setters should not be used. - * You should rather consider mid-day is always 12pm, then if you need to test if it's an other - * hour, test it explicitly: - * $date->format('G') == 13 - * or to set explicitly to a given hour: - * $date->setTime(13, 0, 0, 0) - * Set midday/noon hour - * @method CarbonImmutable setTestNow($testNow = null) Set a Carbon instance (real or mock) to be returned when a "now" - * instance is created. The provided instance will be returned - * specifically under the following conditions: - * - A call to the static now() method, ex. Carbon::now() - * - When a null (or blank string) is passed to the constructor or parse(), ex. new Carbon(null) - * - When the string "now" is passed to the constructor or parse(), ex. new Carbon('now') - * - When a string containing the desired time is passed to Carbon::parse(). - * Note the timezone parameter was left out of the examples above and - * has no affect as the mock value will be returned regardless of its value. - * To clear the test instance call this method using the default - * parameter of null. - * /!\ Use this method for unit tests only. - * @method void setToStringFormat($format) @deprecated To avoid conflict between different third-party libraries, static setters should not be used. - * You should rather let Carbon object being casted to string with DEFAULT_TO_STRING_FORMAT, and - * use other method or custom format passed to format() method if you need to dump an other string - * format. - * Set the default format used when type juggling a Carbon instance to a string - * @method void setTranslator(\Symfony\Component\Translation\TranslatorInterface $translator) Set the default translator instance to use. - * @method CarbonImmutable setUtf8($utf8) @deprecated To avoid conflict between different third-party libraries, static setters should not be used. - * You should rather use UTF-8 language packages on every machine. - * Set if UTF8 will be used for localized date/time. - * @method void setWeekEndsAt($day) @deprecated To avoid conflict between different third-party libraries, static setters should not be used. - * Use $weekStartsAt optional parameter instead when using startOfWeek, floorWeek, ceilWeek - * or roundWeek method. You can also use the 'first_day_of_week' locale setting to change the - * start of week according to current locale selected and implicitly the end of week. - * Set the last day of week - * @method void setWeekStartsAt($day) @deprecated To avoid conflict between different third-party libraries, static setters should not be used. - * Use $weekEndsAt optional parameter instead when using endOfWeek method. You can also use the - * 'first_day_of_week' locale setting to change the start of week according to current locale - * selected and implicitly the end of week. - * Set the first day of week - * @method void setWeekendDays($days) @deprecated To avoid conflict between different third-party libraries, static setters should not be used. - * You should rather consider week-end is always saturday and sunday, and if you have some custom - * week-end days to handle, give to those days an other name and create a macro for them: - * ``` - * Carbon::macro('isDayOff', function ($date) { - * return $date->isSunday() || $date->isMonday(); - * }); - * Carbon::macro('isNotDayOff', function ($date) { - * return !$date->isDayOff(); - * }); - * if ($someDate->isDayOff()) ... - * if ($someDate->isNotDayOff()) ... - * // Add 5 not-off days - * $count = 5; - * while ($someDate->isDayOff() || ($count-- > 0)) { - * $someDate->addDay(); - * } - * ``` - * Set weekend days - * @method bool shouldOverflowMonths() Get the month overflow global behavior (can be overridden in specific instances). - * @method bool shouldOverflowYears() Get the month overflow global behavior (can be overridden in specific instances). - * @method string singularUnit(string $unit) Returns standardized singular of a given singular/plural unit name (in English). - * @method CarbonImmutable today($tz = null) Create a Carbon instance for today. - * @method CarbonImmutable tomorrow($tz = null) Create a Carbon instance for tomorrow. - * @method string translateTimeString($timeString, $from = null, $to = null, $mode = 15) Translate a time string from a locale to an other. - * @method string translateWith(\Symfony\Component\Translation\TranslatorInterface $translator, string $key, array $parameters = [], $number = null) Translate using translation string or callback available. - * @method void useMonthsOverflow($monthsOverflow = true) @deprecated To avoid conflict between different third-party libraries, static setters should not be used. - * You should rather use the ->settings() method. - * Or you can use method variants: addMonthsWithOverflow/addMonthsNoOverflow, same variants - * are available for quarters, years, decade, centuries, millennia (singular and plural forms). - * @method CarbonImmutable useStrictMode($strictModeEnabled = true) @deprecated To avoid conflict between different third-party libraries, static setters should not be used. - * You should rather use the ->settings() method. - * @method void useYearsOverflow($yearsOverflow = true) @deprecated To avoid conflict between different third-party libraries, static setters should not be used. - * You should rather use the ->settings() method. - * Or you can use method variants: addYearsWithOverflow/addYearsNoOverflow, same variants - * are available for quarters, years, decade, centuries, millennia (singular and plural forms). - * @method CarbonImmutable yesterday($tz = null) Create a Carbon instance for yesterday. + * @method CarbonImmutable create($year = 0, $month = 1, $day = 1, $hour = 0, $minute = 0, $second = 0, $tz = null) Create a new Carbon instance from a specific date and time. + * If any of $year, $month or $day are set to null their now() values will + * be used. + * If $hour is null it will be set to its now() value and the default + * values for $minute and $second will be their now() values. + * If $hour is not null then the default values for $minute and $second + * will be 0. + * @method CarbonImmutable createFromDate($year = null, $month = null, $day = null, $tz = null) Create a Carbon instance from just a date. The time portion is set to now. + * @method CarbonImmutable|false createFromFormat($format, $time, $tz = null) Create a Carbon instance from a specific format. + * @method CarbonImmutable|false createFromIsoFormat($format, $time, $tz = null, $locale = 'en', $translator = null) Create a Carbon instance from a specific ISO format (same replacements as ->isoFormat()). + * @method CarbonImmutable|false createFromLocaleFormat($format, $locale, $time, $tz = null) Create a Carbon instance from a specific format and a string in a given language. + * @method CarbonImmutable|false createFromLocaleIsoFormat($format, $locale, $time, $tz = null) Create a Carbon instance from a specific ISO format and a string in a given language. + * @method CarbonImmutable createFromTime($hour = 0, $minute = 0, $second = 0, $tz = null) Create a Carbon instance from just a time. The date portion is set to today. + * @method CarbonImmutable createFromTimeString($time, $tz = null) Create a Carbon instance from a time string. The date portion is set to today. + * @method CarbonImmutable createFromTimestamp($timestamp, $tz = null) Create a Carbon instance from a timestamp. + * @method CarbonImmutable createFromTimestampMs($timestamp, $tz = null) Create a Carbon instance from a timestamp in milliseconds. + * @method CarbonImmutable createFromTimestampUTC($timestamp) Create a Carbon instance from an UTC timestamp. + * @method CarbonImmutable createMidnightDate($year = null, $month = null, $day = null, $tz = null) Create a Carbon instance from just a date. The time portion is set to midnight. + * @method CarbonImmutable|false createSafe($year = null, $month = null, $day = null, $hour = null, $minute = null, $second = null, $tz = null) Create a new safe Carbon instance from a specific date and time. + * If any of $year, $month or $day are set to null their now() values will + * be used. + * If $hour is null it will be set to its now() value and the default + * values for $minute and $second will be their now() values. + * If $hour is not null then the default values for $minute and $second + * will be 0. + * If one of the set values is not valid, an InvalidDateException + * will be thrown. + * @method CarbonImmutable disableHumanDiffOption($humanDiffOption) @deprecated To avoid conflict between different third-party libraries, static setters should not be used. + * You should rather use the ->settings() method. + * @method CarbonImmutable enableHumanDiffOption($humanDiffOption) @deprecated To avoid conflict between different third-party libraries, static setters should not be used. + * You should rather use the ->settings() method. + * @method mixed executeWithLocale($locale, $func) Set the current locale to the given, execute the passed function, reset the locale to previous one, + * then return the result of the closure (or null if the closure was void). + * @method CarbonImmutable fromSerialized($value) Create an instance from a serialized string. + * @method void genericMacro($macro, $priority = 0) Register a custom macro. + * @method array getAvailableLocales() Returns the list of internally available locales and already loaded custom locales. + * (It will ignore custom translator dynamic loading.) + * @method Language[] getAvailableLocalesInfo() Returns list of Language object for each available locale. This object allow you to get the ISO name, native + * name, region and variant of the locale. + * @method array getDays() Get the days of the week + * @method string|null getFallbackLocale() Get the fallback locale. + * @method array getFormatsToIsoReplacements() List of replacements from date() format to isoFormat(). + * @method int getHumanDiffOptions() Return default humanDiff() options (merged flags as integer). + * @method array getIsoUnits() Returns list of locale units for ISO formatting. + * @method CarbonImmutable getLastErrors() {@inheritdoc} + * @method string getLocale() Get the current translator locale. + * @method callable|null getMacro($name) Get the raw callable macro registered globally for a given name. + * @method int getMidDayAt() get midday/noon hour + * @method Closure|CarbonImmutable getTestNow() Get the Carbon instance (real or mock) to be returned when a "now" + * instance is created. + * @method string getTimeFormatByPrecision($unitPrecision) Return a format from H:i to H:i:s.u according to given unit precision. + * @method string getTranslationMessageWith($translator, string $key, string $locale = null, string $default = null) Returns raw translation message for a given key. + * @method TranslatorInterface getTranslator() Get the default translator instance in use. + * @method int getWeekEndsAt() Get the last day of week + * @method int getWeekStartsAt() Get the first day of week + * @method array getWeekendDays() Get weekend days + * @method bool hasFormat($date, $format) Checks if the (date)time string is in a given format. + * @method bool hasMacro($name) Checks if macro is registered globally. + * @method bool hasRelativeKeywords($time) Determine if a time string will produce a relative date. + * @method bool hasTestNow() Determine if there is a valid test instance set. A valid test instance + * is anything that is not null. + * @method CarbonImmutable instance($date) Create a Carbon instance from a DateTime one. + * @method bool isImmutable() Returns true if the current class/instance is immutable. + * @method bool isModifiableUnit($unit) Returns true if a property can be changed via setter. + * @method bool isMutable() Returns true if the current class/instance is mutable. + * @method bool isStrictModeEnabled() Returns true if the strict mode is globally in use, false else. + * (It can be overridden in specific instances.) + * @method bool localeHasDiffOneDayWords($locale) Returns true if the given locale is internally supported and has words for 1-day diff (just now, yesterday, tomorrow). + * Support is considered enabled if the 3 words are translated in the given locale. + * @method bool localeHasDiffSyntax($locale) Returns true if the given locale is internally supported and has diff syntax support (ago, from now, before, after). + * Support is considered enabled if the 4 sentences are translated in the given locale. + * @method bool localeHasDiffTwoDayWords($locale) Returns true if the given locale is internally supported and has words for 2-days diff (before yesterday, after tomorrow). + * Support is considered enabled if the 2 words are translated in the given locale. + * @method bool localeHasPeriodSyntax($locale) Returns true if the given locale is internally supported and has period syntax support (X times, every X, from X, to X). + * Support is considered enabled if the 4 sentences are translated in the given locale. + * @method bool localeHasShortUnits($locale) Returns true if the given locale is internally supported and has short-units support. + * Support is considered enabled if either year, day or hour has a short variant translated. + * @method void macro($name, $macro) Register a custom macro. + * @method CarbonImmutable|null make($var) Make a Carbon instance from given variable if possible. + * Always return a new instance. Parse only strings and only these likely to be dates (skip intervals + * and recurrences). Throw an exception for invalid format, but otherwise return null. + * @method CarbonImmutable maxValue() Create a Carbon instance for the greatest supported date. + * @method CarbonImmutable minValue() Create a Carbon instance for the lowest supported date. + * @method void mixin($mixin) Mix another object into the class. + * @method CarbonImmutable now($tz = null) Get a Carbon instance for the current date and time. + * @method CarbonImmutable parse($time = null, $tz = null) Create a carbon instance from a string. + * This is an alias for the constructor that allows better fluent syntax + * as it allows you to do Carbon::parse('Monday next week')->fn() rather + * than (new Carbon('Monday next week'))->fn(). + * @method CarbonImmutable parseFromLocale($time, $locale, $tz = null) Create a carbon instance from a localized string (in French, Japanese, Arabic, etc.). + * @method string pluralUnit(string $unit) Returns standardized plural of a given singular/plural unit name (in English). + * @method CarbonImmutable|false rawCreateFromFormat($format, $time, $tz = null) Create a Carbon instance from a specific format. + * @method CarbonImmutable rawParse($time = null, $tz = null) Create a carbon instance from a string. + * This is an alias for the constructor that allows better fluent syntax + * as it allows you to do Carbon::parse('Monday next week')->fn() rather + * than (new Carbon('Monday next week'))->fn(). + * @method CarbonImmutable resetMacros() Remove all macros and generic macros. + * @method void resetMonthsOverflow() @deprecated To avoid conflict between different third-party libraries, static setters should not be used. + * You should rather use the ->settings() method. + * Or you can use method variants: addMonthsWithOverflow/addMonthsNoOverflow, same variants + * are available for quarters, years, decade, centuries, millennia (singular and plural forms). + * @method void resetToStringFormat() Reset the format used to the default when type juggling a Carbon instance to a string + * @method void resetYearsOverflow() @deprecated To avoid conflict between different third-party libraries, static setters should not be used. + * You should rather use the ->settings() method. + * Or you can use method variants: addYearsWithOverflow/addYearsNoOverflow, same variants + * are available for quarters, years, decade, centuries, millennia (singular and plural forms). + * @method void serializeUsing($callback) @deprecated To avoid conflict between different third-party libraries, static setters should not be used. + * You should rather transform Carbon object before the serialization. + * JSON serialize all Carbon instances using the given callback. + * @method CarbonImmutable setFallbackLocale($locale) Set the fallback locale. + * @method CarbonImmutable setHumanDiffOptions($humanDiffOptions) @deprecated To avoid conflict between different third-party libraries, static setters should not be used. + * You should rather use the ->settings() method. + * @method bool setLocale($locale) Set the current translator locale and indicate if the source locale file exists. + * Pass 'auto' as locale to use closest language from the current LC_TIME locale. + * @method void setMidDayAt($hour) @deprecated To avoid conflict between different third-party libraries, static setters should not be used. + * You should rather consider mid-day is always 12pm, then if you need to test if it's an other + * hour, test it explicitly: + * $date->format('G') == 13 + * or to set explicitly to a given hour: + * $date->setTime(13, 0, 0, 0) + * Set midday/noon hour + * @method CarbonImmutable setTestNow($testNow = null) Set a Carbon instance (real or mock) to be returned when a "now" + * instance is created. The provided instance will be returned + * specifically under the following conditions: + * - A call to the static now() method, ex. Carbon::now() + * - When a null (or blank string) is passed to the constructor or parse(), ex. new Carbon(null) + * - When the string "now" is passed to the constructor or parse(), ex. new Carbon('now') + * - When a string containing the desired time is passed to Carbon::parse(). + * Note the timezone parameter was left out of the examples above and + * has no affect as the mock value will be returned regardless of its value. + * To clear the test instance call this method using the default + * parameter of null. + * /!\ Use this method for unit tests only. + * @method void setToStringFormat($format) @deprecated To avoid conflict between different third-party libraries, static setters should not be used. + * You should rather let Carbon object being casted to string with DEFAULT_TO_STRING_FORMAT, and + * use other method or custom format passed to format() method if you need to dump an other string + * format. + * Set the default format used when type juggling a Carbon instance to a string + * @method void setTranslator(\Symfony\Contracts\Translation\TranslatorInterface $translator) Set the default translator instance to use. + * @method CarbonImmutable setUtf8($utf8) @deprecated To avoid conflict between different third-party libraries, static setters should not be used. + * You should rather use UTF-8 language packages on every machine. + * Set if UTF8 will be used for localized date/time. + * @method void setWeekEndsAt($day) @deprecated To avoid conflict between different third-party libraries, static setters should not be used. + * Use $weekStartsAt optional parameter instead when using startOfWeek, floorWeek, ceilWeek + * or roundWeek method. You can also use the 'first_day_of_week' locale setting to change the + * start of week according to current locale selected and implicitly the end of week. + * Set the last day of week + * @method void setWeekStartsAt($day) @deprecated To avoid conflict between different third-party libraries, static setters should not be used. + * Use $weekEndsAt optional parameter instead when using endOfWeek method. You can also use the + * 'first_day_of_week' locale setting to change the start of week according to current locale + * selected and implicitly the end of week. + * Set the first day of week + * @method void setWeekendDays($days) @deprecated To avoid conflict between different third-party libraries, static setters should not be used. + * You should rather consider week-end is always saturday and sunday, and if you have some custom + * week-end days to handle, give to those days an other name and create a macro for them: + * ``` + * Carbon::macro('isDayOff', function ($date) { + * return $date->isSunday() || $date->isMonday(); + * }); + * Carbon::macro('isNotDayOff', function ($date) { + * return !$date->isDayOff(); + * }); + * if ($someDate->isDayOff()) ... + * if ($someDate->isNotDayOff()) ... + * // Add 5 not-off days + * $count = 5; + * while ($someDate->isDayOff() || ($count-- > 0)) { + * $someDate->addDay(); + * } + * ``` + * Set weekend days + * @method bool shouldOverflowMonths() Get the month overflow global behavior (can be overridden in specific instances). + * @method bool shouldOverflowYears() Get the month overflow global behavior (can be overridden in specific instances). + * @method string singularUnit(string $unit) Returns standardized singular of a given singular/plural unit name (in English). + * @method CarbonImmutable today($tz = null) Create a Carbon instance for today. + * @method CarbonImmutable tomorrow($tz = null) Create a Carbon instance for tomorrow. + * @method string translateTimeString($timeString, $from = null, $to = null, $mode = 15) Translate a time string from a locale to an other. + * @method string translateWith(\Symfony\Contracts\Translation\TranslatorInterface $translator, string $key, array $parameters = [], $number = null) Translate using translation string or callback available. + * @method void useMonthsOverflow($monthsOverflow = true) @deprecated To avoid conflict between different third-party libraries, static setters should not be used. + * You should rather use the ->settings() method. + * Or you can use method variants: addMonthsWithOverflow/addMonthsNoOverflow, same variants + * are available for quarters, years, decade, centuries, millennia (singular and plural forms). + * @method CarbonImmutable useStrictMode($strictModeEnabled = true) @deprecated To avoid conflict between different third-party libraries, static setters should not be used. + * You should rather use the ->settings() method. + * @method void useYearsOverflow($yearsOverflow = true) @deprecated To avoid conflict between different third-party libraries, static setters should not be used. + * You should rather use the ->settings() method. + * Or you can use method variants: addYearsWithOverflow/addYearsNoOverflow, same variants + * are available for quarters, years, decade, centuries, millennia (singular and plural forms). + * @method CarbonImmutable yesterday($tz = null) Create a Carbon instance for yesterday. * * */ From 8d1d67fd45d8a90a777f81132c9cf3be37ea57d9 Mon Sep 17 00:00:00 2001 From: KyleKatarn Date: Tue, 4 Aug 2020 23:33:57 +0200 Subject: [PATCH 10/38] Remove obsolete rule ignore --- phpstan.neon | 1 - 1 file changed, 1 deletion(-) diff --git a/phpstan.neon b/phpstan.neon index df34ebdadd..78c9fa56a4 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -13,7 +13,6 @@ parameters: - '#^Variable \$this in isset\(\) (always exists and is not nullable|is never defined)\.$#' - '#^Unary operation "\~" on mixed results in an error\.$#' - '#^Call to an undefined method Carbon\\Carbon(Interface|Immutable)?::(super|noThis)\(\)\.$#' - - '#Call to static method set\(\) on an unknown class Symfony\\Component\\Translation\\PluralizationRules\.#' - '#Call to an undefined method DateTimeInterface::setTimezone\(\)\.#' - '#should return (\S*)static\(Carbon\\CarbonPeriod\) but returns Carbon\\CarbonPeriod\.$#' - '#should return (\S*)static\(Carbon\\Carbon\) but returns Carbon\\Carbon(Interface)?\.$#' From 74db99c2f4d0bf3d5f66a801054b230ebfb72b02 Mon Sep 17 00:00:00 2001 From: KyleKatarn Date: Wed, 5 Aug 2020 00:13:02 +0200 Subject: [PATCH 11/38] #2119 Keep only real+float mode for diffInUnit --- src/Carbon/CarbonInterface.php | 264 +++------------- src/Carbon/Traits/Difference.php | 446 +++++---------------------- src/Carbon/Traits/Modifiers.php | 6 +- tests/Carbon/DiffTest.php | 116 +++---- tests/Carbon/ModifyTest.php | 131 ++++---- tests/CarbonImmutable/DiffTest.php | 110 +++---- tests/CarbonImmutable/ModifyTest.php | 189 ++++++------ 7 files changed, 390 insertions(+), 872 deletions(-) diff --git a/src/Carbon/CarbonInterface.php b/src/Carbon/CarbonInterface.php index 69e6274f48..8264526f7a 100644 --- a/src/Carbon/CarbonInterface.php +++ b/src/Carbon/CarbonInterface.php @@ -1220,7 +1220,7 @@ public function diffAsCarbonInterval($date = null, $absolute = true); * * @return int */ - public function diffFiltered(CarbonInterval $ci, Closure $callback, $date = null, $absolute = true); + public function diffFiltered(CarbonInterval $ci, Closure $callback, $date = null, $absolute = true): int; /** * Get the difference in a human readable format in the current locale from current instance to an other @@ -1271,9 +1271,9 @@ public function diffForHumans($other = null, $syntax = null, $short = false, $pa * @param \Carbon\CarbonInterface|\DateTimeInterface|string|null $date * @param bool $absolute Get the absolute of the difference * - * @return int + * @return float */ - public function diffInDays($date = null, $absolute = true); + public function diffInDays($date = null, $absolute = true): float; /** * Get the difference in days using a filter closure rounded down. @@ -1284,7 +1284,7 @@ public function diffInDays($date = null, $absolute = true); * * @return int */ - public function diffInDaysFiltered(Closure $callback, $date = null, $absolute = true); + public function diffInDaysFiltered(Closure $callback, $date = null, $absolute = true): int; /** * Get the difference in hours rounded down. @@ -1292,9 +1292,9 @@ public function diffInDaysFiltered(Closure $callback, $date = null, $absolute = * @param \Carbon\CarbonInterface|\DateTimeInterface|string|null $date * @param bool $absolute Get the absolute of the difference * - * @return int + * @return float */ - public function diffInHours($date = null, $absolute = true); + public function diffInHours($date = null, $absolute = true): float; /** * Get the difference in hours using a filter closure rounded down. @@ -1305,7 +1305,7 @@ public function diffInHours($date = null, $absolute = true); * * @return int */ - public function diffInHoursFiltered(Closure $callback, $date = null, $absolute = true); + public function diffInHoursFiltered(Closure $callback, $date = null, $absolute = true): int; /** * Get the difference in microseconds. @@ -1313,9 +1313,9 @@ public function diffInHoursFiltered(Closure $callback, $date = null, $absolute = * @param \Carbon\CarbonInterface|\DateTimeInterface|string|null $date * @param bool $absolute Get the absolute of the difference * - * @return int + * @return float */ - public function diffInMicroseconds($date = null, $absolute = true); + public function diffInMicroseconds($date = null, $absolute = true): float; /** * Get the difference in milliseconds rounded down. @@ -1323,9 +1323,9 @@ public function diffInMicroseconds($date = null, $absolute = true); * @param \Carbon\CarbonInterface|\DateTimeInterface|string|null $date * @param bool $absolute Get the absolute of the difference * - * @return int + * @return float */ - public function diffInMilliseconds($date = null, $absolute = true); + public function diffInMilliseconds($date = null, $absolute = true): float; /** * Get the difference in minutes rounded down. @@ -1333,9 +1333,9 @@ public function diffInMilliseconds($date = null, $absolute = true); * @param \Carbon\CarbonInterface|\DateTimeInterface|string|null $date * @param bool $absolute Get the absolute of the difference * - * @return int + * @return float */ - public function diffInMinutes($date = null, $absolute = true); + public function diffInMinutes($date = null, $absolute = true): float; /** * Get the difference in months rounded down. @@ -1343,9 +1343,9 @@ public function diffInMinutes($date = null, $absolute = true); * @param \Carbon\CarbonInterface|\DateTimeInterface|string|null $date * @param bool $absolute Get the absolute of the difference * - * @return int + * @return float */ - public function diffInMonths($date = null, $absolute = true); + public function diffInMonths($date = null, $absolute = true): float; /** * Get the difference in quarters rounded down. @@ -1353,59 +1353,9 @@ public function diffInMonths($date = null, $absolute = true); * @param \Carbon\CarbonInterface|\DateTimeInterface|string|null $date * @param bool $absolute Get the absolute of the difference * - * @return int - */ - public function diffInQuarters($date = null, $absolute = true); - - /** - * Get the difference in hours rounded down using timestamps. - * - * @param \Carbon\CarbonInterface|\DateTimeInterface|string|null $date - * @param bool $absolute Get the absolute of the difference - * - * @return int - */ - public function diffInRealHours($date = null, $absolute = true); - - /** - * Get the difference in microseconds using timestamps. - * - * @param \Carbon\CarbonInterface|\DateTimeInterface|string|null $date - * @param bool $absolute Get the absolute of the difference - * - * @return int - */ - public function diffInRealMicroseconds($date = null, $absolute = true); - - /** - * Get the difference in milliseconds rounded down using timestamps. - * - * @param \Carbon\CarbonInterface|\DateTimeInterface|string|null $date - * @param bool $absolute Get the absolute of the difference - * - * @return int - */ - public function diffInRealMilliseconds($date = null, $absolute = true); - - /** - * Get the difference in minutes rounded down using timestamps. - * - * @param \Carbon\CarbonInterface|\DateTimeInterface|string|null $date - * @param bool $absolute Get the absolute of the difference - * - * @return int - */ - public function diffInRealMinutes($date = null, $absolute = true); - - /** - * Get the difference in seconds using timestamps. - * - * @param \Carbon\CarbonInterface|\DateTimeInterface|string|null $date - * @param bool $absolute Get the absolute of the difference - * - * @return int + * @return float */ - public function diffInRealSeconds($date = null, $absolute = true); + public function diffInQuarters($date = null, $absolute = true): float; /** * Get the difference in seconds rounded down. @@ -1413,9 +1363,9 @@ public function diffInRealSeconds($date = null, $absolute = true); * @param \Carbon\CarbonInterface|\DateTimeInterface|string|null $date * @param bool $absolute Get the absolute of the difference * - * @return int + * @return float */ - public function diffInSeconds($date = null, $absolute = true); + public function diffInSeconds($date = null, $absolute = true): float; /** * Get the difference in weekdays rounded down. @@ -1425,7 +1375,7 @@ public function diffInSeconds($date = null, $absolute = true); * * @return int */ - public function diffInWeekdays($date = null, $absolute = true); + public function diffInWeekdays($date = null, $absolute = true): int; /** * Get the difference in weekend days using a filter rounded down. @@ -1435,7 +1385,7 @@ public function diffInWeekdays($date = null, $absolute = true); * * @return int */ - public function diffInWeekendDays($date = null, $absolute = true); + public function diffInWeekendDays($date = null, $absolute = true): int; /** * Get the difference in weeks rounded down. @@ -1445,7 +1395,7 @@ public function diffInWeekendDays($date = null, $absolute = true); * * @return int */ - public function diffInWeeks($date = null, $absolute = true); + public function diffInWeeks($date = null, $absolute = true): float; /** * Get the difference in years @@ -1453,7 +1403,7 @@ public function diffInWeeks($date = null, $absolute = true); * @param \Carbon\CarbonInterface|\DateTimeInterface|string|null $date * @param bool $absolute Get the absolute of the difference * - * @return int + * @return float */ public function diffInYears($date = null, $absolute = true); @@ -1721,126 +1671,6 @@ public function firstOfQuarter($dayOfWeek = null); */ public function firstOfYear($dayOfWeek = null); - /** - * Get the difference in days as float (microsecond-precision). - * - * @param \Carbon\CarbonInterface|\DateTimeInterface|string|null $date - * @param bool $absolute Get the absolute of the difference - * - * @return float - */ - public function floatDiffInDays($date = null, $absolute = true); - - /** - * Get the difference in hours as float (microsecond-precision). - * - * @param \Carbon\CarbonInterface|\DateTimeInterface|string|null $date - * @param bool $absolute Get the absolute of the difference - * - * @return float - */ - public function floatDiffInHours($date = null, $absolute = true); - - /** - * Get the difference in minutes as float (microsecond-precision). - * - * @param \Carbon\CarbonInterface|\DateTimeInterface|string|null $date - * @param bool $absolute Get the absolute of the difference - * - * @return float - */ - public function floatDiffInMinutes($date = null, $absolute = true); - - /** - * Get the difference in months as float (microsecond-precision). - * - * @param \Carbon\CarbonInterface|\DateTimeInterface|string|null $date - * @param bool $absolute Get the absolute of the difference - * - * @return float - */ - public function floatDiffInMonths($date = null, $absolute = true); - - /** - * Get the difference in days as float (microsecond-precision). - * - * @param \Carbon\CarbonInterface|\DateTimeInterface|string|null $date - * @param bool $absolute Get the absolute of the difference - * - * @return float - */ - public function floatDiffInRealDays($date = null, $absolute = true); - - /** - * Get the difference in hours as float (microsecond-precision) using timestamps. - * - * @param \Carbon\CarbonInterface|\DateTimeInterface|string|null $date - * @param bool $absolute Get the absolute of the difference - * - * @return float - */ - public function floatDiffInRealHours($date = null, $absolute = true); - - /** - * Get the difference in minutes as float (microsecond-precision) using timestamps. - * - * @param \Carbon\CarbonInterface|\DateTimeInterface|string|null $date - * @param bool $absolute Get the absolute of the difference - * - * @return float - */ - public function floatDiffInRealMinutes($date = null, $absolute = true); - - /** - * Get the difference in months as float (microsecond-precision) using timestamps. - * - * @param \Carbon\CarbonInterface|\DateTimeInterface|string|null $date - * @param bool $absolute Get the absolute of the difference - * - * @return float - */ - public function floatDiffInRealMonths($date = null, $absolute = true); - - /** - * Get the difference in seconds as float (microsecond-precision) using timestamps. - * - * @param \Carbon\CarbonInterface|\DateTimeInterface|string|null $date - * @param bool $absolute Get the absolute of the difference - * - * @return float - */ - public function floatDiffInRealSeconds($date = null, $absolute = true); - - /** - * Get the difference in year as float (microsecond-precision) using timestamps. - * - * @param \Carbon\CarbonInterface|\DateTimeInterface|string|null $date - * @param bool $absolute Get the absolute of the difference - * - * @return float - */ - public function floatDiffInRealYears($date = null, $absolute = true); - - /** - * Get the difference in seconds as float (microsecond-precision). - * - * @param \Carbon\CarbonInterface|\DateTimeInterface|string|null $date - * @param bool $absolute Get the absolute of the difference - * - * @return float - */ - public function floatDiffInSeconds($date = null, $absolute = true); - - /** - * Get the difference in year as float (microsecond-precision). - * - * @param \Carbon\CarbonInterface|\DateTimeInterface|string|null $date - * @param bool $absolute Get the absolute of the difference - * - * @return float - */ - public function floatDiffInYears($date = null, $absolute = true); - /** * Round the current instance second with given precision if specified. * @@ -2074,7 +1904,7 @@ public function getLocalMacro($name); /** * Get the translator of the current instance or the default if none set. * - * @return TranslatorInterface + * @return \Symfony\Component\Translation\TranslatorInterface */ public function getLocalTranslator(); @@ -2221,10 +2051,10 @@ public function getTranslatedShortMonthName($context = null); /** * Returns raw translation message for a given key. * - * @param string $key key to find - * @param string|null $locale current locale used if null - * @param string|null $default default value if translation returns the key - * @param TranslatorInterface $translator an optional translator to use + * @param string $key key to find + * @param string|null $locale current locale used if null + * @param string|null $default default value if translation returns the key + * @param \Symfony\Component\Translation\TranslatorInterface $translator an optional translator to use * * @return string */ @@ -2233,10 +2063,10 @@ public function getTranslationMessage(string $key, string $locale = null, string /** * Returns raw translation message for a given key. * - * @param TranslatorInterface $translator the translator to use - * @param string $key key to find - * @param string|null $locale current locale used if null - * @param string|null $default default value if translation returns the key + * @param \Symfony\Component\Translation\TranslatorInterface $translator the translator to use + * @param string $key key to find + * @param string|null $locale current locale used if null + * @param string|null $default default value if translation returns the key * * @return string */ @@ -2245,7 +2075,7 @@ public static function getTranslationMessageWith($translator, string $key, strin /** * Get the default translator instance in use. * - * @return TranslatorInterface + * @return \Symfony\Component\Translation\TranslatorInterface */ public static function getTranslator(); @@ -3666,11 +3496,11 @@ public function setISODate($year, $week, $day = 1); /** * Set the translator for the current instance. * - * @param TranslatorInterface $translator + * @param \Symfony\Component\Translation\TranslatorInterface $translator * * @return $this */ - public function setLocalTranslator(\Symfony\Contracts\Translation\TranslatorInterface $translator); + public function setLocalTranslator(\Symfony\Component\Translation\TranslatorInterface $translator); /** * Set the current translator locale and indicate if the source locale file exists. @@ -3788,11 +3618,11 @@ public static function setToStringFormat($format); /** * Set the default translator instance to use. * - * @param TranslatorInterface $translator + * @param \Symfony\Component\Translation\TranslatorInterface $translator * * @return void */ - public static function setTranslator(\Symfony\Contracts\Translation\TranslatorInterface $translator); + public static function setTranslator(\Symfony\Component\Translation\TranslatorInterface $translator); /** * Set specified unit to new given value. @@ -4641,14 +4471,14 @@ public static function tomorrow($tz = null); /** * Translate using translation string or callback available. * - * @param string $key - * @param array $parameters - * @param null $number - * @param TranslatorInterface $translator + * @param string $key + * @param array $parameters + * @param null $number + * @param \Symfony\Component\Translation\TranslatorInterface $translator * * @return string */ - public function translate(string $key, array $parameters = [], $number = null, \Symfony\Contracts\Translation\TranslatorInterface $translator = null, bool $altNumbers = false): string; + public function translate(string $key, array $parameters = [], $number = null, \Symfony\Component\Translation\TranslatorInterface $translator = null, bool $altNumbers = false): string; /** * Returns the alternative number for a given integer if available in the current locale. @@ -4690,14 +4520,14 @@ public function translateTimeStringTo($timeString, $to = null); /** * Translate using translation string or callback available. * - * @param TranslatorInterface $translator - * @param string $key - * @param array $parameters - * @param null $number + * @param \Symfony\Component\Translation\TranslatorInterface $translator + * @param string $key + * @param array $parameters + * @param null $number * * @return string */ - public static function translateWith(\Symfony\Contracts\Translation\TranslatorInterface $translator, string $key, array $parameters = [], $number = null): string; + public static function translateWith(\Symfony\Component\Translation\TranslatorInterface $translator, string $key, array $parameters = [], $number = null): string; /** * Format as ->format() do (using date replacements patterns from http://php.net/manual/fr/function.date.php) diff --git a/src/Carbon/Traits/Difference.php b/src/Carbon/Traits/Difference.php index 28a2f35711..222dcd297c 100644 --- a/src/Carbon/Traits/Difference.php +++ b/src/Carbon/Traits/Difference.php @@ -26,7 +26,6 @@ * Depends on the following methods: * * @method bool lessThan($date) - * @method DateInterval diff(\DateTimeInterface $date, bool $absolute = false) * @method static copy() * @method static resolveCarbon($date = null) * @method static Translator translator() @@ -143,11 +142,33 @@ public function diffAsCarbonInterval($date = null, $absolute = true) * @param \Carbon\CarbonInterface|\DateTimeInterface|string|null $date * @param bool $absolute Get the absolute of the difference * - * @return int + * @return float */ public function diffInYears($date = null, $absolute = true) { - return (int) $this->diff($this->resolveCarbon($date), $absolute)->format('%r%y'); + $start = $this; + $end = $this->resolveCarbon($date); + $ascending = ($start <= $end); + $sign = $absolute || $ascending ? 1 : -1; + if (!$ascending) { + [$start, $end] = [$end, $start]; + } + $yearsDiff = $start->diffInYears($end); + /** @var Carbon|CarbonImmutable $floorEnd */ + $floorEnd = $start->copy()->addYears($yearsDiff); + + if ($floorEnd >= $end) { + return $sign * $yearsDiff; + } + + /** @var Carbon|CarbonImmutable $startOfYearAfterFloorEnd */ + $startOfYearAfterFloorEnd = $floorEnd->copy()->addYear()->startOfYear(); + + if ($startOfYearAfterFloorEnd > $end) { + return $sign * ($yearsDiff + $floorEnd->diffInDays($end) / $floorEnd->daysInYear); + } + + return $sign * ($yearsDiff + $floorEnd->diffInDays($startOfYearAfterFloorEnd) / $floorEnd->daysInYear + $startOfYearAfterFloorEnd->diffInDays($end) / $end->daysInYear); } /** @@ -156,11 +177,11 @@ public function diffInYears($date = null, $absolute = true) * @param \Carbon\CarbonInterface|\DateTimeInterface|string|null $date * @param bool $absolute Get the absolute of the difference * - * @return int + * @return float */ - public function diffInQuarters($date = null, $absolute = true) + public function diffInQuarters($date = null, $absolute = true): float { - return (int) ($this->diffInMonths($date, $absolute) / static::MONTHS_PER_QUARTER); + return $this->diffInMonths($date, $absolute) / static::MONTHS_PER_QUARTER; } /** @@ -169,13 +190,33 @@ public function diffInQuarters($date = null, $absolute = true) * @param \Carbon\CarbonInterface|\DateTimeInterface|string|null $date * @param bool $absolute Get the absolute of the difference * - * @return int + * @return float */ - public function diffInMonths($date = null, $absolute = true) + public function diffInMonths($date = null, $absolute = true): float { - $date = $this->resolveCarbon($date); + $start = $this; + $end = $this->resolveCarbon($date); + $ascending = ($start <= $end); + $sign = $absolute || $ascending ? 1 : -1; + if (!$ascending) { + [$start, $end] = [$end, $start]; + } + $monthsDiff = $start->diffInMonths($end); + /** @var Carbon|CarbonImmutable $floorEnd */ + $floorEnd = $start->copy()->addMonths($monthsDiff); + + if ($floorEnd >= $end) { + return $sign * $monthsDiff; + } + + /** @var Carbon|CarbonImmutable $startOfMonthAfterFloorEnd */ + $startOfMonthAfterFloorEnd = $floorEnd->copy()->addMonth()->startOfMonth(); - return $this->diffInYears($date, $absolute) * static::MONTHS_PER_YEAR + (int) $this->diff($date, $absolute)->format('%r%m'); + if ($startOfMonthAfterFloorEnd > $end) { + return $sign * ($monthsDiff + $floorEnd->diffInDays($end) / $floorEnd->daysInMonth); + } + + return $sign * ($monthsDiff + $floorEnd->diffInDays($startOfMonthAfterFloorEnd) / $floorEnd->daysInMonth + $startOfMonthAfterFloorEnd->diffInDays($end) / $end->daysInMonth); } /** @@ -186,9 +227,9 @@ public function diffInMonths($date = null, $absolute = true) * * @return int */ - public function diffInWeeks($date = null, $absolute = true) + public function diffInWeeks($date = null, $absolute = true): float { - return (int) ($this->diffInDays($date, $absolute) / static::DAYS_PER_WEEK); + return $this->diffInDays($date, $absolute) / static::DAYS_PER_WEEK; } /** @@ -197,11 +238,14 @@ public function diffInWeeks($date = null, $absolute = true) * @param \Carbon\CarbonInterface|\DateTimeInterface|string|null $date * @param bool $absolute Get the absolute of the difference * - * @return int + * @return float */ - public function diffInDays($date = null, $absolute = true) + public function diffInDays($date = null, $absolute = true): float { - return (int) $this->diff($this->resolveCarbon($date), $absolute)->format('%r%a'); + $daysDiff = (int) $this->diff($this->resolveCarbon($date), $absolute)->format('%r%a'); + $hoursDiff = $this->diffInHours($date, $absolute); + + return ($hoursDiff < 0 ? -1 : 1) * $daysDiff + fmod($hoursDiff, static::HOURS_PER_DAY) / static::HOURS_PER_DAY; } /** @@ -213,7 +257,7 @@ public function diffInDays($date = null, $absolute = true) * * @return int */ - public function diffInDaysFiltered(Closure $callback, $date = null, $absolute = true) + public function diffInDaysFiltered(Closure $callback, $date = null, $absolute = true): int { return $this->diffFiltered(CarbonInterval::day(), $callback, $date, $absolute); } @@ -227,7 +271,7 @@ public function diffInDaysFiltered(Closure $callback, $date = null, $absolute = * * @return int */ - public function diffInHoursFiltered(Closure $callback, $date = null, $absolute = true) + public function diffInHoursFiltered(Closure $callback, $date = null, $absolute = true): int { return $this->diffFiltered(CarbonInterval::hour(), $callback, $date, $absolute); } @@ -242,7 +286,7 @@ public function diffInHoursFiltered(Closure $callback, $date = null, $absolute = * * @return int */ - public function diffFiltered(CarbonInterval $ci, Closure $callback, $date = null, $absolute = true) + public function diffFiltered(CarbonInterval $ci, Closure $callback, $date = null, $absolute = true): int { $start = $this; $end = $this->resolveCarbon($date); @@ -268,7 +312,7 @@ public function diffFiltered(CarbonInterval $ci, Closure $callback, $date = null * * @return int */ - public function diffInWeekdays($date = null, $absolute = true) + public function diffInWeekdays($date = null, $absolute = true): int { return $this->diffInDaysFiltered(function (CarbonInterface $date) { return $date->isWeekday(); @@ -283,7 +327,7 @@ public function diffInWeekdays($date = null, $absolute = true) * * @return int */ - public function diffInWeekendDays($date = null, $absolute = true) + public function diffInWeekendDays($date = null, $absolute = true): int { return $this->diffInDaysFiltered(function (CarbonInterface $date) { return $date->isWeekend(); @@ -296,24 +340,11 @@ public function diffInWeekendDays($date = null, $absolute = true) * @param \Carbon\CarbonInterface|\DateTimeInterface|string|null $date * @param bool $absolute Get the absolute of the difference * - * @return int - */ - public function diffInHours($date = null, $absolute = true) - { - return (int) ($this->diffInSeconds($date, $absolute) / static::SECONDS_PER_MINUTE / static::MINUTES_PER_HOUR); - } - - /** - * Get the difference in hours rounded down using timestamps. - * - * @param \Carbon\CarbonInterface|\DateTimeInterface|string|null $date - * @param bool $absolute Get the absolute of the difference - * - * @return int + * @return float */ - public function diffInRealHours($date = null, $absolute = true) + public function diffInHours($date = null, $absolute = true): float { - return (int) ($this->diffInRealSeconds($date, $absolute) / static::SECONDS_PER_MINUTE / static::MINUTES_PER_HOUR); + return $this->diffInMinutes($date, $absolute) / static::MINUTES_PER_HOUR; } /** @@ -322,24 +353,11 @@ public function diffInRealHours($date = null, $absolute = true) * @param \Carbon\CarbonInterface|\DateTimeInterface|string|null $date * @param bool $absolute Get the absolute of the difference * - * @return int - */ - public function diffInMinutes($date = null, $absolute = true) - { - return (int) ($this->diffInSeconds($date, $absolute) / static::SECONDS_PER_MINUTE); - } - - /** - * Get the difference in minutes rounded down using timestamps. - * - * @param \Carbon\CarbonInterface|\DateTimeInterface|string|null $date - * @param bool $absolute Get the absolute of the difference - * - * @return int + * @return float */ - public function diffInRealMinutes($date = null, $absolute = true) + public function diffInMinutes($date = null, $absolute = true): float { - return (int) ($this->diffInRealSeconds($date, $absolute) / static::SECONDS_PER_MINUTE); + return $this->diffInSeconds($date, $absolute) / static::SECONDS_PER_MINUTE; } /** @@ -348,22 +366,11 @@ public function diffInRealMinutes($date = null, $absolute = true) * @param \Carbon\CarbonInterface|\DateTimeInterface|string|null $date * @param bool $absolute Get the absolute of the difference * - * @return int + * @return float */ - public function diffInSeconds($date = null, $absolute = true) + public function diffInSeconds($date = null, $absolute = true): float { - $diff = $this->diff($date); - - if ($diff->days === 0) { - $diff = static::fixDiffInterval($diff, $absolute); - } - - $value = (((($diff->m || $diff->y ? $diff->days : $diff->d) * static::HOURS_PER_DAY) + - $diff->h) * static::MINUTES_PER_HOUR + - $diff->i) * static::SECONDS_PER_MINUTE + - $diff->s; - - return $absolute || !$diff->invert ? $value : -$value; + return $this->diffInMilliseconds($date, $absolute) / static::MILLISECONDS_PER_SECOND; } /** @@ -372,58 +379,9 @@ public function diffInSeconds($date = null, $absolute = true) * @param \Carbon\CarbonInterface|\DateTimeInterface|string|null $date * @param bool $absolute Get the absolute of the difference * - * @return int - */ - public function diffInMicroseconds($date = null, $absolute = true) - { - $diff = $this->diff($date); - $value = (int) round(((((($diff->m || $diff->y ? $diff->days : $diff->d) * static::HOURS_PER_DAY) + - $diff->h) * static::MINUTES_PER_HOUR + - $diff->i) * static::SECONDS_PER_MINUTE + - ($diff->f + $diff->s)) * static::MICROSECONDS_PER_SECOND); - - return $absolute || !$diff->invert ? $value : -$value; - } - - /** - * Get the difference in milliseconds rounded down. - * - * @param \Carbon\CarbonInterface|\DateTimeInterface|string|null $date - * @param bool $absolute Get the absolute of the difference - * - * @return int - */ - public function diffInMilliseconds($date = null, $absolute = true) - { - return (int) ($this->diffInMicroseconds($date, $absolute) / static::MICROSECONDS_PER_MILLISECOND); - } - - /** - * Get the difference in seconds using timestamps. - * - * @param \Carbon\CarbonInterface|\DateTimeInterface|string|null $date - * @param bool $absolute Get the absolute of the difference - * - * @return int - */ - public function diffInRealSeconds($date = null, $absolute = true) - { - /** @var CarbonInterface $date */ - $date = $this->resolveCarbon($date); - $value = $date->getTimestamp() - $this->getTimestamp(); - - return $absolute ? abs($value) : $value; - } - - /** - * Get the difference in microseconds using timestamps. - * - * @param \Carbon\CarbonInterface|\DateTimeInterface|string|null $date - * @param bool $absolute Get the absolute of the difference - * - * @return int + * @return float */ - public function diffInRealMicroseconds($date = null, $absolute = true) + public function diffInMicroseconds($date = null, $absolute = true): float { /** @var CarbonInterface $date */ $date = $this->resolveCarbon($date); @@ -434,264 +392,16 @@ public function diffInRealMicroseconds($date = null, $absolute = true) } /** - * Get the difference in milliseconds rounded down using timestamps. - * - * @param \Carbon\CarbonInterface|\DateTimeInterface|string|null $date - * @param bool $absolute Get the absolute of the difference - * - * @return int - */ - public function diffInRealMilliseconds($date = null, $absolute = true) - { - return (int) ($this->diffInRealMicroseconds($date, $absolute) / static::MICROSECONDS_PER_MILLISECOND); - } - - /** - * Get the difference in seconds as float (microsecond-precision). - * - * @param \Carbon\CarbonInterface|\DateTimeInterface|string|null $date - * @param bool $absolute Get the absolute of the difference - * - * @return float - */ - public function floatDiffInSeconds($date = null, $absolute = true) - { - return $this->diffInMicroseconds($date, $absolute) / static::MICROSECONDS_PER_SECOND; - } - - /** - * Get the difference in minutes as float (microsecond-precision). - * - * @param \Carbon\CarbonInterface|\DateTimeInterface|string|null $date - * @param bool $absolute Get the absolute of the difference - * - * @return float - */ - public function floatDiffInMinutes($date = null, $absolute = true) - { - return $this->floatDiffInSeconds($date, $absolute) / static::SECONDS_PER_MINUTE; - } - - /** - * Get the difference in hours as float (microsecond-precision). - * - * @param \Carbon\CarbonInterface|\DateTimeInterface|string|null $date - * @param bool $absolute Get the absolute of the difference - * - * @return float - */ - public function floatDiffInHours($date = null, $absolute = true) - { - return $this->floatDiffInMinutes($date, $absolute) / static::MINUTES_PER_HOUR; - } - - /** - * Get the difference in days as float (microsecond-precision). - * - * @param \Carbon\CarbonInterface|\DateTimeInterface|string|null $date - * @param bool $absolute Get the absolute of the difference - * - * @return float - */ - public function floatDiffInDays($date = null, $absolute = true) - { - $hoursDiff = $this->floatDiffInHours($date, $absolute); - - return ($hoursDiff < 0 ? -1 : 1) * $this->diffInDays($date) + fmod($hoursDiff, static::HOURS_PER_DAY) / static::HOURS_PER_DAY; - } - - /** - * Get the difference in months as float (microsecond-precision). - * - * @param \Carbon\CarbonInterface|\DateTimeInterface|string|null $date - * @param bool $absolute Get the absolute of the difference - * - * @return float - */ - public function floatDiffInMonths($date = null, $absolute = true) - { - $start = $this; - $end = $this->resolveCarbon($date); - $ascending = ($start <= $end); - $sign = $absolute || $ascending ? 1 : -1; - if (!$ascending) { - [$start, $end] = [$end, $start]; - } - $monthsDiff = $start->diffInMonths($end); - /** @var Carbon|CarbonImmutable $floorEnd */ - $floorEnd = $start->copy()->addMonths($monthsDiff); - - if ($floorEnd >= $end) { - return $sign * $monthsDiff; - } - - /** @var Carbon|CarbonImmutable $startOfMonthAfterFloorEnd */ - $startOfMonthAfterFloorEnd = $floorEnd->copy()->addMonth()->startOfMonth(); - - if ($startOfMonthAfterFloorEnd > $end) { - return $sign * ($monthsDiff + $floorEnd->floatDiffInDays($end) / $floorEnd->daysInMonth); - } - - return $sign * ($monthsDiff + $floorEnd->floatDiffInDays($startOfMonthAfterFloorEnd) / $floorEnd->daysInMonth + $startOfMonthAfterFloorEnd->floatDiffInDays($end) / $end->daysInMonth); - } - - /** - * Get the difference in year as float (microsecond-precision). - * - * @param \Carbon\CarbonInterface|\DateTimeInterface|string|null $date - * @param bool $absolute Get the absolute of the difference - * - * @return float - */ - public function floatDiffInYears($date = null, $absolute = true) - { - $start = $this; - $end = $this->resolveCarbon($date); - $ascending = ($start <= $end); - $sign = $absolute || $ascending ? 1 : -1; - if (!$ascending) { - [$start, $end] = [$end, $start]; - } - $yearsDiff = $start->diffInYears($end); - /** @var Carbon|CarbonImmutable $floorEnd */ - $floorEnd = $start->copy()->addYears($yearsDiff); - - if ($floorEnd >= $end) { - return $sign * $yearsDiff; - } - - /** @var Carbon|CarbonImmutable $startOfYearAfterFloorEnd */ - $startOfYearAfterFloorEnd = $floorEnd->copy()->addYear()->startOfYear(); - - if ($startOfYearAfterFloorEnd > $end) { - return $sign * ($yearsDiff + $floorEnd->floatDiffInDays($end) / $floorEnd->daysInYear); - } - - return $sign * ($yearsDiff + $floorEnd->floatDiffInDays($startOfYearAfterFloorEnd) / $floorEnd->daysInYear + $startOfYearAfterFloorEnd->floatDiffInDays($end) / $end->daysInYear); - } - - /** - * Get the difference in seconds as float (microsecond-precision) using timestamps. - * - * @param \Carbon\CarbonInterface|\DateTimeInterface|string|null $date - * @param bool $absolute Get the absolute of the difference - * - * @return float - */ - public function floatDiffInRealSeconds($date = null, $absolute = true) - { - return $this->diffInRealMicroseconds($date, $absolute) / static::MICROSECONDS_PER_SECOND; - } - - /** - * Get the difference in minutes as float (microsecond-precision) using timestamps. - * - * @param \Carbon\CarbonInterface|\DateTimeInterface|string|null $date - * @param bool $absolute Get the absolute of the difference - * - * @return float - */ - public function floatDiffInRealMinutes($date = null, $absolute = true) - { - return $this->floatDiffInRealSeconds($date, $absolute) / static::SECONDS_PER_MINUTE; - } - - /** - * Get the difference in hours as float (microsecond-precision) using timestamps. - * - * @param \Carbon\CarbonInterface|\DateTimeInterface|string|null $date - * @param bool $absolute Get the absolute of the difference - * - * @return float - */ - public function floatDiffInRealHours($date = null, $absolute = true) - { - return $this->floatDiffInRealMinutes($date, $absolute) / static::MINUTES_PER_HOUR; - } - - /** - * Get the difference in days as float (microsecond-precision). - * - * @param \Carbon\CarbonInterface|\DateTimeInterface|string|null $date - * @param bool $absolute Get the absolute of the difference - * - * @return float - */ - public function floatDiffInRealDays($date = null, $absolute = true) - { - $hoursDiff = $this->floatDiffInRealHours($date, $absolute); - - return ($hoursDiff < 0 ? -1 : 1) * $this->diffInDays($date) + fmod($hoursDiff, static::HOURS_PER_DAY) / static::HOURS_PER_DAY; - } - - /** - * Get the difference in months as float (microsecond-precision) using timestamps. - * - * @param \Carbon\CarbonInterface|\DateTimeInterface|string|null $date - * @param bool $absolute Get the absolute of the difference - * - * @return float - */ - public function floatDiffInRealMonths($date = null, $absolute = true) - { - $start = $this; - $end = $this->resolveCarbon($date); - $ascending = ($start <= $end); - $sign = $absolute || $ascending ? 1 : -1; - if (!$ascending) { - [$start, $end] = [$end, $start]; - } - $monthsDiff = $start->diffInMonths($end); - /** @var Carbon|CarbonImmutable $floorEnd */ - $floorEnd = $start->copy()->addMonths($monthsDiff); - - if ($floorEnd >= $end) { - return $sign * $monthsDiff; - } - - /** @var Carbon|CarbonImmutable $startOfMonthAfterFloorEnd */ - $startOfMonthAfterFloorEnd = $floorEnd->copy()->addMonth()->startOfMonth(); - - if ($startOfMonthAfterFloorEnd > $end) { - return $sign * ($monthsDiff + $floorEnd->floatDiffInRealDays($end) / $floorEnd->daysInMonth); - } - - return $sign * ($monthsDiff + $floorEnd->floatDiffInRealDays($startOfMonthAfterFloorEnd) / $floorEnd->daysInMonth + $startOfMonthAfterFloorEnd->floatDiffInRealDays($end) / $end->daysInMonth); - } - - /** - * Get the difference in year as float (microsecond-precision) using timestamps. + * Get the difference in milliseconds rounded down. * * @param \Carbon\CarbonInterface|\DateTimeInterface|string|null $date * @param bool $absolute Get the absolute of the difference * * @return float */ - public function floatDiffInRealYears($date = null, $absolute = true) + public function diffInMilliseconds($date = null, $absolute = true): float { - $start = $this; - $end = $this->resolveCarbon($date); - $ascending = ($start <= $end); - $sign = $absolute || $ascending ? 1 : -1; - if (!$ascending) { - [$start, $end] = [$end, $start]; - } - $yearsDiff = $start->diffInYears($end); - /** @var Carbon|CarbonImmutable $floorEnd */ - $floorEnd = $start->copy()->addYears($yearsDiff); - - if ($floorEnd >= $end) { - return $sign * $yearsDiff; - } - - /** @var Carbon|CarbonImmutable $startOfYearAfterFloorEnd */ - $startOfYearAfterFloorEnd = $floorEnd->copy()->addYear()->startOfYear(); - - if ($startOfYearAfterFloorEnd > $end) { - return $sign * ($yearsDiff + $floorEnd->floatDiffInRealDays($end) / $floorEnd->daysInYear); - } - - return $sign * ($yearsDiff + $floorEnd->floatDiffInRealDays($startOfYearAfterFloorEnd) / $floorEnd->daysInYear + $startOfYearAfterFloorEnd->floatDiffInRealDays($end) / $end->daysInYear); + return $this->diffInMicroseconds($date, $absolute) / static::MICROSECONDS_PER_MILLISECOND; } /** diff --git a/src/Carbon/Traits/Modifiers.php b/src/Carbon/Traits/Modifiers.php index e83df722a1..f3dfd63841 100644 --- a/src/Carbon/Traits/Modifiers.php +++ b/src/Carbon/Traits/Modifiers.php @@ -339,7 +339,7 @@ public function nthOfYear($nth, $dayOfWeek) */ public function average($date = null) { - return $this->addRealMicroseconds((int) ($this->diffInRealMicroseconds($this->resolveCarbon($date), false) / 2)); + return $this->addRealMicroseconds((int) ($this->diffInMicroseconds($this->resolveCarbon($date), false) / 2)); } /** @@ -352,7 +352,7 @@ public function average($date = null) */ public function closest($date1, $date2) { - return $this->diffInRealMicroseconds($date1) < $this->diffInRealMicroseconds($date2) ? $date1 : $date2; + return $this->diffInMicroseconds($date1) < $this->diffInMicroseconds($date2) ? $date1 : $date2; } /** @@ -365,7 +365,7 @@ public function closest($date1, $date2) */ public function farthest($date1, $date2) { - return $this->diffInRealMicroseconds($date1) > $this->diffInRealMicroseconds($date2) ? $date1 : $date2; + return $this->diffInMicroseconds($date1) > $this->diffInMicroseconds($date2) ? $date1 : $date2; } /** diff --git a/tests/Carbon/DiffTest.php b/tests/Carbon/DiffTest.php index 20047aa64c..2539fb526a 100644 --- a/tests/Carbon/DiffTest.php +++ b/tests/Carbon/DiffTest.php @@ -1557,87 +1557,87 @@ public function testFloatDiff() { date_default_timezone_set('UTC'); - $this->assertSame(8986.665965, Carbon::parse('2018-03-31 23:55:12.321456')->floatDiffInSeconds(Carbon::parse('2018-04-01 02:24:58.987421'))); + $this->assertSame(8986.665965, Carbon::parse('2018-03-31 23:55:12.321456')->diffInSeconds(Carbon::parse('2018-04-01 02:24:58.987421'))); - $this->assertSame(1.0006944444444443, Carbon::parse('2018-12-01 00:00')->floatDiffInDays(Carbon::parse('2018-12-02 00:01'))); + $this->assertSame(1.0006944444444443, Carbon::parse('2018-12-01 00:00')->diffInDays(Carbon::parse('2018-12-02 00:01'))); - $this->assertSame(0.9714742503779745, Carbon::parse('2018-03-13 20:55:12.321456')->floatDiffInMonths(Carbon::parse('2018-04-12 14:24:58.987421'))); - $this->assertSame(1.1724137931034484, Carbon::parse('2000-01-15 00:00')->floatDiffInMonths(Carbon::parse('2000-02-20 00:00'))); - $this->assertSame(1.1724137931034484, Carbon::parse('2000-01-15 00:00')->floatDiffInMonths('2000-02-20 00:00')); - $this->assertSame(11.951612903225806, Carbon::parse('2018-12-16 00:00')->floatDiffInMonths('2019-12-15 00:00')); - $this->assertSame(0.9714742503779745, Carbon::parse('2018-04-12 14:24:58.987421')->floatDiffInMonths(Carbon::parse('2018-03-13 20:55:12.321456'))); - $this->assertSame(0.9714742503779745, Carbon::parse('2018-03-13 20:55:12.321456')->floatDiffInMonths(Carbon::parse('2018-04-12 14:24:58.987421'), false)); - $this->assertSame(-0.9714742503779745, Carbon::parse('2018-04-12 14:24:58.987421')->floatDiffInMonths(Carbon::parse('2018-03-13 20:55:12.321456'), false)); + $this->assertSame(0.9714742503779745, Carbon::parse('2018-03-13 20:55:12.321456')->diffInMonths(Carbon::parse('2018-04-12 14:24:58.987421'))); + $this->assertSame(1.1724137931034484, Carbon::parse('2000-01-15 00:00')->diffInMonths(Carbon::parse('2000-02-20 00:00'))); + $this->assertSame(1.1724137931034484, Carbon::parse('2000-01-15 00:00')->diffInMonths('2000-02-20 00:00')); + $this->assertSame(11.951612903225806, Carbon::parse('2018-12-16 00:00')->diffInMonths('2019-12-15 00:00')); + $this->assertSame(0.9714742503779745, Carbon::parse('2018-04-12 14:24:58.987421')->diffInMonths(Carbon::parse('2018-03-13 20:55:12.321456'))); + $this->assertSame(0.9714742503779745, Carbon::parse('2018-03-13 20:55:12.321456')->diffInMonths(Carbon::parse('2018-04-12 14:24:58.987421'), false)); + $this->assertSame(-0.9714742503779745, Carbon::parse('2018-04-12 14:24:58.987421')->diffInMonths(Carbon::parse('2018-03-13 20:55:12.321456'), false)); - $this->assertSame(16.557633744585264, Carbon::parse('2018-02-13 20:55:12.321456')->floatDiffInMonths(Carbon::parse('2019-06-30 14:24:58.987421'))); + $this->assertSame(16.557633744585264, Carbon::parse('2018-02-13 20:55:12.321456')->diffInMonths(Carbon::parse('2019-06-30 14:24:58.987421'))); - $this->assertSame(15.971474250377973, Carbon::parse('2018-02-13 20:55:12.321456')->floatDiffInMonths(Carbon::parse('2019-06-12 14:24:58.987421'))); - $this->assertSame(15.971474250377973, Carbon::parse('2019-06-12 14:24:58.987421')->floatDiffInMonths(Carbon::parse('2018-02-13 20:55:12.321456'))); - $this->assertSame(15.971474250377973, Carbon::parse('2018-02-13 20:55:12.321456')->floatDiffInMonths(Carbon::parse('2019-06-12 14:24:58.987421'), false)); - $this->assertSame(-15.971474250377973, Carbon::parse('2019-06-12 14:24:58.987421')->floatDiffInMonths(Carbon::parse('2018-02-13 20:55:12.321456'), false)); + $this->assertSame(15.971474250377973, Carbon::parse('2018-02-13 20:55:12.321456')->diffInMonths(Carbon::parse('2019-06-12 14:24:58.987421'))); + $this->assertSame(15.971474250377973, Carbon::parse('2019-06-12 14:24:58.987421')->diffInMonths(Carbon::parse('2018-02-13 20:55:12.321456'))); + $this->assertSame(15.971474250377973, Carbon::parse('2018-02-13 20:55:12.321456')->diffInMonths(Carbon::parse('2019-06-12 14:24:58.987421'), false)); + $this->assertSame(-15.971474250377973, Carbon::parse('2019-06-12 14:24:58.987421')->diffInMonths(Carbon::parse('2018-02-13 20:55:12.321456'), false)); - $this->assertSame(1, Carbon::parse('2018-02-13 20:55:12.321456')->floatDiffInYears(Carbon::parse('2019-02-13 20:55:12.321456'))); - $this->assertSame(1.3746000338015283, Carbon::parse('2018-02-13 20:55:12.321456')->floatDiffInYears(Carbon::parse('2019-06-30 14:24:58.987421'))); - $this->assertSame(0.9609014036645421, Carbon::parse('2018-02-13 20:55:12.321456')->floatDiffInYears(Carbon::parse('2019-01-30 14:24:58.987421'))); + $this->assertSame(1, Carbon::parse('2018-02-13 20:55:12.321456')->diffInYears(Carbon::parse('2019-02-13 20:55:12.321456'))); + $this->assertSame(1.3746000338015283, Carbon::parse('2018-02-13 20:55:12.321456')->diffInYears(Carbon::parse('2019-06-30 14:24:58.987421'))); + $this->assertSame(0.9609014036645421, Carbon::parse('2018-02-13 20:55:12.321456')->diffInYears(Carbon::parse('2019-01-30 14:24:58.987421'))); - $this->assertSame(1.3252849653083778, Carbon::parse('2018-02-13 20:55:12.321456')->floatDiffInYears(Carbon::parse('2019-06-12 14:24:58.987421'))); - $this->assertSame(1.3252849653083778, Carbon::parse('2019-06-12 14:24:58.987421')->floatDiffInYears(Carbon::parse('2018-02-13 20:55:12.321456'))); - $this->assertSame(1.3252849653083778, Carbon::parse('2018-02-13 20:55:12.321456')->floatDiffInYears(Carbon::parse('2019-06-12 14:24:58.987421'), false)); - $this->assertSame(-1.3252849653083778, Carbon::parse('2019-06-12 14:24:58.987421')->floatDiffInYears(Carbon::parse('2018-02-13 20:55:12.321456'), false)); + $this->assertSame(1.3252849653083778, Carbon::parse('2018-02-13 20:55:12.321456')->diffInYears(Carbon::parse('2019-06-12 14:24:58.987421'))); + $this->assertSame(1.3252849653083778, Carbon::parse('2019-06-12 14:24:58.987421')->diffInYears(Carbon::parse('2018-02-13 20:55:12.321456'))); + $this->assertSame(1.3252849653083778, Carbon::parse('2018-02-13 20:55:12.321456')->diffInYears(Carbon::parse('2019-06-12 14:24:58.987421'), false)); + $this->assertSame(-1.3252849653083778, Carbon::parse('2019-06-12 14:24:58.987421')->diffInYears(Carbon::parse('2018-02-13 20:55:12.321456'), false)); - $this->assertSame(5.325284965308378, Carbon::parse('2018-02-13 20:55:12.321456')->floatDiffInYears(Carbon::parse('2023-06-12 14:24:58.987421'))); - $this->assertSame(5.325284965308378, Carbon::parse('2023-06-12 14:24:58.987421')->floatDiffInYears(Carbon::parse('2018-02-13 20:55:12.321456'))); - $this->assertSame(5.325284965308378, Carbon::parse('2018-02-13 20:55:12.321456')->floatDiffInYears(Carbon::parse('2023-06-12 14:24:58.987421'), false)); - $this->assertSame(-5.325284965308378, Carbon::parse('2023-06-12 14:24:58.987421')->floatDiffInYears(Carbon::parse('2018-02-13 20:55:12.321456'), false)); + $this->assertSame(5.325284965308378, Carbon::parse('2018-02-13 20:55:12.321456')->diffInYears(Carbon::parse('2023-06-12 14:24:58.987421'))); + $this->assertSame(5.325284965308378, Carbon::parse('2023-06-12 14:24:58.987421')->diffInYears(Carbon::parse('2018-02-13 20:55:12.321456'))); + $this->assertSame(5.325284965308378, Carbon::parse('2018-02-13 20:55:12.321456')->diffInYears(Carbon::parse('2023-06-12 14:24:58.987421'), false)); + $this->assertSame(-5.325284965308378, Carbon::parse('2023-06-12 14:24:58.987421')->diffInYears(Carbon::parse('2018-02-13 20:55:12.321456'), false)); - $this->assertSame(1, Carbon::parse('2018-10-01 00:00:00', 'Europe/Paris')->floatDiffInMonths(Carbon::parse('2018-11-01 00:00:00', 'Europe/Paris'))); - $this->assertSame(1, Carbon::parse('2018-10-28 00:00:00')->floatDiffInMonths(Carbon::parse('2018-11-28 00:00:00'))); - $this->assertSame(1, Carbon::parse('2018-10-28 00:00:00', 'Europe/Paris')->floatDiffInMonths(Carbon::parse('2018-11-28 00:00:00', 'Europe/Paris'))); + $this->assertSame(1, Carbon::parse('2018-10-01 00:00:00', 'Europe/Paris')->diffInMonths(Carbon::parse('2018-11-01 00:00:00', 'Europe/Paris'))); + $this->assertSame(1, Carbon::parse('2018-10-28 00:00:00')->diffInMonths(Carbon::parse('2018-11-28 00:00:00'))); + $this->assertSame(1, Carbon::parse('2018-10-28 00:00:00', 'Europe/Paris')->diffInMonths(Carbon::parse('2018-11-28 00:00:00', 'Europe/Paris'))); - $this->assertSame(-1, Carbon::parse('2018-11-01 00:00:00', 'Europe/Paris')->floatDiffInMonths(Carbon::parse('2018-10-01 00:00:00', 'Europe/Paris'), false)); - $this->assertSame(-1, Carbon::parse('2018-11-28 00:00:00')->floatDiffInMonths(Carbon::parse('2018-10-28 00:00:00'), false)); - $this->assertSame(-1, Carbon::parse('2018-11-28 00:00:00', 'Europe/Paris')->floatDiffInMonths(Carbon::parse('2018-10-28 00:00:00', 'Europe/Paris'), false)); + $this->assertSame(-1, Carbon::parse('2018-11-01 00:00:00', 'Europe/Paris')->diffInMonths(Carbon::parse('2018-10-01 00:00:00', 'Europe/Paris'), false)); + $this->assertSame(-1, Carbon::parse('2018-11-28 00:00:00')->diffInMonths(Carbon::parse('2018-10-28 00:00:00'), false)); + $this->assertSame(-1, Carbon::parse('2018-11-28 00:00:00', 'Europe/Paris')->diffInMonths(Carbon::parse('2018-10-28 00:00:00', 'Europe/Paris'), false)); } public function testFloatDiffWithRealUnits() { date_default_timezone_set('UTC'); - $this->assertSame(1.0006944444444443, Carbon::parse('2018-12-01 00:00')->floatDiffInRealDays(Carbon::parse('2018-12-02 00:01'))); + $this->assertSame(1.0006944444444443, Carbon::parse('2018-12-01 00:00')->diffInDays(Carbon::parse('2018-12-02 00:01'))); - $this->assertSame(0.9714742503779745, Carbon::parse('2018-03-13 20:55:12.321456')->floatDiffInRealMonths(Carbon::parse('2018-04-12 14:24:58.987421'))); - $this->assertSame(0.9714742503779745, Carbon::parse('2018-04-12 14:24:58.987421')->floatDiffInRealMonths(Carbon::parse('2018-03-13 20:55:12.321456'))); - $this->assertSame(0.9714742503779745, Carbon::parse('2018-03-13 20:55:12.321456')->floatDiffInRealMonths(Carbon::parse('2018-04-12 14:24:58.987421'), false)); - $this->assertSame(-0.9714742503779745, Carbon::parse('2018-04-12 14:24:58.987421')->floatDiffInRealMonths(Carbon::parse('2018-03-13 20:55:12.321456'), false)); + $this->assertSame(0.9714742503779745, Carbon::parse('2018-03-13 20:55:12.321456')->diffInMonths(Carbon::parse('2018-04-12 14:24:58.987421'))); + $this->assertSame(0.9714742503779745, Carbon::parse('2018-04-12 14:24:58.987421')->diffInMonths(Carbon::parse('2018-03-13 20:55:12.321456'))); + $this->assertSame(0.9714742503779745, Carbon::parse('2018-03-13 20:55:12.321456')->diffInMonths(Carbon::parse('2018-04-12 14:24:58.987421'), false)); + $this->assertSame(-0.9714742503779745, Carbon::parse('2018-04-12 14:24:58.987421')->diffInMonths(Carbon::parse('2018-03-13 20:55:12.321456'), false)); - $this->assertSame(16.557633744585264, Carbon::parse('2018-02-13 20:55:12.321456')->floatDiffInRealMonths(Carbon::parse('2019-06-30 14:24:58.987421'))); + $this->assertSame(16.557633744585264, Carbon::parse('2018-02-13 20:55:12.321456')->diffInMonths(Carbon::parse('2019-06-30 14:24:58.987421'))); - $this->assertSame(15.971474250377973, Carbon::parse('2018-02-13 20:55:12.321456')->floatDiffInRealMonths(Carbon::parse('2019-06-12 14:24:58.987421'))); - $this->assertSame(15.971474250377973, Carbon::parse('2019-06-12 14:24:58.987421')->floatDiffInRealMonths(Carbon::parse('2018-02-13 20:55:12.321456'))); - $this->assertSame(15.971474250377973, Carbon::parse('2018-02-13 20:55:12.321456')->floatDiffInRealMonths(Carbon::parse('2019-06-12 14:24:58.987421'), false)); - $this->assertSame(-15.971474250377973, Carbon::parse('2019-06-12 14:24:58.987421')->floatDiffInRealMonths(Carbon::parse('2018-02-13 20:55:12.321456'), false)); + $this->assertSame(15.971474250377973, Carbon::parse('2018-02-13 20:55:12.321456')->diffInMonths(Carbon::parse('2019-06-12 14:24:58.987421'))); + $this->assertSame(15.971474250377973, Carbon::parse('2019-06-12 14:24:58.987421')->diffInMonths(Carbon::parse('2018-02-13 20:55:12.321456'))); + $this->assertSame(15.971474250377973, Carbon::parse('2018-02-13 20:55:12.321456')->diffInMonths(Carbon::parse('2019-06-12 14:24:58.987421'), false)); + $this->assertSame(-15.971474250377973, Carbon::parse('2019-06-12 14:24:58.987421')->diffInMonths(Carbon::parse('2018-02-13 20:55:12.321456'), false)); - $this->assertSame(1, Carbon::parse('2018-02-13 20:55:12.321456')->floatDiffInRealYears(Carbon::parse('2019-02-13 20:55:12.321456'))); - $this->assertSame(1.3746000338015283, Carbon::parse('2018-02-13 20:55:12.321456')->floatDiffInRealYears(Carbon::parse('2019-06-30 14:24:58.987421'))); - $this->assertSame(0.9609014036645421, Carbon::parse('2018-02-13 20:55:12.321456')->floatDiffInRealYears(Carbon::parse('2019-01-30 14:24:58.987421'))); + $this->assertSame(1, Carbon::parse('2018-02-13 20:55:12.321456')->diffInYears(Carbon::parse('2019-02-13 20:55:12.321456'))); + $this->assertSame(1.3746000338015283, Carbon::parse('2018-02-13 20:55:12.321456')->diffInYears(Carbon::parse('2019-06-30 14:24:58.987421'))); + $this->assertSame(0.9609014036645421, Carbon::parse('2018-02-13 20:55:12.321456')->diffInYears(Carbon::parse('2019-01-30 14:24:58.987421'))); - $this->assertSame(1.3252849653083778, Carbon::parse('2018-02-13 20:55:12.321456')->floatDiffInRealYears(Carbon::parse('2019-06-12 14:24:58.987421'))); - $this->assertSame(1.3252849653083778, Carbon::parse('2019-06-12 14:24:58.987421')->floatDiffInRealYears(Carbon::parse('2018-02-13 20:55:12.321456'))); - $this->assertSame(1.3252849653083778, Carbon::parse('2018-02-13 20:55:12.321456')->floatDiffInRealYears(Carbon::parse('2019-06-12 14:24:58.987421'), false)); - $this->assertSame(-1.3252849653083778, Carbon::parse('2019-06-12 14:24:58.987421')->floatDiffInRealYears(Carbon::parse('2018-02-13 20:55:12.321456'), false)); + $this->assertSame(1.3252849653083778, Carbon::parse('2018-02-13 20:55:12.321456')->diffInYears(Carbon::parse('2019-06-12 14:24:58.987421'))); + $this->assertSame(1.3252849653083778, Carbon::parse('2019-06-12 14:24:58.987421')->diffInYears(Carbon::parse('2018-02-13 20:55:12.321456'))); + $this->assertSame(1.3252849653083778, Carbon::parse('2018-02-13 20:55:12.321456')->diffInYears(Carbon::parse('2019-06-12 14:24:58.987421'), false)); + $this->assertSame(-1.3252849653083778, Carbon::parse('2019-06-12 14:24:58.987421')->diffInYears(Carbon::parse('2018-02-13 20:55:12.321456'), false)); - $this->assertSame(5.325284965308378, Carbon::parse('2018-02-13 20:55:12.321456')->floatDiffInRealYears(Carbon::parse('2023-06-12 14:24:58.987421'))); - $this->assertSame(5.325284965308378, Carbon::parse('2023-06-12 14:24:58.987421')->floatDiffInRealYears(Carbon::parse('2018-02-13 20:55:12.321456'))); - $this->assertSame(5.325284965308378, Carbon::parse('2018-02-13 20:55:12.321456')->floatDiffInRealYears(Carbon::parse('2023-06-12 14:24:58.987421'), false)); - $this->assertSame(-5.325284965308378, Carbon::parse('2023-06-12 14:24:58.987421')->floatDiffInRealYears(Carbon::parse('2018-02-13 20:55:12.321456'), false)); + $this->assertSame(5.325284965308378, Carbon::parse('2018-02-13 20:55:12.321456')->diffInYears(Carbon::parse('2023-06-12 14:24:58.987421'))); + $this->assertSame(5.325284965308378, Carbon::parse('2023-06-12 14:24:58.987421')->diffInYears(Carbon::parse('2018-02-13 20:55:12.321456'))); + $this->assertSame(5.325284965308378, Carbon::parse('2018-02-13 20:55:12.321456')->diffInYears(Carbon::parse('2023-06-12 14:24:58.987421'), false)); + $this->assertSame(-5.325284965308378, Carbon::parse('2023-06-12 14:24:58.987421')->diffInYears(Carbon::parse('2018-02-13 20:55:12.321456'), false)); - $this->assertSame(1, Carbon::parse('2018-10-01 00:00:00', 'Europe/Paris')->floatDiffInRealMonths(Carbon::parse('2018-11-01 00:00:00', 'Europe/Paris'))); - $this->assertSame(1, Carbon::parse('2018-10-28 00:00:00')->floatDiffInRealMonths(Carbon::parse('2018-11-28 00:00:00'))); - $this->assertSame(1, Carbon::parse('2018-10-28 00:00:00', 'Europe/Paris')->floatDiffInRealMonths(Carbon::parse('2018-11-28 00:00:00', 'Europe/Paris'))); + $this->assertSame(1, Carbon::parse('2018-10-01 00:00:00', 'Europe/Paris')->diffInMonths(Carbon::parse('2018-11-01 00:00:00', 'Europe/Paris'))); + $this->assertSame(1, Carbon::parse('2018-10-28 00:00:00')->diffInMonths(Carbon::parse('2018-11-28 00:00:00'))); + $this->assertSame(1, Carbon::parse('2018-10-28 00:00:00', 'Europe/Paris')->diffInMonths(Carbon::parse('2018-11-28 00:00:00', 'Europe/Paris'))); - $this->assertSame(-1, Carbon::parse('2018-11-01 00:00:00', 'Europe/Paris')->floatDiffInRealMonths(Carbon::parse('2018-10-01 00:00:00', 'Europe/Paris'), false)); - $this->assertSame(-1, Carbon::parse('2018-11-28 00:00:00')->floatDiffInRealMonths(Carbon::parse('2018-10-28 00:00:00'), false)); - $this->assertSame(-1, Carbon::parse('2018-11-28 00:00:00', 'Europe/Paris')->floatDiffInRealMonths(Carbon::parse('2018-10-28 00:00:00', 'Europe/Paris'), false)); + $this->assertSame(-1, Carbon::parse('2018-11-01 00:00:00', 'Europe/Paris')->diffInMonths(Carbon::parse('2018-10-01 00:00:00', 'Europe/Paris'), false)); + $this->assertSame(-1, Carbon::parse('2018-11-28 00:00:00')->diffInMonths(Carbon::parse('2018-10-28 00:00:00'), false)); + $this->assertSame(-1, Carbon::parse('2018-11-28 00:00:00', 'Europe/Paris')->diffInMonths(Carbon::parse('2018-10-28 00:00:00', 'Europe/Paris'), false)); } /** diff --git a/tests/Carbon/ModifyTest.php b/tests/Carbon/ModifyTest.php index d1133ec293..252de43eff 100644 --- a/tests/Carbon/ModifyTest.php +++ b/tests/Carbon/ModifyTest.php @@ -31,32 +31,25 @@ public function testTimezoneModify() $a = new Carbon('2014-03-30 00:00:00', 'Europe/London'); $b = $a->copy(); $b->addHours(24); - $this->assertSame(24, $a->diffInHours($b)); - $this->assertSame(24, $a->diffInHours($b, false)); - $this->assertSame(24, $b->diffInHours($a)); - $this->assertSame(-24, $b->diffInHours($a, false)); - $this->assertSame(-23, $b->diffInRealHours($a, false)); - $this->assertSame(-23 * 60, $b->diffInRealMinutes($a, false)); - $this->assertSame(-23 * 60 * 60, $b->diffInRealSeconds($a, false)); - $this->assertSame(-24 * 60 * 60 * 1000, $b->diffInMilliseconds($a, false)); - $this->assertSame(-23 * 60 * 60 * 1000, $b->diffInRealMilliseconds($a, false)); - $this->assertSame(-23 * 60 * 60 * 1000000, $b->diffInRealMicroseconds($a, false)); - $this->assertSame(-24 * 60 * 60 * 1000000, $b->diffInMicroseconds($a, false)); + $this->assertSame(23.0, $a->diffInHours($b, false)); + $this->assertSame(23.0, $b->diffInHours($a)); + $this->assertSame(-23.0, $b->diffInHours($a, false)); + $this->assertSame(-23.0 * 60, $b->diffInMinutes($a, false)); + $this->assertSame(-23.0 * 60 * 60, $b->diffInSeconds($a, false)); + $this->assertSame(-23.0 * 60 * 60 * 1000, $b->diffInMilliseconds($a, false)); + $this->assertSame(-23.0 * 60 * 60 * 1000000, $b->diffInMicroseconds($a, false)); $a = new Carbon('2014-03-30 00:00:00', 'Europe/London'); $b = $a->copy(); $b->addRealHours(24); - $this->assertSame(-24, $b->diffInRealHours($a, false)); - $this->assertSame(-24 * 60, $b->diffInRealMinutes($a, false)); - $this->assertSame(-24 * 60 * 60, $b->diffInRealSeconds($a, false)); - $this->assertSame(-25 * 60 * 60 * 1000, $b->diffInMilliseconds($a, false)); - $this->assertSame(-24 * 60 * 60 * 1000, $b->diffInRealMilliseconds($a, false)); - $this->assertSame(-24 * 60 * 60 * 1000000, $b->diffInRealMicroseconds($a, false)); - $this->assertSame(-25 * 60 * 60 * 1000000, $b->diffInMicroseconds($a, false)); - $this->assertSame(-25, $b->diffInHours($a, false)); + $this->assertSame(-24.0, $b->diffInHours($a, false)); + $this->assertSame(-24.0 * 60, $b->diffInMinutes($a, false)); + $this->assertSame(-24.0 * 60 * 60, $b->diffInSeconds($a, false)); + $this->assertSame(-24.0 * 60 * 60 * 1000, $b->diffInMilliseconds($a, false)); + $this->assertSame(-24.0 * 60 * 60 * 1000000, $b->diffInMicroseconds($a, false)); $b->subRealHours(24); - $this->assertSame(0, $b->diffInRealHours($a, false)); - $this->assertSame(0, $b->diffInHours($a, false)); + $this->assertSame(0.0, $b->diffInHours($a, false)); + $this->assertSame(0.0, $b->diffInHours($a, false)); $a = new Carbon('2014-03-30 00:59:00', 'Europe/London'); $a->addRealHour(); @@ -109,90 +102,74 @@ public function testTimezoneModify() $a = new Carbon('2014-03-30 00:00:00', 'Europe/London'); $b = $a->copy(); $b->addRealDay(); - $this->assertSame(-24, $b->diffInRealHours($a, false)); - $this->assertSame(-24 * 60, $b->diffInRealMinutes($a, false)); - $this->assertSame(-24 * 60 * 60, $b->diffInRealSeconds($a, false)); - $this->assertSame(-24 * 60 * 60 * 1000, $b->diffInRealMilliseconds($a, false)); - $this->assertSame(-24 * 60 * 60 * 1000000, $b->diffInRealMicroseconds($a, false)); - $this->assertSame(-25 * 60 * 60 * 1000000, $b->diffInMicroseconds($a, false)); - $this->assertSame(-25, $b->diffInHours($a, false)); + $this->assertSame(-24.0, $b->diffInHours($a, false)); + $this->assertSame(-24.0 * 60, $b->diffInMinutes($a, false)); + $this->assertSame(-24.0 * 60 * 60, $b->diffInSeconds($a, false)); + $this->assertSame(-24.0 * 60 * 60 * 1000, $b->diffInMilliseconds($a, false)); + $this->assertSame(-24.0 * 60 * 60 * 1000000, $b->diffInMicroseconds($a, false)); $a = new Carbon('2014-03-30 00:00:00', 'Europe/London'); $b = $a->copy(); $b->addRealWeeks(1 / 7); - $this->assertSame(-24, $b->diffInRealHours($a, false)); - $this->assertSame(-24 * 60, $b->diffInRealMinutes($a, false)); - $this->assertSame(-24 * 60 * 60, $b->diffInRealSeconds($a, false)); - $this->assertSame(-24 * 60 * 60 * 1000, $b->diffInRealMilliseconds($a, false)); - $this->assertSame(-24 * 60 * 60 * 1000000, $b->diffInRealMicroseconds($a, false)); - $this->assertSame(-25 * 60 * 60 * 1000000, $b->diffInMicroseconds($a, false)); - $this->assertSame(-25, $b->diffInHours($a, false)); + $this->assertSame(-24.0, $b->diffInHours($a, false)); + $this->assertSame(-24.0 * 60, $b->diffInMinutes($a, false)); + $this->assertSame(-24.0 * 60 * 60, $b->diffInSeconds($a, false)); + $this->assertSame(-24.0 * 60 * 60 * 1000, $b->diffInMilliseconds($a, false)); + $this->assertSame(-24.0 * 60 * 60 * 1000000, $b->diffInMicroseconds($a, false)); $a = new Carbon('2014-03-30 00:00:00', 'Europe/London'); $b = $a->copy(); $b->addRealMonths(1 / 30); - $this->assertSame(-24, $b->diffInRealHours($a, false)); - $this->assertSame(-24 * 60, $b->diffInRealMinutes($a, false)); - $this->assertSame(-24 * 60 * 60, $b->diffInRealSeconds($a, false)); - $this->assertSame(-24 * 60 * 60 * 1000, $b->diffInRealMilliseconds($a, false)); - $this->assertSame(-24 * 60 * 60 * 1000000, $b->diffInRealMicroseconds($a, false)); - $this->assertSame(-25 * 60 * 60 * 1000000, $b->diffInMicroseconds($a, false)); - $this->assertSame(-25, $b->diffInHours($a, false)); + $this->assertSame(-24.0, $b->diffInHours($a, false)); + $this->assertSame(-24.0 * 60, $b->diffInMinutes($a, false)); + $this->assertSame(-24.0 * 60 * 60, $b->diffInSeconds($a, false)); + $this->assertSame(-24.0 * 60 * 60 * 1000, $b->diffInMilliseconds($a, false)); + $this->assertSame(-24.0 * 60 * 60 * 1000000, $b->diffInMicroseconds($a, false)); $a = new Carbon('2014-03-30 00:00:00', 'Europe/London'); $b = $a->copy(); $b->addRealQuarters(1 / 90); - $this->assertSame(-24, $b->diffInRealHours($a, false)); - $this->assertSame(-24 * 60, $b->diffInRealMinutes($a, false)); - $this->assertSame(-24 * 60 * 60, $b->diffInRealSeconds($a, false)); - $this->assertSame(-24 * 60 * 60 * 1000, $b->diffInRealMilliseconds($a, false)); - $this->assertSame(-24 * 60 * 60 * 1000000, $b->diffInRealMicroseconds($a, false)); - $this->assertSame(-25 * 60 * 60 * 1000000, $b->diffInMicroseconds($a, false)); - $this->assertSame(-25, $b->diffInHours($a, false)); + $this->assertSame(-24.0, $b->diffInHours($a, false)); + $this->assertSame(-24.0 * 60, $b->diffInMinutes($a, false)); + $this->assertSame(-24.0 * 60 * 60, $b->diffInSeconds($a, false)); + $this->assertSame(-24.0 * 60 * 60 * 1000, $b->diffInMilliseconds($a, false)); + $this->assertSame(-24.0 * 60 * 60 * 1000000, $b->diffInMicroseconds($a, false)); $a = new Carbon('2014-03-30 00:00:00', 'Europe/London'); $b = $a->copy(); $b->addRealYears(1 / 365); - $this->assertSame(-24, $b->diffInRealHours($a, false)); - $this->assertSame(-24 * 60, $b->diffInRealMinutes($a, false)); - $this->assertSame(-24 * 60 * 60, $b->diffInRealSeconds($a, false)); - $this->assertSame(-24 * 60 * 60 * 1000, $b->diffInRealMilliseconds($a, false)); - $this->assertSame(-24 * 60 * 60 * 1000000, $b->diffInRealMicroseconds($a, false)); - $this->assertSame(-25 * 60 * 60 * 1000000, $b->diffInMicroseconds($a, false)); - $this->assertSame(-25, $b->diffInHours($a, false)); + $this->assertSame(-24.0, $b->diffInHours($a, false)); + $this->assertSame(-24.0 * 60, $b->diffInMinutes($a, false)); + $this->assertSame(-24.0 * 60 * 60, $b->diffInSeconds($a, false)); + $this->assertSame(-24.0 * 60 * 60 * 1000, $b->diffInMilliseconds($a, false)); + $this->assertSame(-24.0 * 60 * 60 * 1000000, $b->diffInMicroseconds($a, false)); $a = new Carbon('2014-03-30 00:00:00', 'Europe/London'); $b = $a->copy(); $b->addRealDecades(1 / 3650); - $this->assertSame(-24, $b->diffInRealHours($a, false)); - $this->assertSame(-24 * 60, $b->diffInRealMinutes($a, false)); - $this->assertSame(-24 * 60 * 60, $b->diffInRealSeconds($a, false)); - $this->assertSame(-24 * 60 * 60 * 1000, $b->diffInRealMilliseconds($a, false)); - $this->assertSame(-24 * 60 * 60 * 1000000, $b->diffInRealMicroseconds($a, false)); - $this->assertSame(-25 * 60 * 60 * 1000000, $b->diffInMicroseconds($a, false)); - $this->assertSame(-25, $b->diffInHours($a, false)); + $this->assertSame(-24.0, $b->diffInHours($a, false)); + $this->assertSame(-24.0 * 60, $b->diffInMinutes($a, false)); + $this->assertSame(-24.0 * 60 * 60, $b->diffInSeconds($a, false)); + $this->assertSame(-24.0 * 60 * 60 * 1000, $b->diffInMilliseconds($a, false)); + $this->assertSame(-24.0 * 60 * 60 * 1000000, $b->diffInMicroseconds($a, false)); $a = new Carbon('2014-03-30 00:00:00', 'Europe/London'); $b = $a->copy(); $b->addRealCenturies(1 / 36500); - $this->assertSame(-24, $b->diffInRealHours($a, false)); - $this->assertSame(-24 * 60, $b->diffInRealMinutes($a, false)); - $this->assertSame(-24 * 60 * 60, $b->diffInRealSeconds($a, false)); - $this->assertSame(-24 * 60 * 60 * 1000, $b->diffInRealMilliseconds($a, false)); - $this->assertSame(-24 * 60 * 60 * 1000000, $b->diffInRealMicroseconds($a, false)); - $this->assertSame(-25 * 60 * 60 * 1000000, $b->diffInMicroseconds($a, false)); - $this->assertSame(-25, $b->diffInHours($a, false)); + $this->assertSame(-24.0, $b->diffInHours($a, false)); + $this->assertSame(-24.0 * 60, $b->diffInMinutes($a, false)); + $this->assertSame(-24.0 * 60 * 60, $b->diffInSeconds($a, false)); + $this->assertSame(-24.0 * 60 * 60 * 1000, $b->diffInMilliseconds($a, false)); + $this->assertSame(-24.0 * 60 * 60 * 1000000, $b->diffInMicroseconds($a, false)); $a = new Carbon('2014-03-30 00:00:00', 'Europe/London'); $b = $a->copy(); $b->addRealMillennia(1 / 365000); - $this->assertSame(-24, $b->diffInRealHours($a, false)); - $this->assertSame(-24 * 60, $b->diffInRealMinutes($a, false)); - $this->assertSame(-24 * 60 * 60, $b->diffInRealSeconds($a, false)); - $this->assertSame(-24 * 60 * 60 * 1000, $b->diffInRealMilliseconds($a, false)); - $this->assertSame(-24 * 60 * 60 * 1000000, $b->diffInRealMicroseconds($a, false)); - $this->assertSame(-25 * 60 * 60 * 1000000, $b->diffInMicroseconds($a, false)); - $this->assertSame(-25, $b->diffInHours($a, false)); + $this->assertSame(-24.0, $b->diffInHours($a, false)); + $this->assertSame(-24.0 * 60, $b->diffInMinutes($a, false)); + $this->assertSame(-24.0 * 60 * 60, $b->diffInSeconds($a, false)); + $this->assertSame(-24.0 * 60 * 60 * 1000, $b->diffInMilliseconds($a, false)); + $this->assertSame(-24.0 * 60 * 60 * 1000000, $b->diffInMicroseconds($a, false)); } public function testAddRealUnitException() diff --git a/tests/CarbonImmutable/DiffTest.php b/tests/CarbonImmutable/DiffTest.php index 11c74fb01d..4b9c2af95c 100644 --- a/tests/CarbonImmutable/DiffTest.php +++ b/tests/CarbonImmutable/DiffTest.php @@ -1538,84 +1538,84 @@ public function testFloatDiff() { date_default_timezone_set('UTC'); - $this->assertSame(8986.665965, Carbon::parse('2018-03-31 23:55:12.321456')->floatDiffInSeconds(Carbon::parse('2018-04-01 02:24:58.987421'))); + $this->assertSame(8986.665965, Carbon::parse('2018-03-31 23:55:12.321456')->diffInSeconds(Carbon::parse('2018-04-01 02:24:58.987421'))); - $this->assertSame(1.0006944444444443, Carbon::parse('2018-12-01 00:00')->floatDiffInDays(Carbon::parse('2018-12-02 00:01'))); + $this->assertSame(1.0006944444444443, Carbon::parse('2018-12-01 00:00')->diffInDays(Carbon::parse('2018-12-02 00:01'))); - $this->assertSame(0.9714742503779745, Carbon::parse('2018-03-13 20:55:12.321456')->floatDiffInMonths(Carbon::parse('2018-04-12 14:24:58.987421'))); - $this->assertSame(0.9714742503779745, Carbon::parse('2018-04-12 14:24:58.987421')->floatDiffInMonths(Carbon::parse('2018-03-13 20:55:12.321456'))); - $this->assertSame(0.9714742503779745, Carbon::parse('2018-03-13 20:55:12.321456')->floatDiffInMonths(Carbon::parse('2018-04-12 14:24:58.987421'), false)); - $this->assertSame(-0.9714742503779745, Carbon::parse('2018-04-12 14:24:58.987421')->floatDiffInMonths(Carbon::parse('2018-03-13 20:55:12.321456'), false)); + $this->assertSame(0.9714742503779745, Carbon::parse('2018-03-13 20:55:12.321456')->diffInMonths(Carbon::parse('2018-04-12 14:24:58.987421'))); + $this->assertSame(0.9714742503779745, Carbon::parse('2018-04-12 14:24:58.987421')->diffInMonths(Carbon::parse('2018-03-13 20:55:12.321456'))); + $this->assertSame(0.9714742503779745, Carbon::parse('2018-03-13 20:55:12.321456')->diffInMonths(Carbon::parse('2018-04-12 14:24:58.987421'), false)); + $this->assertSame(-0.9714742503779745, Carbon::parse('2018-04-12 14:24:58.987421')->diffInMonths(Carbon::parse('2018-03-13 20:55:12.321456'), false)); - $this->assertSame(16.557633744585264, Carbon::parse('2018-02-13 20:55:12.321456')->floatDiffInMonths(Carbon::parse('2019-06-30 14:24:58.987421'))); + $this->assertSame(16.557633744585264, Carbon::parse('2018-02-13 20:55:12.321456')->diffInMonths(Carbon::parse('2019-06-30 14:24:58.987421'))); - $this->assertSame(15.971474250377973, Carbon::parse('2018-02-13 20:55:12.321456')->floatDiffInMonths(Carbon::parse('2019-06-12 14:24:58.987421'))); - $this->assertSame(15.971474250377973, Carbon::parse('2019-06-12 14:24:58.987421')->floatDiffInMonths(Carbon::parse('2018-02-13 20:55:12.321456'))); - $this->assertSame(15.971474250377973, Carbon::parse('2018-02-13 20:55:12.321456')->floatDiffInMonths(Carbon::parse('2019-06-12 14:24:58.987421'), false)); - $this->assertSame(-15.971474250377973, Carbon::parse('2019-06-12 14:24:58.987421')->floatDiffInMonths(Carbon::parse('2018-02-13 20:55:12.321456'), false)); + $this->assertSame(15.971474250377973, Carbon::parse('2018-02-13 20:55:12.321456')->diffInMonths(Carbon::parse('2019-06-12 14:24:58.987421'))); + $this->assertSame(15.971474250377973, Carbon::parse('2019-06-12 14:24:58.987421')->diffInMonths(Carbon::parse('2018-02-13 20:55:12.321456'))); + $this->assertSame(15.971474250377973, Carbon::parse('2018-02-13 20:55:12.321456')->diffInMonths(Carbon::parse('2019-06-12 14:24:58.987421'), false)); + $this->assertSame(-15.971474250377973, Carbon::parse('2019-06-12 14:24:58.987421')->diffInMonths(Carbon::parse('2018-02-13 20:55:12.321456'), false)); - $this->assertSame(1, Carbon::parse('2018-02-13 20:55:12.321456')->floatDiffInYears(Carbon::parse('2019-02-13 20:55:12.321456'))); - $this->assertSame(1.3746000338015283, Carbon::parse('2018-02-13 20:55:12.321456')->floatDiffInYears(Carbon::parse('2019-06-30 14:24:58.987421'))); - $this->assertSame(0.9609014036645421, Carbon::parse('2018-02-13 20:55:12.321456')->floatDiffInYears(Carbon::parse('2019-01-30 14:24:58.987421'))); + $this->assertSame(1, Carbon::parse('2018-02-13 20:55:12.321456')->diffInYears(Carbon::parse('2019-02-13 20:55:12.321456'))); + $this->assertSame(1.3746000338015283, Carbon::parse('2018-02-13 20:55:12.321456')->diffInYears(Carbon::parse('2019-06-30 14:24:58.987421'))); + $this->assertSame(0.9609014036645421, Carbon::parse('2018-02-13 20:55:12.321456')->diffInYears(Carbon::parse('2019-01-30 14:24:58.987421'))); - $this->assertSame(1.3252849653083778, Carbon::parse('2018-02-13 20:55:12.321456')->floatDiffInYears(Carbon::parse('2019-06-12 14:24:58.987421'))); - $this->assertSame(1.3252849653083778, Carbon::parse('2019-06-12 14:24:58.987421')->floatDiffInYears(Carbon::parse('2018-02-13 20:55:12.321456'))); - $this->assertSame(1.3252849653083778, Carbon::parse('2018-02-13 20:55:12.321456')->floatDiffInYears(Carbon::parse('2019-06-12 14:24:58.987421'), false)); - $this->assertSame(-1.3252849653083778, Carbon::parse('2019-06-12 14:24:58.987421')->floatDiffInYears(Carbon::parse('2018-02-13 20:55:12.321456'), false)); + $this->assertSame(1.3252849653083778, Carbon::parse('2018-02-13 20:55:12.321456')->diffInYears(Carbon::parse('2019-06-12 14:24:58.987421'))); + $this->assertSame(1.3252849653083778, Carbon::parse('2019-06-12 14:24:58.987421')->diffInYears(Carbon::parse('2018-02-13 20:55:12.321456'))); + $this->assertSame(1.3252849653083778, Carbon::parse('2018-02-13 20:55:12.321456')->diffInYears(Carbon::parse('2019-06-12 14:24:58.987421'), false)); + $this->assertSame(-1.3252849653083778, Carbon::parse('2019-06-12 14:24:58.987421')->diffInYears(Carbon::parse('2018-02-13 20:55:12.321456'), false)); - $this->assertSame(5.325284965308378, Carbon::parse('2018-02-13 20:55:12.321456')->floatDiffInYears(Carbon::parse('2023-06-12 14:24:58.987421'))); - $this->assertSame(5.325284965308378, Carbon::parse('2023-06-12 14:24:58.987421')->floatDiffInYears(Carbon::parse('2018-02-13 20:55:12.321456'))); - $this->assertSame(5.325284965308378, Carbon::parse('2018-02-13 20:55:12.321456')->floatDiffInYears(Carbon::parse('2023-06-12 14:24:58.987421'), false)); - $this->assertSame(-5.325284965308378, Carbon::parse('2023-06-12 14:24:58.987421')->floatDiffInYears(Carbon::parse('2018-02-13 20:55:12.321456'), false)); + $this->assertSame(5.325284965308378, Carbon::parse('2018-02-13 20:55:12.321456')->diffInYears(Carbon::parse('2023-06-12 14:24:58.987421'))); + $this->assertSame(5.325284965308378, Carbon::parse('2023-06-12 14:24:58.987421')->diffInYears(Carbon::parse('2018-02-13 20:55:12.321456'))); + $this->assertSame(5.325284965308378, Carbon::parse('2018-02-13 20:55:12.321456')->diffInYears(Carbon::parse('2023-06-12 14:24:58.987421'), false)); + $this->assertSame(-5.325284965308378, Carbon::parse('2023-06-12 14:24:58.987421')->diffInYears(Carbon::parse('2018-02-13 20:55:12.321456'), false)); - $this->assertSame(1, Carbon::parse('2018-10-01 00:00:00', 'Europe/Paris')->floatDiffInMonths(Carbon::parse('2018-11-01 00:00:00', 'Europe/Paris'))); - $this->assertSame(1, Carbon::parse('2018-10-28 00:00:00')->floatDiffInMonths(Carbon::parse('2018-11-28 00:00:00'))); - $this->assertSame(1, Carbon::parse('2018-10-28 00:00:00', 'Europe/Paris')->floatDiffInMonths(Carbon::parse('2018-11-28 00:00:00', 'Europe/Paris'))); + $this->assertSame(1, Carbon::parse('2018-10-01 00:00:00', 'Europe/Paris')->diffInMonths(Carbon::parse('2018-11-01 00:00:00', 'Europe/Paris'))); + $this->assertSame(1, Carbon::parse('2018-10-28 00:00:00')->diffInMonths(Carbon::parse('2018-11-28 00:00:00'))); + $this->assertSame(1, Carbon::parse('2018-10-28 00:00:00', 'Europe/Paris')->diffInMonths(Carbon::parse('2018-11-28 00:00:00', 'Europe/Paris'))); - $this->assertSame(-1, Carbon::parse('2018-11-01 00:00:00', 'Europe/Paris')->floatDiffInMonths(Carbon::parse('2018-10-01 00:00:00', 'Europe/Paris'), false)); - $this->assertSame(-1, Carbon::parse('2018-11-28 00:00:00')->floatDiffInMonths(Carbon::parse('2018-10-28 00:00:00'), false)); - $this->assertSame(-1, Carbon::parse('2018-11-28 00:00:00', 'Europe/Paris')->floatDiffInMonths(Carbon::parse('2018-10-28 00:00:00', 'Europe/Paris'), false)); + $this->assertSame(-1, Carbon::parse('2018-11-01 00:00:00', 'Europe/Paris')->diffInMonths(Carbon::parse('2018-10-01 00:00:00', 'Europe/Paris'), false)); + $this->assertSame(-1, Carbon::parse('2018-11-28 00:00:00')->diffInMonths(Carbon::parse('2018-10-28 00:00:00'), false)); + $this->assertSame(-1, Carbon::parse('2018-11-28 00:00:00', 'Europe/Paris')->diffInMonths(Carbon::parse('2018-10-28 00:00:00', 'Europe/Paris'), false)); } public function testFloatDiffWithRealUnits() { date_default_timezone_set('UTC'); - $this->assertSame(1.0006944444444443, Carbon::parse('2018-12-01 00:00')->floatDiffInRealDays(Carbon::parse('2018-12-02 00:01'))); + $this->assertSame(1.0006944444444443, Carbon::parse('2018-12-01 00:00')->diffInDays(Carbon::parse('2018-12-02 00:01'))); - $this->assertSame(0.9714742503779745, Carbon::parse('2018-03-13 20:55:12.321456')->floatDiffInRealMonths(Carbon::parse('2018-04-12 14:24:58.987421'))); - $this->assertSame(0.9714742503779745, Carbon::parse('2018-04-12 14:24:58.987421')->floatDiffInRealMonths(Carbon::parse('2018-03-13 20:55:12.321456'))); - $this->assertSame(0.9714742503779745, Carbon::parse('2018-03-13 20:55:12.321456')->floatDiffInRealMonths(Carbon::parse('2018-04-12 14:24:58.987421'), false)); - $this->assertSame(-0.9714742503779745, Carbon::parse('2018-04-12 14:24:58.987421')->floatDiffInRealMonths(Carbon::parse('2018-03-13 20:55:12.321456'), false)); + $this->assertSame(0.9714742503779745, Carbon::parse('2018-03-13 20:55:12.321456')->diffInMonths(Carbon::parse('2018-04-12 14:24:58.987421'))); + $this->assertSame(0.9714742503779745, Carbon::parse('2018-04-12 14:24:58.987421')->diffInMonths(Carbon::parse('2018-03-13 20:55:12.321456'))); + $this->assertSame(0.9714742503779745, Carbon::parse('2018-03-13 20:55:12.321456')->diffInMonths(Carbon::parse('2018-04-12 14:24:58.987421'), false)); + $this->assertSame(-0.9714742503779745, Carbon::parse('2018-04-12 14:24:58.987421')->diffInMonths(Carbon::parse('2018-03-13 20:55:12.321456'), false)); - $this->assertSame(16.557633744585264, Carbon::parse('2018-02-13 20:55:12.321456')->floatDiffInRealMonths(Carbon::parse('2019-06-30 14:24:58.987421'))); + $this->assertSame(16.557633744585264, Carbon::parse('2018-02-13 20:55:12.321456')->diffInMonths(Carbon::parse('2019-06-30 14:24:58.987421'))); - $this->assertSame(15.971474250377973, Carbon::parse('2018-02-13 20:55:12.321456')->floatDiffInRealMonths(Carbon::parse('2019-06-12 14:24:58.987421'))); - $this->assertSame(15.971474250377973, Carbon::parse('2019-06-12 14:24:58.987421')->floatDiffInRealMonths(Carbon::parse('2018-02-13 20:55:12.321456'))); - $this->assertSame(15.971474250377973, Carbon::parse('2018-02-13 20:55:12.321456')->floatDiffInRealMonths(Carbon::parse('2019-06-12 14:24:58.987421'), false)); - $this->assertSame(-15.971474250377973, Carbon::parse('2019-06-12 14:24:58.987421')->floatDiffInRealMonths(Carbon::parse('2018-02-13 20:55:12.321456'), false)); + $this->assertSame(15.971474250377973, Carbon::parse('2018-02-13 20:55:12.321456')->diffInMonths(Carbon::parse('2019-06-12 14:24:58.987421'))); + $this->assertSame(15.971474250377973, Carbon::parse('2019-06-12 14:24:58.987421')->diffInMonths(Carbon::parse('2018-02-13 20:55:12.321456'))); + $this->assertSame(15.971474250377973, Carbon::parse('2018-02-13 20:55:12.321456')->diffInMonths(Carbon::parse('2019-06-12 14:24:58.987421'), false)); + $this->assertSame(-15.971474250377973, Carbon::parse('2019-06-12 14:24:58.987421')->diffInMonths(Carbon::parse('2018-02-13 20:55:12.321456'), false)); - $this->assertSame(1, Carbon::parse('2018-02-13 20:55:12.321456')->floatDiffInRealYears(Carbon::parse('2019-02-13 20:55:12.321456'))); - $this->assertSame(1.3746000338015283, Carbon::parse('2018-02-13 20:55:12.321456')->floatDiffInRealYears(Carbon::parse('2019-06-30 14:24:58.987421'))); - $this->assertSame(0.9609014036645421, Carbon::parse('2018-02-13 20:55:12.321456')->floatDiffInRealYears(Carbon::parse('2019-01-30 14:24:58.987421'))); + $this->assertSame(1, Carbon::parse('2018-02-13 20:55:12.321456')->diffInYears(Carbon::parse('2019-02-13 20:55:12.321456'))); + $this->assertSame(1.3746000338015283, Carbon::parse('2018-02-13 20:55:12.321456')->diffInYears(Carbon::parse('2019-06-30 14:24:58.987421'))); + $this->assertSame(0.9609014036645421, Carbon::parse('2018-02-13 20:55:12.321456')->diffInYears(Carbon::parse('2019-01-30 14:24:58.987421'))); - $this->assertSame(1.3252849653083778, Carbon::parse('2018-02-13 20:55:12.321456')->floatDiffInRealYears(Carbon::parse('2019-06-12 14:24:58.987421'))); - $this->assertSame(1.3252849653083778, Carbon::parse('2019-06-12 14:24:58.987421')->floatDiffInRealYears(Carbon::parse('2018-02-13 20:55:12.321456'))); - $this->assertSame(1.3252849653083778, Carbon::parse('2018-02-13 20:55:12.321456')->floatDiffInRealYears(Carbon::parse('2019-06-12 14:24:58.987421'), false)); - $this->assertSame(-1.3252849653083778, Carbon::parse('2019-06-12 14:24:58.987421')->floatDiffInRealYears(Carbon::parse('2018-02-13 20:55:12.321456'), false)); + $this->assertSame(1.3252849653083778, Carbon::parse('2018-02-13 20:55:12.321456')->diffInYears(Carbon::parse('2019-06-12 14:24:58.987421'))); + $this->assertSame(1.3252849653083778, Carbon::parse('2019-06-12 14:24:58.987421')->diffInYears(Carbon::parse('2018-02-13 20:55:12.321456'))); + $this->assertSame(1.3252849653083778, Carbon::parse('2018-02-13 20:55:12.321456')->diffInYears(Carbon::parse('2019-06-12 14:24:58.987421'), false)); + $this->assertSame(-1.3252849653083778, Carbon::parse('2019-06-12 14:24:58.987421')->diffInYears(Carbon::parse('2018-02-13 20:55:12.321456'), false)); - $this->assertSame(5.325284965308378, Carbon::parse('2018-02-13 20:55:12.321456')->floatDiffInRealYears(Carbon::parse('2023-06-12 14:24:58.987421'))); - $this->assertSame(5.325284965308378, Carbon::parse('2023-06-12 14:24:58.987421')->floatDiffInRealYears(Carbon::parse('2018-02-13 20:55:12.321456'))); - $this->assertSame(5.325284965308378, Carbon::parse('2018-02-13 20:55:12.321456')->floatDiffInRealYears(Carbon::parse('2023-06-12 14:24:58.987421'), false)); - $this->assertSame(-5.325284965308378, Carbon::parse('2023-06-12 14:24:58.987421')->floatDiffInRealYears(Carbon::parse('2018-02-13 20:55:12.321456'), false)); + $this->assertSame(5.325284965308378, Carbon::parse('2018-02-13 20:55:12.321456')->diffInYears(Carbon::parse('2023-06-12 14:24:58.987421'))); + $this->assertSame(5.325284965308378, Carbon::parse('2023-06-12 14:24:58.987421')->diffInYears(Carbon::parse('2018-02-13 20:55:12.321456'))); + $this->assertSame(5.325284965308378, Carbon::parse('2018-02-13 20:55:12.321456')->diffInYears(Carbon::parse('2023-06-12 14:24:58.987421'), false)); + $this->assertSame(-5.325284965308378, Carbon::parse('2023-06-12 14:24:58.987421')->diffInYears(Carbon::parse('2018-02-13 20:55:12.321456'), false)); - $this->assertSame(1, Carbon::parse('2018-10-01 00:00:00', 'Europe/Paris')->floatDiffInRealMonths(Carbon::parse('2018-11-01 00:00:00', 'Europe/Paris'))); - $this->assertSame(1, Carbon::parse('2018-10-28 00:00:00')->floatDiffInRealMonths(Carbon::parse('2018-11-28 00:00:00'))); - $this->assertSame(1, Carbon::parse('2018-10-28 00:00:00', 'Europe/Paris')->floatDiffInRealMonths(Carbon::parse('2018-11-28 00:00:00', 'Europe/Paris'))); + $this->assertSame(1, Carbon::parse('2018-10-01 00:00:00', 'Europe/Paris')->diffInMonths(Carbon::parse('2018-11-01 00:00:00', 'Europe/Paris'))); + $this->assertSame(1, Carbon::parse('2018-10-28 00:00:00')->diffInMonths(Carbon::parse('2018-11-28 00:00:00'))); + $this->assertSame(1, Carbon::parse('2018-10-28 00:00:00', 'Europe/Paris')->diffInMonths(Carbon::parse('2018-11-28 00:00:00', 'Europe/Paris'))); - $this->assertSame(-1, Carbon::parse('2018-11-01 00:00:00', 'Europe/Paris')->floatDiffInRealMonths(Carbon::parse('2018-10-01 00:00:00', 'Europe/Paris'), false)); - $this->assertSame(-1, Carbon::parse('2018-11-28 00:00:00')->floatDiffInRealMonths(Carbon::parse('2018-10-28 00:00:00'), false)); - $this->assertSame(-1, Carbon::parse('2018-11-28 00:00:00', 'Europe/Paris')->floatDiffInRealMonths(Carbon::parse('2018-10-28 00:00:00', 'Europe/Paris'), false)); + $this->assertSame(-1, Carbon::parse('2018-11-01 00:00:00', 'Europe/Paris')->diffInMonths(Carbon::parse('2018-10-01 00:00:00', 'Europe/Paris'), false)); + $this->assertSame(-1, Carbon::parse('2018-11-28 00:00:00')->diffInMonths(Carbon::parse('2018-10-28 00:00:00'), false)); + $this->assertSame(-1, Carbon::parse('2018-11-28 00:00:00', 'Europe/Paris')->diffInMonths(Carbon::parse('2018-10-28 00:00:00', 'Europe/Paris'), false)); } /** diff --git a/tests/CarbonImmutable/ModifyTest.php b/tests/CarbonImmutable/ModifyTest.php index 093ae8bbd7..e0ac885668 100644 --- a/tests/CarbonImmutable/ModifyTest.php +++ b/tests/CarbonImmutable/ModifyTest.php @@ -28,146 +28,147 @@ public function testTimezoneModify() // For daylight saving time reason 2014-03-30 0h59 is immediately followed by 2h00 $a = new Carbon('2014-03-30 00:00:00', 'Europe/London'); - $b = $a->addHours(24); - $this->assertSame(24, $a->diffInHours($b)); - $this->assertSame(24, $a->diffInHours($b, false)); - $this->assertSame(24, $b->diffInHours($a)); - $this->assertSame(-24, $b->diffInHours($a, false)); - $this->assertSame(-23, $b->diffInRealHours($a, false)); - $this->assertSame(-23 * 60, $b->diffInRealMinutes($a, false)); - $this->assertSame(-23 * 60 * 60, $b->diffInRealSeconds($a, false)); + $b = $a->copy(); + $b->addHours(24); + $this->assertSame(23.0, $a->diffInHours($b, false)); + $this->assertSame(23.0, $b->diffInHours($a)); + $this->assertSame(-23.0, $b->diffInHours($a, false)); + $this->assertSame(-23.0 * 60, $b->diffInMinutes($a, false)); + $this->assertSame(-23.0 * 60 * 60, $b->diffInSeconds($a, false)); + $this->assertSame(-23.0 * 60 * 60 * 1000, $b->diffInMilliseconds($a, false)); + $this->assertSame(-23.0 * 60 * 60 * 1000000, $b->diffInMicroseconds($a, false)); $a = new Carbon('2014-03-30 00:00:00', 'Europe/London'); - $b = $a->addRealHours(24); - $this->assertSame(-24, $b->diffInRealHours($a, false)); - $this->assertSame(-24 * 60, $b->diffInRealMinutes($a, false)); - $this->assertSame(-24 * 60 * 60, $b->diffInRealSeconds($a, false)); - $this->assertSame(-25, $b->diffInHours($a, false)); - $b = $b->subRealHours(24); - $this->assertSame(0, $b->diffInRealHours($a, false)); - $this->assertSame(0, $b->diffInHours($a, false)); + $b = $a->copy(); + $b->addRealHours(24); + $this->assertSame(-24.0, $b->diffInHours($a, false)); + $this->assertSame(-24.0 * 60, $b->diffInMinutes($a, false)); + $this->assertSame(-24.0 * 60 * 60, $b->diffInSeconds($a, false)); + $this->assertSame(-24.0 * 60 * 60 * 1000, $b->diffInMilliseconds($a, false)); + $this->assertSame(-24.0 * 60 * 60 * 1000000, $b->diffInMicroseconds($a, false)); + $b->subRealHours(24); + $this->assertSame(0.0, $b->diffInHours($a, false)); + $this->assertSame(0.0, $b->diffInHours($a, false)); $a = new Carbon('2014-03-30 00:59:00', 'Europe/London'); - $a = $a->addRealHour(); + $a->addRealHour(); $this->assertSame('02:59', $a->format('H:i')); - $a = $a->subRealHour(); + $a->subRealHour(); $this->assertSame('00:59', $a->format('H:i')); $a = new Carbon('2014-03-30 00:59:00', 'Europe/London'); - $a = $a->addRealMinutes(2); + $a->addRealMinutes(2); $this->assertSame('02:01', $a->format('H:i')); - $a = $a->subRealMinutes(2); + $a->subRealMinutes(2); $this->assertSame('00:59', $a->format('H:i')); $a = new Carbon('2014-03-30 00:59:30', 'Europe/London'); - $a = $a->addRealMinute(); + $a->addRealMinute(); $this->assertSame('02:00:30', $a->format('H:i:s')); - $a = $a->subRealMinute(); + $a->subRealMinute(); $this->assertSame('00:59:30', $a->format('H:i:s')); $a = new Carbon('2014-03-30 00:59:30', 'Europe/London'); - $a = $a->addRealSeconds(40); + $a->addRealSeconds(40); $this->assertSame('02:00:10', $a->format('H:i:s')); - $a = $a->subRealSeconds(40); + $a->subRealSeconds(40); $this->assertSame('00:59:30', $a->format('H:i:s')); $a = new Carbon('2014-03-30 00:59:59', 'Europe/London'); - $a = $a->addRealSecond(); + $a->addRealSecond(); $this->assertSame('02:00:00', $a->format('H:i:s')); - $a = $a->subRealSecond(); + $a->subRealSecond(); $this->assertSame('00:59:59', $a->format('H:i:s')); + $a = new Carbon('2014-03-30 00:59:59.990000', 'Europe/London'); + $a->addRealMilliseconds(20); + $this->assertSame('02:00:00.010000', $a->format('H:i:s.u')); + $a->subRealMilliseconds(20); + $this->assertSame('00:59:59.990000', $a->format('H:i:s.u')); + $a = new Carbon('2014-03-30 00:59:59.999990', 'Europe/London'); - $a = $a->addRealMicroseconds(20); + $a->addRealMicroseconds(20); $this->assertSame('02:00:00.000010', $a->format('H:i:s.u')); - $a = $a->subRealMicroseconds(20); + $a->subRealMicroseconds(20); $this->assertSame('00:59:59.999990', $a->format('H:i:s.u')); $a = new Carbon('2014-03-30 00:59:59.999999', 'Europe/London'); - $a = $a->addRealMicrosecond(); + $a->addRealMicrosecond(); $this->assertSame('02:00:00.000000', $a->format('H:i:s.u')); - $a = $a->subRealMicrosecond(); + $a->subRealMicrosecond(); $this->assertSame('00:59:59.999999', $a->format('H:i:s.u')); $a = new Carbon('2014-03-30 00:00:00', 'Europe/London'); - $b = $a->addRealDay(); - $this->assertSame(-24, $b->diffInRealHours($a, false)); - $this->assertSame(-24 * 60, $b->diffInRealMinutes($a, false)); - $this->assertSame(-24 * 60 * 60, $b->diffInRealSeconds($a, false)); - $this->assertSame(-24 * 60 * 60 * 1000, $b->diffInRealMilliseconds($a, false)); - $this->assertSame(-24 * 60 * 60 * 1000000, $b->diffInRealMicroseconds($a, false)); - $this->assertSame(-25 * 60 * 60 * 1000000, $b->diffInMicroseconds($a, false)); - $this->assertSame(-25, $b->diffInHours($a, false)); + $b = $a->copy(); + $b->addRealDay(); + $this->assertSame(-24.0, $b->diffInHours($a, false)); + $this->assertSame(-24.0 * 60, $b->diffInMinutes($a, false)); + $this->assertSame(-24.0 * 60 * 60, $b->diffInSeconds($a, false)); + $this->assertSame(-24.0 * 60 * 60 * 1000, $b->diffInMilliseconds($a, false)); + $this->assertSame(-24.0 * 60 * 60 * 1000000, $b->diffInMicroseconds($a, false)); $a = new Carbon('2014-03-30 00:00:00', 'Europe/London'); - $b = $a->addRealWeeks(1 / 7); - $this->assertSame(-24, $b->diffInRealHours($a, false)); - $this->assertSame(-24 * 60, $b->diffInRealMinutes($a, false)); - $this->assertSame(-24 * 60 * 60, $b->diffInRealSeconds($a, false)); - $this->assertSame(-24 * 60 * 60 * 1000, $b->diffInRealMilliseconds($a, false)); - $this->assertSame(-24 * 60 * 60 * 1000000, $b->diffInRealMicroseconds($a, false)); - $this->assertSame(-25 * 60 * 60 * 1000000, $b->diffInMicroseconds($a, false)); - $this->assertSame(-25, $b->diffInHours($a, false)); + $b = $a->copy(); + $b->addRealWeeks(1 / 7); + $this->assertSame(-24.0, $b->diffInHours($a, false)); + $this->assertSame(-24.0 * 60, $b->diffInMinutes($a, false)); + $this->assertSame(-24.0 * 60 * 60, $b->diffInSeconds($a, false)); + $this->assertSame(-24.0 * 60 * 60 * 1000, $b->diffInMilliseconds($a, false)); + $this->assertSame(-24.0 * 60 * 60 * 1000000, $b->diffInMicroseconds($a, false)); $a = new Carbon('2014-03-30 00:00:00', 'Europe/London'); - $b = $a->addRealMonths(1 / 30); - $this->assertSame(-24, $b->diffInRealHours($a, false)); - $this->assertSame(-24 * 60, $b->diffInRealMinutes($a, false)); - $this->assertSame(-24 * 60 * 60, $b->diffInRealSeconds($a, false)); - $this->assertSame(-24 * 60 * 60 * 1000, $b->diffInRealMilliseconds($a, false)); - $this->assertSame(-24 * 60 * 60 * 1000000, $b->diffInRealMicroseconds($a, false)); - $this->assertSame(-25 * 60 * 60 * 1000000, $b->diffInMicroseconds($a, false)); - $this->assertSame(-25, $b->diffInHours($a, false)); + $b = $a->copy(); + $b->addRealMonths(1 / 30); + $this->assertSame(-24.0, $b->diffInHours($a, false)); + $this->assertSame(-24.0 * 60, $b->diffInMinutes($a, false)); + $this->assertSame(-24.0 * 60 * 60, $b->diffInSeconds($a, false)); + $this->assertSame(-24.0 * 60 * 60 * 1000, $b->diffInMilliseconds($a, false)); + $this->assertSame(-24.0 * 60 * 60 * 1000000, $b->diffInMicroseconds($a, false)); $a = new Carbon('2014-03-30 00:00:00', 'Europe/London'); - $b = $a->addRealQuarters(1 / 90); - $this->assertSame(-24, $b->diffInRealHours($a, false)); - $this->assertSame(-24 * 60, $b->diffInRealMinutes($a, false)); - $this->assertSame(-24 * 60 * 60, $b->diffInRealSeconds($a, false)); - $this->assertSame(-24 * 60 * 60 * 1000, $b->diffInRealMilliseconds($a, false)); - $this->assertSame(-24 * 60 * 60 * 1000000, $b->diffInRealMicroseconds($a, false)); - $this->assertSame(-25 * 60 * 60 * 1000000, $b->diffInMicroseconds($a, false)); - $this->assertSame(-25, $b->diffInHours($a, false)); + $b = $a->copy(); + $b->addRealQuarters(1 / 90); + $this->assertSame(-24.0, $b->diffInHours($a, false)); + $this->assertSame(-24.0 * 60, $b->diffInMinutes($a, false)); + $this->assertSame(-24.0 * 60 * 60, $b->diffInSeconds($a, false)); + $this->assertSame(-24.0 * 60 * 60 * 1000, $b->diffInMilliseconds($a, false)); + $this->assertSame(-24.0 * 60 * 60 * 1000000, $b->diffInMicroseconds($a, false)); $a = new Carbon('2014-03-30 00:00:00', 'Europe/London'); - $b = $a->addRealYears(1 / 365); - $this->assertSame(-24, $b->diffInRealHours($a, false)); - $this->assertSame(-24 * 60, $b->diffInRealMinutes($a, false)); - $this->assertSame(-24 * 60 * 60, $b->diffInRealSeconds($a, false)); - $this->assertSame(-24 * 60 * 60 * 1000, $b->diffInRealMilliseconds($a, false)); - $this->assertSame(-24 * 60 * 60 * 1000000, $b->diffInRealMicroseconds($a, false)); - $this->assertSame(-25 * 60 * 60 * 1000000, $b->diffInMicroseconds($a, false)); - $this->assertSame(-25, $b->diffInHours($a, false)); + $b = $a->copy(); + $b->addRealYears(1 / 365); + $this->assertSame(-24.0, $b->diffInHours($a, false)); + $this->assertSame(-24.0 * 60, $b->diffInMinutes($a, false)); + $this->assertSame(-24.0 * 60 * 60, $b->diffInSeconds($a, false)); + $this->assertSame(-24.0 * 60 * 60 * 1000, $b->diffInMilliseconds($a, false)); + $this->assertSame(-24.0 * 60 * 60 * 1000000, $b->diffInMicroseconds($a, false)); $a = new Carbon('2014-03-30 00:00:00', 'Europe/London'); - $b = $a->addRealDecades(1 / 3650); - $this->assertSame(-24, $b->diffInRealHours($a, false)); - $this->assertSame(-24 * 60, $b->diffInRealMinutes($a, false)); - $this->assertSame(-24 * 60 * 60, $b->diffInRealSeconds($a, false)); - $this->assertSame(-24 * 60 * 60 * 1000, $b->diffInRealMilliseconds($a, false)); - $this->assertSame(-24 * 60 * 60 * 1000000, $b->diffInRealMicroseconds($a, false)); - $this->assertSame(-25 * 60 * 60 * 1000000, $b->diffInMicroseconds($a, false)); - $this->assertSame(-25, $b->diffInHours($a, false)); + $b = $a->copy(); + $b->addRealDecades(1 / 3650); + $this->assertSame(-24.0, $b->diffInHours($a, false)); + $this->assertSame(-24.0 * 60, $b->diffInMinutes($a, false)); + $this->assertSame(-24.0 * 60 * 60, $b->diffInSeconds($a, false)); + $this->assertSame(-24.0 * 60 * 60 * 1000, $b->diffInMilliseconds($a, false)); + $this->assertSame(-24.0 * 60 * 60 * 1000000, $b->diffInMicroseconds($a, false)); $a = new Carbon('2014-03-30 00:00:00', 'Europe/London'); - $b = $a->addRealCenturies(1 / 36500); - $this->assertSame(-24, $b->diffInRealHours($a, false)); - $this->assertSame(-24 * 60, $b->diffInRealMinutes($a, false)); - $this->assertSame(-24 * 60 * 60, $b->diffInRealSeconds($a, false)); - $this->assertSame(-24 * 60 * 60 * 1000, $b->diffInRealMilliseconds($a, false)); - $this->assertSame(-24 * 60 * 60 * 1000000, $b->diffInRealMicroseconds($a, false)); - $this->assertSame(-25 * 60 * 60 * 1000000, $b->diffInMicroseconds($a, false)); - $this->assertSame(-25, $b->diffInHours($a, false)); + $b = $a->copy(); + $b->addRealCenturies(1 / 36500); + $this->assertSame(-24.0, $b->diffInHours($a, false)); + $this->assertSame(-24.0 * 60, $b->diffInMinutes($a, false)); + $this->assertSame(-24.0 * 60 * 60, $b->diffInSeconds($a, false)); + $this->assertSame(-24.0 * 60 * 60 * 1000, $b->diffInMilliseconds($a, false)); + $this->assertSame(-24.0 * 60 * 60 * 1000000, $b->diffInMicroseconds($a, false)); $a = new Carbon('2014-03-30 00:00:00', 'Europe/London'); - $b = $a->addRealMillennia(1 / 365000); - $this->assertSame(-24, $b->diffInRealHours($a, false)); - $this->assertSame(-24 * 60, $b->diffInRealMinutes($a, false)); - $this->assertSame(-24 * 60 * 60, $b->diffInRealSeconds($a, false)); - $this->assertSame(-24 * 60 * 60 * 1000, $b->diffInRealMilliseconds($a, false)); - $this->assertSame(-24 * 60 * 60 * 1000000, $b->diffInRealMicroseconds($a, false)); - $this->assertSame(-25 * 60 * 60 * 1000000, $b->diffInMicroseconds($a, false)); - $this->assertSame(-25, $b->diffInHours($a, false)); + $b = $a->copy(); + $b->addRealMillennia(1 / 365000); + $this->assertSame(-24.0, $b->diffInHours($a, false)); + $this->assertSame(-24.0 * 60, $b->diffInMinutes($a, false)); + $this->assertSame(-24.0 * 60 * 60, $b->diffInSeconds($a, false)); + $this->assertSame(-24.0 * 60 * 60 * 1000, $b->diffInMilliseconds($a, false)); + $this->assertSame(-24.0 * 60 * 60 * 1000000, $b->diffInMicroseconds($a, false)); } public function testNextAndPrevious() From 6c5ed369357b5a5c6a7641ac3d22308983c8052f Mon Sep 17 00:00:00 2001 From: KyleKatarn Date: Thu, 6 Aug 2020 17:00:38 +0200 Subject: [PATCH 12/38] Make diff() return CarbonInterval by default --- src/Carbon/CarbonInterface.php | 11 -- src/Carbon/CarbonInterval.php | 2 +- src/Carbon/Traits/Date.php | 2 +- src/Carbon/Traits/Difference.php | 44 ++--- src/Carbon/Traits/Localization.php | 2 +- tests/Carbon/DiffTest.php | 199 +++++++++++---------- tests/Carbon/IsTest.php | 2 +- tests/Carbon/LocalizationTest.php | 2 +- tests/Carbon/MacroTest.php | 2 +- tests/Carbon/ModifyTest.php | 2 +- tests/Carbon/RelativeTest.php | 16 +- tests/Carbon/SettersTest.php | 13 +- tests/CarbonImmutable/DiffTest.php | 163 ++++++++--------- tests/CarbonImmutable/LocalizationTest.php | 2 +- tests/CarbonImmutable/MacroTest.php | 2 +- tests/CarbonImmutable/ModifyTest.php | 66 +++---- tests/CarbonImmutable/RelativeTest.php | 16 +- 17 files changed, 261 insertions(+), 285 deletions(-) diff --git a/src/Carbon/CarbonInterface.php b/src/Carbon/CarbonInterface.php index 8264526f7a..bad7e4daa0 100644 --- a/src/Carbon/CarbonInterface.php +++ b/src/Carbon/CarbonInterface.php @@ -1199,17 +1199,6 @@ public static function createSafe($year = null, $month = null, $day = null, $hou */ public function dayOfYear($value = null); - /** - * Get the difference as a CarbonInterval instance. - * Return absolute interval (always positive) unless you pass false to the second argument. - * - * @param \Carbon\CarbonInterface|\DateTimeInterface|string|null $date - * @param bool $absolute Get the absolute of the difference - * - * @return CarbonInterval - */ - public function diffAsCarbonInterval($date = null, $absolute = true); - /** * Get the difference by the given interval using a filter closure. * diff --git a/src/Carbon/CarbonInterval.php b/src/Carbon/CarbonInterval.php index cfa12264c8..5f5de8995b 100644 --- a/src/Carbon/CarbonInterval.php +++ b/src/Carbon/CarbonInterval.php @@ -2473,7 +2473,7 @@ public function roundUnit($unit, $precision = 1, $function = 'round') $this->copyProperties( $next ->roundUnit($unit, $precision, $function) - ->diffAsCarbonInterval($base) + ->diff($base) ); return $this->invert($inverted); diff --git a/src/Carbon/Traits/Date.php b/src/Carbon/Traits/Date.php index 0110391108..ba0584eb68 100644 --- a/src/Carbon/Traits/Date.php +++ b/src/Carbon/Traits/Date.php @@ -933,7 +933,7 @@ public function get($name) // @property int does a diffInYears() with default parameters case $name === 'age': - return $this->diffInYears(); + return (int) $this->diffInYears(); // @property-read int the quarter of this instance, 1 - 4 // @call isSameUnit diff --git a/src/Carbon/Traits/Difference.php b/src/Carbon/Traits/Difference.php index 222dcd297c..174f351c6b 100644 --- a/src/Carbon/Traits/Difference.php +++ b/src/Carbon/Traits/Difference.php @@ -110,30 +110,20 @@ protected static function fixDiffInterval(DateInterval $diff, $absolute) /** * Get the difference as a DateInterval instance. - * Return relative interval (negative if + * Return relative interval (negative if $absolute flag is not set to true and the given date is before + * current one). * * @param \Carbon\CarbonInterface|\DateTimeInterface|string|null $date * @param bool $absolute Get the absolute of the difference * * @return DateInterval */ - public function diff($date = null, $absolute = false) + public function diff($date = null, $absolute = false): CarbonInterval { - return parent::diff($this->resolveCarbon($date), (bool) $absolute); - } + $interval = static::fixDiffInterval(parent::diff($this->resolveCarbon($date), (bool) $absolute), $absolute); + $interval->setLocalTranslator($this->getLocalTranslator()); - /** - * Get the difference as a CarbonInterval instance. - * Return absolute interval (always positive) unless you pass false to the second argument. - * - * @param \Carbon\CarbonInterface|\DateTimeInterface|string|null $date - * @param bool $absolute Get the absolute of the difference - * - * @return CarbonInterval - */ - public function diffAsCarbonInterval($date = null, $absolute = true) - { - return static::fixDiffInterval($this->diff($this->resolveCarbon($date), $absolute), $absolute); + return $interval; } /** @@ -144,7 +134,7 @@ public function diffAsCarbonInterval($date = null, $absolute = true) * * @return float */ - public function diffInYears($date = null, $absolute = true) + public function diffInYears($date = null, $absolute = true): float { $start = $this; $end = $this->resolveCarbon($date); @@ -153,7 +143,7 @@ public function diffInYears($date = null, $absolute = true) if (!$ascending) { [$start, $end] = [$end, $start]; } - $yearsDiff = $start->diffInYears($end); + $yearsDiff = (int) $start->diff($end, $absolute)->format('%r%y'); /** @var Carbon|CarbonImmutable $floorEnd */ $floorEnd = $start->copy()->addYears($yearsDiff); @@ -198,10 +188,15 @@ public function diffInMonths($date = null, $absolute = true): float $end = $this->resolveCarbon($date); $ascending = ($start <= $end); $sign = $absolute || $ascending ? 1 : -1; + if (!$ascending) { [$start, $end] = [$end, $start]; } - $monthsDiff = $start->diffInMonths($end); + + $monthsDiff = + (int) ($start->diff($end, $absolute)->format('%r%y') * static::MONTHS_PER_YEAR) + + (int) $start->diff($end, $absolute)->format('%r%m'); + /** @var Carbon|CarbonImmutable $floorEnd */ $floorEnd = $start->copy()->addMonths($monthsDiff); @@ -242,10 +237,10 @@ public function diffInWeeks($date = null, $absolute = true): float */ public function diffInDays($date = null, $absolute = true): float { - $daysDiff = (int) $this->diff($this->resolveCarbon($date), $absolute)->format('%r%a'); + $daysDiff = (int) parent::diff($this->resolveCarbon($date), $absolute)->format('%r%a'); $hoursDiff = $this->diffInHours($date, $absolute); - return ($hoursDiff < 0 ? -1 : 1) * $daysDiff + fmod($hoursDiff, static::HOURS_PER_DAY) / static::HOURS_PER_DAY; + return $daysDiff + fmod($hoursDiff, static::HOURS_PER_DAY) / static::HOURS_PER_DAY; } /** @@ -407,7 +402,7 @@ public function diffInMilliseconds($date = null, $absolute = true): float /** * The number of seconds since midnight. * - * @return int + * @return float */ public function secondsSinceMidnight() { @@ -417,7 +412,7 @@ public function secondsSinceMidnight() /** * The number of seconds until 23:59:59. * - * @return int + * @return float */ public function secondsUntilEndOfDay() { @@ -484,8 +479,7 @@ public function diffForHumans($other = null, $syntax = null, $short = false, $pa $parts = min(7, max(1, (int) $parts)); - return $this->diffAsCarbonInterval($other, false) - ->setLocalTranslator($this->getLocalTranslator()) + return $this->diff($other, false) ->forHumans($syntax, (bool) $short, $parts, $options ?? $this->localHumanDiffOptions ?? static::getHumanDiffOptions()); } diff --git a/src/Carbon/Traits/Localization.php b/src/Carbon/Traits/Localization.php index 0f7065e326..a71d62638c 100644 --- a/src/Carbon/Traits/Localization.php +++ b/src/Carbon/Traits/Localization.php @@ -54,7 +54,7 @@ trait Localization * * @var int */ - protected static $humanDiffOptions = CarbonInterface::NO_ZERO_DIFF; + protected static $humanDiffOptions = 0; /** * @deprecated To avoid conflict between different third-party libraries, static setters should not be used. diff --git a/tests/Carbon/DiffTest.php b/tests/Carbon/DiffTest.php index 2539fb526a..9557daa436 100644 --- a/tests/Carbon/DiffTest.php +++ b/tests/Carbon/DiffTest.php @@ -15,6 +15,7 @@ use Carbon\CarbonInterface; use Carbon\CarbonInterval; use Closure; +use DateTime; use Exception; use InvalidArgumentException; use Tests\AbstractTestCase; @@ -29,138 +30,138 @@ public function wrapWithTestNow(Closure $func, CarbonInterface $dt = null) public function testDiffAsCarbonInterval() { $dt = Carbon::createFromDate(2000, 1, 1); - $this->assertCarbonInterval($dt->diffAsCarbonInterval($dt->copy()->addYear()), 1, 0, 0, 0, 0, 0); - $this->assertTrue($dt->diffAsCarbonInterval($dt)->isEmpty()); + $this->assertCarbonInterval($dt->diff($dt->copy()->addYear()), 1, 0, 0, 0, 0, 0); + $this->assertTrue($dt->diff($dt)->isEmpty()); } public function testDiffInYearsPositive() { $dt = Carbon::createFromDate(2000, 1, 1); - $this->assertSame(1, $dt->diffInYears($dt->copy()->addYear())); + $this->assertSame(1, (int) $dt->diffInYears($dt->copy()->addYear())); } public function testDiffInYearsNegativeWithSign() { $dt = Carbon::createFromDate(2000, 1, 1); - $this->assertSame(-1, $dt->diffInYears($dt->copy()->subYear(), false)); + $this->assertSame(-1, (int) $dt->diffInYears($dt->copy()->subYear(), false)); } public function testDiffInYearsNegativeNoSign() { $dt = Carbon::createFromDate(2000, 1, 1); - $this->assertSame(1, $dt->diffInYears($dt->copy()->subYear())); + $this->assertSame(1, (int) $dt->diffInYears($dt->copy()->subYear())); } public function testDiffInYearsVsDefaultNow() { $this->wrapWithTestNow(function () { - $this->assertSame(1, Carbon::now()->subYear()->diffInYears()); + $this->assertSame(1, (int) Carbon::now()->subYear()->diffInYears()); }); } public function testDiffInYearsEnsureIsTruncated() { $dt = Carbon::createFromDate(2000, 1, 1); - $this->assertSame(1, $dt->diffInYears($dt->copy()->addYear()->addMonths(7))); + $this->assertSame(1, (int) $dt->diffInYears($dt->copy()->addYear()->addMonths(7))); } public function testDiffInQuartersPositive() { $dt = Carbon::createFromDate(2000, 1, 1); - $this->assertSame(1, $dt->diffInQuarters($dt->copy()->addQuarter()->addDay())); + $this->assertSame(1, (int) $dt->diffInQuarters($dt->copy()->addQuarter()->addDay())); } public function testDiffInQuartersNegativeWithSign() { $dt = Carbon::createFromDate(2000, 1, 1); - $this->assertSame(-4, $dt->diffInQuarters($dt->copy()->subQuarters(4), false)); + $this->assertSame(-4, (int) $dt->diffInQuarters($dt->copy()->subQuarters(4), false)); } public function testDiffInQuartersNegativeWithNoSign() { $dt = Carbon::createFromDate(2000, 1, 1); - $this->assertSame(4, $dt->diffInQuarters($dt->copy()->subQuarters(4))); + $this->assertSame(4, (int) $dt->diffInQuarters($dt->copy()->subQuarters(4))); } public function testDiffInQuartersVsDefaultNow() { $this->wrapWithTestNow(function () { - $this->assertSame(4, Carbon::now()->subYear()->diffInQuarters()); + $this->assertSame(4, (int) Carbon::now()->subYear()->diffInQuarters()); }); } public function testDiffInQuartersEnsureIsTruncated() { $dt = Carbon::createFromDate(2000, 1, 1); - $this->assertSame(1, $dt->diffInQuarters($dt->copy()->addQuarter()->addDays(12))); + $this->assertSame(1, (int) $dt->diffInQuarters($dt->copy()->addQuarter()->addDays(12))); } public function testDiffInMonthsPositive() { $dt = Carbon::createFromDate(2000, 1, 1); - $this->assertSame(13, $dt->diffInMonths($dt->copy()->addYear()->addMonth())); + $this->assertSame(13, (int) $dt->diffInMonths($dt->copy()->addYear()->addMonth())); } public function testDiffInMonthsNegativeWithSign() { $dt = Carbon::createFromDate(2000, 1, 1); - $this->assertSame(-11, $dt->diffInMonths($dt->copy()->subYear()->addMonth(), false)); + $this->assertSame(-11, (int) $dt->diffInMonths($dt->copy()->subYear()->addMonth(), false)); } public function testDiffInMonthsNegativeNoSign() { $dt = Carbon::createFromDate(2000, 1, 1); - $this->assertSame(11, $dt->diffInMonths($dt->copy()->subYear()->addMonth())); + $this->assertSame(11, (int) $dt->diffInMonths($dt->copy()->subYear()->addMonth())); } public function testDiffInMonthsVsDefaultNow() { $this->wrapWithTestNow(function () { - $this->assertSame(12, Carbon::now()->subYear()->diffInMonths()); + $this->assertSame(12, (int) Carbon::now()->subYear()->diffInMonths()); }); } public function testDiffInMonthsEnsureIsTruncated() { $dt = Carbon::createFromDate(2000, 1, 1); - $this->assertSame(1, $dt->diffInMonths($dt->copy()->addMonth()->addDays(16))); + $this->assertSame(1, (int) $dt->diffInMonths($dt->copy()->addMonth()->addDays(16))); } public function testDiffInDaysPositive() { $dt = Carbon::createFromDate(2000, 1, 1); - $this->assertSame(366, $dt->diffInDays($dt->copy()->addYear())); + $this->assertSame(366, (int) $dt->diffInDays($dt->copy()->addYear())); } public function testDiffInDaysNegativeWithSign() { $dt = Carbon::createFromDate(2000, 1, 1); - $this->assertSame(-365, $dt->diffInDays($dt->copy()->subYear(), false)); + $this->assertSame(-365, (int) $dt->diffInDays($dt->copy()->subYear(), false)); } public function testDiffInDaysNegativeNoSign() { $dt = Carbon::createFromDate(2000, 1, 1); - $this->assertSame(365, $dt->diffInDays($dt->copy()->subYear())); + $this->assertSame(365, (int) $dt->diffInDays($dt->copy()->subYear())); } public function testDiffInDaysVsDefaultNow() { $this->wrapWithTestNow(function () { - $this->assertSame(7, Carbon::now()->subWeek()->diffInDays()); + $this->assertSame(7, (int) Carbon::now()->subWeek()->diffInDays()); }); } public function testDiffInDaysEnsureIsTruncated() { $dt = Carbon::createFromDate(2000, 1, 1); - $this->assertSame(1, $dt->diffInDays($dt->copy()->addDay()->addHours(13))); + $this->assertSame(1, (int) $dt->diffInDays($dt->copy()->addDay()->addHours(13))); } public function testDiffInDaysFilteredPositiveWithMutated() { $dt = Carbon::createFromDate(2000, 1, 1); - $this->assertSame(5, $dt->diffInDaysFiltered(function (Carbon $date) { + $this->assertSame(5, (int) $dt->diffInDaysFiltered(function (Carbon $date) { return $date->dayOfWeek === 1; }, $dt->copy()->endOfMonth())); } @@ -170,7 +171,7 @@ public function testDiffInDaysFilteredPositiveWithSecondObject() $dt1 = Carbon::createFromDate(2000, 1, 1); $dt2 = Carbon::createFromDate(2000, 1, 31); - $this->assertSame(5, $dt1->diffInDaysFiltered(function (Carbon $date) { + $this->assertSame(5, (int) $dt1->diffInDaysFiltered(function (Carbon $date) { return $date->dayOfWeek === Carbon::SUNDAY; }, $dt2)); } @@ -178,7 +179,7 @@ public function testDiffInDaysFilteredPositiveWithSecondObject() public function testDiffInDaysFilteredNegativeNoSignWithMutated() { $dt = Carbon::createFromDate(2000, 1, 31); - $this->assertSame(5, $dt->diffInDaysFiltered(function (Carbon $date) { + $this->assertSame(5, (int) $dt->diffInDaysFiltered(function (Carbon $date) { return $date->dayOfWeek === Carbon::SUNDAY; }, $dt->copy()->startOfMonth())); } @@ -188,7 +189,7 @@ public function testDiffInDaysFilteredNegativeNoSignWithSecondObject() $dt1 = Carbon::createFromDate(2000, 1, 31); $dt2 = Carbon::createFromDate(2000, 1, 1); - $this->assertSame(5, $dt1->diffInDaysFiltered(function (Carbon $date) { + $this->assertSame(5, (int) $dt1->diffInDaysFiltered(function (Carbon $date) { return $date->dayOfWeek === Carbon::SUNDAY; }, $dt2)); } @@ -196,7 +197,7 @@ public function testDiffInDaysFilteredNegativeNoSignWithSecondObject() public function testDiffInDaysFilteredNegativeWithSignWithMutated() { $dt = Carbon::createFromDate(2000, 1, 31); - $this->assertSame(-5, $dt->diffInDaysFiltered(function (Carbon $date) { + $this->assertSame(-5, (int) $dt->diffInDaysFiltered(function (Carbon $date) { return $date->dayOfWeek === 1; }, $dt->copy()->startOfMonth(), false)); } @@ -206,7 +207,7 @@ public function testDiffInDaysFilteredNegativeWithSignWithSecondObject() $dt1 = Carbon::createFromDate(2000, 1, 31); $dt2 = Carbon::createFromDate(2000, 1, 1); - $this->assertSame(-5, $dt1->diffInDaysFiltered(function (Carbon $date) { + $this->assertSame(-5, (int) $dt1->diffInDaysFiltered(function (Carbon $date) { return $date->dayOfWeek === Carbon::SUNDAY; }, $dt2, false)); } @@ -216,7 +217,7 @@ public function testDiffInHoursFiltered() $dt1 = Carbon::createFromDate(2000, 1, 31)->endOfDay(); $dt2 = Carbon::createFromDate(2000, 1, 1)->startOfDay(); - $this->assertSame(31, $dt1->diffInHoursFiltered(function (Carbon $date) { + $this->assertSame(31, (int) $dt1->diffInHoursFiltered(function (Carbon $date) { return $date->hour === 9; }, $dt2)); } @@ -226,7 +227,7 @@ public function testDiffInHoursFilteredNegative() $dt1 = Carbon::createFromDate(2000, 1, 31)->endOfDay(); $dt2 = Carbon::createFromDate(2000, 1, 1)->startOfDay(); - $this->assertSame(-31, $dt1->diffInHoursFiltered(function (Carbon $date) { + $this->assertSame(-31, (int) $dt1->diffInHoursFiltered(function (Carbon $date) { return $date->hour === 9; }, $dt2, false)); } @@ -236,7 +237,7 @@ public function testDiffInHoursFilteredWorkHoursPerWeek() $dt1 = Carbon::createFromDate(2000, 1, 5)->endOfDay(); $dt2 = Carbon::createFromDate(2000, 1, 1)->startOfDay(); - $this->assertSame(40, $dt1->diffInHoursFiltered(function (Carbon $date) { + $this->assertSame(40, (int) $dt1->diffInHoursFiltered(function (Carbon $date) { return $date->hour > 8 && $date->hour < 17; }, $dt2)); } @@ -301,7 +302,7 @@ public function testBug188DiffWithSameDates() $start = Carbon::create(2014, 10, 8, 15, 20, 0); $end = $start->copy(); - $this->assertSame(0, $start->diffInDays($end)); + $this->assertSame(0, (int) $start->diffInDays($end)); $this->assertSame(0, $start->diffInWeekdays($end)); } @@ -310,7 +311,7 @@ public function testBug188DiffWithDatesOnlyHoursApart() $start = Carbon::create(2014, 10, 8, 15, 20, 0); $end = $start->copy(); - $this->assertSame(0, $start->diffInDays($end)); + $this->assertSame(0, (int) $start->diffInDays($end)); $this->assertSame(0, $start->diffInWeekdays($end)); } @@ -319,7 +320,7 @@ public function testBug188DiffWithSameDates1DayApart() $start = Carbon::create(2014, 10, 8, 15, 20, 0); $end = $start->copy()->addDay(); - $this->assertSame(1, $start->diffInDays($end)); + $this->assertSame(1, (int) $start->diffInDays($end)); $this->assertSame(1, $start->diffInWeekdays($end)); } @@ -329,7 +330,7 @@ public function testBug188DiffWithDatesOnTheWeekend() $start->next(Carbon::SATURDAY); $end = $start->copy()->addDay(); - $this->assertSame(1, $start->diffInDays($end)); + $this->assertSame(1, (int) $start->diffInDays($end)); $this->assertSame(0, $start->diffInWeekdays($end)); } @@ -372,63 +373,63 @@ public function testDiffInWeekendDaysNegativeWithSign() public function testDiffInWeeksPositive() { $dt = Carbon::createFromDate(2000, 1, 1); - $this->assertSame(52, $dt->diffInWeeks($dt->copy()->addYear())); + $this->assertSame(52, (int) $dt->diffInWeeks($dt->copy()->addYear())); } public function testDiffInWeeksNegativeWithSign() { $dt = Carbon::createFromDate(2000, 1, 1); - $this->assertSame(-52, $dt->diffInWeeks($dt->copy()->subYear(), false)); + $this->assertSame(-52, (int) $dt->diffInWeeks($dt->copy()->subYear(), false)); } public function testDiffInWeeksNegativeNoSign() { $dt = Carbon::createFromDate(2000, 1, 1); - $this->assertSame(52, $dt->diffInWeeks($dt->copy()->subYear())); + $this->assertSame(52, (int) $dt->diffInWeeks($dt->copy()->subYear())); } public function testDiffInWeeksVsDefaultNow() { $this->wrapWithTestNow(function () { - $this->assertSame(1, Carbon::now()->subWeek()->diffInWeeks()); + $this->assertSame(1, (int) Carbon::now()->subWeek()->diffInWeeks()); }); } public function testDiffInWeeksEnsureIsTruncated() { $dt = Carbon::createFromDate(2000, 1, 1); - $this->assertSame(0, $dt->diffInWeeks($dt->copy()->addWeek()->subDay())); + $this->assertSame(0, (int) $dt->diffInWeeks($dt->copy()->addWeek()->subDay())); } public function testDiffInHoursPositive() { $dt = Carbon::createFromDate(2000, 1, 1); - $this->assertSame(26, $dt->diffInHours($dt->copy()->addDay()->addHours(2))); + $this->assertSame(26, (int) $dt->diffInHours($dt->copy()->addDay()->addHours(2))); } public function testDiffInHoursNegativeWithSign() { $dt = Carbon::createFromDate(2000, 1, 1); - $this->assertSame(-22, $dt->diffInHours($dt->copy()->subDay()->addHours(2), false)); + $this->assertSame(-22, (int) $dt->diffInHours($dt->copy()->subDay()->addHours(2), false)); } public function testDiffInHoursNegativeNoSign() { $dt = Carbon::createFromDate(2000, 1, 1); - $this->assertSame(22, $dt->diffInHours($dt->copy()->subDay()->addHours(2))); + $this->assertSame(22, (int) $dt->diffInHours($dt->copy()->subDay()->addHours(2))); } public function testDiffInHoursVsDefaultNow() { $this->wrapWithTestNow(function () { - $this->assertSame(48, Carbon::now()->subDays(2)->diffInHours()); + $this->assertSame(48, (int) Carbon::now()->subDays(2)->diffInHours()); }, Carbon::create(2012, 1, 15)); } public function testDiffInHoursEnsureIsTruncated() { $dt = Carbon::createFromDate(2000, 1, 1); - $this->assertSame(1, $dt->diffInHours($dt->copy()->addHour()->addMinutes(31))); + $this->assertSame(1, (int) $dt->diffInHours($dt->copy()->addHour()->addMinutes(31))); } public function testDiffInHoursWithTimezones() @@ -439,99 +440,99 @@ public function testDiffInHoursWithTimezones() $dtToronto = Carbon::create(2012, 1, 1, 0, 0, 0, 'America/Toronto'); $dtVancouver = Carbon::create(2012, 1, 1, 0, 0, 0, 'America/Vancouver'); - $this->assertSame(3, $dtVancouver->diffInHours($dtToronto), 'Midnight in Toronto is 3 hours from midnight in Vancouver'); + $this->assertSame(3, (int) $dtVancouver->diffInHours($dtToronto), 'Midnight in Toronto is 3 hours from midnight in Vancouver'); $dtToronto = Carbon::createFromDate(2012, 1, 1, 'America/Toronto'); sleep(2); $dtVancouver = Carbon::createFromDate(2012, 1, 1, 'America/Vancouver'); - $this->assertSame(0, $dtVancouver->diffInHours($dtToronto) % 24); + $this->assertSame(0, (int) $dtVancouver->diffInHours($dtToronto) % 24); $dtToronto = Carbon::createMidnightDate(2012, 1, 1, 'America/Toronto'); $dtVancouver = Carbon::createMidnightDate(2012, 1, 1, 'America/Vancouver'); - $this->assertSame(3, $dtVancouver->diffInHours($dtToronto), 'Midnight in Toronto is 3 hours from midnight in Vancouver'); + $this->assertSame(3, (int) $dtVancouver->diffInHours($dtToronto), 'Midnight in Toronto is 3 hours from midnight in Vancouver'); } public function testDiffInMinutesPositive() { $dt = Carbon::createFromDate(2000, 1, 1); - $this->assertSame(62, $dt->diffInMinutes($dt->copy()->addHour()->addMinutes(2))); + $this->assertSame(62, (int) $dt->diffInMinutes($dt->copy()->addHour()->addMinutes(2))); } public function testDiffInMinutesPositiveALot() { $dt = Carbon::createFromDate(2000, 1, 1); - $this->assertSame(1502, $dt->diffInMinutes($dt->copy()->addHours(25)->addMinutes(2))); + $this->assertSame(1502, (int) $dt->diffInMinutes($dt->copy()->addHours(25)->addMinutes(2))); } public function testDiffInMinutesNegativeWithSign() { $dt = Carbon::createFromDate(2000, 1, 1); - $this->assertSame(-58, $dt->diffInMinutes($dt->copy()->subHour()->addMinutes(2), false)); + $this->assertSame(-58, (int) $dt->diffInMinutes($dt->copy()->subHour()->addMinutes(2), false)); } public function testDiffInMinutesNegativeNoSign() { $dt = Carbon::createFromDate(2000, 1, 1); - $this->assertSame(58, $dt->diffInMinutes($dt->copy()->subHour()->addMinutes(2))); + $this->assertSame(58, (int) $dt->diffInMinutes($dt->copy()->subHour()->addMinutes(2))); } public function testDiffInMinutesVsDefaultNow() { $this->wrapWithTestNow(function () { - $this->assertSame(60, Carbon::now()->subHour()->diffInMinutes()); + $this->assertSame(60, (int) Carbon::now()->subHour()->diffInMinutes()); }); } public function testDiffInMinutesEnsureIsTruncated() { $dt = Carbon::createFromDate(2000, 1, 1); - $this->assertSame(1, $dt->diffInMinutes($dt->copy()->addMinute()->addSeconds(31))); + $this->assertSame(1, (int) $dt->diffInMinutes($dt->copy()->addMinute()->addSeconds(31))); } public function testDiffInSecondsPositive() { $dt = Carbon::createFromDate(2000, 1, 1); - $this->assertSame(62, $dt->diffInSeconds($dt->copy()->addMinute()->addSeconds(2))); + $this->assertSame(62, (int) $dt->diffInSeconds($dt->copy()->addMinute()->addSeconds(2))); } public function testDiffInSecondsPositiveALot() { $dt = Carbon::createFromDate(2000, 1, 1); - $this->assertSame(7202, $dt->diffInSeconds($dt->copy()->addHours(2)->addSeconds(2))); + $this->assertSame(7202, (int) $dt->diffInSeconds($dt->copy()->addHours(2)->addSeconds(2))); } public function testDiffInSecondsNegativeWithSign() { $dt = Carbon::createFromDate(2000, 1, 1); - $this->assertSame(-58, $dt->diffInSeconds($dt->copy()->subMinute()->addSeconds(2), false)); + $this->assertSame(-58, (int) $dt->diffInSeconds($dt->copy()->subMinute()->addSeconds(2), false)); } public function testDiffInSecondsNegativeNoSign() { $dt = Carbon::createFromDate(2000, 1, 1); - $this->assertSame(58, $dt->diffInSeconds($dt->copy()->subMinute()->addSeconds(2))); + $this->assertSame(58, (int) $dt->diffInSeconds($dt->copy()->subMinute()->addSeconds(2))); } public function testDiffInSecondsVsDefaultNow() { $this->wrapWithTestNow(function () { - $this->assertSame(3600, Carbon::now()->subHour()->diffInSeconds()); + $this->assertSame(3600, (int) Carbon::now()->subHour()->diffInSeconds()); }); } public function testDiffInSecondsEnsureIsTruncated() { $dt = Carbon::createFromDate(2000, 1, 1); - $this->assertSame(1, $dt->diffInSeconds($dt->copy()->addSeconds(1.9))); + $this->assertSame(1.0, $dt->diffInSeconds($dt->copy()->addSeconds(1.9))); } public function testDiffInSecondsWithTimezones() { $dtOttawa = Carbon::createFromDate(2000, 1, 1, 'America/Toronto'); $dtVancouver = Carbon::createFromDate(2000, 1, 1, 'America/Vancouver'); - $this->assertSame(3 * 60 * 60, $dtOttawa->diffInSeconds($dtVancouver)); + $this->assertSame(3 * 60 * 60, (int) $dtOttawa->diffInSeconds($dtVancouver)); } public function testDiffInSecondsWithTimezonesAndVsDefault() @@ -539,7 +540,7 @@ public function testDiffInSecondsWithTimezonesAndVsDefault() $vanNow = Carbon::now('America/Vancouver'); $hereNow = $vanNow->copy()->setTimezone(Carbon::now()->tz); $this->wrapWithTestNow(function () use ($vanNow) { - $this->assertSame(0, $vanNow->diffInSeconds()); + $this->assertSame(0, (int) $vanNow->diffInSeconds()); }, $hereNow); } @@ -1284,7 +1285,7 @@ public function testDiffForHumansWithShorterMonthShouldStillBeAMonth() public function testDiffForHumansWithDateTimeInstance() { - $feb15 = new \DateTime('2015-02-15'); + $feb15 = new DateTime('2015-02-15'); $mar15 = Carbon::parse('2015-03-15'); $this->assertSame('1 month after', $mar15->diffForHumans($feb15)); } @@ -1305,15 +1306,15 @@ public function testDiffWithString() { $dt1 = Carbon::createFromDate(2000, 1, 25)->endOfDay(); - $this->assertSame(383, $dt1->diffInHours('2000-01-10')); + $this->assertSame(384.0, round($dt1->diffInHours('2000-01-10'))); } public function testDiffWithDateTime() { $dt1 = Carbon::createFromDate(2000, 1, 25)->endOfDay(); - $dt2 = new \DateTime('2000-01-10'); + $dt2 = new DateTime('2000-01-10'); - $this->assertSame(383, $dt1->diffInHours($dt2)); + $this->assertSame(384.0, round($dt1->diffInHours($dt2))); } public function testDiffOptions() @@ -1576,7 +1577,7 @@ public function testFloatDiff() $this->assertSame(15.971474250377973, Carbon::parse('2018-02-13 20:55:12.321456')->diffInMonths(Carbon::parse('2019-06-12 14:24:58.987421'), false)); $this->assertSame(-15.971474250377973, Carbon::parse('2019-06-12 14:24:58.987421')->diffInMonths(Carbon::parse('2018-02-13 20:55:12.321456'), false)); - $this->assertSame(1, Carbon::parse('2018-02-13 20:55:12.321456')->diffInYears(Carbon::parse('2019-02-13 20:55:12.321456'))); + $this->assertSame(1.0, Carbon::parse('2018-02-13 20:55:12.321456')->diffInYears(Carbon::parse('2019-02-13 20:55:12.321456'))); $this->assertSame(1.3746000338015283, Carbon::parse('2018-02-13 20:55:12.321456')->diffInYears(Carbon::parse('2019-06-30 14:24:58.987421'))); $this->assertSame(0.9609014036645421, Carbon::parse('2018-02-13 20:55:12.321456')->diffInYears(Carbon::parse('2019-01-30 14:24:58.987421'))); @@ -1590,13 +1591,13 @@ public function testFloatDiff() $this->assertSame(5.325284965308378, Carbon::parse('2018-02-13 20:55:12.321456')->diffInYears(Carbon::parse('2023-06-12 14:24:58.987421'), false)); $this->assertSame(-5.325284965308378, Carbon::parse('2023-06-12 14:24:58.987421')->diffInYears(Carbon::parse('2018-02-13 20:55:12.321456'), false)); - $this->assertSame(1, Carbon::parse('2018-10-01 00:00:00', 'Europe/Paris')->diffInMonths(Carbon::parse('2018-11-01 00:00:00', 'Europe/Paris'))); - $this->assertSame(1, Carbon::parse('2018-10-28 00:00:00')->diffInMonths(Carbon::parse('2018-11-28 00:00:00'))); - $this->assertSame(1, Carbon::parse('2018-10-28 00:00:00', 'Europe/Paris')->diffInMonths(Carbon::parse('2018-11-28 00:00:00', 'Europe/Paris'))); + $this->assertSame(1.0, Carbon::parse('2018-10-01 00:00:00', 'Europe/Paris')->diffInMonths(Carbon::parse('2018-11-01 00:00:00', 'Europe/Paris'))); + $this->assertSame(1.0, Carbon::parse('2018-10-28 00:00:00')->diffInMonths(Carbon::parse('2018-11-28 00:00:00'))); + $this->assertSame(1.0, Carbon::parse('2018-10-28 00:00:00', 'Europe/Paris')->diffInMonths(Carbon::parse('2018-11-28 00:00:00', 'Europe/Paris'))); - $this->assertSame(-1, Carbon::parse('2018-11-01 00:00:00', 'Europe/Paris')->diffInMonths(Carbon::parse('2018-10-01 00:00:00', 'Europe/Paris'), false)); - $this->assertSame(-1, Carbon::parse('2018-11-28 00:00:00')->diffInMonths(Carbon::parse('2018-10-28 00:00:00'), false)); - $this->assertSame(-1, Carbon::parse('2018-11-28 00:00:00', 'Europe/Paris')->diffInMonths(Carbon::parse('2018-10-28 00:00:00', 'Europe/Paris'), false)); + $this->assertSame(-1.0, Carbon::parse('2018-11-01 00:00:00', 'Europe/Paris')->diffInMonths(Carbon::parse('2018-10-01 00:00:00', 'Europe/Paris'), false)); + $this->assertSame(-1.0, Carbon::parse('2018-11-28 00:00:00')->diffInMonths(Carbon::parse('2018-10-28 00:00:00'), false)); + $this->assertSame(-1.0, Carbon::parse('2018-11-28 00:00:00', 'Europe/Paris')->diffInMonths(Carbon::parse('2018-10-28 00:00:00', 'Europe/Paris'), false)); } public function testFloatDiffWithRealUnits() @@ -1617,7 +1618,7 @@ public function testFloatDiffWithRealUnits() $this->assertSame(15.971474250377973, Carbon::parse('2018-02-13 20:55:12.321456')->diffInMonths(Carbon::parse('2019-06-12 14:24:58.987421'), false)); $this->assertSame(-15.971474250377973, Carbon::parse('2019-06-12 14:24:58.987421')->diffInMonths(Carbon::parse('2018-02-13 20:55:12.321456'), false)); - $this->assertSame(1, Carbon::parse('2018-02-13 20:55:12.321456')->diffInYears(Carbon::parse('2019-02-13 20:55:12.321456'))); + $this->assertSame(1.0, Carbon::parse('2018-02-13 20:55:12.321456')->diffInYears(Carbon::parse('2019-02-13 20:55:12.321456'))); $this->assertSame(1.3746000338015283, Carbon::parse('2018-02-13 20:55:12.321456')->diffInYears(Carbon::parse('2019-06-30 14:24:58.987421'))); $this->assertSame(0.9609014036645421, Carbon::parse('2018-02-13 20:55:12.321456')->diffInYears(Carbon::parse('2019-01-30 14:24:58.987421'))); @@ -1631,13 +1632,13 @@ public function testFloatDiffWithRealUnits() $this->assertSame(5.325284965308378, Carbon::parse('2018-02-13 20:55:12.321456')->diffInYears(Carbon::parse('2023-06-12 14:24:58.987421'), false)); $this->assertSame(-5.325284965308378, Carbon::parse('2023-06-12 14:24:58.987421')->diffInYears(Carbon::parse('2018-02-13 20:55:12.321456'), false)); - $this->assertSame(1, Carbon::parse('2018-10-01 00:00:00', 'Europe/Paris')->diffInMonths(Carbon::parse('2018-11-01 00:00:00', 'Europe/Paris'))); - $this->assertSame(1, Carbon::parse('2018-10-28 00:00:00')->diffInMonths(Carbon::parse('2018-11-28 00:00:00'))); - $this->assertSame(1, Carbon::parse('2018-10-28 00:00:00', 'Europe/Paris')->diffInMonths(Carbon::parse('2018-11-28 00:00:00', 'Europe/Paris'))); + $this->assertSame(1.0, Carbon::parse('2018-10-01 00:00:00', 'Europe/Paris')->diffInMonths(Carbon::parse('2018-11-01 00:00:00', 'Europe/Paris'))); + $this->assertSame(1.0, Carbon::parse('2018-10-28 00:00:00')->diffInMonths(Carbon::parse('2018-11-28 00:00:00'))); + $this->assertSame(1.0, Carbon::parse('2018-10-28 00:00:00', 'Europe/Paris')->diffInMonths(Carbon::parse('2018-11-28 00:00:00', 'Europe/Paris'))); - $this->assertSame(-1, Carbon::parse('2018-11-01 00:00:00', 'Europe/Paris')->diffInMonths(Carbon::parse('2018-10-01 00:00:00', 'Europe/Paris'), false)); - $this->assertSame(-1, Carbon::parse('2018-11-28 00:00:00')->diffInMonths(Carbon::parse('2018-10-28 00:00:00'), false)); - $this->assertSame(-1, Carbon::parse('2018-11-28 00:00:00', 'Europe/Paris')->diffInMonths(Carbon::parse('2018-10-28 00:00:00', 'Europe/Paris'), false)); + $this->assertSame(-1.0, Carbon::parse('2018-11-01 00:00:00', 'Europe/Paris')->diffInMonths(Carbon::parse('2018-10-01 00:00:00', 'Europe/Paris'), false)); + $this->assertSame(-1.0, Carbon::parse('2018-11-28 00:00:00')->diffInMonths(Carbon::parse('2018-10-28 00:00:00'), false)); + $this->assertSame(-1.0, Carbon::parse('2018-11-28 00:00:00', 'Europe/Paris')->diffInMonths(Carbon::parse('2018-10-28 00:00:00', 'Europe/Paris'), false)); } /** @@ -1646,42 +1647,42 @@ public function testFloatDiffWithRealUnits() */ public function testPhpBug77007() { - $this->assertSame(3, Carbon::now()->addMinutes(3)->diffInMinutes()); + $this->assertSame(3.0, Carbon::now()->addMinutes(3)->diffInMinutes()); $startDate = Carbon::parse('2018-10-11 20:59:06.914653'); $endDate = Carbon::parse('2018-10-11 20:59:07.237419'); - $this->assertSame(0, $startDate->diffInSeconds($endDate)); + $this->assertSame(0.322766, $startDate->diffInSeconds($endDate)); $startDate = Carbon::parse('2018-10-11 20:59:06.914653'); $endDate = Carbon::parse('2018-10-11 20:59:07.237419'); - $this->assertSame('+ 00-00-00 00:00:00.322766', $startDate->diffAsCarbonInterval($endDate)->format('%R %Y-%M-%D %H:%I:%S.%F')); - $this->assertSame(0, $startDate->diffInSeconds($endDate)); + $this->assertSame('+ 00-00-00 00:00:00.322766', $startDate->diff($endDate)->format('%R %Y-%M-%D %H:%I:%S.%F')); + $this->assertSame(0.322766, $startDate->diffInSeconds($endDate)); $startDate = Carbon::parse('2018-10-11 20:59:06.914653'); $endDate = Carbon::parse('2018-10-11 20:59:05.237419'); - $this->assertSame('+ 00-00-00 00:00:01.677234', $startDate->diffAsCarbonInterval($endDate)->format('%R %Y-%M-%D %H:%I:%S.%F')); - $this->assertSame(1, $startDate->diffInSeconds($endDate)); + $this->assertSame('+ 00-00-00 00:00:01.677234', $startDate->diff($endDate, true)->format('%R %Y-%M-%D %H:%I:%S.%F')); + $this->assertSame(1.677234, $startDate->diffInSeconds($endDate)); - $this->assertSame('- 00-00-00 00:00:01.677234', $startDate->diffAsCarbonInterval($endDate, false)->format('%R %Y-%M-%D %H:%I:%S.%F')); - $this->assertSame(-1, $startDate->diffInSeconds($endDate, false)); + $this->assertSame('- 00-00-00 00:00:01.677234', $startDate->diff($endDate)->format('%R %Y-%M-%D %H:%I:%S.%F')); + $this->assertSame(-1.677234, $startDate->diffInSeconds($endDate, false)); $startDate = Carbon::parse('2018-10-11 20:59:06.914653'); $endDate = Carbon::parse('2018-10-11 20:59:06.237419'); - $this->assertSame('+ 00-00-00 00:00:00.677234', $startDate->diffAsCarbonInterval($endDate)->format('%R %Y-%M-%D %H:%I:%S.%F')); - $this->assertSame(0, $startDate->diffInSeconds($endDate)); + $this->assertSame('+ 00-00-00 00:00:00.677234', $startDate->diff($endDate, true)->format('%R %Y-%M-%D %H:%I:%S.%F')); + $this->assertSame(0.677234, $startDate->diffInSeconds($endDate)); - $this->assertSame('- 00-00-00 00:00:00.677234', $startDate->diffAsCarbonInterval($endDate, false)->format('%R %Y-%M-%D %H:%I:%S.%F')); - $this->assertSame(0, $startDate->diffInSeconds($endDate, false)); + $this->assertSame('- 00-00-00 00:00:00.677234', $startDate->diff($endDate)->format('%R %Y-%M-%D %H:%I:%S.%F')); + $this->assertSame(-0.677234, $startDate->diffInSeconds($endDate, false)); $startDate = Carbon::parse('2017-12-31 23:59:59.914653'); $endDate = Carbon::parse('2018-01-01 00:00:00.237419'); - $this->assertSame('+ 00-00-00 00:00:00.322766', $startDate->diffAsCarbonInterval($endDate)->format('%R %Y-%M-%D %H:%I:%S.%F')); - $this->assertSame(0, $startDate->diffInSeconds($endDate)); + $this->assertSame('+ 00-00-00 00:00:00.322766', $startDate->diff($endDate)->format('%R %Y-%M-%D %H:%I:%S.%F')); + $this->assertSame(0.322766, $startDate->diffInSeconds($endDate)); } public function testDiffWithZeroAndNonZeroMicroseconds() @@ -1689,12 +1690,12 @@ public function testDiffWithZeroAndNonZeroMicroseconds() $requestTime = new Carbon('2018-11-14 18:23:12.0 +00:00'); $serverTime = new Carbon('2018-11-14 18:23:12.307628 +00:00'); - $this->assertSame(0, $serverTime->diffInSeconds($requestTime)); + $this->assertSame(0.307628, $serverTime->diffInSeconds($requestTime)); $requestTime = new Carbon('2019-02-10 18:23:12.0 +00:00'); $serverTime = new Carbon('2019-02-10 18:23:12.307628 +00:00'); - $this->assertSame(0, $serverTime->diffInSeconds($requestTime)); + $this->assertSame(0.307628, $serverTime->diffInSeconds($requestTime)); } public function testNearlyFullDayDiffInSeconds() @@ -1705,7 +1706,7 @@ public function testNearlyFullDayDiffInSeconds() // Due to https://bugs.php.net/bug.php?id=77007, this changed in 7.2.12: $expected = version_compare(PHP_VERSION, '7.2.12-dev', '<') ? 86400 // Bad rounding before PHP 7.2.12 - : 86399; // Exact result since PHP 7.2.12 + : 86399.999999; // Exact result since PHP 7.2.12 $this->assertSame($expected, $d2->diffInSeconds($d1)); } @@ -1715,7 +1716,7 @@ public function testNearlyFullDayDiffInMicroseconds() $d1 = Carbon::parse('2019-06-15 12:34:56.123456'); $d2 = Carbon::parse('2019-06-16 12:34:56.123455'); - $this->assertSame(86399999999, $d2->diffInMicroseconds($d1)); + $this->assertSame(86399999999.0, $d2->diffInMicroseconds($d1)); } public function testExactMonthDiffInSeconds() @@ -1723,6 +1724,6 @@ public function testExactMonthDiffInSeconds() $d1 = Carbon::make('2019-01-23 12:00:00'); $d2 = Carbon::make('2019-02-23 12:00:00'); - $this->assertSame(2678400, $d2->diffInSeconds($d1)); + $this->assertSame(2678400.0, $d2->diffInSeconds($d1)); } } diff --git a/tests/Carbon/IsTest.php b/tests/Carbon/IsTest.php index 7fb392d19f..426b03f58e 100644 --- a/tests/Carbon/IsTest.php +++ b/tests/Carbon/IsTest.php @@ -615,7 +615,7 @@ public function testIsSameMicrosecond() $this->assertFalse(Carbon::now()->isNextMicrosecond()); $this->assertTrue(Carbon::now()->addMicrosecond()->isNextMicrosecond()); $this->assertTrue(Carbon::now()->subMicroseconds(Carbon::MICROSECONDS_PER_SECOND)->isLastSecond()); - $this->assertSame(4, Carbon::now()->subMicroseconds(4 * Carbon::MICROSECONDS_PER_SECOND)->diffInSeconds(Carbon::now())); + $this->assertSame(4.0, Carbon::now()->subMicroseconds(4 * Carbon::MICROSECONDS_PER_SECOND)->diffInSeconds(Carbon::now())); } public function testIsDayOfWeek() diff --git a/tests/Carbon/LocalizationTest.php b/tests/Carbon/LocalizationTest.php index a588d37628..a0737f5596 100644 --- a/tests/Carbon/LocalizationTest.php +++ b/tests/Carbon/LocalizationTest.php @@ -733,7 +733,7 @@ public function testTranslationCustomWithCustomTranslator() { $this->expectException(InvalidArgumentException::class); $this->expectExceptionMessage( - 'Translator does not implement Symfony\Contracts\Translation\TranslatorInterface '. + 'Translator does not implement Symfony\Component\Translation\TranslatorInterface '. 'and Symfony\Component\Translation\TranslatorBagInterface. '. 'Symfony\Component\Translation\IdentityTranslator has been given.' ); diff --git a/tests/Carbon/MacroTest.php b/tests/Carbon/MacroTest.php index 54c3809a84..2bee17a0ea 100644 --- a/tests/Carbon/MacroTest.php +++ b/tests/Carbon/MacroTest.php @@ -59,7 +59,7 @@ public function testCarbonIsMacroableWhenNotCalledDynamicallyUsingThis() /** @var CarbonInterface $date */ $date = $this; - return $date->diff( + return $date->toDateTime()->diff( Carbon::create($year, 3, 21) ->setTimezone($date->getTimezone()) ->addDays(easter_days($year)) diff --git a/tests/Carbon/ModifyTest.php b/tests/Carbon/ModifyTest.php index 252de43eff..5ec7912a27 100644 --- a/tests/Carbon/ModifyTest.php +++ b/tests/Carbon/ModifyTest.php @@ -21,7 +21,7 @@ public function testSimpleModify() $a = new Carbon('2014-03-30 00:00:00'); $b = $a->copy(); $b->addHours(24); - $this->assertSame(24, $a->diffInHours($b)); + $this->assertSame(24.0, $a->diffInHours($b)); } public function testTimezoneModify() diff --git a/tests/Carbon/RelativeTest.php b/tests/Carbon/RelativeTest.php index 9d15a6cfce..bb6c38b5a3 100644 --- a/tests/Carbon/RelativeTest.php +++ b/tests/Carbon/RelativeTest.php @@ -19,30 +19,30 @@ class RelativeTest extends AbstractTestCase public function testSecondsSinceMidnight() { $d = Carbon::today()->addSeconds(30); - $this->assertSame(30, $d->secondsSinceMidnight()); + $this->assertSame(30.0, $d->secondsSinceMidnight()); $d = Carbon::today()->addDays(1); - $this->assertSame(0, $d->secondsSinceMidnight()); + $this->assertSame(0.0, $d->secondsSinceMidnight()); $d = Carbon::today()->addDays(1)->addSeconds(120); - $this->assertSame(120, $d->secondsSinceMidnight()); + $this->assertSame(120.0, $d->secondsSinceMidnight()); $d = Carbon::today()->addMonths(3)->addSeconds(42); - $this->assertSame(42, $d->secondsSinceMidnight()); + $this->assertSame(42.0, $d->secondsSinceMidnight()); } public function testSecondsUntilEndOfDay() { $d = Carbon::today()->endOfDay(); - $this->assertSame(0, $d->secondsUntilEndOfDay()); + $this->assertSame(0.0, $d->secondsUntilEndOfDay()); $d = Carbon::today()->endOfDay()->subSeconds(60); - $this->assertSame(60, $d->secondsUntilEndOfDay()); + $this->assertSame(60.0, $d->secondsUntilEndOfDay()); $d = Carbon::create(2014, 10, 24, 12, 34, 56); - $this->assertSame(41103, $d->secondsUntilEndOfDay()); + $this->assertSame(41103.999999, $d->secondsUntilEndOfDay()); $d = Carbon::create(2014, 10, 24, 0, 0, 0); - $this->assertSame(86399, $d->secondsUntilEndOfDay()); + $this->assertSame(86399.999999, $d->secondsUntilEndOfDay()); } } diff --git a/tests/Carbon/SettersTest.php b/tests/Carbon/SettersTest.php index fb215710c3..b38bb0e217 100644 --- a/tests/Carbon/SettersTest.php +++ b/tests/Carbon/SettersTest.php @@ -13,6 +13,7 @@ use Carbon\Carbon; use DateTimeZone; +use Exception; use Tests\AbstractTestCase; class SettersTest extends AbstractTestCase @@ -567,7 +568,7 @@ public function testSetUnitNoOverflow() if ($date->$valueUnit === $value || $date->$valueUnit === $modulo || (method_exists($date, "diffInReal$unit") && $$valueUnit - $date->{"diffInReal$unit"}($original, false) === $value) || - $$valueUnit - $date->{"diffIn$unit"}($original, false) === $value + $$valueUnit - ((int) $date->{"diffIn$unit"}($original, false)) === $value ) { $results['current']++; @@ -586,7 +587,7 @@ public function testSetUnitNoOverflow() continue; } - throw new \Exception('Unhandled result for: '. + throw new Exception('Unhandled result for: '. 'Carbon::parse('.var_export($original->format('Y-m-d H:i:s.u'), true).', '. var_export($original->timezoneName, true). ')->setUnitNoOverflow('.implode(', ', array_map(function ($value) { @@ -675,7 +676,7 @@ public function testAddUnitNoOverflow() if ($date->$valueUnit === $value || $date->$valueUnit === $modulo || (method_exists($date, "diffInReal$unit") && -$date->{"diffInReal$unit"}($original, false) === $value) || - -$date->{"diffIn$unit"}($original, false) === $value + -((int) $date->{"diffIn$unit"}($original, false)) === $value ) { $results['current']++; @@ -694,7 +695,7 @@ public function testAddUnitNoOverflow() continue; } - throw new \Exception('Unhandled result for: '. + throw new Exception('Unhandled result for: '. 'Carbon::parse('.var_export($original->format('Y-m-d H:i:s.u'), true).', '. var_export($original->timezoneName, true). ')->addUnitNoOverflow('.implode(', ', array_map(function ($value) { @@ -763,7 +764,7 @@ public function testSubUnitNoOverflow() if ($date->$valueUnit === $value || $date->$valueUnit === $modulo || (method_exists($date, "diffInReal$unit") && $date->{"diffInReal$unit"}($original, false) === $value) || - $date->{"diffIn$unit"}($original, false) === $value + ((int) $date->{"diffIn$unit"}($original, false)) === $value ) { $results['current']++; @@ -782,7 +783,7 @@ public function testSubUnitNoOverflow() continue; } - throw new \Exception('Unhandled result for: '. + throw new Exception('Unhandled result for: '. 'Carbon::parse('.var_export($original->format('Y-m-d H:i:s.u'), true).', '. var_export($original->timezoneName, true). ')->subUnitNoOverflow('.implode(', ', array_map(function ($value) { diff --git a/tests/CarbonImmutable/DiffTest.php b/tests/CarbonImmutable/DiffTest.php index 4b9c2af95c..5fb90f13f3 100644 --- a/tests/CarbonImmutable/DiffTest.php +++ b/tests/CarbonImmutable/DiffTest.php @@ -27,100 +27,100 @@ public function wrapWithTestNow(Closure $func, CarbonInterface $dt = null) public function testDiffAsCarbonInterval() { $dt = Carbon::createFromDate(2000, 1, 1); - $this->assertCarbonInterval($dt->diffAsCarbonInterval($dt->copy()->addYear()), 1, 0, 0, 0, 0, 0); + $this->assertCarbonInterval($dt->diff($dt->copy()->addYear()), 1, 0, 0, 0, 0, 0); } public function testDiffInYearsPositive() { $dt = Carbon::createFromDate(2000, 1, 1); - $this->assertSame(1, $dt->diffInYears($dt->copy()->addYear())); + $this->assertSame(1.0, $dt->diffInYears($dt->copy()->addYear())); } public function testDiffInYearsNegativeWithSign() { $dt = Carbon::createFromDate(2000, 1, 1); - $this->assertSame(-1, $dt->diffInYears($dt->copy()->subYear(), false)); + $this->assertSame(-1.0, $dt->diffInYears($dt->copy()->subYear(), false)); } public function testDiffInYearsNegativeNoSign() { $dt = Carbon::createFromDate(2000, 1, 1); - $this->assertSame(1, $dt->diffInYears($dt->copy()->subYear())); + $this->assertSame(1.0, $dt->diffInYears($dt->copy()->subYear())); } public function testDiffInYearsVsDefaultNow() { $this->wrapWithTestNow(function () { - $this->assertSame(1, Carbon::now()->subYear()->diffInYears()); + $this->assertSame(1.0, Carbon::now()->subYear()->diffInYears()); }); } public function testDiffInYearsEnsureIsTruncated() { $dt = Carbon::createFromDate(2000, 1, 1); - $this->assertSame(1, $dt->diffInYears($dt->copy()->addYear()->addMonths(7))); + $this->assertSame(1, (int) $dt->diffInYears($dt->copy()->addYear()->addMonths(7))); } public function testDiffInMonthsPositive() { $dt = Carbon::createFromDate(2000, 1, 1); - $this->assertSame(13, $dt->diffInMonths($dt->copy()->addYear()->addMonth())); + $this->assertSame(13, (int) $dt->diffInMonths($dt->copy()->addYear()->addMonth())); } public function testDiffInMonthsNegativeWithSign() { $dt = Carbon::createFromDate(2000, 1, 1); - $this->assertSame(-11, $dt->diffInMonths($dt->copy()->subYear()->addMonth(), false)); + $this->assertSame(-11, (int) $dt->diffInMonths($dt->copy()->subYear()->addMonth(), false)); } public function testDiffInMonthsNegativeNoSign() { $dt = Carbon::createFromDate(2000, 1, 1); - $this->assertSame(11, $dt->diffInMonths($dt->copy()->subYear()->addMonth())); + $this->assertSame(11, (int) $dt->diffInMonths($dt->copy()->subYear()->addMonth())); } public function testDiffInMonthsVsDefaultNow() { $this->wrapWithTestNow(function () { - $this->assertSame(12, Carbon::now()->subYear()->diffInMonths()); + $this->assertSame(12, (int) Carbon::now()->subYear()->diffInMonths()); }); } public function testDiffInMonthsEnsureIsTruncated() { $dt = Carbon::createFromDate(2000, 1, 1); - $this->assertSame(1, $dt->diffInMonths($dt->copy()->addMonth()->addDays(16))); + $this->assertSame(1, (int) $dt->diffInMonths($dt->copy()->addMonth()->addDays(16))); } public function testDiffInDaysPositive() { $dt = Carbon::createFromDate(2000, 1, 1); - $this->assertSame(366, $dt->diffInDays($dt->copy()->addYear())); + $this->assertSame(366.0, $dt->diffInDays($dt->copy()->addYear())); } public function testDiffInDaysNegativeWithSign() { $dt = Carbon::createFromDate(2000, 1, 1); - $this->assertSame(-365, $dt->diffInDays($dt->copy()->subYear(), false)); + $this->assertSame(-365.0, $dt->diffInDays($dt->copy()->subYear(), false)); } public function testDiffInDaysNegativeNoSign() { $dt = Carbon::createFromDate(2000, 1, 1); - $this->assertSame(365, $dt->diffInDays($dt->copy()->subYear())); + $this->assertSame(365.0, $dt->diffInDays($dt->copy()->subYear())); } public function testDiffInDaysVsDefaultNow() { $this->wrapWithTestNow(function () { - $this->assertSame(7, Carbon::now()->subWeek()->diffInDays()); + $this->assertSame(7.0, Carbon::now()->subWeek()->diffInDays()); }); } public function testDiffInDaysEnsureIsTruncated() { $dt = Carbon::createFromDate(2000, 1, 1); - $this->assertSame(1, $dt->diffInDays($dt->copy()->addDay()->addHours(13))); + $this->assertSame(1, (int) $dt->diffInDays($dt->copy()->addDay()->addHours(13))); } public function testDiffInDaysFilteredPositiveWithMutated() @@ -267,7 +267,7 @@ public function testBug188DiffWithSameDates() $start = Carbon::create(2014, 10, 8, 15, 20, 0); $end = $start->copy(); - $this->assertSame(0, $start->diffInDays($end)); + $this->assertSame(0.0, $start->diffInDays($end)); $this->assertSame(0, $start->diffInWeekdays($end)); } @@ -276,7 +276,7 @@ public function testBug188DiffWithDatesOnlyHoursApart() $start = Carbon::create(2014, 10, 8, 15, 20, 0); $end = $start->copy(); - $this->assertSame(0, $start->diffInDays($end)); + $this->assertSame(0.0, $start->diffInDays($end)); $this->assertSame(0, $start->diffInWeekdays($end)); } @@ -285,7 +285,7 @@ public function testBug188DiffWithSameDates1DayApart() $start = Carbon::create(2014, 10, 8, 15, 20, 0); $end = $start->copy()->addDay(); - $this->assertSame(1, $start->diffInDays($end)); + $this->assertSame(1.0, $start->diffInDays($end)); $this->assertSame(1, $start->diffInWeekdays($end)); } @@ -295,7 +295,7 @@ public function testBug188DiffWithDatesOnTheWeekend() $start = $start->next(Carbon::SATURDAY); $end = $start->copy()->addDay(); - $this->assertSame(1, $start->diffInDays($end)); + $this->assertSame(1.0, $start->diffInDays($end)); $this->assertSame(0, $start->diffInWeekdays($end)); } @@ -338,63 +338,63 @@ public function testDiffInWeekendDaysNegativeWithSign() public function testDiffInWeeksPositive() { $dt = Carbon::createFromDate(2000, 1, 1); - $this->assertSame(52, $dt->diffInWeeks($dt->copy()->addYear())); + $this->assertSame(52, (int) $dt->diffInWeeks($dt->copy()->addYear())); } public function testDiffInWeeksNegativeWithSign() { $dt = Carbon::createFromDate(2000, 1, 1); - $this->assertSame(-52, $dt->diffInWeeks($dt->copy()->subYear(), false)); + $this->assertSame(-52, (int) $dt->diffInWeeks($dt->copy()->subYear(), false)); } public function testDiffInWeeksNegativeNoSign() { $dt = Carbon::createFromDate(2000, 1, 1); - $this->assertSame(52, $dt->diffInWeeks($dt->copy()->subYear())); + $this->assertSame(52, (int) $dt->diffInWeeks($dt->copy()->subYear())); } public function testDiffInWeeksVsDefaultNow() { $this->wrapWithTestNow(function () { - $this->assertSame(1, Carbon::now()->subWeek()->diffInWeeks()); + $this->assertSame(1.0, Carbon::now()->subWeek()->diffInWeeks()); }); } public function testDiffInWeeksEnsureIsTruncated() { $dt = Carbon::createFromDate(2000, 1, 1); - $this->assertSame(0, $dt->diffInWeeks($dt->copy()->addWeek()->subDay())); + $this->assertSame(0.86, round($dt->diffInWeeks($dt->copy()->addWeek()->subDay()), 2)); } public function testDiffInHoursPositive() { $dt = Carbon::createFromDate(2000, 1, 1); - $this->assertSame(26, $dt->diffInHours($dt->copy()->addDay()->addHours(2))); + $this->assertSame(26.0, $dt->diffInHours($dt->copy()->addDay()->addHours(2))); } public function testDiffInHoursNegativeWithSign() { $dt = Carbon::createFromDate(2000, 1, 1); - $this->assertSame(-22, $dt->diffInHours($dt->copy()->subDay()->addHours(2), false)); + $this->assertSame(-22.0, $dt->diffInHours($dt->copy()->subDay()->addHours(2), false)); } public function testDiffInHoursNegativeNoSign() { $dt = Carbon::createFromDate(2000, 1, 1); - $this->assertSame(22, $dt->diffInHours($dt->copy()->subDay()->addHours(2))); + $this->assertSame(22.0, $dt->diffInHours($dt->copy()->subDay()->addHours(2))); } public function testDiffInHoursVsDefaultNow() { $this->wrapWithTestNow(function () { - $this->assertSame(48, Carbon::now()->subDays(2)->diffInHours()); + $this->assertSame(48.0, Carbon::now()->subDays(2)->diffInHours()); }, Carbon::create(2012, 1, 15)); } public function testDiffInHoursEnsureIsTruncated() { $dt = Carbon::createFromDate(2000, 1, 1); - $this->assertSame(1, $dt->diffInHours($dt->copy()->addHour()->addMinutes(31))); + $this->assertSame(1.52, round($dt->diffInHours($dt->copy()->addHour()->addMinutes(31)), 2)); } public function testDiffInHoursWithTimezones() @@ -405,7 +405,7 @@ public function testDiffInHoursWithTimezones() $dtToronto = Carbon::create(2012, 1, 1, 0, 0, 0, 'America/Toronto'); $dtVancouver = Carbon::create(2012, 1, 1, 0, 0, 0, 'America/Vancouver'); - $this->assertSame(3, $dtVancouver->diffInHours($dtToronto), 'Midnight in Toronto is 3 hours from midnight in Vancouver'); + $this->assertSame(3.0, $dtVancouver->diffInHours($dtToronto), 'Midnight in Toronto is 3 hours from midnight in Vancouver'); $dtToronto = Carbon::createFromDate(2012, 1, 1, 'America/Toronto'); sleep(2); @@ -416,88 +416,88 @@ public function testDiffInHoursWithTimezones() $dtToronto = Carbon::createMidnightDate(2012, 1, 1, 'America/Toronto'); $dtVancouver = Carbon::createMidnightDate(2012, 1, 1, 'America/Vancouver'); - $this->assertSame(3, $dtVancouver->diffInHours($dtToronto), 'Midnight in Toronto is 3 hours from midnight in Vancouver'); + $this->assertSame(3.0, $dtVancouver->diffInHours($dtToronto), 'Midnight in Toronto is 3 hours from midnight in Vancouver'); } public function testDiffInMinutesPositive() { $dt = Carbon::createFromDate(2000, 1, 1); - $this->assertSame(62, $dt->diffInMinutes($dt->copy()->addHour()->addMinutes(2))); + $this->assertSame(62.0, $dt->diffInMinutes($dt->copy()->addHour()->addMinutes(2))); } public function testDiffInMinutesPositiveALot() { $dt = Carbon::createFromDate(2000, 1, 1); - $this->assertSame(1502, $dt->diffInMinutes($dt->copy()->addHours(25)->addMinutes(2))); + $this->assertSame(1502.0, $dt->diffInMinutes($dt->copy()->addHours(25)->addMinutes(2))); } public function testDiffInMinutesNegativeWithSign() { $dt = Carbon::createFromDate(2000, 1, 1); - $this->assertSame(-58, $dt->diffInMinutes($dt->copy()->subHour()->addMinutes(2), false)); + $this->assertSame(-58.0, $dt->diffInMinutes($dt->copy()->subHour()->addMinutes(2), false)); } public function testDiffInMinutesNegativeNoSign() { $dt = Carbon::createFromDate(2000, 1, 1); - $this->assertSame(58, $dt->diffInMinutes($dt->copy()->subHour()->addMinutes(2))); + $this->assertSame(58.0, $dt->diffInMinutes($dt->copy()->subHour()->addMinutes(2))); } public function testDiffInMinutesVsDefaultNow() { $this->wrapWithTestNow(function () { - $this->assertSame(60, Carbon::now()->subHour()->diffInMinutes()); + $this->assertSame(60.0, Carbon::now()->subHour()->diffInMinutes()); }); } public function testDiffInMinutesEnsureIsTruncated() { $dt = Carbon::createFromDate(2000, 1, 1); - $this->assertSame(1, $dt->diffInMinutes($dt->copy()->addMinute()->addSeconds(31))); + $this->assertSame(1.52, round($dt->diffInMinutes($dt->copy()->addMinute()->addSeconds(31)), 2)); } public function testDiffInSecondsPositive() { $dt = Carbon::createFromDate(2000, 1, 1); - $this->assertSame(62, $dt->diffInSeconds($dt->copy()->addMinute()->addSeconds(2))); + $this->assertSame(62.0, $dt->diffInSeconds($dt->copy()->addMinute()->addSeconds(2))); } public function testDiffInSecondsPositiveALot() { $dt = Carbon::createFromDate(2000, 1, 1); - $this->assertSame(7202, $dt->diffInSeconds($dt->copy()->addHours(2)->addSeconds(2))); + $this->assertSame(7202.0, $dt->diffInSeconds($dt->copy()->addHours(2)->addSeconds(2))); } public function testDiffInSecondsNegativeWithSign() { $dt = Carbon::createFromDate(2000, 1, 1); - $this->assertSame(-58, $dt->diffInSeconds($dt->copy()->subMinute()->addSeconds(2), false)); + $this->assertSame(-58.0, $dt->diffInSeconds($dt->copy()->subMinute()->addSeconds(2), false)); } public function testDiffInSecondsNegativeNoSign() { $dt = Carbon::createFromDate(2000, 1, 1); - $this->assertSame(58, $dt->diffInSeconds($dt->copy()->subMinute()->addSeconds(2))); + $this->assertSame(58.0, $dt->diffInSeconds($dt->copy()->subMinute()->addSeconds(2))); } public function testDiffInSecondsVsDefaultNow() { $this->wrapWithTestNow(function () { - $this->assertSame(3600, Carbon::now()->subHour()->diffInSeconds()); + $this->assertSame(3600.0, Carbon::now()->subHour()->diffInSeconds()); }); } public function testDiffInSecondsEnsureIsTruncated() { $dt = Carbon::createFromDate(2000, 1, 1); - $this->assertSame(1, $dt->diffInSeconds($dt->copy()->addSeconds(1.9))); + $this->assertSame(1.0, $dt->diffInSeconds($dt->copy()->addSeconds(1.9))); } public function testDiffInSecondsWithTimezones() { $dtOttawa = Carbon::createFromDate(2000, 1, 1, 'America/Toronto'); $dtVancouver = Carbon::createFromDate(2000, 1, 1, 'America/Vancouver'); - $this->assertSame(3 * 60 * 60, $dtOttawa->diffInSeconds($dtVancouver)); + $this->assertSame(3.0 * 60 * 60, $dtOttawa->diffInSeconds($dtVancouver)); } public function testDiffInSecondsWithTimezonesAndVsDefault() @@ -505,7 +505,7 @@ public function testDiffInSecondsWithTimezonesAndVsDefault() $vanNow = Carbon::now('America/Vancouver'); $hereNow = $vanNow->copy()->setTimezone(Carbon::now()->tz); $this->wrapWithTestNow(function () use ($vanNow) { - $this->assertSame(0, $vanNow->diffInSeconds()); + $this->assertSame(0.0, $vanNow->diffInSeconds()); }, $hereNow); } @@ -1216,7 +1216,8 @@ public function testDiffWithString() { $dt1 = Carbon::createFromDate(2000, 1, 25)->endOfDay(); - $this->assertSame(383, $dt1->diffInHours('2000-01-10')); + $this->assertSame(384.0, round($dt1->diffInHours('2000-01-10'))); + $this->assertSame(383.0, floor($dt1->diffInHours('2000-01-10'))); } public function testDiffWithDateTime() @@ -1224,7 +1225,7 @@ public function testDiffWithDateTime() $dt1 = Carbon::createFromDate(2000, 1, 25)->endOfDay(); $dt2 = new \DateTime('2000-01-10'); - $this->assertSame(383, $dt1->diffInHours($dt2)); + $this->assertSame(383, (int) $dt1->diffInHours($dt2)); } public function testDiffOptions() @@ -1554,7 +1555,7 @@ public function testFloatDiff() $this->assertSame(15.971474250377973, Carbon::parse('2018-02-13 20:55:12.321456')->diffInMonths(Carbon::parse('2019-06-12 14:24:58.987421'), false)); $this->assertSame(-15.971474250377973, Carbon::parse('2019-06-12 14:24:58.987421')->diffInMonths(Carbon::parse('2018-02-13 20:55:12.321456'), false)); - $this->assertSame(1, Carbon::parse('2018-02-13 20:55:12.321456')->diffInYears(Carbon::parse('2019-02-13 20:55:12.321456'))); + $this->assertSame(1.0, Carbon::parse('2018-02-13 20:55:12.321456')->diffInYears(Carbon::parse('2019-02-13 20:55:12.321456'))); $this->assertSame(1.3746000338015283, Carbon::parse('2018-02-13 20:55:12.321456')->diffInYears(Carbon::parse('2019-06-30 14:24:58.987421'))); $this->assertSame(0.9609014036645421, Carbon::parse('2018-02-13 20:55:12.321456')->diffInYears(Carbon::parse('2019-01-30 14:24:58.987421'))); @@ -1568,13 +1569,13 @@ public function testFloatDiff() $this->assertSame(5.325284965308378, Carbon::parse('2018-02-13 20:55:12.321456')->diffInYears(Carbon::parse('2023-06-12 14:24:58.987421'), false)); $this->assertSame(-5.325284965308378, Carbon::parse('2023-06-12 14:24:58.987421')->diffInYears(Carbon::parse('2018-02-13 20:55:12.321456'), false)); - $this->assertSame(1, Carbon::parse('2018-10-01 00:00:00', 'Europe/Paris')->diffInMonths(Carbon::parse('2018-11-01 00:00:00', 'Europe/Paris'))); - $this->assertSame(1, Carbon::parse('2018-10-28 00:00:00')->diffInMonths(Carbon::parse('2018-11-28 00:00:00'))); - $this->assertSame(1, Carbon::parse('2018-10-28 00:00:00', 'Europe/Paris')->diffInMonths(Carbon::parse('2018-11-28 00:00:00', 'Europe/Paris'))); + $this->assertSame(1.0, Carbon::parse('2018-10-01 00:00:00', 'Europe/Paris')->diffInMonths(Carbon::parse('2018-11-01 00:00:00', 'Europe/Paris'))); + $this->assertSame(1.0, Carbon::parse('2018-10-28 00:00:00')->diffInMonths(Carbon::parse('2018-11-28 00:00:00'))); + $this->assertSame(1.0, Carbon::parse('2018-10-28 00:00:00', 'Europe/Paris')->diffInMonths(Carbon::parse('2018-11-28 00:00:00', 'Europe/Paris'))); - $this->assertSame(-1, Carbon::parse('2018-11-01 00:00:00', 'Europe/Paris')->diffInMonths(Carbon::parse('2018-10-01 00:00:00', 'Europe/Paris'), false)); - $this->assertSame(-1, Carbon::parse('2018-11-28 00:00:00')->diffInMonths(Carbon::parse('2018-10-28 00:00:00'), false)); - $this->assertSame(-1, Carbon::parse('2018-11-28 00:00:00', 'Europe/Paris')->diffInMonths(Carbon::parse('2018-10-28 00:00:00', 'Europe/Paris'), false)); + $this->assertSame(-1.0, Carbon::parse('2018-11-01 00:00:00', 'Europe/Paris')->diffInMonths(Carbon::parse('2018-10-01 00:00:00', 'Europe/Paris'), false)); + $this->assertSame(-1.0, Carbon::parse('2018-11-28 00:00:00')->diffInMonths(Carbon::parse('2018-10-28 00:00:00'), false)); + $this->assertSame(-1.0, Carbon::parse('2018-11-28 00:00:00', 'Europe/Paris')->diffInMonths(Carbon::parse('2018-10-28 00:00:00', 'Europe/Paris'), false)); } public function testFloatDiffWithRealUnits() @@ -1595,7 +1596,7 @@ public function testFloatDiffWithRealUnits() $this->assertSame(15.971474250377973, Carbon::parse('2018-02-13 20:55:12.321456')->diffInMonths(Carbon::parse('2019-06-12 14:24:58.987421'), false)); $this->assertSame(-15.971474250377973, Carbon::parse('2019-06-12 14:24:58.987421')->diffInMonths(Carbon::parse('2018-02-13 20:55:12.321456'), false)); - $this->assertSame(1, Carbon::parse('2018-02-13 20:55:12.321456')->diffInYears(Carbon::parse('2019-02-13 20:55:12.321456'))); + $this->assertSame(1.0, Carbon::parse('2018-02-13 20:55:12.321456')->diffInYears(Carbon::parse('2019-02-13 20:55:12.321456'))); $this->assertSame(1.3746000338015283, Carbon::parse('2018-02-13 20:55:12.321456')->diffInYears(Carbon::parse('2019-06-30 14:24:58.987421'))); $this->assertSame(0.9609014036645421, Carbon::parse('2018-02-13 20:55:12.321456')->diffInYears(Carbon::parse('2019-01-30 14:24:58.987421'))); @@ -1609,13 +1610,13 @@ public function testFloatDiffWithRealUnits() $this->assertSame(5.325284965308378, Carbon::parse('2018-02-13 20:55:12.321456')->diffInYears(Carbon::parse('2023-06-12 14:24:58.987421'), false)); $this->assertSame(-5.325284965308378, Carbon::parse('2023-06-12 14:24:58.987421')->diffInYears(Carbon::parse('2018-02-13 20:55:12.321456'), false)); - $this->assertSame(1, Carbon::parse('2018-10-01 00:00:00', 'Europe/Paris')->diffInMonths(Carbon::parse('2018-11-01 00:00:00', 'Europe/Paris'))); - $this->assertSame(1, Carbon::parse('2018-10-28 00:00:00')->diffInMonths(Carbon::parse('2018-11-28 00:00:00'))); - $this->assertSame(1, Carbon::parse('2018-10-28 00:00:00', 'Europe/Paris')->diffInMonths(Carbon::parse('2018-11-28 00:00:00', 'Europe/Paris'))); + $this->assertSame(1.0, Carbon::parse('2018-10-01 00:00:00', 'Europe/Paris')->diffInMonths(Carbon::parse('2018-11-01 00:00:00', 'Europe/Paris'))); + $this->assertSame(1.0, Carbon::parse('2018-10-28 00:00:00')->diffInMonths(Carbon::parse('2018-11-28 00:00:00'))); + $this->assertSame(1.0, Carbon::parse('2018-10-28 00:00:00', 'Europe/Paris')->diffInMonths(Carbon::parse('2018-11-28 00:00:00', 'Europe/Paris'))); - $this->assertSame(-1, Carbon::parse('2018-11-01 00:00:00', 'Europe/Paris')->diffInMonths(Carbon::parse('2018-10-01 00:00:00', 'Europe/Paris'), false)); - $this->assertSame(-1, Carbon::parse('2018-11-28 00:00:00')->diffInMonths(Carbon::parse('2018-10-28 00:00:00'), false)); - $this->assertSame(-1, Carbon::parse('2018-11-28 00:00:00', 'Europe/Paris')->diffInMonths(Carbon::parse('2018-10-28 00:00:00', 'Europe/Paris'), false)); + $this->assertSame(-1.0, Carbon::parse('2018-11-01 00:00:00', 'Europe/Paris')->diffInMonths(Carbon::parse('2018-10-01 00:00:00', 'Europe/Paris'), false)); + $this->assertSame(-1.0, Carbon::parse('2018-11-28 00:00:00')->diffInMonths(Carbon::parse('2018-10-28 00:00:00'), false)); + $this->assertSame(-1.0, Carbon::parse('2018-11-28 00:00:00', 'Europe/Paris')->diffInMonths(Carbon::parse('2018-10-28 00:00:00', 'Europe/Paris'), false)); } /** @@ -1624,42 +1625,42 @@ public function testFloatDiffWithRealUnits() */ public function testPhpBug77007() { - $this->assertSame(3, Carbon::now()->addMinutes(3)->diffInMinutes()); + $this->assertSame(3.0, Carbon::now()->addMinutes(3)->diffInMinutes()); $startDate = Carbon::parse('2018-10-11 20:59:06.914653'); $endDate = Carbon::parse('2018-10-11 20:59:07.237419'); - $this->assertSame(0, $startDate->diffInSeconds($endDate)); + $this->assertSame(0.322766, $startDate->diffInSeconds($endDate)); $startDate = Carbon::parse('2018-10-11 20:59:06.914653'); $endDate = Carbon::parse('2018-10-11 20:59:07.237419'); - $this->assertSame('+ 00-00-00 00:00:00.322766', $startDate->diffAsCarbonInterval($endDate)->format('%R %Y-%M-%D %H:%I:%S.%F')); - $this->assertSame(0, $startDate->diffInSeconds($endDate)); + $this->assertSame('+ 00-00-00 00:00:00.322766', $startDate->diff($endDate)->format('%R %Y-%M-%D %H:%I:%S.%F')); + $this->assertSame(0.322766, $startDate->diffInSeconds($endDate)); $startDate = Carbon::parse('2018-10-11 20:59:06.914653'); $endDate = Carbon::parse('2018-10-11 20:59:05.237419'); - $this->assertSame('+ 00-00-00 00:00:01.677234', $startDate->diffAsCarbonInterval($endDate)->format('%R %Y-%M-%D %H:%I:%S.%F')); - $this->assertSame(1, $startDate->diffInSeconds($endDate)); + $this->assertSame('+ 00-00-00 00:00:01.677234', $startDate->diff($endDate, true)->format('%R %Y-%M-%D %H:%I:%S.%F')); + $this->assertSame(1.677234, $startDate->diffInSeconds($endDate)); - $this->assertSame('- 00-00-00 00:00:01.677234', $startDate->diffAsCarbonInterval($endDate, false)->format('%R %Y-%M-%D %H:%I:%S.%F')); - $this->assertSame(-1, $startDate->diffInSeconds($endDate, false)); + $this->assertSame('- 00-00-00 00:00:01.677234', $startDate->diff($endDate)->format('%R %Y-%M-%D %H:%I:%S.%F')); + $this->assertSame(-1.677234, $startDate->diffInSeconds($endDate, false)); $startDate = Carbon::parse('2018-10-11 20:59:06.914653'); $endDate = Carbon::parse('2018-10-11 20:59:06.237419'); - $this->assertSame('+ 00-00-00 00:00:00.677234', $startDate->diffAsCarbonInterval($endDate)->format('%R %Y-%M-%D %H:%I:%S.%F')); - $this->assertSame(0, $startDate->diffInSeconds($endDate)); + $this->assertSame('+ 00-00-00 00:00:00.677234', $startDate->diff($endDate, true)->format('%R %Y-%M-%D %H:%I:%S.%F')); + $this->assertSame(0.677234, $startDate->diffInSeconds($endDate)); - $this->assertSame('- 00-00-00 00:00:00.677234', $startDate->diffAsCarbonInterval($endDate, false)->format('%R %Y-%M-%D %H:%I:%S.%F')); - $this->assertSame(0, $startDate->diffInSeconds($endDate, false)); + $this->assertSame('- 00-00-00 00:00:00.677234', $startDate->diff($endDate)->format('%R %Y-%M-%D %H:%I:%S.%F')); + $this->assertSame(-0.677234, $startDate->diffInSeconds($endDate, false)); $startDate = Carbon::parse('2017-12-31 23:59:59.914653'); $endDate = Carbon::parse('2018-01-01 00:00:00.237419'); - $this->assertSame('+ 00-00-00 00:00:00.322766', $startDate->diffAsCarbonInterval($endDate)->format('%R %Y-%M-%D %H:%I:%S.%F')); - $this->assertSame(0, $startDate->diffInSeconds($endDate)); + $this->assertSame('+ 00-00-00 00:00:00.322766', $startDate->diff($endDate)->format('%R %Y-%M-%D %H:%I:%S.%F')); + $this->assertSame(0.322766, $startDate->diffInSeconds($endDate)); } public function testDiffWithZeroAndNonZeroMicroseconds() @@ -1667,12 +1668,12 @@ public function testDiffWithZeroAndNonZeroMicroseconds() $requestTime = new Carbon('2018-11-14 18:23:12.0 +00:00'); $serverTime = new Carbon('2018-11-14 18:23:12.307628 +00:00'); - $this->assertSame(0, $serverTime->diffInSeconds($requestTime)); + $this->assertSame(0.307628, $serverTime->diffInSeconds($requestTime)); $requestTime = new Carbon('2019-02-10 18:23:12.0 +00:00'); $serverTime = new Carbon('2019-02-10 18:23:12.307628 +00:00'); - $this->assertSame(0, $serverTime->diffInSeconds($requestTime)); + $this->assertSame(0.307628, $serverTime->diffInSeconds($requestTime)); } public function testNearlyFullDayDiffInSeconds() @@ -1683,7 +1684,7 @@ public function testNearlyFullDayDiffInSeconds() // Due to https://bugs.php.net/bug.php?id=77007, this changed in 7.2.12: $expected = version_compare(PHP_VERSION, '7.2.12-dev', '<') ? 86400 // Bad rounding before PHP 7.2.12 - : 86399; // Exact result since PHP 7.2.12 + : 86399.999999; // Exact result since PHP 7.2.12 $this->assertSame($expected, $d2->diffInSeconds($d1)); } @@ -1693,7 +1694,7 @@ public function testNearlyFullDayDiffInMicroseconds() $d1 = Carbon::parse('2019-06-15 12:34:56.123456'); $d2 = Carbon::parse('2019-06-16 12:34:56.123455'); - $this->assertSame(86399999999, $d2->diffInMicroseconds($d1)); + $this->assertSame(86399999999.0, $d2->diffInMicroseconds($d1)); } public function testExactMonthDiffInSeconds() @@ -1701,6 +1702,6 @@ public function testExactMonthDiffInSeconds() $d1 = Carbon::make('2019-01-23 12:00:00'); $d2 = Carbon::make('2019-02-23 12:00:00'); - $this->assertSame(2678400, $d2->diffInSeconds($d1)); + $this->assertSame(2678400.0, $d2->diffInSeconds($d1)); } } diff --git a/tests/CarbonImmutable/LocalizationTest.php b/tests/CarbonImmutable/LocalizationTest.php index 6f578dced1..cc4b84f23e 100644 --- a/tests/CarbonImmutable/LocalizationTest.php +++ b/tests/CarbonImmutable/LocalizationTest.php @@ -694,7 +694,7 @@ public function testTranslationCustomWithCustomTranslator() { $this->expectException(\InvalidArgumentException::class); $this->expectExceptionMessage( - 'Translator does not implement Symfony\Contracts\Translation\TranslatorInterface '. + 'Translator does not implement Symfony\Component\Translation\TranslatorInterface '. 'and Symfony\Component\Translation\TranslatorBagInterface. '. 'Symfony\Component\Translation\IdentityTranslator has been given.' ); diff --git a/tests/CarbonImmutable/MacroTest.php b/tests/CarbonImmutable/MacroTest.php index 1d3afd1e35..b3f3492133 100644 --- a/tests/CarbonImmutable/MacroTest.php +++ b/tests/CarbonImmutable/MacroTest.php @@ -51,7 +51,7 @@ public function testCarbonIsMacroableWhenNotCalledDynamicallyUsingThis() /** @var Carbon $date */ $date = $this; - return $date->diff( + return $date->toDateTime()->diff( Carbon::create($year, 3, 21) ->setTimezone($date->getTimezone()) ->addDays(easter_days($year)) diff --git a/tests/CarbonImmutable/ModifyTest.php b/tests/CarbonImmutable/ModifyTest.php index e0ac885668..d902b15353 100644 --- a/tests/CarbonImmutable/ModifyTest.php +++ b/tests/CarbonImmutable/ModifyTest.php @@ -20,7 +20,7 @@ public function testSimpleModify() { $a = new Carbon('2014-03-30 00:00:00'); $b = $a->addHours(24); - $this->assertSame(24, $a->diffInHours($b)); + $this->assertSame(24.0, $a->diffInHours($b)); } public function testTimezoneModify() @@ -28,8 +28,7 @@ public function testTimezoneModify() // For daylight saving time reason 2014-03-30 0h59 is immediately followed by 2h00 $a = new Carbon('2014-03-30 00:00:00', 'Europe/London'); - $b = $a->copy(); - $b->addHours(24); + $b = $a->addHours(24); $this->assertSame(23.0, $a->diffInHours($b, false)); $this->assertSame(23.0, $b->diffInHours($a)); $this->assertSame(-23.0, $b->diffInHours($a, false)); @@ -39,68 +38,66 @@ public function testTimezoneModify() $this->assertSame(-23.0 * 60 * 60 * 1000000, $b->diffInMicroseconds($a, false)); $a = new Carbon('2014-03-30 00:00:00', 'Europe/London'); - $b = $a->copy(); - $b->addRealHours(24); + $b = $a->addRealHours(24); $this->assertSame(-24.0, $b->diffInHours($a, false)); $this->assertSame(-24.0 * 60, $b->diffInMinutes($a, false)); $this->assertSame(-24.0 * 60 * 60, $b->diffInSeconds($a, false)); $this->assertSame(-24.0 * 60 * 60 * 1000, $b->diffInMilliseconds($a, false)); $this->assertSame(-24.0 * 60 * 60 * 1000000, $b->diffInMicroseconds($a, false)); - $b->subRealHours(24); + $b = $b->subRealHours(24); $this->assertSame(0.0, $b->diffInHours($a, false)); $this->assertSame(0.0, $b->diffInHours($a, false)); $a = new Carbon('2014-03-30 00:59:00', 'Europe/London'); - $a->addRealHour(); + $a = $a->addRealHour(); $this->assertSame('02:59', $a->format('H:i')); - $a->subRealHour(); + $a = $a->subRealHour(); $this->assertSame('00:59', $a->format('H:i')); $a = new Carbon('2014-03-30 00:59:00', 'Europe/London'); - $a->addRealMinutes(2); + $a = $a->addRealMinutes(2); $this->assertSame('02:01', $a->format('H:i')); - $a->subRealMinutes(2); + $a = $a->subRealMinutes(2); $this->assertSame('00:59', $a->format('H:i')); $a = new Carbon('2014-03-30 00:59:30', 'Europe/London'); - $a->addRealMinute(); + $a = $a->addRealMinute(); $this->assertSame('02:00:30', $a->format('H:i:s')); - $a->subRealMinute(); + $a = $a->subRealMinute(); $this->assertSame('00:59:30', $a->format('H:i:s')); $a = new Carbon('2014-03-30 00:59:30', 'Europe/London'); - $a->addRealSeconds(40); + $a = $a->addRealSeconds(40); $this->assertSame('02:00:10', $a->format('H:i:s')); - $a->subRealSeconds(40); + $a = $a->subRealSeconds(40); $this->assertSame('00:59:30', $a->format('H:i:s')); $a = new Carbon('2014-03-30 00:59:59', 'Europe/London'); - $a->addRealSecond(); + $a = $a->addRealSecond(); $this->assertSame('02:00:00', $a->format('H:i:s')); - $a->subRealSecond(); + $a = $a->subRealSecond(); $this->assertSame('00:59:59', $a->format('H:i:s')); $a = new Carbon('2014-03-30 00:59:59.990000', 'Europe/London'); - $a->addRealMilliseconds(20); + $a = $a->addRealMilliseconds(20); $this->assertSame('02:00:00.010000', $a->format('H:i:s.u')); - $a->subRealMilliseconds(20); + $a = $a->subRealMilliseconds(20); $this->assertSame('00:59:59.990000', $a->format('H:i:s.u')); $a = new Carbon('2014-03-30 00:59:59.999990', 'Europe/London'); - $a->addRealMicroseconds(20); + $a = $a->addRealMicroseconds(20); $this->assertSame('02:00:00.000010', $a->format('H:i:s.u')); - $a->subRealMicroseconds(20); + $a = $a->subRealMicroseconds(20); $this->assertSame('00:59:59.999990', $a->format('H:i:s.u')); $a = new Carbon('2014-03-30 00:59:59.999999', 'Europe/London'); - $a->addRealMicrosecond(); + $a = $a->addRealMicrosecond(); $this->assertSame('02:00:00.000000', $a->format('H:i:s.u')); - $a->subRealMicrosecond(); + $a = $a->subRealMicrosecond(); $this->assertSame('00:59:59.999999', $a->format('H:i:s.u')); $a = new Carbon('2014-03-30 00:00:00', 'Europe/London'); - $b = $a->copy(); - $b->addRealDay(); + $b = $a->addRealDay(); $this->assertSame(-24.0, $b->diffInHours($a, false)); $this->assertSame(-24.0 * 60, $b->diffInMinutes($a, false)); $this->assertSame(-24.0 * 60 * 60, $b->diffInSeconds($a, false)); @@ -108,8 +105,7 @@ public function testTimezoneModify() $this->assertSame(-24.0 * 60 * 60 * 1000000, $b->diffInMicroseconds($a, false)); $a = new Carbon('2014-03-30 00:00:00', 'Europe/London'); - $b = $a->copy(); - $b->addRealWeeks(1 / 7); + $b = $a->addRealWeeks(1 / 7); $this->assertSame(-24.0, $b->diffInHours($a, false)); $this->assertSame(-24.0 * 60, $b->diffInMinutes($a, false)); $this->assertSame(-24.0 * 60 * 60, $b->diffInSeconds($a, false)); @@ -117,8 +113,7 @@ public function testTimezoneModify() $this->assertSame(-24.0 * 60 * 60 * 1000000, $b->diffInMicroseconds($a, false)); $a = new Carbon('2014-03-30 00:00:00', 'Europe/London'); - $b = $a->copy(); - $b->addRealMonths(1 / 30); + $b = $a->addRealMonths(1 / 30); $this->assertSame(-24.0, $b->diffInHours($a, false)); $this->assertSame(-24.0 * 60, $b->diffInMinutes($a, false)); $this->assertSame(-24.0 * 60 * 60, $b->diffInSeconds($a, false)); @@ -126,8 +121,7 @@ public function testTimezoneModify() $this->assertSame(-24.0 * 60 * 60 * 1000000, $b->diffInMicroseconds($a, false)); $a = new Carbon('2014-03-30 00:00:00', 'Europe/London'); - $b = $a->copy(); - $b->addRealQuarters(1 / 90); + $b = $a->addRealQuarters(1 / 90); $this->assertSame(-24.0, $b->diffInHours($a, false)); $this->assertSame(-24.0 * 60, $b->diffInMinutes($a, false)); $this->assertSame(-24.0 * 60 * 60, $b->diffInSeconds($a, false)); @@ -135,8 +129,7 @@ public function testTimezoneModify() $this->assertSame(-24.0 * 60 * 60 * 1000000, $b->diffInMicroseconds($a, false)); $a = new Carbon('2014-03-30 00:00:00', 'Europe/London'); - $b = $a->copy(); - $b->addRealYears(1 / 365); + $b = $a->addRealYears(1 / 365); $this->assertSame(-24.0, $b->diffInHours($a, false)); $this->assertSame(-24.0 * 60, $b->diffInMinutes($a, false)); $this->assertSame(-24.0 * 60 * 60, $b->diffInSeconds($a, false)); @@ -144,8 +137,7 @@ public function testTimezoneModify() $this->assertSame(-24.0 * 60 * 60 * 1000000, $b->diffInMicroseconds($a, false)); $a = new Carbon('2014-03-30 00:00:00', 'Europe/London'); - $b = $a->copy(); - $b->addRealDecades(1 / 3650); + $b = $a->addRealDecades(1 / 3650); $this->assertSame(-24.0, $b->diffInHours($a, false)); $this->assertSame(-24.0 * 60, $b->diffInMinutes($a, false)); $this->assertSame(-24.0 * 60 * 60, $b->diffInSeconds($a, false)); @@ -153,8 +145,7 @@ public function testTimezoneModify() $this->assertSame(-24.0 * 60 * 60 * 1000000, $b->diffInMicroseconds($a, false)); $a = new Carbon('2014-03-30 00:00:00', 'Europe/London'); - $b = $a->copy(); - $b->addRealCenturies(1 / 36500); + $b = $a->addRealCenturies(1 / 36500); $this->assertSame(-24.0, $b->diffInHours($a, false)); $this->assertSame(-24.0 * 60, $b->diffInMinutes($a, false)); $this->assertSame(-24.0 * 60 * 60, $b->diffInSeconds($a, false)); @@ -162,8 +153,7 @@ public function testTimezoneModify() $this->assertSame(-24.0 * 60 * 60 * 1000000, $b->diffInMicroseconds($a, false)); $a = new Carbon('2014-03-30 00:00:00', 'Europe/London'); - $b = $a->copy(); - $b->addRealMillennia(1 / 365000); + $b = $a->addRealMillennia(1 / 365000); $this->assertSame(-24.0, $b->diffInHours($a, false)); $this->assertSame(-24.0 * 60, $b->diffInMinutes($a, false)); $this->assertSame(-24.0 * 60 * 60, $b->diffInSeconds($a, false)); diff --git a/tests/CarbonImmutable/RelativeTest.php b/tests/CarbonImmutable/RelativeTest.php index 43d6e95705..65ffa5af84 100644 --- a/tests/CarbonImmutable/RelativeTest.php +++ b/tests/CarbonImmutable/RelativeTest.php @@ -19,30 +19,30 @@ class RelativeTest extends AbstractTestCase public function testSecondsSinceMidnight() { $d = Carbon::today()->addSeconds(30); - $this->assertSame(30, $d->secondsSinceMidnight()); + $this->assertSame(30.0, $d->secondsSinceMidnight()); $d = Carbon::today()->addDays(1); - $this->assertSame(0, $d->secondsSinceMidnight()); + $this->assertSame(0.0, $d->secondsSinceMidnight()); $d = Carbon::today()->addDays(1)->addSeconds(120); - $this->assertSame(120, $d->secondsSinceMidnight()); + $this->assertSame(120.0, $d->secondsSinceMidnight()); $d = Carbon::today()->addMonths(3)->addSeconds(42); - $this->assertSame(42, $d->secondsSinceMidnight()); + $this->assertSame(42.0, $d->secondsSinceMidnight()); } public function testSecondsUntilEndOfDay() { $d = Carbon::today()->endOfDay(); - $this->assertSame(0, $d->secondsUntilEndOfDay()); + $this->assertSame(0.0, $d->secondsUntilEndOfDay()); $d = Carbon::today()->endOfDay()->subSeconds(60); - $this->assertSame(60, $d->secondsUntilEndOfDay()); + $this->assertSame(60.0, $d->secondsUntilEndOfDay()); $d = Carbon::create(2014, 10, 24, 12, 34, 56); - $this->assertSame(41103, $d->secondsUntilEndOfDay()); + $this->assertSame(41103.999999, $d->secondsUntilEndOfDay()); $d = Carbon::create(2014, 10, 24, 0, 0, 0); - $this->assertSame(86399, $d->secondsUntilEndOfDay()); + $this->assertSame(86399.999999, $d->secondsUntilEndOfDay()); } } From 9926745088156b4c75ce4f46f033f656490376e1 Mon Sep 17 00:00:00 2001 From: KyleKatarn Date: Thu, 6 Aug 2020 17:32:29 +0200 Subject: [PATCH 13/38] Make $absolute default to false in diff* methods --- src/Carbon/CarbonInterface.php | 30 +++++++++++++++--------------- src/Carbon/Traits/Difference.php | 30 +++++++++++++++--------------- src/Carbon/Traits/Modifiers.php | 4 ++-- 3 files changed, 32 insertions(+), 32 deletions(-) diff --git a/src/Carbon/CarbonInterface.php b/src/Carbon/CarbonInterface.php index bad7e4daa0..9de8db44f8 100644 --- a/src/Carbon/CarbonInterface.php +++ b/src/Carbon/CarbonInterface.php @@ -1209,7 +1209,7 @@ public function dayOfYear($value = null); * * @return int */ - public function diffFiltered(CarbonInterval $ci, Closure $callback, $date = null, $absolute = true): int; + public function diffFiltered(CarbonInterval $ci, Closure $callback, $date = null, $absolute = false): int; /** * Get the difference in a human readable format in the current locale from current instance to an other @@ -1262,7 +1262,7 @@ public function diffForHumans($other = null, $syntax = null, $short = false, $pa * * @return float */ - public function diffInDays($date = null, $absolute = true): float; + public function diffInDays($date = null, $absolute = false): float; /** * Get the difference in days using a filter closure rounded down. @@ -1273,7 +1273,7 @@ public function diffInDays($date = null, $absolute = true): float; * * @return int */ - public function diffInDaysFiltered(Closure $callback, $date = null, $absolute = true): int; + public function diffInDaysFiltered(Closure $callback, $date = null, $absolute = false): int; /** * Get the difference in hours rounded down. @@ -1283,7 +1283,7 @@ public function diffInDaysFiltered(Closure $callback, $date = null, $absolute = * * @return float */ - public function diffInHours($date = null, $absolute = true): float; + public function diffInHours($date = null, $absolute = false): float; /** * Get the difference in hours using a filter closure rounded down. @@ -1294,7 +1294,7 @@ public function diffInHours($date = null, $absolute = true): float; * * @return int */ - public function diffInHoursFiltered(Closure $callback, $date = null, $absolute = true): int; + public function diffInHoursFiltered(Closure $callback, $date = null, $absolute = false): int; /** * Get the difference in microseconds. @@ -1304,7 +1304,7 @@ public function diffInHoursFiltered(Closure $callback, $date = null, $absolute = * * @return float */ - public function diffInMicroseconds($date = null, $absolute = true): float; + public function diffInMicroseconds($date = null, $absolute = false): float; /** * Get the difference in milliseconds rounded down. @@ -1314,7 +1314,7 @@ public function diffInMicroseconds($date = null, $absolute = true): float; * * @return float */ - public function diffInMilliseconds($date = null, $absolute = true): float; + public function diffInMilliseconds($date = null, $absolute = false): float; /** * Get the difference in minutes rounded down. @@ -1324,7 +1324,7 @@ public function diffInMilliseconds($date = null, $absolute = true): float; * * @return float */ - public function diffInMinutes($date = null, $absolute = true): float; + public function diffInMinutes($date = null, $absolute = false): float; /** * Get the difference in months rounded down. @@ -1334,7 +1334,7 @@ public function diffInMinutes($date = null, $absolute = true): float; * * @return float */ - public function diffInMonths($date = null, $absolute = true): float; + public function diffInMonths($date = null, $absolute = false): float; /** * Get the difference in quarters rounded down. @@ -1344,7 +1344,7 @@ public function diffInMonths($date = null, $absolute = true): float; * * @return float */ - public function diffInQuarters($date = null, $absolute = true): float; + public function diffInQuarters($date = null, $absolute = false): float; /** * Get the difference in seconds rounded down. @@ -1354,7 +1354,7 @@ public function diffInQuarters($date = null, $absolute = true): float; * * @return float */ - public function diffInSeconds($date = null, $absolute = true): float; + public function diffInSeconds($date = null, $absolute = false): float; /** * Get the difference in weekdays rounded down. @@ -1364,7 +1364,7 @@ public function diffInSeconds($date = null, $absolute = true): float; * * @return int */ - public function diffInWeekdays($date = null, $absolute = true): int; + public function diffInWeekdays($date = null, $absolute = false): int; /** * Get the difference in weekend days using a filter rounded down. @@ -1374,7 +1374,7 @@ public function diffInWeekdays($date = null, $absolute = true): int; * * @return int */ - public function diffInWeekendDays($date = null, $absolute = true): int; + public function diffInWeekendDays($date = null, $absolute = false): int; /** * Get the difference in weeks rounded down. @@ -1384,7 +1384,7 @@ public function diffInWeekendDays($date = null, $absolute = true): int; * * @return int */ - public function diffInWeeks($date = null, $absolute = true): float; + public function diffInWeeks($date = null, $absolute = false): float; /** * Get the difference in years @@ -1394,7 +1394,7 @@ public function diffInWeeks($date = null, $absolute = true): float; * * @return float */ - public function diffInYears($date = null, $absolute = true); + public function diffInYears($date = null, $absolute = false); /** * @deprecated To avoid conflict between different third-party libraries, static setters should not be used. diff --git a/src/Carbon/Traits/Difference.php b/src/Carbon/Traits/Difference.php index 174f351c6b..d2a83aead9 100644 --- a/src/Carbon/Traits/Difference.php +++ b/src/Carbon/Traits/Difference.php @@ -134,7 +134,7 @@ public function diff($date = null, $absolute = false): CarbonInterval * * @return float */ - public function diffInYears($date = null, $absolute = true): float + public function diffInYears($date = null, $absolute = false): float { $start = $this; $end = $this->resolveCarbon($date); @@ -169,7 +169,7 @@ public function diffInYears($date = null, $absolute = true): float * * @return float */ - public function diffInQuarters($date = null, $absolute = true): float + public function diffInQuarters($date = null, $absolute = false): float { return $this->diffInMonths($date, $absolute) / static::MONTHS_PER_QUARTER; } @@ -182,7 +182,7 @@ public function diffInQuarters($date = null, $absolute = true): float * * @return float */ - public function diffInMonths($date = null, $absolute = true): float + public function diffInMonths($date = null, $absolute = false): float { $start = $this; $end = $this->resolveCarbon($date); @@ -222,7 +222,7 @@ public function diffInMonths($date = null, $absolute = true): float * * @return int */ - public function diffInWeeks($date = null, $absolute = true): float + public function diffInWeeks($date = null, $absolute = false): float { return $this->diffInDays($date, $absolute) / static::DAYS_PER_WEEK; } @@ -235,7 +235,7 @@ public function diffInWeeks($date = null, $absolute = true): float * * @return float */ - public function diffInDays($date = null, $absolute = true): float + public function diffInDays($date = null, $absolute = false): float { $daysDiff = (int) parent::diff($this->resolveCarbon($date), $absolute)->format('%r%a'); $hoursDiff = $this->diffInHours($date, $absolute); @@ -252,7 +252,7 @@ public function diffInDays($date = null, $absolute = true): float * * @return int */ - public function diffInDaysFiltered(Closure $callback, $date = null, $absolute = true): int + public function diffInDaysFiltered(Closure $callback, $date = null, $absolute = false): int { return $this->diffFiltered(CarbonInterval::day(), $callback, $date, $absolute); } @@ -266,7 +266,7 @@ public function diffInDaysFiltered(Closure $callback, $date = null, $absolute = * * @return int */ - public function diffInHoursFiltered(Closure $callback, $date = null, $absolute = true): int + public function diffInHoursFiltered(Closure $callback, $date = null, $absolute = false): int { return $this->diffFiltered(CarbonInterval::hour(), $callback, $date, $absolute); } @@ -281,7 +281,7 @@ public function diffInHoursFiltered(Closure $callback, $date = null, $absolute = * * @return int */ - public function diffFiltered(CarbonInterval $ci, Closure $callback, $date = null, $absolute = true): int + public function diffFiltered(CarbonInterval $ci, Closure $callback, $date = null, $absolute = false): int { $start = $this; $end = $this->resolveCarbon($date); @@ -307,7 +307,7 @@ public function diffFiltered(CarbonInterval $ci, Closure $callback, $date = null * * @return int */ - public function diffInWeekdays($date = null, $absolute = true): int + public function diffInWeekdays($date = null, $absolute = false): int { return $this->diffInDaysFiltered(function (CarbonInterface $date) { return $date->isWeekday(); @@ -322,7 +322,7 @@ public function diffInWeekdays($date = null, $absolute = true): int * * @return int */ - public function diffInWeekendDays($date = null, $absolute = true): int + public function diffInWeekendDays($date = null, $absolute = false): int { return $this->diffInDaysFiltered(function (CarbonInterface $date) { return $date->isWeekend(); @@ -337,7 +337,7 @@ public function diffInWeekendDays($date = null, $absolute = true): int * * @return float */ - public function diffInHours($date = null, $absolute = true): float + public function diffInHours($date = null, $absolute = false): float { return $this->diffInMinutes($date, $absolute) / static::MINUTES_PER_HOUR; } @@ -350,7 +350,7 @@ public function diffInHours($date = null, $absolute = true): float * * @return float */ - public function diffInMinutes($date = null, $absolute = true): float + public function diffInMinutes($date = null, $absolute = false): float { return $this->diffInSeconds($date, $absolute) / static::SECONDS_PER_MINUTE; } @@ -363,7 +363,7 @@ public function diffInMinutes($date = null, $absolute = true): float * * @return float */ - public function diffInSeconds($date = null, $absolute = true): float + public function diffInSeconds($date = null, $absolute = false): float { return $this->diffInMilliseconds($date, $absolute) / static::MILLISECONDS_PER_SECOND; } @@ -376,7 +376,7 @@ public function diffInSeconds($date = null, $absolute = true): float * * @return float */ - public function diffInMicroseconds($date = null, $absolute = true): float + public function diffInMicroseconds($date = null, $absolute = false): float { /** @var CarbonInterface $date */ $date = $this->resolveCarbon($date); @@ -394,7 +394,7 @@ public function diffInMicroseconds($date = null, $absolute = true): float * * @return float */ - public function diffInMilliseconds($date = null, $absolute = true): float + public function diffInMilliseconds($date = null, $absolute = false): float { return $this->diffInMicroseconds($date, $absolute) / static::MICROSECONDS_PER_MILLISECOND; } diff --git a/src/Carbon/Traits/Modifiers.php b/src/Carbon/Traits/Modifiers.php index f3dfd63841..c287acf898 100644 --- a/src/Carbon/Traits/Modifiers.php +++ b/src/Carbon/Traits/Modifiers.php @@ -352,7 +352,7 @@ public function average($date = null) */ public function closest($date1, $date2) { - return $this->diffInMicroseconds($date1) < $this->diffInMicroseconds($date2) ? $date1 : $date2; + return $this->diffInMicroseconds($date1, true) < $this->diffInMicroseconds($date2, true) ? $date1 : $date2; } /** @@ -365,7 +365,7 @@ public function closest($date1, $date2) */ public function farthest($date1, $date2) { - return $this->diffInMicroseconds($date1) > $this->diffInMicroseconds($date2) ? $date1 : $date2; + return $this->diffInMicroseconds($date1, true) > $this->diffInMicroseconds($date2, true) ? $date1 : $date2; } /** From 9705d15eb1e52d7504fc2b97be3e5018c9f728e4 Mon Sep 17 00:00:00 2001 From: KyleKatarn Date: Sun, 9 Aug 2020 23:02:14 +0200 Subject: [PATCH 14/38] Add methods to keep the track of the date interval input --- src/Carbon/CarbonInterval.php | 100 ++++++++++++++++--- src/Carbon/Traits/Difference.php | 33 +++++- src/Carbon/Traits/Options.php | 2 +- tests/CarbonInterval/FromStringTest.php | 2 +- tests/CarbonInterval/ParseFromLocaleTest.php | 2 +- tests/CarbonInterval/SpecTest.php | 2 +- tests/CarbonPeriod/AliasTest.php | 4 +- 7 files changed, 124 insertions(+), 21 deletions(-) diff --git a/src/Carbon/CarbonInterval.php b/src/Carbon/CarbonInterval.php index 5f5de8995b..c69b3a5874 100644 --- a/src/Carbon/CarbonInterval.php +++ b/src/Carbon/CarbonInterval.php @@ -25,6 +25,7 @@ use Carbon\Traits\Options; use Closure; use DateInterval; +use DateTimeInterface; use Exception; use ReflectionException; use Symfony\Contracts\Translation\TranslatorInterface; @@ -234,6 +235,27 @@ class CarbonInterval extends DateInterval implements CarbonConverterInterface */ protected $tzName; + /** + * The input used to create the interval. + * + * @var mixed + */ + protected $originalInput; + + /** + * Start date if interval was created from a difference between 2 dates. + * + * @var DateTimeInterface + */ + protected $startDate; + + /** + * End date if interval was created from a difference between 2 dates. + * + * @var DateTimeInterface + */ + protected $endDate; + /** * Set the instance's timezone from a string or object and add/subtract the offset difference. * @@ -320,6 +342,8 @@ public static function setCascadeFactors(array $cascadeFactors) */ public function __construct($years = null, $months = null, $weeks = null, $days = null, $hours = null, $minutes = null, $seconds = null, $microseconds = null) { + $this->originalInput = func_num_args() === 1 ? $years : func_get_args(); + if ($years instanceof Closure) { $this->step = $years; $years = null; @@ -543,6 +567,50 @@ public static function createFromFormat(string $format, ?string $interval) return $instance; } + /** + * Return the original source used to create the current interval. + * + * @return array|int|string|DateInterval|mixed|null + */ + public function original() + { + return $this->originalInput; + } + + /** + * Return the start date if interval was created from a difference between 2 dates. + * + * @return DateTimeInterface + */ + public function start(): DateTimeInterface + { + return $this->startDate; + } + + /** + * Return the end date if interval was created from a difference between 2 dates. + * + * @return DateTimeInterface + */ + public function end(): DateTimeInterface + { + return $this->endDate; + } + + /** + * Get rid of the original input, start date and end date that may be kept in memory. + * + * @return $this + */ + public function optimize(): self + { + $this->originalInput = null; + $this->startDate = null; + $this->endDate = null; + + return $this; + } + /** * Get a copy of the instance. * @@ -807,6 +875,10 @@ private static function castIntervalToClass(DateInterval $interval, string $clas $microseconds = $interval->f; $instance = new $className(static::getDateIntervalSpec($interval)); + if ($instance instanceof self) { + $instance->originalInput = $interval; + } + if ($microseconds) { $instance->f = $microseconds; } @@ -958,41 +1030,41 @@ public function get($name) return $this->total(substr($name, 5)); } - switch ($name) { - case 'years': + switch (Carbon::singularUnit(rtrim($name, 'z'))) { + case 'year': return $this->y; - case 'months': + case 'month': return $this->m; - case 'dayz': + case 'day': return $this->d; - case 'hours': + case 'hour': return $this->h; - case 'minutes': + case 'minute': return $this->i; - case 'seconds': + case 'second': return $this->s; case 'milli': - case 'milliseconds': + case 'millisecond': return (int) (round($this->f * Carbon::MICROSECONDS_PER_SECOND) / Carbon::MICROSECONDS_PER_MILLISECOND); case 'micro': - case 'microseconds': + case 'microsecond': return (int) round($this->f * Carbon::MICROSECONDS_PER_SECOND); - case 'microExcludeMilli': + case 'microexcludemilli': return (int) round($this->f * Carbon::MICROSECONDS_PER_SECOND) % Carbon::MICROSECONDS_PER_MILLISECOND; - case 'weeks': + case 'week': return (int) ($this->d / static::getDaysPerWeek()); - case 'daysExcludeWeeks': - case 'dayzExcludeWeeks': + case 'daysexcludeweek': + case 'dayzexcludeweek': return $this->d % static::getDaysPerWeek(); case 'locale': @@ -1246,7 +1318,7 @@ protected function callMacro($name, $parameters) * * @throws BadFluentSetterException|Throwable * - * @return static + * @return static|int|float|string */ public function __call($method, $parameters) { diff --git a/src/Carbon/Traits/Difference.php b/src/Carbon/Traits/Difference.php index 174f351c6b..68a878391a 100644 --- a/src/Carbon/Traits/Difference.php +++ b/src/Carbon/Traits/Difference.php @@ -108,6 +108,37 @@ protected static function fixDiffInterval(DateInterval $diff, $absolute) return $diff; } + /** + * Get the difference as a DateInterval instance. + * Return relative interval (negative if + * + * @param \Carbon\CarbonInterface|\DateTimeInterface|string|null $date + * @param bool $absolute Get the absolute of the difference + * + * @return DateInterval + */ + public function diffAsDateInterval($date = null, $absolute = false): DateInterval + { + return parent::diff($this->resolveCarbon($date), (bool) $absolute); + } + + /** + * Get the difference as a CarbonInterval instance. + * Return absolute interval (always positive) unless you pass false to the second argument. + * + * @param \Carbon\CarbonInterface|\DateTimeInterface|string|null $date + * @param bool $absolute Get the absolute of the difference + * + * @return CarbonInterval + */ + public function diffAsCarbonInterval($date = null, $absolute = true) + { + $interval = static::fixDiffInterval($this->diffAsDateInterval($date, $absolute), $absolute); + $interval->setLocalTranslator($this->getLocalTranslator()); + + return $interval; + } + /** * Get the difference as a DateInterval instance. * Return relative interval (negative if $absolute flag is not set to true and the given date is before @@ -123,7 +154,7 @@ public function diff($date = null, $absolute = false): CarbonInterval $interval = static::fixDiffInterval(parent::diff($this->resolveCarbon($date), (bool) $absolute), $absolute); $interval->setLocalTranslator($this->getLocalTranslator()); - return $interval; + return $this->diffAsCarbonInterval($date, $absolute); } /** diff --git a/src/Carbon/Traits/Options.php b/src/Carbon/Traits/Options.php index 9ca824a551..1103c99047 100644 --- a/src/Carbon/Traits/Options.php +++ b/src/Carbon/Traits/Options.php @@ -417,7 +417,7 @@ public function __debugInfo() return $var; }); - foreach (['dumpProperties', 'constructedObjectId'] as $property) { + foreach (['dumpProperties', 'constructedObjectId', 'originalInput'] as $property) { if (isset($infos[$property])) { unset($infos[$property]); } diff --git a/tests/CarbonInterval/FromStringTest.php b/tests/CarbonInterval/FromStringTest.php index d536b3f192..22526dbcbb 100644 --- a/tests/CarbonInterval/FromStringTest.php +++ b/tests/CarbonInterval/FromStringTest.php @@ -18,7 +18,7 @@ public function testReturnsInterval($string, $expected) { $result = CarbonInterval::fromString($string); - $this->assertEquals($expected, $result, "'$string' does not return expected interval."); + $this->assertEquals($expected->optimize(), $result->optimize(), "'$string' does not return expected interval."); } public function provideValidStrings() diff --git a/tests/CarbonInterval/ParseFromLocaleTest.php b/tests/CarbonInterval/ParseFromLocaleTest.php index 946474b20b..c3e6b5fb45 100644 --- a/tests/CarbonInterval/ParseFromLocaleTest.php +++ b/tests/CarbonInterval/ParseFromLocaleTest.php @@ -19,7 +19,7 @@ public function testReturnsInterval($string, $locale, $expected) { $result = CarbonInterval::parseFromLocale($string, $locale); - $this->assertEquals($expected, $result, "'{$string}' does not return expected interval."); + $this->assertEquals($expected->optimize(), $result->optimize(), "'{$string}' does not return expected interval."); } public function provideValidStrings() diff --git a/tests/CarbonInterval/SpecTest.php b/tests/CarbonInterval/SpecTest.php index 7427ae2a63..695efef9e4 100644 --- a/tests/CarbonInterval/SpecTest.php +++ b/tests/CarbonInterval/SpecTest.php @@ -92,6 +92,6 @@ public function testMixedDateAndTimeInterval() public function testCreatingInstanceEquals() { $ci = new CarbonInterval(1, 2, 0, 3, 4, 5, 6); - $this->assertEquals($ci, CarbonInterval::instance(new DateInterval($ci->spec()))); + $this->assertEquals($ci->optimize(), CarbonInterval::instance(new DateInterval($ci->spec()))->optimize()); } } diff --git a/tests/CarbonPeriod/AliasTest.php b/tests/CarbonPeriod/AliasTest.php index 89e34c170c..af71ef4dbd 100644 --- a/tests/CarbonPeriod/AliasTest.php +++ b/tests/CarbonPeriod/AliasTest.php @@ -117,13 +117,13 @@ public function testSetDates() public function testManageInterval() { $period = CarbonPeriod::interval('PT6H'); - $this->assertEquals(CarbonInterval::create('PT6H'), $period->getDateInterval()); + $this->assertEquals(CarbonInterval::create('PT6H')->optimize(), $period->getDateInterval()->optimize()); } public function testInvertInterval() { $period = CarbonPeriod::invert(); - $this->assertEquals(CarbonInterval::create('P1D')->invert(), $period->getDateInterval()); + $this->assertEquals(CarbonInterval::create('P1D')->invert()->optimize(), $period->getDateInterval()->optimize()); } public function testModifyIntervalPlural() From 38eefb1fb8cf26d9cbe664d8d796b7fa5cf4c1dd Mon Sep 17 00:00:00 2001 From: KyleKatarn Date: Mon, 10 Aug 2020 08:05:05 +0200 Subject: [PATCH 15/38] Fix unit tests --- tests/Carbon/DiffTest.php | 7 +------ tests/CarbonImmutable/DiffTest.php | 7 +------ tests/CarbonPeriod/ToArrayTest.php | 6 ++++-- 3 files changed, 6 insertions(+), 14 deletions(-) diff --git a/tests/Carbon/DiffTest.php b/tests/Carbon/DiffTest.php index 9557daa436..1a29f88428 100644 --- a/tests/Carbon/DiffTest.php +++ b/tests/Carbon/DiffTest.php @@ -1703,12 +1703,7 @@ public function testNearlyFullDayDiffInSeconds() $d1 = Carbon::parse('2019-06-15 12:34:56.123456'); $d2 = Carbon::parse('2019-06-16 12:34:56.123455'); - // Due to https://bugs.php.net/bug.php?id=77007, this changed in 7.2.12: - $expected = version_compare(PHP_VERSION, '7.2.12-dev', '<') - ? 86400 // Bad rounding before PHP 7.2.12 - : 86399.999999; // Exact result since PHP 7.2.12 - - $this->assertSame($expected, $d2->diffInSeconds($d1)); + $this->assertSame(86399.999999, $d2->diffInSeconds($d1)); } public function testNearlyFullDayDiffInMicroseconds() diff --git a/tests/CarbonImmutable/DiffTest.php b/tests/CarbonImmutable/DiffTest.php index 5fb90f13f3..4425d85551 100644 --- a/tests/CarbonImmutable/DiffTest.php +++ b/tests/CarbonImmutable/DiffTest.php @@ -1681,12 +1681,7 @@ public function testNearlyFullDayDiffInSeconds() $d1 = Carbon::parse('2019-06-15 12:34:56.123456'); $d2 = Carbon::parse('2019-06-16 12:34:56.123455'); - // Due to https://bugs.php.net/bug.php?id=77007, this changed in 7.2.12: - $expected = version_compare(PHP_VERSION, '7.2.12-dev', '<') - ? 86400 // Bad rounding before PHP 7.2.12 - : 86399.999999; // Exact result since PHP 7.2.12 - - $this->assertSame($expected, $d2->diffInSeconds($d1)); + $this->assertSame(86399.999999, $d2->diffInSeconds($d1)); } public function testNearlyFullDayDiffInMicroseconds() diff --git a/tests/CarbonPeriod/ToArrayTest.php b/tests/CarbonPeriod/ToArrayTest.php index 76529f2156..7448ab9fa9 100644 --- a/tests/CarbonPeriod/ToArrayTest.php +++ b/tests/CarbonPeriod/ToArrayTest.php @@ -169,7 +169,7 @@ public function testDebugInfo() $expected = [ 'dateClass' => Carbon::class, - 'dateInterval' => CarbonInterval::hour(), + 'dateInterval' => CarbonInterval::hour()->optimize(), 'filters' => [ [ 'Carbon\CarbonPeriod::filterRecurrences', @@ -179,7 +179,9 @@ public function testDebugInfo() 'startDate' => Carbon::parse('2018-05-13 12:00 Asia/Kabul'), 'recurrences' => 3, ]; + $actual = $period->__debugInfo(); + $actual['dateInterval']->optimize(); - $this->assertEquals($expected, $period->__debugInfo()); + $this->assertEquals($expected, $actual); } } From 661ed88f4bd5c69e77da53629b20d4a124bf8de1 Mon Sep 17 00:00:00 2001 From: KyleKatarn Date: Mon, 10 Aug 2020 09:22:53 +0200 Subject: [PATCH 16/38] Fix typehints --- src/Carbon/CarbonInterface.php | 10 +++++----- src/Carbon/CarbonInterval.php | 4 ++-- src/Carbon/Traits/Difference.php | 10 +++++----- src/Carbon/Traits/Units.php | 9 +++++++++ 4 files changed, 21 insertions(+), 12 deletions(-) diff --git a/src/Carbon/CarbonInterface.php b/src/Carbon/CarbonInterface.php index bad7e4daa0..7494e6edac 100644 --- a/src/Carbon/CarbonInterface.php +++ b/src/Carbon/CarbonInterface.php @@ -1382,7 +1382,7 @@ public function diffInWeekendDays($date = null, $absolute = true): int; * @param \Carbon\CarbonInterface|\DateTimeInterface|string|null $date * @param bool $absolute Get the absolute of the difference * - * @return int + * @return float */ public function diffInWeeks($date = null, $absolute = true): float; @@ -3363,16 +3363,16 @@ public function roundWeek($weekStartsAt = null); /** * The number of seconds since midnight. * - * @return int + * @return float */ - public function secondsSinceMidnight(); + public function secondsSinceMidnight(): float; /** * The number of seconds until 23:59:59. * - * @return int + * @return float */ - public function secondsUntilEndOfDay(); + public function secondsUntilEndOfDay(): float; /** * Return a serialized string of the instance. diff --git a/src/Carbon/CarbonInterval.php b/src/Carbon/CarbonInterval.php index c69b3a5874..f69b007dd6 100644 --- a/src/Carbon/CarbonInterval.php +++ b/src/Carbon/CarbonInterval.php @@ -245,14 +245,14 @@ class CarbonInterval extends DateInterval implements CarbonConverterInterface /** * Start date if interval was created from a difference between 2 dates. * - * @var DateTimeInterface + * @var DateTimeInterface|null */ protected $startDate; /** * End date if interval was created from a difference between 2 dates. * - * @var DateTimeInterface + * @var DateTimeInterface|null */ protected $endDate; diff --git a/src/Carbon/Traits/Difference.php b/src/Carbon/Traits/Difference.php index 68a878391a..41b330757d 100644 --- a/src/Carbon/Traits/Difference.php +++ b/src/Carbon/Traits/Difference.php @@ -147,11 +147,11 @@ public function diffAsCarbonInterval($date = null, $absolute = true) * @param \Carbon\CarbonInterface|\DateTimeInterface|string|null $date * @param bool $absolute Get the absolute of the difference * - * @return DateInterval + * @return CarbonInterval */ public function diff($date = null, $absolute = false): CarbonInterval { - $interval = static::fixDiffInterval(parent::diff($this->resolveCarbon($date), (bool) $absolute), $absolute); + $interval = static::fixDiffInterval($this->diffAsDateInterval($date, $absolute), $absolute); $interval->setLocalTranslator($this->getLocalTranslator()); return $this->diffAsCarbonInterval($date, $absolute); @@ -251,7 +251,7 @@ public function diffInMonths($date = null, $absolute = true): float * @param \Carbon\CarbonInterface|\DateTimeInterface|string|null $date * @param bool $absolute Get the absolute of the difference * - * @return int + * @return float */ public function diffInWeeks($date = null, $absolute = true): float { @@ -435,7 +435,7 @@ public function diffInMilliseconds($date = null, $absolute = true): float * * @return float */ - public function secondsSinceMidnight() + public function secondsSinceMidnight(): float { return $this->diffInSeconds($this->copy()->startOfDay()); } @@ -445,7 +445,7 @@ public function secondsSinceMidnight() * * @return float */ - public function secondsUntilEndOfDay() + public function secondsUntilEndOfDay(): float { return $this->diffInSeconds($this->copy()->endOfDay()); } diff --git a/src/Carbon/Traits/Units.php b/src/Carbon/Traits/Units.php index 6f4a92487a..460a5f70ac 100644 --- a/src/Carbon/Traits/Units.php +++ b/src/Carbon/Traits/Units.php @@ -136,6 +136,15 @@ public function addRealUnit($unit, $value = 1) return $this->setTimestamp((int) ($this->getTimestamp() + $value)); } + /** + * Subtract seconds to the instance using timestamp. Positive $value travels + * into the past while negative $value travels forward. + * + * @param string $unit + * @param int $value + * + * @return static + */ public function subRealUnit($unit, $value = 1) { return $this->addRealUnit($unit, -$value); From dfc30aec4c8c1c0a922f4030033dceba15b8f3d6 Mon Sep 17 00:00:00 2001 From: KyleKatarn Date: Mon, 10 Aug 2020 09:35:41 +0200 Subject: [PATCH 17/38] Restore old diff methods as deprecated aliases --- src/Carbon/Traits/Date.php | 4 + tests/Carbon/DiffTest.php | 113 ++++++++++++++--------------- tests/CarbonImmutable/DiffTest.php | 110 ++++++++++++++-------------- 3 files changed, 114 insertions(+), 113 deletions(-) diff --git a/src/Carbon/Traits/Date.php b/src/Carbon/Traits/Date.php index ba0584eb68..0242592c9a 100644 --- a/src/Carbon/Traits/Date.php +++ b/src/Carbon/Traits/Date.php @@ -2444,6 +2444,10 @@ protected static function getGenericMacros() */ public function __call($method, $parameters) { + if (preg_match('/^(?:diff|floatDiff)In(?:Real)?(.+)$/', $method, $match)) { + return $this->{'diffIn'.$match[1]}(...$parameters); + } + $diffSizes = [ // @mode diffForHumans 'short' => true, diff --git a/tests/Carbon/DiffTest.php b/tests/Carbon/DiffTest.php index 1a29f88428..8caa89feaf 100644 --- a/tests/Carbon/DiffTest.php +++ b/tests/Carbon/DiffTest.php @@ -1558,87 +1558,84 @@ public function testFloatDiff() { date_default_timezone_set('UTC'); - $this->assertSame(8986.665965, Carbon::parse('2018-03-31 23:55:12.321456')->diffInSeconds(Carbon::parse('2018-04-01 02:24:58.987421'))); + $this->assertSame(8986.665965, Carbon::parse('2018-03-31 23:55:12.321456')->floatDiffInSeconds(Carbon::parse('2018-04-01 02:24:58.987421'))); - $this->assertSame(1.0006944444444443, Carbon::parse('2018-12-01 00:00')->diffInDays(Carbon::parse('2018-12-02 00:01'))); + $this->assertSame(1.0006944444444443, Carbon::parse('2018-12-01 00:00')->floatDiffInDays(Carbon::parse('2018-12-02 00:01'))); - $this->assertSame(0.9714742503779745, Carbon::parse('2018-03-13 20:55:12.321456')->diffInMonths(Carbon::parse('2018-04-12 14:24:58.987421'))); - $this->assertSame(1.1724137931034484, Carbon::parse('2000-01-15 00:00')->diffInMonths(Carbon::parse('2000-02-20 00:00'))); - $this->assertSame(1.1724137931034484, Carbon::parse('2000-01-15 00:00')->diffInMonths('2000-02-20 00:00')); - $this->assertSame(11.951612903225806, Carbon::parse('2018-12-16 00:00')->diffInMonths('2019-12-15 00:00')); - $this->assertSame(0.9714742503779745, Carbon::parse('2018-04-12 14:24:58.987421')->diffInMonths(Carbon::parse('2018-03-13 20:55:12.321456'))); - $this->assertSame(0.9714742503779745, Carbon::parse('2018-03-13 20:55:12.321456')->diffInMonths(Carbon::parse('2018-04-12 14:24:58.987421'), false)); - $this->assertSame(-0.9714742503779745, Carbon::parse('2018-04-12 14:24:58.987421')->diffInMonths(Carbon::parse('2018-03-13 20:55:12.321456'), false)); + $this->assertSame(0.9714742503779745, Carbon::parse('2018-03-13 20:55:12.321456')->floatDiffInMonths(Carbon::parse('2018-04-12 14:24:58.987421'))); + $this->assertSame(0.9714742503779745, Carbon::parse('2018-04-12 14:24:58.987421')->floatDiffInMonths(Carbon::parse('2018-03-13 20:55:12.321456'))); + $this->assertSame(0.9714742503779745, Carbon::parse('2018-03-13 20:55:12.321456')->floatDiffInMonths(Carbon::parse('2018-04-12 14:24:58.987421'), false)); + $this->assertSame(-0.9714742503779745, Carbon::parse('2018-04-12 14:24:58.987421')->floatDiffInMonths(Carbon::parse('2018-03-13 20:55:12.321456'), false)); - $this->assertSame(16.557633744585264, Carbon::parse('2018-02-13 20:55:12.321456')->diffInMonths(Carbon::parse('2019-06-30 14:24:58.987421'))); + $this->assertSame(16.557633744585264, Carbon::parse('2018-02-13 20:55:12.321456')->floatDiffInMonths(Carbon::parse('2019-06-30 14:24:58.987421'))); - $this->assertSame(15.971474250377973, Carbon::parse('2018-02-13 20:55:12.321456')->diffInMonths(Carbon::parse('2019-06-12 14:24:58.987421'))); - $this->assertSame(15.971474250377973, Carbon::parse('2019-06-12 14:24:58.987421')->diffInMonths(Carbon::parse('2018-02-13 20:55:12.321456'))); - $this->assertSame(15.971474250377973, Carbon::parse('2018-02-13 20:55:12.321456')->diffInMonths(Carbon::parse('2019-06-12 14:24:58.987421'), false)); - $this->assertSame(-15.971474250377973, Carbon::parse('2019-06-12 14:24:58.987421')->diffInMonths(Carbon::parse('2018-02-13 20:55:12.321456'), false)); + $this->assertSame(15.971474250377973, Carbon::parse('2018-02-13 20:55:12.321456')->floatDiffInMonths(Carbon::parse('2019-06-12 14:24:58.987421'))); + $this->assertSame(15.971474250377973, Carbon::parse('2019-06-12 14:24:58.987421')->floatDiffInMonths(Carbon::parse('2018-02-13 20:55:12.321456'))); + $this->assertSame(15.971474250377973, Carbon::parse('2018-02-13 20:55:12.321456')->floatDiffInMonths(Carbon::parse('2019-06-12 14:24:58.987421'), false)); + $this->assertSame(-15.971474250377973, Carbon::parse('2019-06-12 14:24:58.987421')->floatDiffInMonths(Carbon::parse('2018-02-13 20:55:12.321456'), false)); - $this->assertSame(1.0, Carbon::parse('2018-02-13 20:55:12.321456')->diffInYears(Carbon::parse('2019-02-13 20:55:12.321456'))); - $this->assertSame(1.3746000338015283, Carbon::parse('2018-02-13 20:55:12.321456')->diffInYears(Carbon::parse('2019-06-30 14:24:58.987421'))); - $this->assertSame(0.9609014036645421, Carbon::parse('2018-02-13 20:55:12.321456')->diffInYears(Carbon::parse('2019-01-30 14:24:58.987421'))); + $this->assertSame(1.0, Carbon::parse('2018-02-13 20:55:12.321456')->floatDiffInYears(Carbon::parse('2019-02-13 20:55:12.321456'))); + $this->assertSame(1.3746000338015283, Carbon::parse('2018-02-13 20:55:12.321456')->floatDiffInYears(Carbon::parse('2019-06-30 14:24:58.987421'))); + $this->assertSame(0.9609014036645421, Carbon::parse('2018-02-13 20:55:12.321456')->floatDiffInYears(Carbon::parse('2019-01-30 14:24:58.987421'))); - $this->assertSame(1.3252849653083778, Carbon::parse('2018-02-13 20:55:12.321456')->diffInYears(Carbon::parse('2019-06-12 14:24:58.987421'))); - $this->assertSame(1.3252849653083778, Carbon::parse('2019-06-12 14:24:58.987421')->diffInYears(Carbon::parse('2018-02-13 20:55:12.321456'))); - $this->assertSame(1.3252849653083778, Carbon::parse('2018-02-13 20:55:12.321456')->diffInYears(Carbon::parse('2019-06-12 14:24:58.987421'), false)); - $this->assertSame(-1.3252849653083778, Carbon::parse('2019-06-12 14:24:58.987421')->diffInYears(Carbon::parse('2018-02-13 20:55:12.321456'), false)); + $this->assertSame(1.3252849653083778, Carbon::parse('2018-02-13 20:55:12.321456')->floatDiffInYears(Carbon::parse('2019-06-12 14:24:58.987421'))); + $this->assertSame(1.3252849653083778, Carbon::parse('2019-06-12 14:24:58.987421')->floatDiffInYears(Carbon::parse('2018-02-13 20:55:12.321456'))); + $this->assertSame(1.3252849653083778, Carbon::parse('2018-02-13 20:55:12.321456')->floatDiffInYears(Carbon::parse('2019-06-12 14:24:58.987421'), false)); + $this->assertSame(-1.3252849653083778, Carbon::parse('2019-06-12 14:24:58.987421')->floatDiffInYears(Carbon::parse('2018-02-13 20:55:12.321456'), false)); - $this->assertSame(5.325284965308378, Carbon::parse('2018-02-13 20:55:12.321456')->diffInYears(Carbon::parse('2023-06-12 14:24:58.987421'))); - $this->assertSame(5.325284965308378, Carbon::parse('2023-06-12 14:24:58.987421')->diffInYears(Carbon::parse('2018-02-13 20:55:12.321456'))); - $this->assertSame(5.325284965308378, Carbon::parse('2018-02-13 20:55:12.321456')->diffInYears(Carbon::parse('2023-06-12 14:24:58.987421'), false)); - $this->assertSame(-5.325284965308378, Carbon::parse('2023-06-12 14:24:58.987421')->diffInYears(Carbon::parse('2018-02-13 20:55:12.321456'), false)); + $this->assertSame(5.325284965308378, Carbon::parse('2018-02-13 20:55:12.321456')->floatDiffInYears(Carbon::parse('2023-06-12 14:24:58.987421'))); + $this->assertSame(5.325284965308378, Carbon::parse('2023-06-12 14:24:58.987421')->floatDiffInYears(Carbon::parse('2018-02-13 20:55:12.321456'))); + $this->assertSame(5.325284965308378, Carbon::parse('2018-02-13 20:55:12.321456')->floatDiffInYears(Carbon::parse('2023-06-12 14:24:58.987421'), false)); + $this->assertSame(-5.325284965308378, Carbon::parse('2023-06-12 14:24:58.987421')->floatDiffInYears(Carbon::parse('2018-02-13 20:55:12.321456'), false)); - $this->assertSame(1.0, Carbon::parse('2018-10-01 00:00:00', 'Europe/Paris')->diffInMonths(Carbon::parse('2018-11-01 00:00:00', 'Europe/Paris'))); - $this->assertSame(1.0, Carbon::parse('2018-10-28 00:00:00')->diffInMonths(Carbon::parse('2018-11-28 00:00:00'))); - $this->assertSame(1.0, Carbon::parse('2018-10-28 00:00:00', 'Europe/Paris')->diffInMonths(Carbon::parse('2018-11-28 00:00:00', 'Europe/Paris'))); + $this->assertSame(1.0, Carbon::parse('2018-10-01 00:00:00', 'Europe/Paris')->floatDiffInMonths(Carbon::parse('2018-11-01 00:00:00', 'Europe/Paris'))); + $this->assertSame(1.0, Carbon::parse('2018-10-28 00:00:00')->floatDiffInMonths(Carbon::parse('2018-11-28 00:00:00'))); + $this->assertSame(1.0, Carbon::parse('2018-10-28 00:00:00', 'Europe/Paris')->floatDiffInMonths(Carbon::parse('2018-11-28 00:00:00', 'Europe/Paris'))); - $this->assertSame(-1.0, Carbon::parse('2018-11-01 00:00:00', 'Europe/Paris')->diffInMonths(Carbon::parse('2018-10-01 00:00:00', 'Europe/Paris'), false)); - $this->assertSame(-1.0, Carbon::parse('2018-11-28 00:00:00')->diffInMonths(Carbon::parse('2018-10-28 00:00:00'), false)); - $this->assertSame(-1.0, Carbon::parse('2018-11-28 00:00:00', 'Europe/Paris')->diffInMonths(Carbon::parse('2018-10-28 00:00:00', 'Europe/Paris'), false)); + $this->assertSame(-1.0, Carbon::parse('2018-11-01 00:00:00', 'Europe/Paris')->floatDiffInMonths(Carbon::parse('2018-10-01 00:00:00', 'Europe/Paris'), false)); + $this->assertSame(-1.0, Carbon::parse('2018-11-28 00:00:00')->floatDiffInMonths(Carbon::parse('2018-10-28 00:00:00'), false)); + $this->assertSame(-1.0, Carbon::parse('2018-11-28 00:00:00', 'Europe/Paris')->floatDiffInMonths(Carbon::parse('2018-10-28 00:00:00', 'Europe/Paris'), false)); } public function testFloatDiffWithRealUnits() { date_default_timezone_set('UTC'); - $this->assertSame(1.0006944444444443, Carbon::parse('2018-12-01 00:00')->diffInDays(Carbon::parse('2018-12-02 00:01'))); + $this->assertSame(1.0006944444444443, Carbon::parse('2018-12-01 00:00')->floatDiffInRealDays(Carbon::parse('2018-12-02 00:01'))); - $this->assertSame(0.9714742503779745, Carbon::parse('2018-03-13 20:55:12.321456')->diffInMonths(Carbon::parse('2018-04-12 14:24:58.987421'))); - $this->assertSame(0.9714742503779745, Carbon::parse('2018-04-12 14:24:58.987421')->diffInMonths(Carbon::parse('2018-03-13 20:55:12.321456'))); - $this->assertSame(0.9714742503779745, Carbon::parse('2018-03-13 20:55:12.321456')->diffInMonths(Carbon::parse('2018-04-12 14:24:58.987421'), false)); - $this->assertSame(-0.9714742503779745, Carbon::parse('2018-04-12 14:24:58.987421')->diffInMonths(Carbon::parse('2018-03-13 20:55:12.321456'), false)); + $this->assertSame(0.9714742503779745, Carbon::parse('2018-03-13 20:55:12.321456')->floatDiffInRealMonths(Carbon::parse('2018-04-12 14:24:58.987421'))); + $this->assertSame(0.9714742503779745, Carbon::parse('2018-04-12 14:24:58.987421')->floatDiffInRealMonths(Carbon::parse('2018-03-13 20:55:12.321456'))); + $this->assertSame(0.9714742503779745, Carbon::parse('2018-03-13 20:55:12.321456')->floatDiffInRealMonths(Carbon::parse('2018-04-12 14:24:58.987421'), false)); + $this->assertSame(-0.9714742503779745, Carbon::parse('2018-04-12 14:24:58.987421')->floatDiffInRealMonths(Carbon::parse('2018-03-13 20:55:12.321456'), false)); - $this->assertSame(16.557633744585264, Carbon::parse('2018-02-13 20:55:12.321456')->diffInMonths(Carbon::parse('2019-06-30 14:24:58.987421'))); + $this->assertSame(16.557633744585264, Carbon::parse('2018-02-13 20:55:12.321456')->floatDiffInRealMonths(Carbon::parse('2019-06-30 14:24:58.987421'))); - $this->assertSame(15.971474250377973, Carbon::parse('2018-02-13 20:55:12.321456')->diffInMonths(Carbon::parse('2019-06-12 14:24:58.987421'))); - $this->assertSame(15.971474250377973, Carbon::parse('2019-06-12 14:24:58.987421')->diffInMonths(Carbon::parse('2018-02-13 20:55:12.321456'))); - $this->assertSame(15.971474250377973, Carbon::parse('2018-02-13 20:55:12.321456')->diffInMonths(Carbon::parse('2019-06-12 14:24:58.987421'), false)); - $this->assertSame(-15.971474250377973, Carbon::parse('2019-06-12 14:24:58.987421')->diffInMonths(Carbon::parse('2018-02-13 20:55:12.321456'), false)); + $this->assertSame(15.971474250377973, Carbon::parse('2018-02-13 20:55:12.321456')->floatDiffInRealMonths(Carbon::parse('2019-06-12 14:24:58.987421'))); + $this->assertSame(15.971474250377973, Carbon::parse('2019-06-12 14:24:58.987421')->floatDiffInRealMonths(Carbon::parse('2018-02-13 20:55:12.321456'))); + $this->assertSame(15.971474250377973, Carbon::parse('2018-02-13 20:55:12.321456')->floatDiffInRealMonths(Carbon::parse('2019-06-12 14:24:58.987421'), false)); + $this->assertSame(-15.971474250377973, Carbon::parse('2019-06-12 14:24:58.987421')->floatDiffInRealMonths(Carbon::parse('2018-02-13 20:55:12.321456'), false)); - $this->assertSame(1.0, Carbon::parse('2018-02-13 20:55:12.321456')->diffInYears(Carbon::parse('2019-02-13 20:55:12.321456'))); - $this->assertSame(1.3746000338015283, Carbon::parse('2018-02-13 20:55:12.321456')->diffInYears(Carbon::parse('2019-06-30 14:24:58.987421'))); - $this->assertSame(0.9609014036645421, Carbon::parse('2018-02-13 20:55:12.321456')->diffInYears(Carbon::parse('2019-01-30 14:24:58.987421'))); + $this->assertSame(1.0, Carbon::parse('2018-02-13 20:55:12.321456')->floatDiffInRealYears(Carbon::parse('2019-02-13 20:55:12.321456'))); + $this->assertSame(1.3746000338015283, Carbon::parse('2018-02-13 20:55:12.321456')->floatDiffInRealYears(Carbon::parse('2019-06-30 14:24:58.987421'))); + $this->assertSame(0.9609014036645421, Carbon::parse('2018-02-13 20:55:12.321456')->floatDiffInRealYears(Carbon::parse('2019-01-30 14:24:58.987421'))); - $this->assertSame(1.3252849653083778, Carbon::parse('2018-02-13 20:55:12.321456')->diffInYears(Carbon::parse('2019-06-12 14:24:58.987421'))); - $this->assertSame(1.3252849653083778, Carbon::parse('2019-06-12 14:24:58.987421')->diffInYears(Carbon::parse('2018-02-13 20:55:12.321456'))); - $this->assertSame(1.3252849653083778, Carbon::parse('2018-02-13 20:55:12.321456')->diffInYears(Carbon::parse('2019-06-12 14:24:58.987421'), false)); - $this->assertSame(-1.3252849653083778, Carbon::parse('2019-06-12 14:24:58.987421')->diffInYears(Carbon::parse('2018-02-13 20:55:12.321456'), false)); + $this->assertSame(1.3252849653083778, Carbon::parse('2018-02-13 20:55:12.321456')->floatDiffInRealYears(Carbon::parse('2019-06-12 14:24:58.987421'))); + $this->assertSame(1.3252849653083778, Carbon::parse('2019-06-12 14:24:58.987421')->floatDiffInRealYears(Carbon::parse('2018-02-13 20:55:12.321456'))); + $this->assertSame(1.3252849653083778, Carbon::parse('2018-02-13 20:55:12.321456')->floatDiffInRealYears(Carbon::parse('2019-06-12 14:24:58.987421'), false)); + $this->assertSame(-1.3252849653083778, Carbon::parse('2019-06-12 14:24:58.987421')->floatDiffInRealYears(Carbon::parse('2018-02-13 20:55:12.321456'), false)); - $this->assertSame(5.325284965308378, Carbon::parse('2018-02-13 20:55:12.321456')->diffInYears(Carbon::parse('2023-06-12 14:24:58.987421'))); - $this->assertSame(5.325284965308378, Carbon::parse('2023-06-12 14:24:58.987421')->diffInYears(Carbon::parse('2018-02-13 20:55:12.321456'))); - $this->assertSame(5.325284965308378, Carbon::parse('2018-02-13 20:55:12.321456')->diffInYears(Carbon::parse('2023-06-12 14:24:58.987421'), false)); - $this->assertSame(-5.325284965308378, Carbon::parse('2023-06-12 14:24:58.987421')->diffInYears(Carbon::parse('2018-02-13 20:55:12.321456'), false)); + $this->assertSame(5.325284965308378, Carbon::parse('2018-02-13 20:55:12.321456')->floatDiffInRealYears(Carbon::parse('2023-06-12 14:24:58.987421'))); + $this->assertSame(5.325284965308378, Carbon::parse('2023-06-12 14:24:58.987421')->floatDiffInRealYears(Carbon::parse('2018-02-13 20:55:12.321456'))); + $this->assertSame(5.325284965308378, Carbon::parse('2018-02-13 20:55:12.321456')->floatDiffInRealYears(Carbon::parse('2023-06-12 14:24:58.987421'), false)); + $this->assertSame(-5.325284965308378, Carbon::parse('2023-06-12 14:24:58.987421')->floatDiffInRealYears(Carbon::parse('2018-02-13 20:55:12.321456'), false)); - $this->assertSame(1.0, Carbon::parse('2018-10-01 00:00:00', 'Europe/Paris')->diffInMonths(Carbon::parse('2018-11-01 00:00:00', 'Europe/Paris'))); - $this->assertSame(1.0, Carbon::parse('2018-10-28 00:00:00')->diffInMonths(Carbon::parse('2018-11-28 00:00:00'))); - $this->assertSame(1.0, Carbon::parse('2018-10-28 00:00:00', 'Europe/Paris')->diffInMonths(Carbon::parse('2018-11-28 00:00:00', 'Europe/Paris'))); + $this->assertSame(1.0, Carbon::parse('2018-10-01 00:00:00', 'Europe/Paris')->floatDiffInRealMonths(Carbon::parse('2018-11-01 00:00:00', 'Europe/Paris'))); + $this->assertSame(1.0, Carbon::parse('2018-10-28 00:00:00')->floatDiffInRealMonths(Carbon::parse('2018-11-28 00:00:00'))); + $this->assertSame(1.0, Carbon::parse('2018-10-28 00:00:00', 'Europe/Paris')->floatDiffInRealMonths(Carbon::parse('2018-11-28 00:00:00', 'Europe/Paris'))); - $this->assertSame(-1.0, Carbon::parse('2018-11-01 00:00:00', 'Europe/Paris')->diffInMonths(Carbon::parse('2018-10-01 00:00:00', 'Europe/Paris'), false)); - $this->assertSame(-1.0, Carbon::parse('2018-11-28 00:00:00')->diffInMonths(Carbon::parse('2018-10-28 00:00:00'), false)); - $this->assertSame(-1.0, Carbon::parse('2018-11-28 00:00:00', 'Europe/Paris')->diffInMonths(Carbon::parse('2018-10-28 00:00:00', 'Europe/Paris'), false)); + $this->assertSame(-1.0, Carbon::parse('2018-11-01 00:00:00', 'Europe/Paris')->floatDiffInRealMonths(Carbon::parse('2018-10-01 00:00:00', 'Europe/Paris'), false)); + $this->assertSame(-1.0, Carbon::parse('2018-11-28 00:00:00')->floatDiffInRealMonths(Carbon::parse('2018-10-28 00:00:00'), false)); + $this->assertSame(-1.0, Carbon::parse('2018-11-28 00:00:00', 'Europe/Paris')->floatDiffInRealMonths(Carbon::parse('2018-10-28 00:00:00', 'Europe/Paris'), false)); } /** diff --git a/tests/CarbonImmutable/DiffTest.php b/tests/CarbonImmutable/DiffTest.php index 4425d85551..b2ac05db07 100644 --- a/tests/CarbonImmutable/DiffTest.php +++ b/tests/CarbonImmutable/DiffTest.php @@ -1539,84 +1539,84 @@ public function testFloatDiff() { date_default_timezone_set('UTC'); - $this->assertSame(8986.665965, Carbon::parse('2018-03-31 23:55:12.321456')->diffInSeconds(Carbon::parse('2018-04-01 02:24:58.987421'))); + $this->assertSame(8986.665965, Carbon::parse('2018-03-31 23:55:12.321456')->floatDiffInSeconds(Carbon::parse('2018-04-01 02:24:58.987421'))); - $this->assertSame(1.0006944444444443, Carbon::parse('2018-12-01 00:00')->diffInDays(Carbon::parse('2018-12-02 00:01'))); + $this->assertSame(1.0006944444444443, Carbon::parse('2018-12-01 00:00')->floatDiffInDays(Carbon::parse('2018-12-02 00:01'))); - $this->assertSame(0.9714742503779745, Carbon::parse('2018-03-13 20:55:12.321456')->diffInMonths(Carbon::parse('2018-04-12 14:24:58.987421'))); - $this->assertSame(0.9714742503779745, Carbon::parse('2018-04-12 14:24:58.987421')->diffInMonths(Carbon::parse('2018-03-13 20:55:12.321456'))); - $this->assertSame(0.9714742503779745, Carbon::parse('2018-03-13 20:55:12.321456')->diffInMonths(Carbon::parse('2018-04-12 14:24:58.987421'), false)); - $this->assertSame(-0.9714742503779745, Carbon::parse('2018-04-12 14:24:58.987421')->diffInMonths(Carbon::parse('2018-03-13 20:55:12.321456'), false)); + $this->assertSame(0.9714742503779745, Carbon::parse('2018-03-13 20:55:12.321456')->floatDiffInMonths(Carbon::parse('2018-04-12 14:24:58.987421'))); + $this->assertSame(0.9714742503779745, Carbon::parse('2018-04-12 14:24:58.987421')->floatDiffInMonths(Carbon::parse('2018-03-13 20:55:12.321456'))); + $this->assertSame(0.9714742503779745, Carbon::parse('2018-03-13 20:55:12.321456')->floatDiffInMonths(Carbon::parse('2018-04-12 14:24:58.987421'), false)); + $this->assertSame(-0.9714742503779745, Carbon::parse('2018-04-12 14:24:58.987421')->floatDiffInMonths(Carbon::parse('2018-03-13 20:55:12.321456'), false)); - $this->assertSame(16.557633744585264, Carbon::parse('2018-02-13 20:55:12.321456')->diffInMonths(Carbon::parse('2019-06-30 14:24:58.987421'))); + $this->assertSame(16.557633744585264, Carbon::parse('2018-02-13 20:55:12.321456')->floatDiffInMonths(Carbon::parse('2019-06-30 14:24:58.987421'))); - $this->assertSame(15.971474250377973, Carbon::parse('2018-02-13 20:55:12.321456')->diffInMonths(Carbon::parse('2019-06-12 14:24:58.987421'))); - $this->assertSame(15.971474250377973, Carbon::parse('2019-06-12 14:24:58.987421')->diffInMonths(Carbon::parse('2018-02-13 20:55:12.321456'))); - $this->assertSame(15.971474250377973, Carbon::parse('2018-02-13 20:55:12.321456')->diffInMonths(Carbon::parse('2019-06-12 14:24:58.987421'), false)); - $this->assertSame(-15.971474250377973, Carbon::parse('2019-06-12 14:24:58.987421')->diffInMonths(Carbon::parse('2018-02-13 20:55:12.321456'), false)); + $this->assertSame(15.971474250377973, Carbon::parse('2018-02-13 20:55:12.321456')->floatDiffInMonths(Carbon::parse('2019-06-12 14:24:58.987421'))); + $this->assertSame(15.971474250377973, Carbon::parse('2019-06-12 14:24:58.987421')->floatDiffInMonths(Carbon::parse('2018-02-13 20:55:12.321456'))); + $this->assertSame(15.971474250377973, Carbon::parse('2018-02-13 20:55:12.321456')->floatDiffInMonths(Carbon::parse('2019-06-12 14:24:58.987421'), false)); + $this->assertSame(-15.971474250377973, Carbon::parse('2019-06-12 14:24:58.987421')->floatDiffInMonths(Carbon::parse('2018-02-13 20:55:12.321456'), false)); - $this->assertSame(1.0, Carbon::parse('2018-02-13 20:55:12.321456')->diffInYears(Carbon::parse('2019-02-13 20:55:12.321456'))); - $this->assertSame(1.3746000338015283, Carbon::parse('2018-02-13 20:55:12.321456')->diffInYears(Carbon::parse('2019-06-30 14:24:58.987421'))); - $this->assertSame(0.9609014036645421, Carbon::parse('2018-02-13 20:55:12.321456')->diffInYears(Carbon::parse('2019-01-30 14:24:58.987421'))); + $this->assertSame(1.0, Carbon::parse('2018-02-13 20:55:12.321456')->floatDiffInYears(Carbon::parse('2019-02-13 20:55:12.321456'))); + $this->assertSame(1.3746000338015283, Carbon::parse('2018-02-13 20:55:12.321456')->floatDiffInYears(Carbon::parse('2019-06-30 14:24:58.987421'))); + $this->assertSame(0.9609014036645421, Carbon::parse('2018-02-13 20:55:12.321456')->floatDiffInYears(Carbon::parse('2019-01-30 14:24:58.987421'))); - $this->assertSame(1.3252849653083778, Carbon::parse('2018-02-13 20:55:12.321456')->diffInYears(Carbon::parse('2019-06-12 14:24:58.987421'))); - $this->assertSame(1.3252849653083778, Carbon::parse('2019-06-12 14:24:58.987421')->diffInYears(Carbon::parse('2018-02-13 20:55:12.321456'))); - $this->assertSame(1.3252849653083778, Carbon::parse('2018-02-13 20:55:12.321456')->diffInYears(Carbon::parse('2019-06-12 14:24:58.987421'), false)); - $this->assertSame(-1.3252849653083778, Carbon::parse('2019-06-12 14:24:58.987421')->diffInYears(Carbon::parse('2018-02-13 20:55:12.321456'), false)); + $this->assertSame(1.3252849653083778, Carbon::parse('2018-02-13 20:55:12.321456')->floatDiffInYears(Carbon::parse('2019-06-12 14:24:58.987421'))); + $this->assertSame(1.3252849653083778, Carbon::parse('2019-06-12 14:24:58.987421')->floatDiffInYears(Carbon::parse('2018-02-13 20:55:12.321456'))); + $this->assertSame(1.3252849653083778, Carbon::parse('2018-02-13 20:55:12.321456')->floatDiffInYears(Carbon::parse('2019-06-12 14:24:58.987421'), false)); + $this->assertSame(-1.3252849653083778, Carbon::parse('2019-06-12 14:24:58.987421')->floatDiffInYears(Carbon::parse('2018-02-13 20:55:12.321456'), false)); - $this->assertSame(5.325284965308378, Carbon::parse('2018-02-13 20:55:12.321456')->diffInYears(Carbon::parse('2023-06-12 14:24:58.987421'))); - $this->assertSame(5.325284965308378, Carbon::parse('2023-06-12 14:24:58.987421')->diffInYears(Carbon::parse('2018-02-13 20:55:12.321456'))); - $this->assertSame(5.325284965308378, Carbon::parse('2018-02-13 20:55:12.321456')->diffInYears(Carbon::parse('2023-06-12 14:24:58.987421'), false)); - $this->assertSame(-5.325284965308378, Carbon::parse('2023-06-12 14:24:58.987421')->diffInYears(Carbon::parse('2018-02-13 20:55:12.321456'), false)); + $this->assertSame(5.325284965308378, Carbon::parse('2018-02-13 20:55:12.321456')->floatDiffInYears(Carbon::parse('2023-06-12 14:24:58.987421'))); + $this->assertSame(5.325284965308378, Carbon::parse('2023-06-12 14:24:58.987421')->floatDiffInYears(Carbon::parse('2018-02-13 20:55:12.321456'))); + $this->assertSame(5.325284965308378, Carbon::parse('2018-02-13 20:55:12.321456')->floatDiffInYears(Carbon::parse('2023-06-12 14:24:58.987421'), false)); + $this->assertSame(-5.325284965308378, Carbon::parse('2023-06-12 14:24:58.987421')->floatDiffInYears(Carbon::parse('2018-02-13 20:55:12.321456'), false)); - $this->assertSame(1.0, Carbon::parse('2018-10-01 00:00:00', 'Europe/Paris')->diffInMonths(Carbon::parse('2018-11-01 00:00:00', 'Europe/Paris'))); - $this->assertSame(1.0, Carbon::parse('2018-10-28 00:00:00')->diffInMonths(Carbon::parse('2018-11-28 00:00:00'))); - $this->assertSame(1.0, Carbon::parse('2018-10-28 00:00:00', 'Europe/Paris')->diffInMonths(Carbon::parse('2018-11-28 00:00:00', 'Europe/Paris'))); + $this->assertSame(1.0, Carbon::parse('2018-10-01 00:00:00', 'Europe/Paris')->floatDiffInMonths(Carbon::parse('2018-11-01 00:00:00', 'Europe/Paris'))); + $this->assertSame(1.0, Carbon::parse('2018-10-28 00:00:00')->floatDiffInMonths(Carbon::parse('2018-11-28 00:00:00'))); + $this->assertSame(1.0, Carbon::parse('2018-10-28 00:00:00', 'Europe/Paris')->floatDiffInMonths(Carbon::parse('2018-11-28 00:00:00', 'Europe/Paris'))); - $this->assertSame(-1.0, Carbon::parse('2018-11-01 00:00:00', 'Europe/Paris')->diffInMonths(Carbon::parse('2018-10-01 00:00:00', 'Europe/Paris'), false)); - $this->assertSame(-1.0, Carbon::parse('2018-11-28 00:00:00')->diffInMonths(Carbon::parse('2018-10-28 00:00:00'), false)); - $this->assertSame(-1.0, Carbon::parse('2018-11-28 00:00:00', 'Europe/Paris')->diffInMonths(Carbon::parse('2018-10-28 00:00:00', 'Europe/Paris'), false)); + $this->assertSame(-1.0, Carbon::parse('2018-11-01 00:00:00', 'Europe/Paris')->floatDiffInMonths(Carbon::parse('2018-10-01 00:00:00', 'Europe/Paris'), false)); + $this->assertSame(-1.0, Carbon::parse('2018-11-28 00:00:00')->floatDiffInMonths(Carbon::parse('2018-10-28 00:00:00'), false)); + $this->assertSame(-1.0, Carbon::parse('2018-11-28 00:00:00', 'Europe/Paris')->floatDiffInMonths(Carbon::parse('2018-10-28 00:00:00', 'Europe/Paris'), false)); } public function testFloatDiffWithRealUnits() { date_default_timezone_set('UTC'); - $this->assertSame(1.0006944444444443, Carbon::parse('2018-12-01 00:00')->diffInDays(Carbon::parse('2018-12-02 00:01'))); + $this->assertSame(1.0006944444444443, Carbon::parse('2018-12-01 00:00')->floatDiffInRealDays(Carbon::parse('2018-12-02 00:01'))); - $this->assertSame(0.9714742503779745, Carbon::parse('2018-03-13 20:55:12.321456')->diffInMonths(Carbon::parse('2018-04-12 14:24:58.987421'))); - $this->assertSame(0.9714742503779745, Carbon::parse('2018-04-12 14:24:58.987421')->diffInMonths(Carbon::parse('2018-03-13 20:55:12.321456'))); - $this->assertSame(0.9714742503779745, Carbon::parse('2018-03-13 20:55:12.321456')->diffInMonths(Carbon::parse('2018-04-12 14:24:58.987421'), false)); - $this->assertSame(-0.9714742503779745, Carbon::parse('2018-04-12 14:24:58.987421')->diffInMonths(Carbon::parse('2018-03-13 20:55:12.321456'), false)); + $this->assertSame(0.9714742503779745, Carbon::parse('2018-03-13 20:55:12.321456')->floatDiffInRealMonths(Carbon::parse('2018-04-12 14:24:58.987421'))); + $this->assertSame(0.9714742503779745, Carbon::parse('2018-04-12 14:24:58.987421')->floatDiffInRealMonths(Carbon::parse('2018-03-13 20:55:12.321456'))); + $this->assertSame(0.9714742503779745, Carbon::parse('2018-03-13 20:55:12.321456')->floatDiffInRealMonths(Carbon::parse('2018-04-12 14:24:58.987421'), false)); + $this->assertSame(-0.9714742503779745, Carbon::parse('2018-04-12 14:24:58.987421')->floatDiffInRealMonths(Carbon::parse('2018-03-13 20:55:12.321456'), false)); - $this->assertSame(16.557633744585264, Carbon::parse('2018-02-13 20:55:12.321456')->diffInMonths(Carbon::parse('2019-06-30 14:24:58.987421'))); + $this->assertSame(16.557633744585264, Carbon::parse('2018-02-13 20:55:12.321456')->floatDiffInRealMonths(Carbon::parse('2019-06-30 14:24:58.987421'))); - $this->assertSame(15.971474250377973, Carbon::parse('2018-02-13 20:55:12.321456')->diffInMonths(Carbon::parse('2019-06-12 14:24:58.987421'))); - $this->assertSame(15.971474250377973, Carbon::parse('2019-06-12 14:24:58.987421')->diffInMonths(Carbon::parse('2018-02-13 20:55:12.321456'))); - $this->assertSame(15.971474250377973, Carbon::parse('2018-02-13 20:55:12.321456')->diffInMonths(Carbon::parse('2019-06-12 14:24:58.987421'), false)); - $this->assertSame(-15.971474250377973, Carbon::parse('2019-06-12 14:24:58.987421')->diffInMonths(Carbon::parse('2018-02-13 20:55:12.321456'), false)); + $this->assertSame(15.971474250377973, Carbon::parse('2018-02-13 20:55:12.321456')->floatDiffInRealMonths(Carbon::parse('2019-06-12 14:24:58.987421'))); + $this->assertSame(15.971474250377973, Carbon::parse('2019-06-12 14:24:58.987421')->floatDiffInRealMonths(Carbon::parse('2018-02-13 20:55:12.321456'))); + $this->assertSame(15.971474250377973, Carbon::parse('2018-02-13 20:55:12.321456')->floatDiffInRealMonths(Carbon::parse('2019-06-12 14:24:58.987421'), false)); + $this->assertSame(-15.971474250377973, Carbon::parse('2019-06-12 14:24:58.987421')->floatDiffInRealMonths(Carbon::parse('2018-02-13 20:55:12.321456'), false)); - $this->assertSame(1.0, Carbon::parse('2018-02-13 20:55:12.321456')->diffInYears(Carbon::parse('2019-02-13 20:55:12.321456'))); - $this->assertSame(1.3746000338015283, Carbon::parse('2018-02-13 20:55:12.321456')->diffInYears(Carbon::parse('2019-06-30 14:24:58.987421'))); - $this->assertSame(0.9609014036645421, Carbon::parse('2018-02-13 20:55:12.321456')->diffInYears(Carbon::parse('2019-01-30 14:24:58.987421'))); + $this->assertSame(1.0, Carbon::parse('2018-02-13 20:55:12.321456')->floatDiffInRealYears(Carbon::parse('2019-02-13 20:55:12.321456'))); + $this->assertSame(1.3746000338015283, Carbon::parse('2018-02-13 20:55:12.321456')->floatDiffInRealYears(Carbon::parse('2019-06-30 14:24:58.987421'))); + $this->assertSame(0.9609014036645421, Carbon::parse('2018-02-13 20:55:12.321456')->floatDiffInRealYears(Carbon::parse('2019-01-30 14:24:58.987421'))); - $this->assertSame(1.3252849653083778, Carbon::parse('2018-02-13 20:55:12.321456')->diffInYears(Carbon::parse('2019-06-12 14:24:58.987421'))); - $this->assertSame(1.3252849653083778, Carbon::parse('2019-06-12 14:24:58.987421')->diffInYears(Carbon::parse('2018-02-13 20:55:12.321456'))); - $this->assertSame(1.3252849653083778, Carbon::parse('2018-02-13 20:55:12.321456')->diffInYears(Carbon::parse('2019-06-12 14:24:58.987421'), false)); - $this->assertSame(-1.3252849653083778, Carbon::parse('2019-06-12 14:24:58.987421')->diffInYears(Carbon::parse('2018-02-13 20:55:12.321456'), false)); + $this->assertSame(1.3252849653083778, Carbon::parse('2018-02-13 20:55:12.321456')->floatDiffInRealYears(Carbon::parse('2019-06-12 14:24:58.987421'))); + $this->assertSame(1.3252849653083778, Carbon::parse('2019-06-12 14:24:58.987421')->floatDiffInRealYears(Carbon::parse('2018-02-13 20:55:12.321456'))); + $this->assertSame(1.3252849653083778, Carbon::parse('2018-02-13 20:55:12.321456')->floatDiffInRealYears(Carbon::parse('2019-06-12 14:24:58.987421'), false)); + $this->assertSame(-1.3252849653083778, Carbon::parse('2019-06-12 14:24:58.987421')->floatDiffInRealYears(Carbon::parse('2018-02-13 20:55:12.321456'), false)); - $this->assertSame(5.325284965308378, Carbon::parse('2018-02-13 20:55:12.321456')->diffInYears(Carbon::parse('2023-06-12 14:24:58.987421'))); - $this->assertSame(5.325284965308378, Carbon::parse('2023-06-12 14:24:58.987421')->diffInYears(Carbon::parse('2018-02-13 20:55:12.321456'))); - $this->assertSame(5.325284965308378, Carbon::parse('2018-02-13 20:55:12.321456')->diffInYears(Carbon::parse('2023-06-12 14:24:58.987421'), false)); - $this->assertSame(-5.325284965308378, Carbon::parse('2023-06-12 14:24:58.987421')->diffInYears(Carbon::parse('2018-02-13 20:55:12.321456'), false)); + $this->assertSame(5.325284965308378, Carbon::parse('2018-02-13 20:55:12.321456')->floatDiffInRealYears(Carbon::parse('2023-06-12 14:24:58.987421'))); + $this->assertSame(5.325284965308378, Carbon::parse('2023-06-12 14:24:58.987421')->floatDiffInRealYears(Carbon::parse('2018-02-13 20:55:12.321456'))); + $this->assertSame(5.325284965308378, Carbon::parse('2018-02-13 20:55:12.321456')->floatDiffInRealYears(Carbon::parse('2023-06-12 14:24:58.987421'), false)); + $this->assertSame(-5.325284965308378, Carbon::parse('2023-06-12 14:24:58.987421')->floatDiffInRealYears(Carbon::parse('2018-02-13 20:55:12.321456'), false)); - $this->assertSame(1.0, Carbon::parse('2018-10-01 00:00:00', 'Europe/Paris')->diffInMonths(Carbon::parse('2018-11-01 00:00:00', 'Europe/Paris'))); - $this->assertSame(1.0, Carbon::parse('2018-10-28 00:00:00')->diffInMonths(Carbon::parse('2018-11-28 00:00:00'))); - $this->assertSame(1.0, Carbon::parse('2018-10-28 00:00:00', 'Europe/Paris')->diffInMonths(Carbon::parse('2018-11-28 00:00:00', 'Europe/Paris'))); + $this->assertSame(1.0, Carbon::parse('2018-10-01 00:00:00', 'Europe/Paris')->floatDiffInRealMonths(Carbon::parse('2018-11-01 00:00:00', 'Europe/Paris'))); + $this->assertSame(1.0, Carbon::parse('2018-10-28 00:00:00')->floatDiffInRealMonths(Carbon::parse('2018-11-28 00:00:00'))); + $this->assertSame(1.0, Carbon::parse('2018-10-28 00:00:00', 'Europe/Paris')->floatDiffInRealMonths(Carbon::parse('2018-11-28 00:00:00', 'Europe/Paris'))); - $this->assertSame(-1.0, Carbon::parse('2018-11-01 00:00:00', 'Europe/Paris')->diffInMonths(Carbon::parse('2018-10-01 00:00:00', 'Europe/Paris'), false)); - $this->assertSame(-1.0, Carbon::parse('2018-11-28 00:00:00')->diffInMonths(Carbon::parse('2018-10-28 00:00:00'), false)); - $this->assertSame(-1.0, Carbon::parse('2018-11-28 00:00:00', 'Europe/Paris')->diffInMonths(Carbon::parse('2018-10-28 00:00:00', 'Europe/Paris'), false)); + $this->assertSame(-1.0, Carbon::parse('2018-11-01 00:00:00', 'Europe/Paris')->floatDiffInRealMonths(Carbon::parse('2018-10-01 00:00:00', 'Europe/Paris'), false)); + $this->assertSame(-1.0, Carbon::parse('2018-11-28 00:00:00')->floatDiffInRealMonths(Carbon::parse('2018-10-28 00:00:00'), false)); + $this->assertSame(-1.0, Carbon::parse('2018-11-28 00:00:00', 'Europe/Paris')->floatDiffInRealMonths(Carbon::parse('2018-10-28 00:00:00', 'Europe/Paris'), false)); } /** From 6487352ca889196c71042568ff39a6635184fd57 Mon Sep 17 00:00:00 2001 From: KyleKatarn Date: Mon, 10 Aug 2020 10:24:03 +0200 Subject: [PATCH 18/38] Call the diff* method only if available --- src/Carbon/Traits/Date.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/Carbon/Traits/Date.php b/src/Carbon/Traits/Date.php index 0242592c9a..bc50395a24 100644 --- a/src/Carbon/Traits/Date.php +++ b/src/Carbon/Traits/Date.php @@ -2445,7 +2445,11 @@ protected static function getGenericMacros() public function __call($method, $parameters) { if (preg_match('/^(?:diff|floatDiff)In(?:Real)?(.+)$/', $method, $match)) { - return $this->{'diffIn'.$match[1]}(...$parameters); + $method = 'diffIn'.$match[1]; + + if (method_exists($this, $method)) { + return $this->$method(...$parameters); + } } $diffSizes = [ From 82047798933b42595032df0f623d78deaaf3a955 Mon Sep 17 00:00:00 2001 From: KyleKatarn Date: Mon, 10 Aug 2020 10:47:26 +0200 Subject: [PATCH 19/38] Add PHPStan exceptions for deprecated aliases --- phpstan.neon | 2 ++ 1 file changed, 2 insertions(+) diff --git a/phpstan.neon b/phpstan.neon index 78c9fa56a4..957a6e68aa 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -8,6 +8,8 @@ parameters: ignoreErrors: - '#^Call to an undefined static method#' - '#^Call to an undefined method .*::(foo|foobar|this)\(\)\.$#' + - '#^Call to an undefined method Carbon\\Carbon(Immutable)?::floatDiffIn([A-Za-z]+)\(\)\.$#' + - '#^Call to an undefined method Carbon\\Carbon(Immutable)?::diffInReal([A-Za-z]+)\(\)\.$#' - '#^Unsafe usage of new static\(\)\.#' - '#^Method Carbon\\Carbon(Interface|Immutable)?::(add|sub)[A-Z][A-Za-z]+\(\) invoked with 1 parameter, 0 required\.#' - '#^Variable \$this in isset\(\) (always exists and is not nullable|is never defined)\.$#' From ebef777ee1416b529f55831e0e4b5e6c97d27ac3 Mon Sep 17 00:00:00 2001 From: KyleKatarn Date: Mon, 10 Aug 2020 11:33:54 +0200 Subject: [PATCH 20/38] Keep occurrences of diffInReal* in unit tests --- tests/Carbon/ModifyTest.php | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/tests/Carbon/ModifyTest.php b/tests/Carbon/ModifyTest.php index 5ec7912a27..33614de649 100644 --- a/tests/Carbon/ModifyTest.php +++ b/tests/Carbon/ModifyTest.php @@ -31,13 +31,13 @@ public function testTimezoneModify() $a = new Carbon('2014-03-30 00:00:00', 'Europe/London'); $b = $a->copy(); $b->addHours(24); - $this->assertSame(23.0, $a->diffInHours($b, false)); - $this->assertSame(23.0, $b->diffInHours($a)); - $this->assertSame(-23.0, $b->diffInHours($a, false)); - $this->assertSame(-23.0 * 60, $b->diffInMinutes($a, false)); - $this->assertSame(-23.0 * 60 * 60, $b->diffInSeconds($a, false)); - $this->assertSame(-23.0 * 60 * 60 * 1000, $b->diffInMilliseconds($a, false)); - $this->assertSame(-23.0 * 60 * 60 * 1000000, $b->diffInMicroseconds($a, false)); + $this->assertSame(23.0, $a->diffInRealHours($b, false)); + $this->assertSame(23.0, $b->diffInRealHours($a)); + $this->assertSame(-23.0, $b->diffInRealHours($a, false)); + $this->assertSame(-23.0 * 60, $b->diffInRealMinutes($a, false)); + $this->assertSame(-23.0 * 60 * 60, $b->diffInRealSeconds($a, false)); + $this->assertSame(-23.0 * 60 * 60 * 1000, $b->diffInRealMilliseconds($a, false)); + $this->assertSame(-23.0 * 60 * 60 * 1000000, $b->diffInRealMicroseconds($a, false)); $a = new Carbon('2014-03-30 00:00:00', 'Europe/London'); $b = $a->copy(); From e4d63b8eb2af67119c1a4a3ee4899441a2f28693 Mon Sep 17 00:00:00 2001 From: KyleKatarn Date: Mon, 10 Aug 2020 12:28:40 +0200 Subject: [PATCH 21/38] Fix coverage annotation --- src/Carbon/Traits/Date.php | 3 ++- src/Carbon/Traits/Difference.php | 4 ++-- tests/Carbon/SettersTest.php | 9 ++++++--- 3 files changed, 10 insertions(+), 6 deletions(-) diff --git a/src/Carbon/Traits/Date.php b/src/Carbon/Traits/Date.php index bc50395a24..44b3e921ef 100644 --- a/src/Carbon/Traits/Date.php +++ b/src/Carbon/Traits/Date.php @@ -1329,8 +1329,9 @@ public function setUnitNoOverflow($valueUnit, $value, $overflowUnit) $original = $this->copy(); /** @var static $date */ $date = $this->$valueUnit($value); - $end = $original->copy()->endOf($overflowUnit); $start = $original->copy()->startOf($overflowUnit); + $end = $original->copy()->endOf($overflowUnit); + if ($date < $start) { $date = $date->setDateTimeFrom($start); } elseif ($date > $end) { diff --git a/src/Carbon/Traits/Difference.php b/src/Carbon/Traits/Difference.php index 41b330757d..d4f57c4e89 100644 --- a/src/Carbon/Traits/Difference.php +++ b/src/Carbon/Traits/Difference.php @@ -99,11 +99,11 @@ protected static function fixDiffInterval(DateInterval $diff, $absolute) } elseif ($diff->f < 0) { static::fixNegativeMicroseconds($diff); } - // @codeCoverageIgnoreEnd if ($absolute && $diff->invert) { $diff->invert(); } + // @codeCoverageIgnoreEnd return $diff; } @@ -268,7 +268,7 @@ public function diffInWeeks($date = null, $absolute = true): float */ public function diffInDays($date = null, $absolute = true): float { - $daysDiff = (int) parent::diff($this->resolveCarbon($date), $absolute)->format('%r%a'); + $daysDiff = (int) $this->diffAsDateInterval($date, $absolute)->format('%r%a'); $hoursDiff = $this->diffInHours($date, $absolute); return $daysDiff + fmod($hoursDiff, static::HOURS_PER_DAY) / static::HOURS_PER_DAY; diff --git a/tests/Carbon/SettersTest.php b/tests/Carbon/SettersTest.php index b38bb0e217..535c683aea 100644 --- a/tests/Carbon/SettersTest.php +++ b/tests/Carbon/SettersTest.php @@ -548,7 +548,7 @@ public function testSetUnitNoOverflow() mt_rand(-9999, 9999) : mt_rand(-60, 60); - $date = Carbon::create($year, $month, $day, $hour, $minute, $second + $microsecond / 1000000); + $date = Carbon::create($year, $month, $day, $hour, $minute, $second + $microsecond / 1000000, 'UTC'); $original = $date->copy(); $date->setUnitNoOverflow($valueUnit, $value, $overflowUnit); $start = $original->copy()->startOf($overflowUnit); @@ -562,13 +562,16 @@ public function testSetUnitNoOverflow() $unit = ucfirst(Carbon::pluralUnit($valueUnit)); $modulo = $value % $units[$valueUnit]; + if ($modulo < 0) { $modulo += $units[$valueUnit]; } + if ($date->$valueUnit === $value || $date->$valueUnit === $modulo || - (method_exists($date, "diffInReal$unit") && $$valueUnit - $date->{"diffInReal$unit"}($original, false) === $value) || - $$valueUnit - ((int) $date->{"diffIn$unit"}($original, false)) === $value + $$valueUnit - ((int) $date->{"diffIn$unit"}($original, false)) === $value || + ($valueUnit === 'day' && + $date->format('Y-m-d H:i:s.u') === $original->copy()->modify(($original->day + $value).' days')) ) { $results['current']++; From 48ae10b500cc1fe7ffed41ccebd050d46e660337 Mon Sep 17 00:00:00 2001 From: KyleKatarn Date: Mon, 10 Aug 2020 17:45:16 +0200 Subject: [PATCH 22/38] Keep total for diff interval --- phpdoc.php | 7 +- src/Carbon/CarbonInterface.php | 84 ++++++++++++--- src/Carbon/CarbonInterval.php | 146 +++++++++++++++++++++++++-- src/Carbon/CarbonPeriod.php | 9 +- src/Carbon/Traits/Date.php | 1 + src/Carbon/Traits/Difference.php | 145 ++++++++------------------ src/Carbon/Traits/Rounding.php | 4 +- src/Carbon/Traits/Week.php | 4 +- tests/CarbonInterval/GettersTest.php | 14 +++ tests/CarbonInterval/TotalTest.php | 5 + 10 files changed, 287 insertions(+), 132 deletions(-) diff --git a/phpdoc.php b/phpdoc.php index d749ac931e..290b0cde26 100644 --- a/phpdoc.php +++ b/phpdoc.php @@ -521,8 +521,9 @@ function compileDoc($autoDocLines, $file) $methods = ''; $carbonMethods = get_class_methods(\Carbon\Carbon::class); sort($carbonMethods); + foreach ($carbonMethods as $method) { - if (method_exists(\Carbon\CarbonImmutable::class, $method) && !method_exists(DateTimeInterface::class, $method)) { + if ($method === 'diff' || method_exists(\Carbon\CarbonImmutable::class, $method) && !method_exists(DateTimeInterface::class, $method)) { $function = new ReflectionMethod(\Carbon\Carbon::class, $method); $static = $function->isStatic() ? ' static' : ''; $parameters = implode(', ', array_map(function (ReflectionParameter $parameter) use ($method) { @@ -565,7 +566,9 @@ function compileDoc($autoDocLines, $file) } } - $return = $function->getReturnType() ? ': '.$function->getReturnType()->getName() : ''; + $return = $function->getReturnType() + ? ': '.preg_replace('/^Carbon\\\\/', '', $function->getReturnType()->getName()) + : ''; if (!empty($methodDocBlock)) { $methodDocBlock = "\n $methodDocBlock"; diff --git a/src/Carbon/CarbonInterface.php b/src/Carbon/CarbonInterface.php index 7494e6edac..d219bcbeef 100644 --- a/src/Carbon/CarbonInterface.php +++ b/src/Carbon/CarbonInterface.php @@ -1199,6 +1199,42 @@ public static function createSafe($year = null, $month = null, $day = null, $hou */ public function dayOfYear($value = null); + /** + * @alias diffAsCarbonInterval + * + * Get the difference as a DateInterval instance. + * Return relative interval (negative if $absolute flag is not set to true and the given date is before + * current one). + * + * @param \Carbon\CarbonInterface|\DateTimeInterface|string|null $date + * @param bool $absolute Get the absolute of the difference + * + * @return CarbonInterval + */ + public function diff($date = null, $absolute = false): CarbonInterval; + + /** + * Get the difference as a CarbonInterval instance. + * Return absolute interval (always positive) unless you pass false to the second argument. + * + * @param \Carbon\CarbonInterface|\DateTimeInterface|string|null $date + * @param bool $absolute Get the absolute of the difference + * + * @return CarbonInterval + */ + public function diffAsCarbonInterval($date = null, bool $absolute = true): CarbonInterval; + + /** + * Get the difference as a DateInterval instance. + * Return relative interval (negative if + * + * @param \Carbon\CarbonInterface|\DateTimeInterface|string|null $date + * @param bool $absolute Get the absolute of the difference + * + * @return DateInterval + */ + public function diffAsDateInterval($date = null, bool $absolute = false): DateInterval; + /** * Get the difference by the given interval using a filter closure. * @@ -1209,7 +1245,7 @@ public function dayOfYear($value = null); * * @return int */ - public function diffFiltered(CarbonInterval $ci, Closure $callback, $date = null, $absolute = true): int; + public function diffFiltered(CarbonInterval $ci, Closure $callback, $date = null, bool $absolute = true): int; /** * Get the difference in a human readable format in the current locale from current instance to an other @@ -1262,7 +1298,7 @@ public function diffForHumans($other = null, $syntax = null, $short = false, $pa * * @return float */ - public function diffInDays($date = null, $absolute = true): float; + public function diffInDays($date = null, bool $absolute = true): float; /** * Get the difference in days using a filter closure rounded down. @@ -1273,7 +1309,7 @@ public function diffInDays($date = null, $absolute = true): float; * * @return int */ - public function diffInDaysFiltered(Closure $callback, $date = null, $absolute = true): int; + public function diffInDaysFiltered(Closure $callback, $date = null, bool $absolute = true): int; /** * Get the difference in hours rounded down. @@ -1283,7 +1319,7 @@ public function diffInDaysFiltered(Closure $callback, $date = null, $absolute = * * @return float */ - public function diffInHours($date = null, $absolute = true): float; + public function diffInHours($date = null, bool $absolute = true): float; /** * Get the difference in hours using a filter closure rounded down. @@ -1294,7 +1330,7 @@ public function diffInHours($date = null, $absolute = true): float; * * @return int */ - public function diffInHoursFiltered(Closure $callback, $date = null, $absolute = true): int; + public function diffInHoursFiltered(Closure $callback, $date = null, bool $absolute = true): int; /** * Get the difference in microseconds. @@ -1304,7 +1340,7 @@ public function diffInHoursFiltered(Closure $callback, $date = null, $absolute = * * @return float */ - public function diffInMicroseconds($date = null, $absolute = true): float; + public function diffInMicroseconds($date = null, bool $absolute = true): float; /** * Get the difference in milliseconds rounded down. @@ -1314,7 +1350,7 @@ public function diffInMicroseconds($date = null, $absolute = true): float; * * @return float */ - public function diffInMilliseconds($date = null, $absolute = true): float; + public function diffInMilliseconds($date = null, bool $absolute = true): float; /** * Get the difference in minutes rounded down. @@ -1324,7 +1360,7 @@ public function diffInMilliseconds($date = null, $absolute = true): float; * * @return float */ - public function diffInMinutes($date = null, $absolute = true): float; + public function diffInMinutes($date = null, bool $absolute = true): float; /** * Get the difference in months rounded down. @@ -1334,7 +1370,7 @@ public function diffInMinutes($date = null, $absolute = true): float; * * @return float */ - public function diffInMonths($date = null, $absolute = true): float; + public function diffInMonths($date = null, bool $absolute = true): float; /** * Get the difference in quarters rounded down. @@ -1344,7 +1380,7 @@ public function diffInMonths($date = null, $absolute = true): float; * * @return float */ - public function diffInQuarters($date = null, $absolute = true): float; + public function diffInQuarters($date = null, bool $absolute = true): float; /** * Get the difference in seconds rounded down. @@ -1354,7 +1390,16 @@ public function diffInQuarters($date = null, $absolute = true): float; * * @return float */ - public function diffInSeconds($date = null, $absolute = true): float; + public function diffInSeconds($date = null, bool $absolute = true): float; + + /** + * @param string $unit + * @param null $date + * @param false $absolute + * + * @return float + */ + public function diffInUnit(string $unit, $date = null, bool $absolute = false): float; /** * Get the difference in weekdays rounded down. @@ -1364,7 +1409,7 @@ public function diffInSeconds($date = null, $absolute = true): float; * * @return int */ - public function diffInWeekdays($date = null, $absolute = true): int; + public function diffInWeekdays($date = null, bool $absolute = true): int; /** * Get the difference in weekend days using a filter rounded down. @@ -1374,7 +1419,7 @@ public function diffInWeekdays($date = null, $absolute = true): int; * * @return int */ - public function diffInWeekendDays($date = null, $absolute = true): int; + public function diffInWeekendDays($date = null, bool $absolute = true): int; /** * Get the difference in weeks rounded down. @@ -1384,7 +1429,7 @@ public function diffInWeekendDays($date = null, $absolute = true): int; * * @return float */ - public function diffInWeeks($date = null, $absolute = true): float; + public function diffInWeeks($date = null, bool $absolute = true): float; /** * Get the difference in years @@ -1394,7 +1439,7 @@ public function diffInWeeks($date = null, $absolute = true): float; * * @return float */ - public function diffInYears($date = null, $absolute = true); + public function diffInYears($date = null, bool $absolute = true): float; /** * @deprecated To avoid conflict between different third-party libraries, static setters should not be used. @@ -3931,6 +3976,15 @@ public function startOfYear(); */ public function sub($unit, $value = 1, $overflow = null); + /** + * Subtract seconds to the instance using timestamp. Positive $value travels + * into the past while negative $value travels forward. + * + * @param string $unit + * @param int $value + * + * @return static + */ public function subRealUnit($unit, $value = 1); /** diff --git a/src/Carbon/CarbonInterval.php b/src/Carbon/CarbonInterval.php index f69b007dd6..e99c5003f3 100644 --- a/src/Carbon/CarbonInterval.php +++ b/src/Carbon/CarbonInterval.php @@ -245,14 +245,14 @@ class CarbonInterval extends DateInterval implements CarbonConverterInterface /** * Start date if interval was created from a difference between 2 dates. * - * @var DateTimeInterface|null + * @var CarbonInterface|null */ protected $startDate; /** * End date if interval was created from a difference between 2 dates. * - * @var DateTimeInterface|null + * @var CarbonInterface|null */ protected $endDate; @@ -580,9 +580,9 @@ public function original() /** * Return the start date if interval was created from a difference between 2 dates. * - * @return DateTimeInterface + * @return CarbonInterface|null */ - public function start(): DateTimeInterface + public function start(): ?CarbonInterface { return $this->startDate; } @@ -590,9 +590,9 @@ public function start(): DateTimeInterface /** * Return the end date if interval was created from a difference between 2 dates. * - * @return DateTimeInterface + * @return CarbonInterface|null */ - public function end(): DateTimeInterface + public function end(): ?CarbonInterface { return $this->endDate; } @@ -864,6 +864,118 @@ public static function parseFromLocale($interval, $locale = null) return static::fromString(Carbon::translateTimeString($interval, $locale ?: static::getLocale(), 'en')); } + /** + * Create an interval from the difference between 2 dates. + * + * @param \Carbon\Carbon|\DateTimeInterface|mixed $start + * @param \Carbon\Carbon|\DateTimeInterface|mixed $end + * + * @return static + */ + public static function diff($start, $end = null, bool $absolute = false) + { + $start = $start instanceof CarbonInterface ? $start : Carbon::make($start); + $end = $end instanceof CarbonInterface ? $end : Carbon::make($end); + $interval = static::instance($start->diffAsDateInterval($end, $absolute)); + $interval->fixDiffInterval(); + $interval->startDate = $start; + $interval->endDate = $end; + + return $interval; + } + + /** + * Invert the interval if it's inverted. + * + * @param bool $absolute do nothing if set to false + * + * @return $this + */ + public function abs(bool $absolute = false) + { + if ($absolute && $this->invert) { + $this->invert(); + } + + return $this; + } + + /** + * @alias abs + * + * Invert the interval if it's inverted. + * + * @param bool $absolute do nothing if set to false + * + * @return $this + */ + public function absolute(bool $absolute = false) + { + return $this->abs($absolute); + } + + /** + * @codeCoverageIgnore + */ + private function fixNegativeMicroseconds() + { + if ($this->s !== 0 || $this->i !== 0 || $this->h !== 0 || $this->d !== 0 || $this->m !== 0 || $this->y !== 0) { + $this->f = (round($this->f * 1000000) + 1000000) / 1000000; + $this->s--; + + if ($this->s < 0) { + $this->s += 60; + $this->i--; + + if ($this->i < 0) { + $this->i += 60; + $this->h--; + + if ($this->h < 0) { + $this->h += 24; + $this->d--; + + if ($this->d < 0) { + $this->d += 30; + $this->m--; + + if ($this->m < 0) { + $this->m += 12; + $this->y--; + } + } + } + } + } + + return; + } + + $this->f *= -1; + $this->invert(); + } + + /** + * Work-around for https://bugs.php.net/bug.php?id=77145 + * + * @codeCoverageIgnore + */ + private function fixDiffInterval() + { + if ($this->f > 0 && $this->y === -1 && $this->m === 11 && $this->d >= 27 && $this->h === 23 && $this->i === 59 && $this->s === 59) { + $this->y = 0; + $this->m = 0; + $this->d = 0; + $this->h = 0; + $this->i = 0; + $this->s = 0; + $this->f = (1000000 - round($this->f * 1000000)) / 1000000; + $this->invert(); + } elseif ($this->f < 0) { + $this->fixNegativeMicroseconds(); + } + } + private static function castIntervalToClass(DateInterval $interval, string $className) { $mainClass = DateInterval::class; @@ -1791,6 +1903,22 @@ public function toPeriod(...$params) return CarbonPeriod::create($this, ...$params); } + /** + * Decompose the current interval into + * + * @param mixed|int|DateInterval|string|Closure|null $interval interval or number of the given $unit + * @param string|null $unit if specified, $interval must be an integer + * + * @return CarbonPeriod + */ + public function stepBy($interval, $unit = null) + { + $start = $this->startDate ?: Carbon::make($this->startDate); + $end = $this->endDate ?: $start->copy()->add($this); + + return CarbonPeriod::create(static::make($interval, $unit), $start, $end); + } + /** * Invert the interval. * @@ -2181,7 +2309,7 @@ public function hasPositiveValues(): bool * * @return float */ - public function total($unit) + public function total($unit): float { $realUnit = $unit = strtolower($unit); @@ -2191,6 +2319,10 @@ public function total($unit) throw new UnknownUnitException($unit); } + if ($this->startDate && $this->endDate) { + return $this->startDate->diffInUnit($unit, $this->endDate); + } + $result = 0; $cumulativeFactor = 0; $unitFound = false; diff --git a/src/Carbon/CarbonPeriod.php b/src/Carbon/CarbonPeriod.php index be3aee0e78..4ef3b4e4a0 100644 --- a/src/Carbon/CarbonPeriod.php +++ b/src/Carbon/CarbonPeriod.php @@ -726,15 +726,16 @@ public function getDateClass(): string /** * Change the period date interval. * - * @param DateInterval|string $interval + * @param DateInterval|string|int $interval + * @param string $unit the unit of $interval if it's a number * * @throws InvalidIntervalException * * @return $this */ - public function setDateInterval($interval) + public function setDateInterval($interval, $unit = null) { - if (!$interval = CarbonInterval::make($interval)) { + if (!$interval = CarbonInterval::make($interval, $unit)) { throw new InvalidIntervalException('Invalid interval.'); } @@ -1784,7 +1785,7 @@ public function __call($method, $parameters) case 'every': case 'step': case 'stepBy': - return $this->setDateInterval($first); + return $this->setDateInterval($first, $second); case 'invert': return $this->invertDateInterval(); diff --git a/src/Carbon/Traits/Date.php b/src/Carbon/Traits/Date.php index 44b3e921ef..7634722430 100644 --- a/src/Carbon/Traits/Date.php +++ b/src/Carbon/Traits/Date.php @@ -702,6 +702,7 @@ public function nowWithSameTz() protected static function expectDateTime($date, $other = []) { $message = 'Expected '; + foreach ((array) $other as $expect) { $message .= "$expect, "; } diff --git a/src/Carbon/Traits/Difference.php b/src/Carbon/Traits/Difference.php index d4f57c4e89..50749cc737 100644 --- a/src/Carbon/Traits/Difference.php +++ b/src/Carbon/Traits/Difference.php @@ -15,6 +15,7 @@ use Carbon\CarbonInterface; use Carbon\CarbonInterval; use Carbon\CarbonPeriod; +use Carbon\Exceptions\UnknownUnitException; use Carbon\Translator; use Closure; use DateInterval; @@ -32,82 +33,6 @@ */ trait Difference { - /** - * @codeCoverageIgnore - * - * @param CarbonInterval $diff - */ - protected static function fixNegativeMicroseconds(CarbonInterval $diff) - { - if ($diff->s !== 0 || $diff->i !== 0 || $diff->h !== 0 || $diff->d !== 0 || $diff->m !== 0 || $diff->y !== 0) { - $diff->f = (round($diff->f * 1000000) + 1000000) / 1000000; - $diff->s--; - - if ($diff->s < 0) { - $diff->s += 60; - $diff->i--; - - if ($diff->i < 0) { - $diff->i += 60; - $diff->h--; - - if ($diff->h < 0) { - $diff->h += 24; - $diff->d--; - - if ($diff->d < 0) { - $diff->d += 30; - $diff->m--; - - if ($diff->m < 0) { - $diff->m += 12; - $diff->y--; - } - } - } - } - } - - return; - } - - $diff->f *= -1; - $diff->invert(); - } - - /** - * @param DateInterval $diff - * @param bool $absolute - * - * @return CarbonInterval - */ - protected static function fixDiffInterval(DateInterval $diff, $absolute) - { - $diff = CarbonInterval::instance($diff); - - // Work-around for https://bugs.php.net/bug.php?id=77145 - // @codeCoverageIgnoreStart - if ($diff->f > 0 && $diff->y === -1 && $diff->m === 11 && $diff->d >= 27 && $diff->h === 23 && $diff->i === 59 && $diff->s === 59) { - $diff->y = 0; - $diff->m = 0; - $diff->d = 0; - $diff->h = 0; - $diff->i = 0; - $diff->s = 0; - $diff->f = (1000000 - round($diff->f * 1000000)) / 1000000; - $diff->invert(); - } elseif ($diff->f < 0) { - static::fixNegativeMicroseconds($diff); - } - - if ($absolute && $diff->invert) { - $diff->invert(); - } - // @codeCoverageIgnoreEnd - - return $diff; - } - /** * Get the difference as a DateInterval instance. * Return relative interval (negative if @@ -117,9 +42,9 @@ protected static function fixDiffInterval(DateInterval $diff, $absolute) * * @return DateInterval */ - public function diffAsDateInterval($date = null, $absolute = false): DateInterval + public function diffAsDateInterval($date = null, bool $absolute = false): DateInterval { - return parent::diff($this->resolveCarbon($date), (bool) $absolute); + return parent::diff($this->resolveCarbon($date), $absolute); } /** @@ -131,15 +56,15 @@ public function diffAsDateInterval($date = null, $absolute = false): DateInterva * * @return CarbonInterval */ - public function diffAsCarbonInterval($date = null, $absolute = true) + public function diffAsCarbonInterval($date = null, bool $absolute = true): CarbonInterval { - $interval = static::fixDiffInterval($this->diffAsDateInterval($date, $absolute), $absolute); - $interval->setLocalTranslator($this->getLocalTranslator()); - - return $interval; + return CarbonInterval::diff($this, $date, $absolute) + ->setLocalTranslator($this->getLocalTranslator()); } /** + * @alias diffAsCarbonInterval + * * Get the difference as a DateInterval instance. * Return relative interval (negative if $absolute flag is not set to true and the given date is before * current one). @@ -151,12 +76,30 @@ public function diffAsCarbonInterval($date = null, $absolute = true) */ public function diff($date = null, $absolute = false): CarbonInterval { - $interval = static::fixDiffInterval($this->diffAsDateInterval($date, $absolute), $absolute); - $interval->setLocalTranslator($this->getLocalTranslator()); - return $this->diffAsCarbonInterval($date, $absolute); } + /** + * @param string $unit microsecond, millisecond, second, minute, + * hour, day, week, months, quarter, years, + * century, millennium + * @param \Carbon\CarbonInterface|\DateTimeInterface|string|null $date + * @param bool $absolute Get the absolute of the difference + * + * @return float + */ + public function diffInUnit(string $unit, $date = null, bool $absolute = false): float + { + $unit = static::pluralUnit(rtrim($unit, 'z')); + $method = 'diffIn'.$unit; + + if (!method_exists($this, $method)) { + throw new UnknownUnitException($unit); + } + + return $this->$method($date, $absolute); + } + /** * Get the difference in years * @@ -165,7 +108,7 @@ public function diff($date = null, $absolute = false): CarbonInterval * * @return float */ - public function diffInYears($date = null, $absolute = true): float + public function diffInYears($date = null, bool $absolute = true): float { $start = $this; $end = $this->resolveCarbon($date); @@ -200,7 +143,7 @@ public function diffInYears($date = null, $absolute = true): float * * @return float */ - public function diffInQuarters($date = null, $absolute = true): float + public function diffInQuarters($date = null, bool $absolute = true): float { return $this->diffInMonths($date, $absolute) / static::MONTHS_PER_QUARTER; } @@ -213,7 +156,7 @@ public function diffInQuarters($date = null, $absolute = true): float * * @return float */ - public function diffInMonths($date = null, $absolute = true): float + public function diffInMonths($date = null, bool $absolute = true): float { $start = $this; $end = $this->resolveCarbon($date); @@ -253,7 +196,7 @@ public function diffInMonths($date = null, $absolute = true): float * * @return float */ - public function diffInWeeks($date = null, $absolute = true): float + public function diffInWeeks($date = null, bool $absolute = true): float { return $this->diffInDays($date, $absolute) / static::DAYS_PER_WEEK; } @@ -266,7 +209,7 @@ public function diffInWeeks($date = null, $absolute = true): float * * @return float */ - public function diffInDays($date = null, $absolute = true): float + public function diffInDays($date = null, bool $absolute = true): float { $daysDiff = (int) $this->diffAsDateInterval($date, $absolute)->format('%r%a'); $hoursDiff = $this->diffInHours($date, $absolute); @@ -283,7 +226,7 @@ public function diffInDays($date = null, $absolute = true): float * * @return int */ - public function diffInDaysFiltered(Closure $callback, $date = null, $absolute = true): int + public function diffInDaysFiltered(Closure $callback, $date = null, bool $absolute = true): int { return $this->diffFiltered(CarbonInterval::day(), $callback, $date, $absolute); } @@ -297,7 +240,7 @@ public function diffInDaysFiltered(Closure $callback, $date = null, $absolute = * * @return int */ - public function diffInHoursFiltered(Closure $callback, $date = null, $absolute = true): int + public function diffInHoursFiltered(Closure $callback, $date = null, bool $absolute = true): int { return $this->diffFiltered(CarbonInterval::hour(), $callback, $date, $absolute); } @@ -312,7 +255,7 @@ public function diffInHoursFiltered(Closure $callback, $date = null, $absolute = * * @return int */ - public function diffFiltered(CarbonInterval $ci, Closure $callback, $date = null, $absolute = true): int + public function diffFiltered(CarbonInterval $ci, Closure $callback, $date = null, bool $absolute = true): int { $start = $this; $end = $this->resolveCarbon($date); @@ -338,7 +281,7 @@ public function diffFiltered(CarbonInterval $ci, Closure $callback, $date = null * * @return int */ - public function diffInWeekdays($date = null, $absolute = true): int + public function diffInWeekdays($date = null, bool $absolute = true): int { return $this->diffInDaysFiltered(function (CarbonInterface $date) { return $date->isWeekday(); @@ -353,7 +296,7 @@ public function diffInWeekdays($date = null, $absolute = true): int * * @return int */ - public function diffInWeekendDays($date = null, $absolute = true): int + public function diffInWeekendDays($date = null, bool $absolute = true): int { return $this->diffInDaysFiltered(function (CarbonInterface $date) { return $date->isWeekend(); @@ -368,7 +311,7 @@ public function diffInWeekendDays($date = null, $absolute = true): int * * @return float */ - public function diffInHours($date = null, $absolute = true): float + public function diffInHours($date = null, bool $absolute = true): float { return $this->diffInMinutes($date, $absolute) / static::MINUTES_PER_HOUR; } @@ -381,7 +324,7 @@ public function diffInHours($date = null, $absolute = true): float * * @return float */ - public function diffInMinutes($date = null, $absolute = true): float + public function diffInMinutes($date = null, bool $absolute = true): float { return $this->diffInSeconds($date, $absolute) / static::SECONDS_PER_MINUTE; } @@ -394,7 +337,7 @@ public function diffInMinutes($date = null, $absolute = true): float * * @return float */ - public function diffInSeconds($date = null, $absolute = true): float + public function diffInSeconds($date = null, bool $absolute = true): float { return $this->diffInMilliseconds($date, $absolute) / static::MILLISECONDS_PER_SECOND; } @@ -407,7 +350,7 @@ public function diffInSeconds($date = null, $absolute = true): float * * @return float */ - public function diffInMicroseconds($date = null, $absolute = true): float + public function diffInMicroseconds($date = null, bool $absolute = true): float { /** @var CarbonInterface $date */ $date = $this->resolveCarbon($date); @@ -425,7 +368,7 @@ public function diffInMicroseconds($date = null, $absolute = true): float * * @return float */ - public function diffInMilliseconds($date = null, $absolute = true): float + public function diffInMilliseconds($date = null, bool $absolute = true): float { return $this->diffInMicroseconds($date, $absolute) / static::MICROSECONDS_PER_MILLISECOND; } diff --git a/src/Carbon/Traits/Rounding.php b/src/Carbon/Traits/Rounding.php index d641417ec9..1c3b1c52fd 100644 --- a/src/Carbon/Traits/Rounding.php +++ b/src/Carbon/Traits/Rounding.php @@ -20,8 +20,8 @@ * * Depends on the following methods: * - * @method static copy() - * @method static startOfWeek(int $weekStartsAt = null) + * @method CarbonInterface copy() + * @method CarbonInterface startOfWeek(int $weekStartsAt = null) */ trait Rounding { diff --git a/src/Carbon/Traits/Week.php b/src/Carbon/Traits/Week.php index 98ee84a1ab..a60668a0e1 100644 --- a/src/Carbon/Traits/Week.php +++ b/src/Carbon/Traits/Week.php @@ -10,6 +10,8 @@ */ namespace Carbon\Traits; +use Carbon\CarbonInterface; + /** * Trait Week. * @@ -29,7 +31,7 @@ * @method static dayOfYear(int $dayOfYear) * @method string getTranslationMessage(string $key, string $locale = null, string $default = null, $translator = null) * @method static next(int|string $day = null) - * @method static startOfWeek(int $day = 1) + * @method CarbonInterface startOfWeek(int $day = null) * @method static subWeeks(int $weeks = 1) * @method static year(int $year = null) */ diff --git a/tests/CarbonInterval/GettersTest.php b/tests/CarbonInterval/GettersTest.php index 99bc5c87e8..934e7e8ea9 100644 --- a/tests/CarbonInterval/GettersTest.php +++ b/tests/CarbonInterval/GettersTest.php @@ -11,6 +11,7 @@ */ namespace Tests\CarbonInterval; +use Carbon\Carbon; use Carbon\CarbonInterval; use Carbon\Translator; use Tests\AbstractTestCase; @@ -84,6 +85,19 @@ public function testSecondsGetter() $this->assertSame(10, $ci->seconds); } + public function testStartAndEnd() + { + $interval = Carbon::parse('2020-08-10')->diff('2020-09-30'); + + $this->assertSame('2020-08-10 00:00:00', $interval->start()->format('Y-m-d H:i:s')); + $this->assertSame('2020-09-30 00:00:00', $interval->end()->format('Y-m-d H:i:s')); + + $interval = CarbonInterval::fromString('1 month 20 days'); + + $this->assertNull($interval->start()); + $this->assertNull($interval->end()); + } + public function testDebugInfo() { $ci = CarbonInterval::create(4, 0, 6, 5, 0, 9, 10); diff --git a/tests/CarbonInterval/TotalTest.php b/tests/CarbonInterval/TotalTest.php index dccf1fcb6d..c7ccdf0c9e 100644 --- a/tests/CarbonInterval/TotalTest.php +++ b/tests/CarbonInterval/TotalTest.php @@ -205,4 +205,9 @@ public function testMicrosecondsInterval() $this->assertSame(10, $interval->totalMinutes); $this->assertSame(1 / 6, $interval->totalHours); } + + public function testWithDiffInterval() + { + $this->assertSame(51, Carbon::parse('2020-08-10')->diff('2020-09-30')->totalDays); + } } From b92322f9b2105496a88ff27237220097cbbb26db Mon Sep 17 00:00:00 2001 From: KyleKatarn Date: Tue, 11 Aug 2020 17:50:24 +0200 Subject: [PATCH 23/38] Enhance interval comparison --- src/Carbon/CarbonInterval.php | 21 ++++++- tests/CarbonInterval/ComparisonTest.php | 13 ++++ tests/CarbonInterval/ConstructTest.php | 14 ++--- tests/CarbonInterval/RoundingTest.php | 82 ++++++++++++------------ tests/CarbonInterval/TotalTest.php | 84 ++++++++++++------------- tests/CarbonPeriod/CreateTest.php | 12 ++-- 6 files changed, 128 insertions(+), 98 deletions(-) diff --git a/src/Carbon/CarbonInterval.php b/src/Carbon/CarbonInterval.php index e99c5003f3..a6aee278d3 100644 --- a/src/Carbon/CarbonInterval.php +++ b/src/Carbon/CarbonInterval.php @@ -25,7 +25,6 @@ use Carbon\Traits\Options; use Closure; use DateInterval; -use DateTimeInterface; use Exception; use ReflectionException; use Symfony\Contracts\Translation\TranslatorInterface; @@ -2425,7 +2424,25 @@ public function equalTo($interval): bool { $interval = $this->resolveInterval($interval); - return $interval !== null && $this->totalMicroseconds === $interval->totalMicroseconds; + if ($interval === null) { + return false; + } + + $step = $this->getStep(); + + if ($step) { + return $step === $interval->getStep(); + } + + if ($this->isEmpty()) { + return $interval->isEmpty(); + } + + $cascadedInterval = $this->copy()->cascade(); + $cascadedComparedInterval = $interval->copy()->cascade(); + + return $cascadedInterval->invert === $cascadedComparedInterval->invert && + $cascadedInterval->getNonZeroValues() === $cascadedComparedInterval->getNonZeroValues(); } /** diff --git a/tests/CarbonInterval/ComparisonTest.php b/tests/CarbonInterval/ComparisonTest.php index 63622b7f9c..3fab0642c1 100644 --- a/tests/CarbonInterval/ComparisonTest.php +++ b/tests/CarbonInterval/ComparisonTest.php @@ -11,6 +11,7 @@ */ namespace Tests\CarbonInterval; +use Carbon\CarbonInterface; use Carbon\CarbonInterval; use DateInterval; use Tests\AbstractTestCase; @@ -28,14 +29,26 @@ public function testEqualToTrue() $this->assertTrue($oneDay->eq(CarbonInterval::hours(23)->minutes(60))); $this->assertTrue($oneDay->eq('24 hours')); $this->assertTrue($oneDay->eq('P1D')); + $this->assertTrue(CarbonInterval::day()->invert()->eq(CarbonInterval::days(-1))); + $this->assertTrue(CarbonInterval::day()->sub('1 day')->eq(CarbonInterval::create())); + $step = function () {}; + $this->assertTrue(CarbonInterval::create($step)->eq(CarbonInterval::create($step))); } public function testEqualToFalse() { $oneDay = CarbonInterval::day(); $this->assertFalse($oneDay->equalTo(CarbonInterval::hours(24)->microsecond(1))); + $this->assertFalse($oneDay->equalTo(['not-valid'])); $this->assertFalse($oneDay->eq(CarbonInterval::hours(24)->microsecond(1))); $this->assertFalse($oneDay->eq(CarbonInterval::hours(23)->minutes(59)->seconds(59)->microseconds(999999))); + $nextWeekday = function (CarbonInterface $date) { + return $date->nextWeekday(); + }; + $nextWeekendDay = function (CarbonInterface $date) { + return $date->nextWeekendDay(); + }; + $this->assertFalse(CarbonInterval::create($nextWeekday)->eq(CarbonInterval::create($nextWeekendDay))); } public function testNotEqualToTrue() diff --git a/tests/CarbonInterval/ConstructTest.php b/tests/CarbonInterval/ConstructTest.php index 8e026854c5..1f8dc5e0f1 100644 --- a/tests/CarbonInterval/ConstructTest.php +++ b/tests/CarbonInterval/ConstructTest.php @@ -303,13 +303,13 @@ public function testMake() $this->assertCarbonInterval(CarbonInterval::make(new CarbonInterval('P2M')), 0, 2, 0, 0, 0, 0); $this->assertNull(CarbonInterval::make(3)); - $this->assertSame(3, CarbonInterval::make('3 milliseconds')->totalMilliseconds); - $this->assertSame(3, CarbonInterval::make('3 microseconds')->totalMicroseconds); - $this->assertSame(21, CarbonInterval::make('3 weeks')->totalDays); - $this->assertSame(9, CarbonInterval::make('3 quarters')->totalMonths); - $this->assertSame(30, CarbonInterval::make('3 decades')->totalYears); - $this->assertSame(300, CarbonInterval::make('3 centuries')->totalYears); - $this->assertSame(3000, CarbonInterval::make('3 millennia')->totalYears); + $this->assertSame(3.0, CarbonInterval::make('3 milliseconds')->totalMilliseconds); + $this->assertSame(3.0, CarbonInterval::make('3 microseconds')->totalMicroseconds); + $this->assertSame(21.0, CarbonInterval::make('3 weeks')->totalDays); + $this->assertSame(9.0, CarbonInterval::make('3 quarters')->totalMonths); + $this->assertSame(30.0, CarbonInterval::make('3 decades')->totalYears); + $this->assertSame(300.0, CarbonInterval::make('3 centuries')->totalYears); + $this->assertSame(3000.0, CarbonInterval::make('3 millennia')->totalYears); } public function testCallInvalidStaticMethod() diff --git a/tests/CarbonInterval/RoundingTest.php b/tests/CarbonInterval/RoundingTest.php index 45232ce54c..986aac1381 100644 --- a/tests/CarbonInterval/RoundingTest.php +++ b/tests/CarbonInterval/RoundingTest.php @@ -21,57 +21,57 @@ public function testThrowsExceptionForCompositeInterval() public function testFloor() { - $this->assertSame(21, CarbonInterval::days(21)->floorWeeks()->totalDays); - $this->assertSame(21, CarbonInterval::days(24)->floorWeeks()->totalDays); - $this->assertSame(21, CarbonInterval::days(25)->floorWeeks()->totalDays); - $this->assertSame(21, CarbonInterval::days(27)->floorWeeks()->totalDays); - $this->assertSame(28, CarbonInterval::days(28)->floorWeeks()->totalDays); + $this->assertSame(21.0, CarbonInterval::days(21)->floorWeeks()->totalDays); + $this->assertSame(21.0, CarbonInterval::days(24)->floorWeeks()->totalDays); + $this->assertSame(21.0, CarbonInterval::days(25)->floorWeeks()->totalDays); + $this->assertSame(21.0, CarbonInterval::days(27)->floorWeeks()->totalDays); + $this->assertSame(28.0, CarbonInterval::days(28)->floorWeeks()->totalDays); - $this->assertSame(1000, CarbonInterval::milliseconds(1234)->floor()->totalMilliseconds); - $this->assertSame(1000, CarbonInterval::milliseconds(1834)->floor()->totalMilliseconds); - $this->assertSame(20, CarbonInterval::days(21)->floor('2 days')->totalDays); - $this->assertSame(18, CarbonInterval::days(21)->floor(CarbonInterval::days(6))->totalDays); - $this->assertSame(18, CarbonInterval::days(22)->floorUnit('day', 6)->totalDays); + $this->assertSame(1000.0, CarbonInterval::milliseconds(1234)->floor()->totalMilliseconds); + $this->assertSame(1000.0, CarbonInterval::milliseconds(1834)->floor()->totalMilliseconds); + $this->assertSame(20.0, CarbonInterval::days(21)->floor('2 days')->totalDays); + $this->assertSame(18.0, CarbonInterval::days(21)->floor(CarbonInterval::days(6))->totalDays); + $this->assertSame(18.0, CarbonInterval::days(22)->floorUnit('day', 6)->totalDays); } public function testRound() { - $this->assertSame(21, CarbonInterval::days(21)->roundWeeks()->totalDays); - $this->assertSame(21, CarbonInterval::days(24)->roundWeeks()->totalDays); - $this->assertSame(28, CarbonInterval::days(25)->roundWeeks()->totalDays); - $this->assertSame(28, CarbonInterval::days(27)->roundWeeks()->totalDays); - $this->assertSame(28, CarbonInterval::days(28)->roundWeeks()->totalDays); - $this->assertSame(-7, CarbonInterval::make('7 days 23 hours 34 minutes')->invert()->roundWeeks()->totalDays); - $this->assertSame(-7, CarbonInterval::make('-7 days 23 hours 34 minutes')->roundWeeks()->totalDays); - $this->assertSame(7, CarbonInterval::make('-7 days 23 hours 34 minutes')->invert()->roundWeeks()->totalDays); + $this->assertSame(21.0, CarbonInterval::days(21)->roundWeeks()->totalDays); + $this->assertSame(21.0, CarbonInterval::days(24)->roundWeeks()->totalDays); + $this->assertSame(28.0, CarbonInterval::days(25)->roundWeeks()->totalDays); + $this->assertSame(28.0, CarbonInterval::days(27)->roundWeeks()->totalDays); + $this->assertSame(28.0, CarbonInterval::days(28)->roundWeeks()->totalDays); + $this->assertSame(-7.0, CarbonInterval::make('7 days 23 hours 34 minutes')->invert()->roundWeeks()->totalDays); + $this->assertSame(-7.0, CarbonInterval::make('-7 days 23 hours 34 minutes')->roundWeeks()->totalDays); + $this->assertSame(7.0, CarbonInterval::make('-7 days 23 hours 34 minutes')->invert()->roundWeeks()->totalDays); - $this->assertSame(1000, CarbonInterval::milliseconds(1234)->round()->totalMilliseconds); - $this->assertSame(2000, CarbonInterval::milliseconds(1834)->round()->totalMilliseconds); - $this->assertSame(20, CarbonInterval::days(20)->round('2 days')->totalDays); - $this->assertSame(18, CarbonInterval::days(20)->round(CarbonInterval::days(6))->totalDays); - $this->assertSame(22, CarbonInterval::days(21)->round('2 days')->totalDays); - $this->assertSame(24, CarbonInterval::days(21)->round(CarbonInterval::days(6))->totalDays); - $this->assertSame(22, CarbonInterval::days(22)->round('2 days')->totalDays); - $this->assertSame(24, CarbonInterval::days(22)->round(CarbonInterval::days(6))->totalDays); - $this->assertSame(24, CarbonInterval::days(22)->roundUnit('day', 6)->totalDays); + $this->assertSame(1000.0, CarbonInterval::milliseconds(1234)->round()->totalMilliseconds); + $this->assertSame(2000.0, CarbonInterval::milliseconds(1834)->round()->totalMilliseconds); + $this->assertSame(20.0, CarbonInterval::days(20)->round('2 days')->totalDays); + $this->assertSame(18.0, CarbonInterval::days(20)->round(CarbonInterval::days(6))->totalDays); + $this->assertSame(22.0, CarbonInterval::days(21)->round('2 days')->totalDays); + $this->assertSame(24.0, CarbonInterval::days(21)->round(CarbonInterval::days(6))->totalDays); + $this->assertSame(22.0, CarbonInterval::days(22)->round('2 days')->totalDays); + $this->assertSame(24.0, CarbonInterval::days(22)->round(CarbonInterval::days(6))->totalDays); + $this->assertSame(24.0, CarbonInterval::days(22)->roundUnit('day', 6)->totalDays); } public function testCeil() { - $this->assertSame(21, CarbonInterval::days(21)->ceilWeeks()->totalDays); - $this->assertSame(28, CarbonInterval::days(24)->ceilWeeks()->totalDays); - $this->assertSame(28, CarbonInterval::days(25)->ceilWeeks()->totalDays); - $this->assertSame(28, CarbonInterval::days(27)->ceilWeeks()->totalDays); - $this->assertSame(28, CarbonInterval::days(28)->ceilWeeks()->totalDays); + $this->assertSame(21.0, CarbonInterval::days(21)->ceilWeeks()->totalDays); + $this->assertSame(28.0, CarbonInterval::days(24)->ceilWeeks()->totalDays); + $this->assertSame(28.0, CarbonInterval::days(25)->ceilWeeks()->totalDays); + $this->assertSame(28.0, CarbonInterval::days(27)->ceilWeeks()->totalDays); + $this->assertSame(28.0, CarbonInterval::days(28)->ceilWeeks()->totalDays); - $this->assertSame(2000, CarbonInterval::milliseconds(1234)->ceil()->totalMilliseconds); - $this->assertSame(2000, CarbonInterval::milliseconds(1834)->ceil()->totalMilliseconds); - $this->assertSame(20, CarbonInterval::days(20)->ceil('2 days')->totalDays); - $this->assertSame(24, CarbonInterval::days(20)->ceil(CarbonInterval::days(6))->totalDays); - $this->assertSame(22, CarbonInterval::days(21)->ceil('2 days')->totalDays); - $this->assertSame(24, CarbonInterval::days(21)->ceil(CarbonInterval::days(6))->totalDays); - $this->assertSame(22, CarbonInterval::days(22)->ceil('2 days')->totalDays); - $this->assertSame(24, CarbonInterval::days(22)->ceil(CarbonInterval::days(6))->totalDays); - $this->assertSame(24, CarbonInterval::days(22)->ceilUnit('day', 6)->totalDays); + $this->assertSame(2000.0, CarbonInterval::milliseconds(1234)->ceil()->totalMilliseconds); + $this->assertSame(2000.0, CarbonInterval::milliseconds(1834)->ceil()->totalMilliseconds); + $this->assertSame(20.0, CarbonInterval::days(20)->ceil('2 days')->totalDays); + $this->assertSame(24.0, CarbonInterval::days(20)->ceil(CarbonInterval::days(6))->totalDays); + $this->assertSame(22.0, CarbonInterval::days(21)->ceil('2 days')->totalDays); + $this->assertSame(24.0, CarbonInterval::days(21)->ceil(CarbonInterval::days(6))->totalDays); + $this->assertSame(22.0, CarbonInterval::days(22)->ceil('2 days')->totalDays); + $this->assertSame(24.0, CarbonInterval::days(22)->ceil(CarbonInterval::days(6))->totalDays); + $this->assertSame(24.0, CarbonInterval::days(22)->ceilUnit('day', 6)->totalDays); } } diff --git a/tests/CarbonInterval/TotalTest.php b/tests/CarbonInterval/TotalTest.php index c7ccdf0c9e..7d0f0397ca 100644 --- a/tests/CarbonInterval/TotalTest.php +++ b/tests/CarbonInterval/TotalTest.php @@ -15,7 +15,7 @@ class TotalTest extends AbstractTestCase public function testReturnsTotalValue($spec, $unit, $expected) { $this->assertSame( - $expected, + (float) $expected, CarbonInterval::fromString($spec)->total($unit), "$spec as $unit did not get the expected total" ); @@ -57,25 +57,25 @@ public function testGetTotalsViaGetters() { $interval = CarbonInterval::create(0, 0, 0, 0, 150, 0, 0); - $this->assertSame(150 * 60 * 60 * 1000 * 1000, $interval->totalMicroseconds); - $this->assertSame(150 * 60 * 60 * 1000, $interval->totalMilliseconds); - $this->assertSame(150 * 60 * 60, $interval->totalSeconds); - $this->assertSame(150 * 60, $interval->totalMinutes); - $this->assertSame(150, $interval->totalHours); - $this->assertSame(150 / 24, $interval->totalDays); - $this->assertSame(150 / 24 / 7, $interval->totalWeeks); - $this->assertSame(150 / 24 / 7 / 4, $interval->totalMonths); - $this->assertSame(150 / 24 / 7 / 4 / 12, $interval->totalYears); + $this->assertSame(150.0 * 60 * 60 * 1000 * 1000, $interval->totalMicroseconds); + $this->assertSame(150.0 * 60 * 60 * 1000, $interval->totalMilliseconds); + $this->assertSame(150.0 * 60 * 60, $interval->totalSeconds); + $this->assertSame(150.0 * 60, $interval->totalMinutes); + $this->assertSame(150.0, $interval->totalHours); + $this->assertSame(150.0 / 24, $interval->totalDays); + $this->assertSame(150.0 / 24 / 7, $interval->totalWeeks); + $this->assertSame(150.0 / 24 / 7 / 4, $interval->totalMonths); + $this->assertSame(150.0 / 24 / 7 / 4 / 12, $interval->totalYears); $interval = CarbonInterval::milliseconds(12312); - $this->assertSame(12312000, $interval->totalMicroseconds); - $this->assertSame(12312, $interval->totalMilliseconds); + $this->assertSame(12312000.0, $interval->totalMicroseconds); + $this->assertSame(12312.0, $interval->totalMilliseconds); $interval = CarbonInterval::milliseconds(-12312); - $this->assertSame(-12312000, $interval->totalMicroseconds); - $this->assertSame(-12312, $interval->totalMilliseconds); + $this->assertSame(-12312000.0, $interval->totalMicroseconds); + $this->assertSame(-12312.0, $interval->totalMilliseconds); } public function getNegativeIntervals() @@ -92,15 +92,15 @@ public function getNegativeIntervals() */ public function testGetNegativeTotalsViaGetters($factor, $interval) { - $this->assertSame($factor * 150 * 60 * 60 * 1000 * 1000, $interval->totalMicroseconds); - $this->assertSame($factor * 150 * 60 * 60 * 1000, $interval->totalMilliseconds); - $this->assertSame($factor * 150 * 60 * 60, $interval->totalSeconds); - $this->assertSame($factor * 150 * 60, $interval->totalMinutes); - $this->assertSame($factor * 150, $interval->totalHours); - $this->assertSame($factor * 150 / 24, $interval->totalDays); - $this->assertSame($factor * 150 / 24 / 7, $interval->totalWeeks); - $this->assertSame($factor * 150 / 24 / 7 / 4, $interval->totalMonths); - $this->assertSame($factor * 150 / 24 / 7 / 4 / 12, $interval->totalYears); + $this->assertSame($factor * 150.0 * 60 * 60 * 1000 * 1000, $interval->totalMicroseconds); + $this->assertSame($factor * 150.0 * 60 * 60 * 1000, $interval->totalMilliseconds); + $this->assertSame($factor * 150.0 * 60 * 60, $interval->totalSeconds); + $this->assertSame($factor * 150.0 * 60, $interval->totalMinutes); + $this->assertSame($factor * 150.0, $interval->totalHours); + $this->assertSame($factor * 150.0 / 24, $interval->totalDays); + $this->assertSame($factor * 150.0 / 24 / 7, $interval->totalWeeks); + $this->assertSame($factor * 150.0 / 24 / 7 / 4, $interval->totalMonths); + $this->assertSame($factor * 150.0 / 24 / 7 / 4 / 12, $interval->totalYears); } public function testTotalsWithCustomFactors() @@ -113,9 +113,9 @@ public function testTotalsWithCustomFactors() 'week' => [5, 'days'], ]); - $this->assertSame(1, CarbonInterval::make('1d')->totalDays); - $this->assertSame(5, CarbonInterval::make('1w')->totalDays); - $this->assertSame(1, CarbonInterval::make('1w')->totalWeeks); + $this->assertSame(1.0, CarbonInterval::make('1d')->totalDays); + $this->assertSame(5.0, CarbonInterval::make('1w')->totalDays); + $this->assertSame(1.0, CarbonInterval::make('1w')->totalWeeks); CarbonInterval::setCascadeFactors($factors); } @@ -153,11 +153,11 @@ public function testGetTotalsViaGettersWithCustomFactors() CarbonInterval::setCascadeFactors($cascades); - $this->assertSame(150 * 60 * 60, $totalSeconds); - $this->assertSame(150 * 60, $totalMinutes); - $this->assertSame(150, $totalHours); - $this->assertSame(150 / 8, $totalDays); - $this->assertSame(150 / 8 / 5, $totalWeeks); + $this->assertSame(150.0 * 60 * 60, $totalSeconds); + $this->assertSame(150.0 * 60, $totalMinutes); + $this->assertSame(150.0, $totalHours); + $this->assertSame(150.0 / 8, $totalDays); + $this->assertSame(150.0 / 8 / 5, $totalWeeks); $this->assertSame('Unit months have no configuration to get total from other units.', $monthsError); $this->assertSame('Unit years have no configuration to get total from other units.', $yearsError); } @@ -181,12 +181,12 @@ public function testGetTotalsViaGettersWithFalseFactor() $totalYears = $interval->totalYears; CarbonInterval::setCascadeFactors($cascades); - $this->assertSame(150 * 60 * 60, $totalSeconds); - $this->assertSame(150 * 60, $totalMinutes); - $this->assertSame(150, $totalHours); - $this->assertSame(1146, $totalDays); - $this->assertSame(1146 / 30, $totalMonths); - $this->assertSame(1146 / 30 / 12, $totalYears); + $this->assertSame(150.0 * 60 * 60, $totalSeconds); + $this->assertSame(150.0 * 60, $totalMinutes); + $this->assertSame(150.0, $totalHours); + $this->assertSame(1146.0, $totalDays); + $this->assertSame(1146.0 / 30, $totalMonths); + $this->assertSame(1146.0 / 30 / 12, $totalYears); } public function testMicrosecondsInterval() @@ -199,15 +199,15 @@ public function testMicrosecondsInterval() $interval = CarbonInterval::milliseconds(600000)->cascade(); - $this->assertSame(600000000, $interval->totalMicroseconds); - $this->assertSame(600000, $interval->totalMilliseconds); - $this->assertSame(600, $interval->totalSeconds); - $this->assertSame(10, $interval->totalMinutes); + $this->assertSame(600000000.0, $interval->totalMicroseconds); + $this->assertSame(600000.0, $interval->totalMilliseconds); + $this->assertSame(600.0, $interval->totalSeconds); + $this->assertSame(10.0, $interval->totalMinutes); $this->assertSame(1 / 6, $interval->totalHours); } public function testWithDiffInterval() { - $this->assertSame(51, Carbon::parse('2020-08-10')->diff('2020-09-30')->totalDays); + $this->assertSame(51.0, Carbon::parse('2020-08-10')->diff('2020-09-30')->totalDays); } } diff --git a/tests/CarbonPeriod/CreateTest.php b/tests/CarbonPeriod/CreateTest.php index e0d25b5ff0..9d36ff3cbf 100644 --- a/tests/CarbonPeriod/CreateTest.php +++ b/tests/CarbonPeriod/CreateTest.php @@ -515,7 +515,7 @@ public function testCreateFromCarbonInstances() { $period = Carbon::create('2019-01-02')->toPeriod(7); - $this->assertSame(24, $period->getDateInterval()->totalHours); + $this->assertSame(24.0, $period->getDateInterval()->totalHours); $this->assertInstanceOf(Carbon::class, $period->getStartDate()); $this->assertSame('2019-01-02', $period->getStartDate()->format('Y-m-d')); $this->assertNull($period->getEndDate()); @@ -527,7 +527,7 @@ public function testCreateFromCarbonInstances() $period = Carbon::create('2019-01-02')->toPeriod('2019-02-05'); $this->assertNull($period->getRecurrences()); - $this->assertSame(24, $period->getDateInterval()->totalHours); + $this->assertSame(24.0, $period->getDateInterval()->totalHours); $this->assertInstanceOf(Carbon::class, $period->getStartDate()); $this->assertSame('2019-01-02', $period->getStartDate()->format('Y-m-d')); $this->assertInstanceOf(Carbon::class, $period->getEndDate()); @@ -542,7 +542,7 @@ public function testCreateFromCarbonInstances() $period = Carbon::create('2019-01-02')->daysUntil('2019-02-05'); - $this->assertSame(24, $period->getDateInterval()->totalHours); + $this->assertSame(24.0, $period->getDateInterval()->totalHours); $this->assertInstanceOf(Carbon::class, $period->getStartDate()); $this->assertSame('2019-01-02', $period->getStartDate()->format('Y-m-d')); $this->assertInstanceOf(Carbon::class, $period->getEndDate()); @@ -564,14 +564,14 @@ public function testCreateFromCarbonInstances() $this->assertSame('2019-02-05', $period->getEndDate()->format('Y-m-d')); $period = Carbon::create('2019-01-02')->hoursUntil('2019-02-05'); - $this->assertSame(1, $period->getDateInterval()->totalHours); + $this->assertSame(1.0, $period->getDateInterval()->totalHours); $this->assertSame('1 minute', Carbon::create('2019-01-02')->minutesUntil('2019-02-05')->getDateInterval()->forHumans()); $this->assertSame('3 minutes', Carbon::create('2019-01-02')->minutesUntil('2019-02-05', 3)->getDateInterval()->forHumans()); $this->assertSame('3 seconds', Carbon::create('2019-01-02')->range('2019-02-05', 3, 'seconds')->getDateInterval()->forHumans()); $this->assertSame('1 second', Carbon::create('2019-01-02')->secondsUntil('2019-02-05')->getDateInterval()->forHumans()); - $this->assertSame(1, Carbon::create('2019-01-02')->millisecondsUntil('2019-02-05')->getDateInterval()->totalMilliseconds); - $this->assertSame(1, Carbon::create('2019-01-02')->microsecondsUntil('2019-02-05')->getDateInterval()->totalMicroseconds); + $this->assertSame(1.0, Carbon::create('2019-01-02')->millisecondsUntil('2019-02-05')->getDateInterval()->totalMilliseconds); + $this->assertSame(1.0, Carbon::create('2019-01-02')->microsecondsUntil('2019-02-05')->getDateInterval()->totalMicroseconds); $this->assertSame('1 week', Carbon::create('2019-01-02')->weeksUntil('2019-02-05')->getDateInterval()->forHumans()); $this->assertSame('1 month', Carbon::create('2019-01-02')->monthsUntil('2019-02-05')->getDateInterval()->forHumans()); $this->assertSame('3 months', Carbon::create('2019-01-02')->quartersUntil('2019-02-05')->getDateInterval()->forHumans()); From 3e01b781a44ff9de7212dd1c1f32646ac2aac6b6 Mon Sep 17 00:00:00 2001 From: KyleKatarn Date: Tue, 11 Aug 2020 17:54:17 +0200 Subject: [PATCH 24/38] Use more concrete example in unit tests --- tests/CarbonInterval/ComparisonTest.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/tests/CarbonInterval/ComparisonTest.php b/tests/CarbonInterval/ComparisonTest.php index 3fab0642c1..fa56e6bcca 100644 --- a/tests/CarbonInterval/ComparisonTest.php +++ b/tests/CarbonInterval/ComparisonTest.php @@ -31,8 +31,10 @@ public function testEqualToTrue() $this->assertTrue($oneDay->eq('P1D')); $this->assertTrue(CarbonInterval::day()->invert()->eq(CarbonInterval::days(-1))); $this->assertTrue(CarbonInterval::day()->sub('1 day')->eq(CarbonInterval::create())); - $step = function () {}; - $this->assertTrue(CarbonInterval::create($step)->eq(CarbonInterval::create($step))); + $nextWeekday = function (CarbonInterface $date) { + return $date->nextWeekday(); + }; + $this->assertTrue(CarbonInterval::create($nextWeekday)->eq(CarbonInterval::create($nextWeekday))); } public function testEqualToFalse() From 693ddc303bcb6bf2eb105bfc706e128f359a9083 Mon Sep 17 00:00:00 2001 From: KyleKatarn Date: Tue, 11 Aug 2020 18:03:17 +0200 Subject: [PATCH 25/38] Add unit test for absolute() method --- src/Carbon/CarbonInterval.php | 6 +++++- tests/CarbonInterval/SettersTest.php | 14 ++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/src/Carbon/CarbonInterval.php b/src/Carbon/CarbonInterval.php index a6aee278d3..19d703f925 100644 --- a/src/Carbon/CarbonInterval.php +++ b/src/Carbon/CarbonInterval.php @@ -877,6 +877,10 @@ public static function diff($start, $end = null, bool $absolute = false) $end = $end instanceof CarbonInterface ? $end : Carbon::make($end); $interval = static::instance($start->diffAsDateInterval($end, $absolute)); $interval->fixDiffInterval(); + + // The line below fixes https://bugs.php.net/bug.php?id=77007 + $interval->abs($absolute); + $interval->startDate = $start; $interval->endDate = $end; @@ -908,7 +912,7 @@ public function abs(bool $absolute = false) * * @return $this */ - public function absolute(bool $absolute = false) + public function absolute(bool $absolute = true) { return $this->abs($absolute); } diff --git a/tests/CarbonInterval/SettersTest.php b/tests/CarbonInterval/SettersTest.php index b60992ff05..6f07338420 100644 --- a/tests/CarbonInterval/SettersTest.php +++ b/tests/CarbonInterval/SettersTest.php @@ -176,6 +176,20 @@ public function testInvert() $this->assertSame(0, $ci->invert); } + public function testAbsolute() + { + $ci = CarbonInterval::day(); + + $this->assertSame($ci, $ci->absolute()); + $this->assertSame(1.0, $ci->totalDays); + + $this->assertSame(1.0, $ci->invert()->absolute()->totalDays); + $this->assertSame(-1.0, $ci->invert()->absolute(false)->totalDays); + + $this->assertSame(1.0, $ci->invert()->abs(true)->totalDays); + $this->assertSame(-1.0, $ci->invert()->abs(false)->totalDays); + } + public function testInvalidSetter() { $this->expectException(\InvalidArgumentException::class); From 51c5404919dadedac1f4e0591cbe3e0a9dd3652e Mon Sep 17 00:00:00 2001 From: KyleKatarn Date: Tue, 11 Aug 2020 18:41:48 +0200 Subject: [PATCH 26/38] Mute false positive of unused private method --- src/Carbon/CarbonInterval.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/Carbon/CarbonInterval.php b/src/Carbon/CarbonInterval.php index 19d703f925..b2a38b0178 100644 --- a/src/Carbon/CarbonInterval.php +++ b/src/Carbon/CarbonInterval.php @@ -961,6 +961,8 @@ private function fixNegativeMicroseconds() /** * Work-around for https://bugs.php.net/bug.php?id=77145 * + * @SuppressWarnings(UnusedPrivateMethod) + * * @codeCoverageIgnore */ private function fixDiffInterval() From f4854e10a977732f1ab82e2c2dd01507f79921d6 Mon Sep 17 00:00:00 2001 From: KyleKatarn Date: Tue, 11 Aug 2020 23:40:27 +0200 Subject: [PATCH 27/38] Add strict typing for translation methods --- src/Carbon/Traits/Localization.php | 10 +++++----- src/Carbon/Translator.php | 2 +- tests/Carbon/LocalizationTest.php | 20 +++++--------------- tests/CarbonImmutable/LocalizationTest.php | 20 +++++--------------- 4 files changed, 16 insertions(+), 36 deletions(-) diff --git a/src/Carbon/Traits/Localization.php b/src/Carbon/Traits/Localization.php index a71d62638c..927380716b 100644 --- a/src/Carbon/Traits/Localization.php +++ b/src/Carbon/Traits/Localization.php @@ -321,7 +321,7 @@ public function translateNumber(int $number): string * * @return string */ - public static function translateTimeString($timeString, $from = null, $to = null, $mode = CarbonInterface::TRANSLATE_ALL) + public static function translateTimeString(string $timeString, string $from = null, string $to = null, int $mode = CarbonInterface::TRANSLATE_ALL): string { // Fallback source and destination locales $from = $from ?: static::getLocale(); @@ -417,7 +417,7 @@ public static function translateTimeString($timeString, $from = null, $to = null * * @return string */ - public function translateTimeStringTo($timeString, $to = null) + public function translateTimeStringTo(string $timeString, string $to = null): string { return static::translateTimeString($timeString, $this->getTranslatorLocale(), $to); } @@ -462,7 +462,7 @@ public function locale(string $locale = null, ...$fallbackLocales) * * @return string */ - public static function getLocale() + public static function getLocale(): string { return static::getLocaleAwareTranslator()->getLocale(); } @@ -475,7 +475,7 @@ public static function getLocale() * * @return bool */ - public static function setLocale($locale) + public static function setLocale(string $locale): bool { return static::getLocaleAwareTranslator()->setLocale($locale) !== false; } @@ -487,7 +487,7 @@ public static function setLocale($locale) * * @param string $locale */ - public static function setFallbackLocale($locale) + public static function setFallbackLocale(string $locale): void { $translator = static::getTranslator(); diff --git a/src/Carbon/Translator.php b/src/Carbon/Translator.php index 84af25bc9e..2a07b0a6db 100644 --- a/src/Carbon/Translator.php +++ b/src/Carbon/Translator.php @@ -311,7 +311,7 @@ public function getMessages($locale = null) * * @return bool */ - public function setLocale($locale) + public function setLocale(string $locale): bool { $locale = preg_replace_callback('/[-_]([a-z]{2,})/', function ($matches) { // _2-letters or YUE is a region, _3+-letters is a variant diff --git a/tests/Carbon/LocalizationTest.php b/tests/Carbon/LocalizationTest.php index a0737f5596..0d91c5b5ef 100644 --- a/tests/Carbon/LocalizationTest.php +++ b/tests/Carbon/LocalizationTest.php @@ -162,10 +162,8 @@ public function testSetLocaleToAuto() /** * @see \Tests\Carbon\LocalizationTest::testSetLocale * @see \Tests\Carbon\LocalizationTest::testSetTranslator - * - * @return array */ - public function providerLocales() + public function providerLocales(): array { return [ ['af'], @@ -309,10 +307,8 @@ public function providerLocales() /** * @dataProvider \Tests\Carbon\LocalizationTest::providerLocales - * - * @param string $locale */ - public function testSetLocale($locale) + public function testSetLocale(string $locale) { $this->assertTrue(Carbon::setLocale($locale)); $this->assertTrue($this->areSameLocales($locale, Carbon::getLocale())); @@ -320,10 +316,8 @@ public function testSetLocale($locale) /** * @dataProvider \Tests\Carbon\LocalizationTest::providerLocales - * - * @param string $locale */ - public function testSetTranslator($locale) + public function testSetTranslator(string $locale) { $ori = Carbon::getTranslator(); $t = new Translator($locale); @@ -345,10 +339,8 @@ public function testSetLocaleWithKnownLocale() /** * @see \Tests\Carbon\LocalizationTest::testSetLocaleWithMalformedLocale - * - * @return array */ - public function dataProviderTestSetLocaleWithMalformedLocale() + public function dataProviderTestSetLocaleWithMalformedLocale(): array { return [ ['DE'], @@ -364,10 +356,8 @@ public function dataProviderTestSetLocaleWithMalformedLocale() /** * @dataProvider \Tests\Carbon\LocalizationTest::dataProviderTestSetLocaleWithMalformedLocale - * - * @param string $malformedLocale */ - public function testSetLocaleWithMalformedLocale($malformedLocale) + public function testSetLocaleWithMalformedLocale(string $malformedLocale) { $this->assertTrue(Carbon::setLocale($malformedLocale)); } diff --git a/tests/CarbonImmutable/LocalizationTest.php b/tests/CarbonImmutable/LocalizationTest.php index cc4b84f23e..c425d24a93 100644 --- a/tests/CarbonImmutable/LocalizationTest.php +++ b/tests/CarbonImmutable/LocalizationTest.php @@ -161,10 +161,8 @@ public function testSetLocaleToAuto() /** * @see \Tests\Carbon\LocalizationTest::testSetLocale * @see \Tests\Carbon\LocalizationTest::testSetTranslator - * - * @return array */ - public function providerLocales() + public function providerLocales(): array { return [ ['af'], @@ -308,10 +306,8 @@ public function providerLocales() /** * @dataProvider \Tests\Carbon\LocalizationTest::providerLocales - * - * @param string $locale */ - public function testSetLocale($locale) + public function testSetLocale(string $locale) { $this->assertTrue(Carbon::setLocale($locale)); $this->assertTrue($this->areSameLocales($locale, Carbon::getLocale())); @@ -319,10 +315,8 @@ public function testSetLocale($locale) /** * @dataProvider \Tests\Carbon\LocalizationTest::providerLocales - * - * @param string $locale */ - public function testSetTranslator($locale) + public function testSetTranslator(string $locale) { $ori = Carbon::getTranslator(); $t = new Translator($locale); @@ -343,10 +337,8 @@ public function testSetLocaleWithKnownLocale() /** * @see \Tests\Carbon\LocalizationTest::testSetLocaleWithMalformedLocale - * - * @return array */ - public function dataProviderTestSetLocaleWithMalformedLocale() + public function dataProviderTestSetLocaleWithMalformedLocale(): array { return [ ['DE'], @@ -362,10 +354,8 @@ public function dataProviderTestSetLocaleWithMalformedLocale() /** * @dataProvider \Tests\Carbon\LocalizationTest::dataProviderTestSetLocaleWithMalformedLocale - * - * @param string $malformedLocale */ - public function testSetLocaleWithMalformedLocale($malformedLocale) + public function testSetLocaleWithMalformedLocale(string $malformedLocale) { $this->assertTrue(Carbon::setLocale($malformedLocale)); } From f59ce8f3e997a6d60c081edb0519b4fc4c74c001 Mon Sep 17 00:00:00 2001 From: KyleKatarn Date: Tue, 11 Aug 2020 23:53:00 +0200 Subject: [PATCH 28/38] Update CarbonInterface --- src/Carbon/CarbonInterface.php | 18 ++++++++++-------- src/Carbon/Factory.php | 6 +++--- src/Carbon/FactoryImmutable.php | 6 +++--- 3 files changed, 16 insertions(+), 14 deletions(-) diff --git a/src/Carbon/CarbonInterface.php b/src/Carbon/CarbonInterface.php index d219bcbeef..2cd31e8aae 100644 --- a/src/Carbon/CarbonInterface.php +++ b/src/Carbon/CarbonInterface.php @@ -1393,9 +1393,11 @@ public function diffInQuarters($date = null, bool $absolute = true): float; public function diffInSeconds($date = null, bool $absolute = true): float; /** - * @param string $unit - * @param null $date - * @param false $absolute + * @param string $unit microsecond, millisecond, second, minute, + * hour, day, week, months, quarter, years, + * century, millennium + * @param \Carbon\CarbonInterface|\DateTimeInterface|string|null $date + * @param bool $absolute Get the absolute of the difference * * @return float */ @@ -1947,7 +1949,7 @@ public function getLocalTranslator(); * * @return string */ - public static function getLocale(); + public static function getLocale(): string; /** * Get the raw callable macro registered globally for a given name. @@ -3503,7 +3505,7 @@ public function setDateTimeFrom($date = null); * * @param string $locale */ - public static function setFallbackLocale($locale); + public static function setFallbackLocale(string $locale): void; /** * @deprecated To avoid conflict between different third-party libraries, static setters should not be used. @@ -3544,7 +3546,7 @@ public function setLocalTranslator(\Symfony\Component\Translation\TranslatorInte * * @return bool */ - public static function setLocale($locale); + public static function setLocale(string $locale): bool; /** * @deprecated To avoid conflict between different third-party libraries, static setters should not be used. @@ -4548,7 +4550,7 @@ public function translateNumber(int $number): string; * * @return string */ - public static function translateTimeString($timeString, $from = null, $to = null, $mode = self::TRANSLATE_ALL); + public static function translateTimeString(string $timeString, string $from = null, string $to = null, int $mode = self::TRANSLATE_ALL): string; /** * Translate a time string from the current locale (`$date->locale()`) to an other. @@ -4558,7 +4560,7 @@ public static function translateTimeString($timeString, $from = null, $to = null * * @return string */ - public function translateTimeStringTo($timeString, $to = null); + public function translateTimeStringTo(string $timeString, string $to = null): string; /** * Translate using translation string or callback available. diff --git a/src/Carbon/Factory.php b/src/Carbon/Factory.php index 1029ffeb0c..17d0be0716 100644 --- a/src/Carbon/Factory.php +++ b/src/Carbon/Factory.php @@ -126,10 +126,10 @@ * @method void serializeUsing($callback) @deprecated To avoid conflict between different third-party libraries, static setters should not be used. * You should rather transform Carbon object before the serialization. * JSON serialize all Carbon instances using the given callback. - * @method Carbon setFallbackLocale($locale) Set the fallback locale. + * @method void setFallbackLocale(string $locale) Set the fallback locale. * @method Carbon setHumanDiffOptions($humanDiffOptions) @deprecated To avoid conflict between different third-party libraries, static setters should not be used. * You should rather use the ->settings() method. - * @method bool setLocale($locale) Set the current translator locale and indicate if the source locale file exists. + * @method bool setLocale(string $locale) Set the current translator locale and indicate if the source locale file exists. * Pass 'auto' as locale to use closest language from the current LC_TIME locale. * @method void setMidDayAt($hour) @deprecated To avoid conflict between different third-party libraries, static setters should not be used. * You should rather consider mid-day is always 12pm, then if you need to test if it's an other @@ -193,7 +193,7 @@ * @method string singularUnit(string $unit) Returns standardized singular of a given singular/plural unit name (in English). * @method Carbon today($tz = null) Create a Carbon instance for today. * @method Carbon tomorrow($tz = null) Create a Carbon instance for tomorrow. - * @method string translateTimeString($timeString, $from = null, $to = null, $mode = CarbonInterface::TRANSLATE_ALL) Translate a time string from a locale to an other. + * @method string translateTimeString(string $timeString, string $from = null, string $to = null, int $mode = CarbonInterface::TRANSLATE_ALL) Translate a time string from a locale to an other. * @method string translateWith(\Symfony\Component\Translation\TranslatorInterface $translator, string $key, array $parameters = [], $number = null) Translate using translation string or callback available. * @method void useMonthsOverflow($monthsOverflow = true) @deprecated To avoid conflict between different third-party libraries, static setters should not be used. * You should rather use the ->settings() method. diff --git a/src/Carbon/FactoryImmutable.php b/src/Carbon/FactoryImmutable.php index 8775c4c5b9..d070a6736b 100644 --- a/src/Carbon/FactoryImmutable.php +++ b/src/Carbon/FactoryImmutable.php @@ -126,10 +126,10 @@ * @method void serializeUsing($callback) @deprecated To avoid conflict between different third-party libraries, static setters should not be used. * You should rather transform Carbon object before the serialization. * JSON serialize all Carbon instances using the given callback. - * @method CarbonImmutable setFallbackLocale($locale) Set the fallback locale. + * @method void setFallbackLocale(string $locale) Set the fallback locale. * @method CarbonImmutable setHumanDiffOptions($humanDiffOptions) @deprecated To avoid conflict between different third-party libraries, static setters should not be used. * You should rather use the ->settings() method. - * @method bool setLocale($locale) Set the current translator locale and indicate if the source locale file exists. + * @method bool setLocale(string $locale) Set the current translator locale and indicate if the source locale file exists. * Pass 'auto' as locale to use closest language from the current LC_TIME locale. * @method void setMidDayAt($hour) @deprecated To avoid conflict between different third-party libraries, static setters should not be used. * You should rather consider mid-day is always 12pm, then if you need to test if it's an other @@ -193,7 +193,7 @@ * @method string singularUnit(string $unit) Returns standardized singular of a given singular/plural unit name (in English). * @method CarbonImmutable today($tz = null) Create a Carbon instance for today. * @method CarbonImmutable tomorrow($tz = null) Create a Carbon instance for tomorrow. - * @method string translateTimeString($timeString, $from = null, $to = null, $mode = CarbonInterface::TRANSLATE_ALL) Translate a time string from a locale to an other. + * @method string translateTimeString(string $timeString, string $from = null, string $to = null, int $mode = CarbonInterface::TRANSLATE_ALL) Translate a time string from a locale to an other. * @method string translateWith(\Symfony\Component\Translation\TranslatorInterface $translator, string $key, array $parameters = [], $number = null) Translate using translation string or callback available. * @method void useMonthsOverflow($monthsOverflow = true) @deprecated To avoid conflict between different third-party libraries, static setters should not be used. * You should rather use the ->settings() method. From 4a99e60273462623152e84363b8a3242ce682893 Mon Sep 17 00:00:00 2001 From: KyleKatarn Date: Wed, 12 Aug 2020 00:31:05 +0200 Subject: [PATCH 29/38] Add fallback locale in tests --- src/Carbon/Laravel/ServiceProvider.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Carbon/Laravel/ServiceProvider.php b/src/Carbon/Laravel/ServiceProvider.php index 2c6290c5a5..fa17e6c24d 100644 --- a/src/Carbon/Laravel/ServiceProvider.php +++ b/src/Carbon/Laravel/ServiceProvider.php @@ -36,7 +36,7 @@ public function boot() public function updateLocale() { $app = $this->app && method_exists($this->app, 'getLocale') ? $this->app : app('translator'); - $locale = $app->getLocale(); + $locale = $app->getLocale() ?: 'en'; Carbon::setLocale($locale); CarbonImmutable::setLocale($locale); CarbonPeriod::setLocale($locale); From db74787a0735221f25ef1a9f7b812992a4bc0816 Mon Sep 17 00:00:00 2001 From: KyleKatarn Date: Wed, 12 Aug 2020 11:58:41 +0200 Subject: [PATCH 30/38] Add unit tests and typing --- src/Carbon/CarbonInterface.php | 4 +- src/Carbon/CarbonInterval.php | 368 +++++++++++++------------ src/Carbon/Factory.php | 4 +- src/Carbon/FactoryImmutable.php | 4 +- src/Carbon/Traits/Timestamp.php | 6 +- tests/CarbonInterval/ConstructTest.php | 11 + tests/CarbonInterval/ToPeriodTest.php | 47 ++++ 7 files changed, 256 insertions(+), 188 deletions(-) diff --git a/src/Carbon/CarbonInterface.php b/src/Carbon/CarbonInterface.php index 2cd31e8aae..2b0ec9e157 100644 --- a/src/Carbon/CarbonInterface.php +++ b/src/Carbon/CarbonInterface.php @@ -1126,7 +1126,7 @@ public static function createFromTimeString($time, $tz = null); * * @return static */ - public static function createFromTimestamp($timestamp, $tz = null); + public static function createFromTimestamp(int $timestamp, $tz = null): self; /** * Create a Carbon instance from a timestamp in milliseconds. @@ -1136,7 +1136,7 @@ public static function createFromTimestamp($timestamp, $tz = null); * * @return static */ - public static function createFromTimestampMs($timestamp, $tz = null); + public static function createFromTimestampMs(float $timestamp, $tz = null): self; /** * Create a Carbon instance from an UTC timestamp. diff --git a/src/Carbon/CarbonInterval.php b/src/Carbon/CarbonInterval.php index b2a38b0178..e4a3d97043 100644 --- a/src/Carbon/CarbonInterval.php +++ b/src/Carbon/CarbonInterval.php @@ -290,26 +290,6 @@ public static function getCascadeFactors() ]; } - private static function standardizeUnit($unit) - { - $unit = rtrim($unit, 'sz').'s'; - - return $unit === 'days' ? 'dayz' : $unit; - } - - private static function getFlipCascadeFactors() - { - if (!self::$flipCascadeFactors) { - self::$flipCascadeFactors = []; - - foreach (static::getCascadeFactors() as $to => [$factor, $from]) { - self::$flipCascadeFactors[self::standardizeUnit($from)] = [self::standardizeUnit($to), $factor]; - } - } - - return self::$flipCascadeFactors; - } - /** * Set default cascading factors for ->cascade() method. * @@ -693,7 +673,7 @@ public static function __callStatic($method, $parameters) public static function fromString($intervalDefinition) { if (empty($intervalDefinition)) { - return new static(0); + return self::withOriginal(new static(0), $intervalDefinition); } $years = 0; @@ -847,7 +827,10 @@ public static function fromString($intervalDefinition) } } - return new static($years, $months, $weeks, $days, $hours, $minutes, $seconds, $milliseconds * Carbon::MICROSECONDS_PER_MILLISECOND + $microseconds); + return self::withOriginal( + new static($years, $months, $weeks, $days, $hours, $minutes, $seconds, $milliseconds * Carbon::MICROSECONDS_PER_MILLISECOND + $microseconds), + $intervalDefinition + ); } /** @@ -917,109 +900,6 @@ public function absolute(bool $absolute = true) return $this->abs($absolute); } - /** - * @codeCoverageIgnore - */ - private function fixNegativeMicroseconds() - { - if ($this->s !== 0 || $this->i !== 0 || $this->h !== 0 || $this->d !== 0 || $this->m !== 0 || $this->y !== 0) { - $this->f = (round($this->f * 1000000) + 1000000) / 1000000; - $this->s--; - - if ($this->s < 0) { - $this->s += 60; - $this->i--; - - if ($this->i < 0) { - $this->i += 60; - $this->h--; - - if ($this->h < 0) { - $this->h += 24; - $this->d--; - - if ($this->d < 0) { - $this->d += 30; - $this->m--; - - if ($this->m < 0) { - $this->m += 12; - $this->y--; - } - } - } - } - } - - return; - } - - $this->f *= -1; - $this->invert(); - } - - /** - * Work-around for https://bugs.php.net/bug.php?id=77145 - * - * @SuppressWarnings(UnusedPrivateMethod) - * - * @codeCoverageIgnore - */ - private function fixDiffInterval() - { - if ($this->f > 0 && $this->y === -1 && $this->m === 11 && $this->d >= 27 && $this->h === 23 && $this->i === 59 && $this->s === 59) { - $this->y = 0; - $this->m = 0; - $this->d = 0; - $this->h = 0; - $this->i = 0; - $this->s = 0; - $this->f = (1000000 - round($this->f * 1000000)) / 1000000; - $this->invert(); - } elseif ($this->f < 0) { - $this->fixNegativeMicroseconds(); - } - } - - private static function castIntervalToClass(DateInterval $interval, string $className) - { - $mainClass = DateInterval::class; - - if (!is_a($className, $mainClass, true)) { - throw new InvalidCastException("$className is not a sub-class of $mainClass."); - } - - $microseconds = $interval->f; - $instance = new $className(static::getDateIntervalSpec($interval)); - - if ($instance instanceof self) { - $instance->originalInput = $interval; - } - - if ($microseconds) { - $instance->f = $microseconds; - } - - if ($interval instanceof self && is_a($className, self::class, true)) { - $instance->setStep($interval->getStep()); - } - - static::copyNegativeUnits($interval, $instance); - - return $instance; - } - - private static function copyNegativeUnits(DateInterval $from, DateInterval $to) - { - $to->invert = $from->invert; - - foreach (['y', 'm', 'd', 'h', 'i', 's'] as $unit) { - if ($from->$unit < 0) { - $to->$unit *= -1; - } - } - } - /** * Cast the current instance into the given class. * @@ -1068,7 +948,7 @@ public static function make($interval, $unit = null) } if ($interval instanceof Closure) { - return new static($interval); + return self::withOriginal(new static($interval), $interval); } if (!is_string($interval)) { @@ -1125,7 +1005,7 @@ public static function createFromDateString($time) $interval = static::instance($interval); } - return $interval; + return self::withOriginal($interval, $time); } /////////////////////////////////////////////////////////////////// @@ -2221,58 +2101,6 @@ public function compare(DateInterval $interval) return static::compareDateIntervals($this, $interval); } - private function invertCascade(array $values) - { - return $this->set(array_map(function ($value) { - return -$value; - }, $values))->doCascade(true)->invert(); - } - - private function doCascade(bool $deep) - { - $originalData = $this->toArray(); - $originalData['milliseconds'] = (int) ($originalData['microseconds'] / static::getMicrosecondsPerMillisecond()); - $originalData['microseconds'] = $originalData['microseconds'] % static::getMicrosecondsPerMillisecond(); - $originalData['daysExcludeWeeks'] = $originalData['days']; - unset($originalData['days']); - $newData = $originalData; - - foreach (static::getFlipCascadeFactors() as $source => [$target, $factor]) { - foreach (['source', 'target'] as $key) { - if ($$key === 'dayz') { - $$key = 'daysExcludeWeeks'; - } - } - - $value = $newData[$source]; - $modulo = ($factor + ($value % $factor)) % $factor; - $newData[$source] = $modulo; - $newData[$target] += ($value - $modulo) / $factor; - } - - $positive = null; - - if (!$deep) { - foreach ($newData as $value) { - if ($value) { - if ($positive === null) { - $positive = ($value > 0); - - continue; - } - - if (($value > 0) !== $positive) { - return $this->invertCascade($originalData) - ->solveNegativeInterval(); - } - } - } - } - - return $this->set($newData) - ->solveNegativeInterval(); - } - /** * Convert overflowed values into bigger units. * @@ -2778,4 +2606,186 @@ public function ceil($precision = 1) { return $this->round($precision, 'ceil'); } + + private static function withOriginal(self $interval, $original): self + { + $interval->originalInput = $original; + + return $interval; + } + + private static function standardizeUnit($unit) + { + $unit = rtrim($unit, 'sz').'s'; + + return $unit === 'days' ? 'dayz' : $unit; + } + + private static function getFlipCascadeFactors() + { + if (!self::$flipCascadeFactors) { + self::$flipCascadeFactors = []; + + foreach (static::getCascadeFactors() as $to => [$factor, $from]) { + self::$flipCascadeFactors[self::standardizeUnit($from)] = [self::standardizeUnit($to), $factor]; + } + } + + return self::$flipCascadeFactors; + } + + /** + * @codeCoverageIgnore + */ + private function fixNegativeMicroseconds() + { + if ($this->s !== 0 || $this->i !== 0 || $this->h !== 0 || $this->d !== 0 || $this->m !== 0 || $this->y !== 0) { + $this->f = (round($this->f * 1000000) + 1000000) / 1000000; + $this->s--; + + if ($this->s < 0) { + $this->s += 60; + $this->i--; + + if ($this->i < 0) { + $this->i += 60; + $this->h--; + + if ($this->h < 0) { + $this->h += 24; + $this->d--; + + if ($this->d < 0) { + $this->d += 30; + $this->m--; + + if ($this->m < 0) { + $this->m += 12; + $this->y--; + } + } + } + } + } + + return; + } + + $this->f *= -1; + $this->invert(); + } + + /** + * Work-around for https://bugs.php.net/bug.php?id=77145 + * + * @SuppressWarnings(UnusedPrivateMethod) + * + * @codeCoverageIgnore + */ + private function fixDiffInterval() + { + if ($this->f > 0 && $this->y === -1 && $this->m === 11 && $this->d >= 27 && $this->h === 23 && $this->i === 59 && $this->s === 59) { + $this->y = 0; + $this->m = 0; + $this->d = 0; + $this->h = 0; + $this->i = 0; + $this->s = 0; + $this->f = (1000000 - round($this->f * 1000000)) / 1000000; + $this->invert(); + } elseif ($this->f < 0) { + $this->fixNegativeMicroseconds(); + } + } + + private static function castIntervalToClass(DateInterval $interval, string $className) + { + $mainClass = DateInterval::class; + + if (!is_a($className, $mainClass, true)) { + throw new InvalidCastException("$className is not a sub-class of $mainClass."); + } + + $microseconds = $interval->f; + $instance = new $className(static::getDateIntervalSpec($interval)); + + if ($instance instanceof self) { + $instance->originalInput = $interval; + } + + if ($microseconds) { + $instance->f = $microseconds; + } + + if ($interval instanceof self && is_a($className, self::class, true)) { + $instance->setStep($interval->getStep()); + } + + static::copyNegativeUnits($interval, $instance); + + return self::withOriginal($instance, $interval); + } + + private static function copyNegativeUnits(DateInterval $from, DateInterval $to) + { + $to->invert = $from->invert; + + foreach (['y', 'm', 'd', 'h', 'i', 's'] as $unit) { + if ($from->$unit < 0) { + $to->$unit *= -1; + } + } + } + + private function invertCascade(array $values) + { + return $this->set(array_map(function ($value) { + return -$value; + }, $values))->doCascade(true)->invert(); + } + + private function doCascade(bool $deep) + { + $originalData = $this->toArray(); + $originalData['milliseconds'] = (int) ($originalData['microseconds'] / static::getMicrosecondsPerMillisecond()); + $originalData['microseconds'] = $originalData['microseconds'] % static::getMicrosecondsPerMillisecond(); + $originalData['daysExcludeWeeks'] = $originalData['days']; + unset($originalData['days']); + $newData = $originalData; + + foreach (static::getFlipCascadeFactors() as $source => [$target, $factor]) { + foreach (['source', 'target'] as $key) { + if ($$key === 'dayz') { + $$key = 'daysExcludeWeeks'; + } + } + + $value = $newData[$source]; + $modulo = ($factor + ($value % $factor)) % $factor; + $newData[$source] = $modulo; + $newData[$target] += ($value - $modulo) / $factor; + } + + $positive = null; + + if (!$deep) { + foreach ($newData as $value) { + if ($value) { + if ($positive === null) { + $positive = ($value > 0); + + continue; + } + + if (($value > 0) !== $positive) { + return $this->invertCascade($originalData) + ->solveNegativeInterval(); + } + } + } + } + + return $this->set($newData) + ->solveNegativeInterval(); + } } diff --git a/src/Carbon/Factory.php b/src/Carbon/Factory.php index 17d0be0716..52f8309f8c 100644 --- a/src/Carbon/Factory.php +++ b/src/Carbon/Factory.php @@ -31,8 +31,8 @@ * @method Carbon|false createFromLocaleIsoFormat($format, $locale, $time, $tz = null) Create a Carbon instance from a specific ISO format and a string in a given language. * @method Carbon createFromTime($hour = 0, $minute = 0, $second = 0, $tz = null) Create a Carbon instance from just a time. The date portion is set to today. * @method Carbon createFromTimeString($time, $tz = null) Create a Carbon instance from a time string. The date portion is set to today. - * @method Carbon createFromTimestamp($timestamp, $tz = null) Create a Carbon instance from a timestamp. - * @method Carbon createFromTimestampMs($timestamp, $tz = null) Create a Carbon instance from a timestamp in milliseconds. + * @method CarbonInterface createFromTimestamp(int $timestamp, $tz = null) Create a Carbon instance from a timestamp. + * @method CarbonInterface createFromTimestampMs(float $timestamp, $tz = null) Create a Carbon instance from a timestamp in milliseconds. * @method Carbon createFromTimestampUTC($timestamp) Create a Carbon instance from an UTC timestamp. * @method Carbon createMidnightDate($year = null, $month = null, $day = null, $tz = null) Create a Carbon instance from just a date. The time portion is set to midnight. * @method Carbon|false createSafe($year = null, $month = null, $day = null, $hour = null, $minute = null, $second = null, $tz = null) Create a new safe Carbon instance from a specific date and time. diff --git a/src/Carbon/FactoryImmutable.php b/src/Carbon/FactoryImmutable.php index d070a6736b..fec486bd30 100644 --- a/src/Carbon/FactoryImmutable.php +++ b/src/Carbon/FactoryImmutable.php @@ -31,8 +31,8 @@ * @method CarbonImmutable|false createFromLocaleIsoFormat($format, $locale, $time, $tz = null) Create a Carbon instance from a specific ISO format and a string in a given language. * @method CarbonImmutable createFromTime($hour = 0, $minute = 0, $second = 0, $tz = null) Create a Carbon instance from just a time. The date portion is set to today. * @method CarbonImmutable createFromTimeString($time, $tz = null) Create a Carbon instance from a time string. The date portion is set to today. - * @method CarbonImmutable createFromTimestamp($timestamp, $tz = null) Create a Carbon instance from a timestamp. - * @method CarbonImmutable createFromTimestampMs($timestamp, $tz = null) Create a Carbon instance from a timestamp in milliseconds. + * @method CarbonInterface createFromTimestamp(int $timestamp, $tz = null) Create a Carbon instance from a timestamp. + * @method CarbonInterface createFromTimestampMs(float $timestamp, $tz = null) Create a Carbon instance from a timestamp in milliseconds. * @method CarbonImmutable createFromTimestampUTC($timestamp) Create a Carbon instance from an UTC timestamp. * @method CarbonImmutable createMidnightDate($year = null, $month = null, $day = null, $tz = null) Create a Carbon instance from just a date. The time portion is set to midnight. * @method CarbonImmutable|false createSafe($year = null, $month = null, $day = null, $hour = null, $minute = null, $second = null, $tz = null) Create a new safe Carbon instance from a specific date and time. diff --git a/src/Carbon/Traits/Timestamp.php b/src/Carbon/Traits/Timestamp.php index d50de55442..665a963392 100644 --- a/src/Carbon/Traits/Timestamp.php +++ b/src/Carbon/Traits/Timestamp.php @@ -25,9 +25,9 @@ trait Timestamp * * @return static */ - public static function createFromTimestamp($timestamp, $tz = null) + public static function createFromTimestamp(int $timestamp, $tz = null): self { - $date = new DateTime('@'.((int) $timestamp)); + $date = new DateTime('@'.$timestamp); $tz = static::safeCreateDateTimeZone($tz); if ($tz) { @@ -45,7 +45,7 @@ public static function createFromTimestamp($timestamp, $tz = null) * * @return static */ - public static function createFromTimestampMs($timestamp, $tz = null) + public static function createFromTimestampMs(float $timestamp, $tz = null): self { return static::rawCreateFromFormat('U.u', sprintf('%F', $timestamp / 1000)) ->setTimezone($tz); diff --git a/tests/CarbonInterval/ConstructTest.php b/tests/CarbonInterval/ConstructTest.php index 1f8dc5e0f1..ccb6a1d272 100644 --- a/tests/CarbonInterval/ConstructTest.php +++ b/tests/CarbonInterval/ConstructTest.php @@ -321,4 +321,15 @@ public function testCallInvalidStaticMethod() CarbonInterval::anything(); } + + public function testOriginal() + { + $this->assertSame('3 hours', CarbonInterval::make(3, 'hours')->original()); + $this->assertSame('3 hours 30 m', CarbonInterval::make('3 hours 30 m')->original()); + $this->assertSame('PT5H', CarbonInterval::make('PT5H')->original()); + $interval = new DateInterval('P1D'); + $this->assertSame($interval, CarbonInterval::make($interval)->original()); + $interval = new CarbonInterval('P2M'); + $this->assertSame($interval, CarbonInterval::make($interval)->original()); + } } diff --git a/tests/CarbonInterval/ToPeriodTest.php b/tests/CarbonInterval/ToPeriodTest.php index 1b9c193187..38f7226e48 100644 --- a/tests/CarbonInterval/ToPeriodTest.php +++ b/tests/CarbonInterval/ToPeriodTest.php @@ -5,6 +5,7 @@ use Carbon\Carbon; use Carbon\CarbonInterval; +use Carbon\CarbonPeriod; use Tests\AbstractTestCase; class ToPeriodTest extends AbstractTestCase @@ -46,4 +47,50 @@ public function provideToPeriodParameters() ], ]; } + + public function testStepBy() + { + $days = []; + + foreach (Carbon::parse('2020-08-29')->diff('2020-09-02')->stepBy('day') as $day) { + $days[] = "$day"; + } + + $this->assertSame([ + '2020-08-29 00:00:00', + '2020-08-30 00:00:00', + '2020-08-31 00:00:00', + '2020-09-01 00:00:00', + '2020-09-02 00:00:00', + ], $days); + + $times = []; + + foreach (Carbon::parse('2020-08-29')->diff('2020-08-31')->stepBy('12 hours') as $time) { + $times[] = "$time"; + } + + $this->assertSame([ + '2020-08-29 00:00:00', + '2020-08-29 12:00:00', + '2020-08-30 00:00:00', + '2020-08-30 12:00:00', + '2020-08-31 00:00:00', + ], $times); + + $days = []; + /** @var CarbonPeriod $period */ + $period = Carbon::parse('2020-08-29')->diff('2020-09-02')->stepBy('day'); + + foreach ($period->excludeEndDate() as $day) { + $days[] = "$day"; + } + + $this->assertSame([ + '2020-08-29 00:00:00', + '2020-08-30 00:00:00', + '2020-08-31 00:00:00', + '2020-09-01 00:00:00', + ], $days); + } } From 1e94f013f11c5a579e95df33d6c7c878576f6258 Mon Sep 17 00:00:00 2001 From: KyleKatarn Date: Wed, 12 Aug 2020 13:35:59 +0200 Subject: [PATCH 31/38] Create interval from now if no start available via stepBy() --- src/Carbon/CarbonInterval.php | 8 +++++--- tests/CarbonInterval/ConstructTest.php | 7 +++++++ tests/CarbonInterval/ToPeriodTest.php | 20 ++++++++++++++++++++ 3 files changed, 32 insertions(+), 3 deletions(-) diff --git a/src/Carbon/CarbonInterval.php b/src/Carbon/CarbonInterval.php index e4a3d97043..f1606402c8 100644 --- a/src/Carbon/CarbonInterval.php +++ b/src/Carbon/CarbonInterval.php @@ -1798,7 +1798,7 @@ public function toPeriod(...$params) */ public function stepBy($interval, $unit = null) { - $start = $this->startDate ?: Carbon::make($this->startDate); + $start = $this->startDate ?: Carbon::make($this->startDate ?: 'now'); $end = $this->endDate ?: $start->copy()->add($this); return CarbonPeriod::create(static::make($interval, $unit), $start, $end); @@ -2607,9 +2607,11 @@ public function ceil($precision = 1) return $this->round($precision, 'ceil'); } - private static function withOriginal(self $interval, $original): self + private static function withOriginal($interval, $original) { - $interval->originalInput = $original; + if ($interval instanceof self) { + $interval->originalInput = $original; + } return $interval; } diff --git a/tests/CarbonInterval/ConstructTest.php b/tests/CarbonInterval/ConstructTest.php index ccb6a1d272..837264be8d 100644 --- a/tests/CarbonInterval/ConstructTest.php +++ b/tests/CarbonInterval/ConstructTest.php @@ -332,4 +332,11 @@ public function testOriginal() $interval = new CarbonInterval('P2M'); $this->assertSame($interval, CarbonInterval::make($interval)->original()); } + + public function testCreateFromDateString() + { + $this->assertFalse(CarbonInterval::createFromDateString('foo bar')); + $this->assertCarbonInterval(CarbonInterval::createFromDateString('3 hours'), 0, 0, 0, 3, 0, 0); + $this->assertCarbonInterval(CarbonInterval::createFromDateString('46 days, 43 hours and 57 minutes'), 0, 0, 46, 43, 57, 0); + } } diff --git a/tests/CarbonInterval/ToPeriodTest.php b/tests/CarbonInterval/ToPeriodTest.php index 38f7226e48..9bf821eea7 100644 --- a/tests/CarbonInterval/ToPeriodTest.php +++ b/tests/CarbonInterval/ToPeriodTest.php @@ -92,5 +92,25 @@ public function testStepBy() '2020-08-31 00:00:00', '2020-09-01 00:00:00', ], $days); + + Carbon::setTestNow('2020-08-12 06:00:50'); + $days = []; + + foreach (CarbonInterval::week()->stepBy('day') as $day) { + $days[] = "$day"; + } + + $this->assertSame([ + '2020-08-12 06:00:50', + '2020-08-13 06:00:50', + '2020-08-14 06:00:50', + '2020-08-15 06:00:50', + '2020-08-16 06:00:50', + '2020-08-17 06:00:50', + '2020-08-18 06:00:50', + '2020-08-19 06:00:50', + ], $days); + + Carbon::setTestNow(null); } } From d0e048c8783dee4f7e1c904f38509337c2371228 Mon Sep 17 00:00:00 2001 From: KyleKatarn Date: Wed, 12 Aug 2020 14:32:55 +0200 Subject: [PATCH 32/38] Improve typing --- phpdoc.php | 23 ++++++++++++++++++----- src/Carbon/CarbonInterface.php | 6 +++--- src/Carbon/Factory.php | 6 +++--- src/Carbon/FactoryImmutable.php | 6 +++--- src/Carbon/Traits/Timestamp.php | 2 +- 5 files changed, 28 insertions(+), 15 deletions(-) diff --git a/phpdoc.php b/phpdoc.php index 290b0cde26..230b727e46 100644 --- a/phpdoc.php +++ b/phpdoc.php @@ -461,6 +461,7 @@ function compileDoc($autoDocLines, $file) $autoDoc = ''; $columnsMaxLengths = []; + foreach ($autoDocLines as &$editableLine) { if (is_array($editableLine)) { if (($editableLine[1] ?? '') === 'self') { @@ -522,6 +523,20 @@ function compileDoc($autoDocLines, $file) $carbonMethods = get_class_methods(\Carbon\Carbon::class); sort($carbonMethods); +function getMethodReturnType(ReflectionMethod $method) { + $type = $method->getReturnType(); + + if (!$type) { + return ''; + } + + $type = $type->getName(); + $type = preg_replace('/^Carbon\\\\/', '', $type); + $type = preg_replace('/^self$/', 'CarbonInterface', $type); + + return ": $type"; +} + foreach ($carbonMethods as $method) { if ($method === 'diff' || method_exists(\Carbon\CarbonImmutable::class, $method) && !method_exists(DateTimeInterface::class, $method)) { $function = new ReflectionMethod(\Carbon\Carbon::class, $method); @@ -549,13 +564,13 @@ function compileDoc($autoDocLines, $file) $returnType = str_replace('static|CarbonInterface', 'static', $returnType ?: 'static'); $staticMethods[] = [ '@method', - str_replace('static', 'Carbon', $returnType), + str_replace(['self', 'static'], 'Carbon', $returnType), "$method($parameters)", $doc[0], ]; $staticImmutableMethods[] = [ '@method', - str_replace('static', 'CarbonImmutable', $returnType), + str_replace(['self', 'static'], 'CarbonImmutable', $returnType), "$method($parameters)", $doc[0], ]; @@ -566,9 +581,7 @@ function compileDoc($autoDocLines, $file) } } - $return = $function->getReturnType() - ? ': '.preg_replace('/^Carbon\\\\/', '', $function->getReturnType()->getName()) - : ''; + $return = getMethodReturnType($function); if (!empty($methodDocBlock)) { $methodDocBlock = "\n $methodDocBlock"; diff --git a/src/Carbon/CarbonInterface.php b/src/Carbon/CarbonInterface.php index 2b0ec9e157..1948d803f1 100644 --- a/src/Carbon/CarbonInterface.php +++ b/src/Carbon/CarbonInterface.php @@ -1126,7 +1126,7 @@ public static function createFromTimeString($time, $tz = null); * * @return static */ - public static function createFromTimestamp(int $timestamp, $tz = null): self; + public static function createFromTimestamp(int $timestamp, $tz = null): CarbonInterface; /** * Create a Carbon instance from a timestamp in milliseconds. @@ -1136,7 +1136,7 @@ public static function createFromTimestamp(int $timestamp, $tz = null): self; * * @return static */ - public static function createFromTimestampMs(float $timestamp, $tz = null): self; + public static function createFromTimestampMs(float $timestamp, $tz = null): CarbonInterface; /** * Create a Carbon instance from an UTC timestamp. @@ -1145,7 +1145,7 @@ public static function createFromTimestampMs(float $timestamp, $tz = null): self * * @return static */ - public static function createFromTimestampUTC($timestamp); + public static function createFromTimestampUTC(int $timestamp): CarbonInterface; /** * Create a Carbon instance from just a date. The time portion is set to midnight. diff --git a/src/Carbon/Factory.php b/src/Carbon/Factory.php index 52f8309f8c..5d45f63e4b 100644 --- a/src/Carbon/Factory.php +++ b/src/Carbon/Factory.php @@ -31,9 +31,9 @@ * @method Carbon|false createFromLocaleIsoFormat($format, $locale, $time, $tz = null) Create a Carbon instance from a specific ISO format and a string in a given language. * @method Carbon createFromTime($hour = 0, $minute = 0, $second = 0, $tz = null) Create a Carbon instance from just a time. The date portion is set to today. * @method Carbon createFromTimeString($time, $tz = null) Create a Carbon instance from a time string. The date portion is set to today. - * @method CarbonInterface createFromTimestamp(int $timestamp, $tz = null) Create a Carbon instance from a timestamp. - * @method CarbonInterface createFromTimestampMs(float $timestamp, $tz = null) Create a Carbon instance from a timestamp in milliseconds. - * @method Carbon createFromTimestampUTC($timestamp) Create a Carbon instance from an UTC timestamp. + * @method Carbon createFromTimestamp(int $timestamp, $tz = null) Create a Carbon instance from a timestamp. + * @method Carbon createFromTimestampMs(float $timestamp, $tz = null) Create a Carbon instance from a timestamp in milliseconds. + * @method Carbon createFromTimestampUTC(int $timestamp) Create a Carbon instance from an UTC timestamp. * @method Carbon createMidnightDate($year = null, $month = null, $day = null, $tz = null) Create a Carbon instance from just a date. The time portion is set to midnight. * @method Carbon|false createSafe($year = null, $month = null, $day = null, $hour = null, $minute = null, $second = null, $tz = null) Create a new safe Carbon instance from a specific date and time. * If any of $year, $month or $day are set to null their now() values will diff --git a/src/Carbon/FactoryImmutable.php b/src/Carbon/FactoryImmutable.php index fec486bd30..eeb9d06cc1 100644 --- a/src/Carbon/FactoryImmutable.php +++ b/src/Carbon/FactoryImmutable.php @@ -31,9 +31,9 @@ * @method CarbonImmutable|false createFromLocaleIsoFormat($format, $locale, $time, $tz = null) Create a Carbon instance from a specific ISO format and a string in a given language. * @method CarbonImmutable createFromTime($hour = 0, $minute = 0, $second = 0, $tz = null) Create a Carbon instance from just a time. The date portion is set to today. * @method CarbonImmutable createFromTimeString($time, $tz = null) Create a Carbon instance from a time string. The date portion is set to today. - * @method CarbonInterface createFromTimestamp(int $timestamp, $tz = null) Create a Carbon instance from a timestamp. - * @method CarbonInterface createFromTimestampMs(float $timestamp, $tz = null) Create a Carbon instance from a timestamp in milliseconds. - * @method CarbonImmutable createFromTimestampUTC($timestamp) Create a Carbon instance from an UTC timestamp. + * @method CarbonImmutable createFromTimestamp(int $timestamp, $tz = null) Create a Carbon instance from a timestamp. + * @method CarbonImmutable createFromTimestampMs(float $timestamp, $tz = null) Create a Carbon instance from a timestamp in milliseconds. + * @method CarbonImmutable createFromTimestampUTC(int $timestamp) Create a Carbon instance from an UTC timestamp. * @method CarbonImmutable createMidnightDate($year = null, $month = null, $day = null, $tz = null) Create a Carbon instance from just a date. The time portion is set to midnight. * @method CarbonImmutable|false createSafe($year = null, $month = null, $day = null, $hour = null, $minute = null, $second = null, $tz = null) Create a new safe Carbon instance from a specific date and time. * If any of $year, $month or $day are set to null their now() values will diff --git a/src/Carbon/Traits/Timestamp.php b/src/Carbon/Traits/Timestamp.php index 665a963392..4be8404f6b 100644 --- a/src/Carbon/Traits/Timestamp.php +++ b/src/Carbon/Traits/Timestamp.php @@ -58,7 +58,7 @@ public static function createFromTimestampMs(float $timestamp, $tz = null): self * * @return static */ - public static function createFromTimestampUTC($timestamp) + public static function createFromTimestampUTC(int $timestamp): self { return new static('@'.$timestamp); } From 9ef85019d70895c9e004e940b7d239b9e457a369 Mon Sep 17 00:00:00 2001 From: KyleKatarn Date: Wed, 12 Aug 2020 14:57:39 +0200 Subject: [PATCH 33/38] Fix PHP < 7.4 compatibility --- phpdoc.php | 11 ++++------- src/Carbon/CarbonInterface.php | 6 +++--- tests/Carbon/CreateFromTimestampTest.php | 2 +- 3 files changed, 8 insertions(+), 11 deletions(-) diff --git a/phpdoc.php b/phpdoc.php index 230b727e46..c24a4bdab5 100644 --- a/phpdoc.php +++ b/phpdoc.php @@ -523,18 +523,15 @@ function compileDoc($autoDocLines, $file) $carbonMethods = get_class_methods(\Carbon\Carbon::class); sort($carbonMethods); -function getMethodReturnType(ReflectionMethod $method) { +function getMethodReturnType(ReflectionMethod $method) +{ $type = $method->getReturnType(); - if (!$type) { + if (!$type || $type->getName() === 'self') { return ''; } - $type = $type->getName(); - $type = preg_replace('/^Carbon\\\\/', '', $type); - $type = preg_replace('/^self$/', 'CarbonInterface', $type); - - return ": $type"; + return ': '.preg_replace('/^Carbon\\\\/', '', $type->getName()); } foreach ($carbonMethods as $method) { diff --git a/src/Carbon/CarbonInterface.php b/src/Carbon/CarbonInterface.php index 1948d803f1..f00c861a85 100644 --- a/src/Carbon/CarbonInterface.php +++ b/src/Carbon/CarbonInterface.php @@ -1126,7 +1126,7 @@ public static function createFromTimeString($time, $tz = null); * * @return static */ - public static function createFromTimestamp(int $timestamp, $tz = null): CarbonInterface; + public static function createFromTimestamp(int $timestamp, $tz = null); /** * Create a Carbon instance from a timestamp in milliseconds. @@ -1136,7 +1136,7 @@ public static function createFromTimestamp(int $timestamp, $tz = null): CarbonIn * * @return static */ - public static function createFromTimestampMs(float $timestamp, $tz = null): CarbonInterface; + public static function createFromTimestampMs(float $timestamp, $tz = null); /** * Create a Carbon instance from an UTC timestamp. @@ -1145,7 +1145,7 @@ public static function createFromTimestampMs(float $timestamp, $tz = null): Carb * * @return static */ - public static function createFromTimestampUTC(int $timestamp): CarbonInterface; + public static function createFromTimestampUTC(int $timestamp); /** * Create a Carbon instance from just a date. The time portion is set to midnight. diff --git a/tests/Carbon/CreateFromTimestampTest.php b/tests/Carbon/CreateFromTimestampTest.php index 51443f6b56..af20da2f8f 100644 --- a/tests/Carbon/CreateFromTimestampTest.php +++ b/tests/Carbon/CreateFromTimestampTest.php @@ -86,7 +86,7 @@ public function testComaDecimalSeparatorLocale() public function testCreateFromTimestampWithTimezone() { - $carbon = Carbon::createFromTimestamp('468370800', '+0100'); + $carbon = Carbon::createFromTimestamp((int) '468370800', '+0100'); $this->assertSame(468370800, $carbon->getTimestamp()); $this->assertSame('+01:00', $carbon->tzName); From e6687c06c027b20c9c81c1d6ede79f634a8b98ec Mon Sep 17 00:00:00 2001 From: KyleKatarn Date: Wed, 12 Aug 2020 15:11:35 +0200 Subject: [PATCH 34/38] Fix unit tests PHP < 7.2.17 and 7.3.0 to 7.3.3 compatibility --- tests/CarbonInterval/ConstructTest.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/tests/CarbonInterval/ConstructTest.php b/tests/CarbonInterval/ConstructTest.php index 837264be8d..ebc594ecdd 100644 --- a/tests/CarbonInterval/ConstructTest.php +++ b/tests/CarbonInterval/ConstructTest.php @@ -335,7 +335,11 @@ public function testOriginal() public function testCreateFromDateString() { - $this->assertFalse(CarbonInterval::createFromDateString('foo bar')); + // Before PHP 7.2.17 and from 7.3.0 to 7.3.3, createFromDateString() returned an empty interval + // when passing incorrect strings, after that, it returns false ("Bad format" warning of the + // native DateInterval method is muted, when Carbon will drop PHP <= 7.3.3, this warning will + // no longer be muted. + $this->assertCarbonInterval(CarbonInterval::createFromDateString('foo bar') ?: CarbonInterval::create(), 0, 0, 0, 0, 0, 0); $this->assertCarbonInterval(CarbonInterval::createFromDateString('3 hours'), 0, 0, 0, 3, 0, 0); $this->assertCarbonInterval(CarbonInterval::createFromDateString('46 days, 43 hours and 57 minutes'), 0, 0, 46, 43, 57, 0); } From 77dc26f01376d378cedf79b51bf7cb8182f92ddf Mon Sep 17 00:00:00 2001 From: KyleKatarn Date: Wed, 12 Aug 2020 15:23:30 +0200 Subject: [PATCH 35/38] Add unit tests --- tests/Carbon/DiffTest.php | 13 ++++++++++++ tests/Carbon/Fixtures/NoLocaleTranslator.php | 22 ++++++++++++++++++++ tests/Carbon/LocalizationTest.php | 15 +++++++++++++ 3 files changed, 50 insertions(+) create mode 100644 tests/Carbon/Fixtures/NoLocaleTranslator.php diff --git a/tests/Carbon/DiffTest.php b/tests/Carbon/DiffTest.php index 8caa89feaf..b2d9360bcb 100644 --- a/tests/Carbon/DiffTest.php +++ b/tests/Carbon/DiffTest.php @@ -14,6 +14,7 @@ use Carbon\Carbon; use Carbon\CarbonInterface; use Carbon\CarbonInterval; +use Carbon\Exceptions\UnknownUnitException; use Closure; use DateTime; use Exception; @@ -1718,4 +1719,16 @@ public function testExactMonthDiffInSeconds() $this->assertSame(2678400.0, $d2->diffInSeconds($d1)); } + + public function testDiffInUnit() + { + $this->assertSame(5.5, Carbon::make('2020-08-13 05:00')->diffInUnit('hour', '2020-08-13 10:30')); + } + + public function testDiffInUnitException() + { + $this->expectException(UnknownUnitException::class); + $this->expectExceptionMessage("Unknown unit 'moons'."); + $this->assertSame(5.5, Carbon::make('2020-08-13 05:00')->diffInUnit('moon', '2020-08-13 10:30')); + } } diff --git a/tests/Carbon/Fixtures/NoLocaleTranslator.php b/tests/Carbon/Fixtures/NoLocaleTranslator.php new file mode 100644 index 0000000000..44e47c9aa9 --- /dev/null +++ b/tests/Carbon/Fixtures/NoLocaleTranslator.php @@ -0,0 +1,22 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ +namespace Tests\Carbon\Fixtures; + +use Symfony\Contracts\Translation\TranslatorInterface; + +class NoLocaleTranslator implements TranslatorInterface +{ + public function trans(string $id, array $parameters = [], string $domain = null, string $locale = null) + { + return $id; + } +} diff --git a/tests/Carbon/LocalizationTest.php b/tests/Carbon/LocalizationTest.php index 0d91c5b5ef..90dd7f3bec 100644 --- a/tests/Carbon/LocalizationTest.php +++ b/tests/Carbon/LocalizationTest.php @@ -21,6 +21,7 @@ use Symfony\Component\Translation\MessageCatalogue; use Tests\AbstractTestCase; use Tests\Carbon\Fixtures\MyCarbon; +use Tests\Carbon\Fixtures\NoLocaleTranslator; class LocalizationTest extends AbstractTestCase { @@ -734,6 +735,20 @@ public function testTranslationCustomWithCustomTranslator() $date->getTranslationMessage('foo'); } + public function testNoLocaleTranslator() + { + $this->expectException(InvalidArgumentException::class); + $this->expectExceptionMessage( + 'Tests\Carbon\Fixtures\NoLocaleTranslator does neither implements '. + 'Symfony\Contracts\Translation\LocaleAwareInterface nor getLocale() method.' + ); + + $date = Carbon::create(2018, 1, 1, 0, 0, 0); + $date->setLocalTranslator(new NoLocaleTranslator()); + + $date->locale; + } + public function testTranslateTimeStringTo() { $date = Carbon::parse('2019-07-05')->locale('de'); From 89d6197ec536a084d42d4235a8f127a255aecb8e Mon Sep 17 00:00:00 2001 From: KyleKatarn Date: Wed, 12 Aug 2020 17:58:49 +0200 Subject: [PATCH 36/38] Align diff tests with new absolute default --- src/Carbon/Traits/Difference.php | 4 ++-- tests/Carbon/DiffTest.php | 18 +++++++++--------- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/src/Carbon/Traits/Difference.php b/src/Carbon/Traits/Difference.php index cdb97fe0b6..1948746bc0 100644 --- a/src/Carbon/Traits/Difference.php +++ b/src/Carbon/Traits/Difference.php @@ -380,7 +380,7 @@ public function diffInMilliseconds($date = null, bool $absolute = false): float */ public function secondsSinceMidnight(): float { - return $this->diffInSeconds($this->copy()->startOfDay()); + return $this->diffInSeconds($this->copy()->startOfDay(), true); } /** @@ -390,7 +390,7 @@ public function secondsSinceMidnight(): float */ public function secondsUntilEndOfDay(): float { - return $this->diffInSeconds($this->copy()->endOfDay()); + return $this->diffInSeconds($this->copy()->endOfDay(), true); } /** diff --git a/tests/Carbon/DiffTest.php b/tests/Carbon/DiffTest.php index b2d9360bcb..8c733c0e3a 100644 --- a/tests/Carbon/DiffTest.php +++ b/tests/Carbon/DiffTest.php @@ -1645,7 +1645,7 @@ public function testFloatDiffWithRealUnits() */ public function testPhpBug77007() { - $this->assertSame(3.0, Carbon::now()->addMinutes(3)->diffInMinutes()); + $this->assertSame(-3.0, Carbon::now()->addMinutes(3)->diffInMinutes()); $startDate = Carbon::parse('2018-10-11 20:59:06.914653'); $endDate = Carbon::parse('2018-10-11 20:59:07.237419'); @@ -1662,19 +1662,19 @@ public function testPhpBug77007() $endDate = Carbon::parse('2018-10-11 20:59:05.237419'); $this->assertSame('+ 00-00-00 00:00:01.677234', $startDate->diff($endDate, true)->format('%R %Y-%M-%D %H:%I:%S.%F')); - $this->assertSame(1.677234, $startDate->diffInSeconds($endDate)); + $this->assertSame(1.677234, $startDate->diffInSeconds($endDate, true)); $this->assertSame('- 00-00-00 00:00:01.677234', $startDate->diff($endDate)->format('%R %Y-%M-%D %H:%I:%S.%F')); - $this->assertSame(-1.677234, $startDate->diffInSeconds($endDate, false)); + $this->assertSame(-1.677234, $startDate->diffInSeconds($endDate)); $startDate = Carbon::parse('2018-10-11 20:59:06.914653'); $endDate = Carbon::parse('2018-10-11 20:59:06.237419'); $this->assertSame('+ 00-00-00 00:00:00.677234', $startDate->diff($endDate, true)->format('%R %Y-%M-%D %H:%I:%S.%F')); - $this->assertSame(0.677234, $startDate->diffInSeconds($endDate)); + $this->assertSame(0.677234, $startDate->diffInSeconds($endDate, true)); $this->assertSame('- 00-00-00 00:00:00.677234', $startDate->diff($endDate)->format('%R %Y-%M-%D %H:%I:%S.%F')); - $this->assertSame(-0.677234, $startDate->diffInSeconds($endDate, false)); + $this->assertSame(-0.677234, $startDate->diffInSeconds($endDate)); $startDate = Carbon::parse('2017-12-31 23:59:59.914653'); $endDate = Carbon::parse('2018-01-01 00:00:00.237419'); @@ -1688,12 +1688,12 @@ public function testDiffWithZeroAndNonZeroMicroseconds() $requestTime = new Carbon('2018-11-14 18:23:12.0 +00:00'); $serverTime = new Carbon('2018-11-14 18:23:12.307628 +00:00'); - $this->assertSame(0.307628, $serverTime->diffInSeconds($requestTime)); + $this->assertSame(-0.307628, $serverTime->diffInSeconds($requestTime)); $requestTime = new Carbon('2019-02-10 18:23:12.0 +00:00'); $serverTime = new Carbon('2019-02-10 18:23:12.307628 +00:00'); - $this->assertSame(0.307628, $serverTime->diffInSeconds($requestTime)); + $this->assertSame(-0.307628, $serverTime->diffInSeconds($requestTime)); } public function testNearlyFullDayDiffInSeconds() @@ -1701,7 +1701,7 @@ public function testNearlyFullDayDiffInSeconds() $d1 = Carbon::parse('2019-06-15 12:34:56.123456'); $d2 = Carbon::parse('2019-06-16 12:34:56.123455'); - $this->assertSame(86399.999999, $d2->diffInSeconds($d1)); + $this->assertSame(-86399.999999, $d2->diffInSeconds($d1)); } public function testNearlyFullDayDiffInMicroseconds() @@ -1709,7 +1709,7 @@ public function testNearlyFullDayDiffInMicroseconds() $d1 = Carbon::parse('2019-06-15 12:34:56.123456'); $d2 = Carbon::parse('2019-06-16 12:34:56.123455'); - $this->assertSame(86399999999.0, $d2->diffInMicroseconds($d1)); + $this->assertSame(-86399999999.0, $d2->diffInMicroseconds($d1)); } public function testExactMonthDiffInSeconds() From 2576d310eb7736b353d936ed13c0bdc8e3b43f98 Mon Sep 17 00:00:00 2001 From: KyleKatarn Date: Wed, 12 Aug 2020 22:45:21 +0200 Subject: [PATCH 37/38] Fix diff test according to new absolute default --- src/Carbon/CarbonInterface.php | 2 +- src/Carbon/Traits/Difference.php | 2 +- tests/Carbon/DiffTest.php | 272 +++++++++++++++++---------- tests/CarbonImmutable/DiffTest.php | 291 ++++++++++++++++++++--------- 4 files changed, 374 insertions(+), 193 deletions(-) diff --git a/src/Carbon/CarbonInterface.php b/src/Carbon/CarbonInterface.php index dcf3654c70..2164641ce7 100644 --- a/src/Carbon/CarbonInterface.php +++ b/src/Carbon/CarbonInterface.php @@ -1222,7 +1222,7 @@ public function diff($date = null, $absolute = false): CarbonInterval; * * @return CarbonInterval */ - public function diffAsCarbonInterval($date = null, bool $absolute = true): CarbonInterval; + public function diffAsCarbonInterval($date = null, bool $absolute = false): CarbonInterval; /** * Get the difference as a DateInterval instance. diff --git a/src/Carbon/Traits/Difference.php b/src/Carbon/Traits/Difference.php index 1948746bc0..8161b19cee 100644 --- a/src/Carbon/Traits/Difference.php +++ b/src/Carbon/Traits/Difference.php @@ -56,7 +56,7 @@ public function diffAsDateInterval($date = null, bool $absolute = false): DateIn * * @return CarbonInterval */ - public function diffAsCarbonInterval($date = null, bool $absolute = true): CarbonInterval + public function diffAsCarbonInterval($date = null, bool $absolute = false): CarbonInterval { return CarbonInterval::diff($this, $date, $absolute) ->setLocalTranslator($this->getLocalTranslator()); diff --git a/tests/Carbon/DiffTest.php b/tests/Carbon/DiffTest.php index 8c733c0e3a..c3444c06aa 100644 --- a/tests/Carbon/DiffTest.php +++ b/tests/Carbon/DiffTest.php @@ -25,7 +25,7 @@ class DiffTest extends AbstractTestCase { public function wrapWithTestNow(Closure $func, CarbonInterface $dt = null) { - parent::wrapWithTestNow($func, $dt ?: Carbon::createFromDate(2012, 1, 1)); + parent::wrapWithTestNow($func, $dt ?: Carbon::createMidnightDate(2012, 1, 1)); } public function testDiffAsCarbonInterval() @@ -44,13 +44,13 @@ public function testDiffInYearsPositive() public function testDiffInYearsNegativeWithSign() { $dt = Carbon::createFromDate(2000, 1, 1); - $this->assertSame(-1, (int) $dt->diffInYears($dt->copy()->subYear(), false)); + $this->assertSame(-1, (int) $dt->diffInYears($dt->copy()->subYear())); } public function testDiffInYearsNegativeNoSign() { $dt = Carbon::createFromDate(2000, 1, 1); - $this->assertSame(1, (int) $dt->diffInYears($dt->copy()->subYear())); + $this->assertSame(1, (int) $dt->diffInYears($dt->copy()->subYear(), true)); } public function testDiffInYearsVsDefaultNow() @@ -75,13 +75,13 @@ public function testDiffInQuartersPositive() public function testDiffInQuartersNegativeWithSign() { $dt = Carbon::createFromDate(2000, 1, 1); - $this->assertSame(-4, (int) $dt->diffInQuarters($dt->copy()->subQuarters(4), false)); + $this->assertSame(-4, (int) $dt->diffInQuarters($dt->copy()->subQuarters(4))); } public function testDiffInQuartersNegativeWithNoSign() { $dt = Carbon::createFromDate(2000, 1, 1); - $this->assertSame(4, (int) $dt->diffInQuarters($dt->copy()->subQuarters(4))); + $this->assertSame(4, (int) $dt->diffInQuarters($dt->copy()->subQuarters(4), true)); } public function testDiffInQuartersVsDefaultNow() @@ -106,13 +106,13 @@ public function testDiffInMonthsPositive() public function testDiffInMonthsNegativeWithSign() { $dt = Carbon::createFromDate(2000, 1, 1); - $this->assertSame(-11, (int) $dt->diffInMonths($dt->copy()->subYear()->addMonth(), false)); + $this->assertSame(-11, (int) $dt->diffInMonths($dt->copy()->subYear()->addMonth())); } public function testDiffInMonthsNegativeNoSign() { $dt = Carbon::createFromDate(2000, 1, 1); - $this->assertSame(11, (int) $dt->diffInMonths($dt->copy()->subYear()->addMonth())); + $this->assertSame(11, (int) $dt->diffInMonths($dt->copy()->subYear()->addMonth(), true)); } public function testDiffInMonthsVsDefaultNow() @@ -143,7 +143,7 @@ public function testDiffInDaysNegativeWithSign() public function testDiffInDaysNegativeNoSign() { $dt = Carbon::createFromDate(2000, 1, 1); - $this->assertSame(365, (int) $dt->diffInDays($dt->copy()->subYear())); + $this->assertSame(-365, (int) $dt->diffInDays($dt->copy()->subYear())); } public function testDiffInDaysVsDefaultNow() @@ -172,7 +172,7 @@ public function testDiffInDaysFilteredPositiveWithSecondObject() $dt1 = Carbon::createFromDate(2000, 1, 1); $dt2 = Carbon::createFromDate(2000, 1, 31); - $this->assertSame(5, (int) $dt1->diffInDaysFiltered(function (Carbon $date) { + $this->assertSame(5, $dt1->diffInDaysFiltered(function (Carbon $date) { return $date->dayOfWeek === Carbon::SUNDAY; }, $dt2)); } @@ -180,7 +180,7 @@ public function testDiffInDaysFilteredPositiveWithSecondObject() public function testDiffInDaysFilteredNegativeNoSignWithMutated() { $dt = Carbon::createFromDate(2000, 1, 31); - $this->assertSame(5, (int) $dt->diffInDaysFiltered(function (Carbon $date) { + $this->assertSame(-5, $dt->diffInDaysFiltered(function (Carbon $date) { return $date->dayOfWeek === Carbon::SUNDAY; }, $dt->copy()->startOfMonth())); } @@ -190,17 +190,17 @@ public function testDiffInDaysFilteredNegativeNoSignWithSecondObject() $dt1 = Carbon::createFromDate(2000, 1, 31); $dt2 = Carbon::createFromDate(2000, 1, 1); - $this->assertSame(5, (int) $dt1->diffInDaysFiltered(function (Carbon $date) { + $this->assertSame(5, $dt1->diffInDaysFiltered(function (Carbon $date) { return $date->dayOfWeek === Carbon::SUNDAY; - }, $dt2)); + }, $dt2, true)); } public function testDiffInDaysFilteredNegativeWithSignWithMutated() { $dt = Carbon::createFromDate(2000, 1, 31); - $this->assertSame(-5, (int) $dt->diffInDaysFiltered(function (Carbon $date) { + $this->assertSame(-5, $dt->diffInDaysFiltered(function (Carbon $date) { return $date->dayOfWeek === 1; - }, $dt->copy()->startOfMonth(), false)); + }, $dt->copy()->startOfMonth())); } public function testDiffInDaysFilteredNegativeWithSignWithSecondObject() @@ -208,9 +208,9 @@ public function testDiffInDaysFilteredNegativeWithSignWithSecondObject() $dt1 = Carbon::createFromDate(2000, 1, 31); $dt2 = Carbon::createFromDate(2000, 1, 1); - $this->assertSame(-5, (int) $dt1->diffInDaysFiltered(function (Carbon $date) { + $this->assertSame(-5, $dt1->diffInDaysFiltered(function (Carbon $date) { return $date->dayOfWeek === Carbon::SUNDAY; - }, $dt2, false)); + }, $dt2)); } public function testDiffInHoursFiltered() @@ -218,7 +218,7 @@ public function testDiffInHoursFiltered() $dt1 = Carbon::createFromDate(2000, 1, 31)->endOfDay(); $dt2 = Carbon::createFromDate(2000, 1, 1)->startOfDay(); - $this->assertSame(31, (int) $dt1->diffInHoursFiltered(function (Carbon $date) { + $this->assertSame(-31, $dt1->diffInHoursFiltered(function (Carbon $date) { return $date->hour === 9; }, $dt2)); } @@ -228,9 +228,9 @@ public function testDiffInHoursFilteredNegative() $dt1 = Carbon::createFromDate(2000, 1, 31)->endOfDay(); $dt2 = Carbon::createFromDate(2000, 1, 1)->startOfDay(); - $this->assertSame(-31, (int) $dt1->diffInHoursFiltered(function (Carbon $date) { + $this->assertSame(-31, $dt1->diffInHoursFiltered(function (Carbon $date) { return $date->hour === 9; - }, $dt2, false)); + }, $dt2)); } public function testDiffInHoursFilteredWorkHoursPerWeek() @@ -238,7 +238,7 @@ public function testDiffInHoursFilteredWorkHoursPerWeek() $dt1 = Carbon::createFromDate(2000, 1, 5)->endOfDay(); $dt2 = Carbon::createFromDate(2000, 1, 1)->startOfDay(); - $this->assertSame(40, (int) $dt1->diffInHoursFiltered(function (Carbon $date) { + $this->assertSame(-40, $dt1->diffInHoursFiltered(function (Carbon $date) { return $date->hour > 8 && $date->hour < 17; }, $dt2)); } @@ -265,7 +265,7 @@ public function testDiffFilteredNegativeNoSignWithMutated() { $dt = Carbon::createFromDate(2000, 1, 31); - $this->assertSame(2, $dt->diffFiltered(CarbonInterval::days(2), function (Carbon $date) { + $this->assertSame(-2, $dt->diffFiltered(CarbonInterval::days(2), function (Carbon $date) { return $date->dayOfWeek === Carbon::SUNDAY; }, $dt->copy()->startOfMonth())); } @@ -275,7 +275,7 @@ public function testDiffFilteredNegativeNoSignWithSecondObject() $dt1 = Carbon::createFromDate(2006, 1, 31); $dt2 = Carbon::createFromDate(2000, 1, 1); - $this->assertSame(7, $dt1->diffFiltered(CarbonInterval::year(), function (Carbon $date) { + $this->assertSame(-7, $dt1->diffFiltered(CarbonInterval::year(), function (Carbon $date) { return $date->month === 1; }, $dt2)); } @@ -303,7 +303,7 @@ public function testBug188DiffWithSameDates() $start = Carbon::create(2014, 10, 8, 15, 20, 0); $end = $start->copy(); - $this->assertSame(0, (int) $start->diffInDays($end)); + $this->assertSame(0.0, $start->diffInDays($end)); $this->assertSame(0, $start->diffInWeekdays($end)); } @@ -344,13 +344,13 @@ public function testDiffInWeekdaysPositive() public function testDiffInWeekdaysNegativeNoSign() { $dt = Carbon::createFromDate(2000, 1, 31); - $this->assertSame(21, $dt->diffInWeekdays($dt->copy()->startOfMonth())); + $this->assertSame(21, $dt->diffInWeekdays($dt->copy()->startOfMonth(), true)); } public function testDiffInWeekdaysNegativeWithSign() { $dt = Carbon::createFromDate(2000, 1, 31); - $this->assertSame(-21, $dt->diffInWeekdays($dt->copy()->startOfMonth(), false)); + $this->assertSame(-21, $dt->diffInWeekdays($dt->copy()->startOfMonth())); } public function testDiffInWeekendDaysPositive() @@ -362,13 +362,13 @@ public function testDiffInWeekendDaysPositive() public function testDiffInWeekendDaysNegativeNoSign() { $dt = Carbon::createFromDate(2000, 1, 31); - $this->assertSame(10, $dt->diffInWeekendDays($dt->copy()->startOfMonth())); + $this->assertSame(10, $dt->diffInWeekendDays($dt->copy()->startOfMonth(), true)); } public function testDiffInWeekendDaysNegativeWithSign() { $dt = Carbon::createFromDate(2000, 1, 31); - $this->assertSame(-10, $dt->diffInWeekendDays($dt->copy()->startOfMonth(), false)); + $this->assertSame(-10, $dt->diffInWeekendDays($dt->copy()->startOfMonth())); } public function testDiffInWeeksPositive() @@ -380,13 +380,13 @@ public function testDiffInWeeksPositive() public function testDiffInWeeksNegativeWithSign() { $dt = Carbon::createFromDate(2000, 1, 1); - $this->assertSame(-52, (int) $dt->diffInWeeks($dt->copy()->subYear(), false)); + $this->assertSame(-52, (int) $dt->diffInWeeks($dt->copy()->subYear())); } public function testDiffInWeeksNegativeNoSign() { $dt = Carbon::createFromDate(2000, 1, 1); - $this->assertSame(52, (int) $dt->diffInWeeks($dt->copy()->subYear())); + $this->assertSame(52, (int) $dt->diffInWeeks($dt->copy()->subYear(), true)); } public function testDiffInWeeksVsDefaultNow() @@ -411,13 +411,13 @@ public function testDiffInHoursPositive() public function testDiffInHoursNegativeWithSign() { $dt = Carbon::createFromDate(2000, 1, 1); - $this->assertSame(-22, (int) $dt->diffInHours($dt->copy()->subDay()->addHours(2), false)); + $this->assertSame(-22.0, $dt->diffInHours($dt->copy()->subDay()->addHours(2))); } public function testDiffInHoursNegativeNoSign() { $dt = Carbon::createFromDate(2000, 1, 1); - $this->assertSame(22, (int) $dt->diffInHours($dt->copy()->subDay()->addHours(2))); + $this->assertSame(22.0, $dt->diffInHours($dt->copy()->subDay()->addHours(2), true)); } public function testDiffInHoursVsDefaultNow() @@ -441,10 +441,10 @@ public function testDiffInHoursWithTimezones() $dtToronto = Carbon::create(2012, 1, 1, 0, 0, 0, 'America/Toronto'); $dtVancouver = Carbon::create(2012, 1, 1, 0, 0, 0, 'America/Vancouver'); - $this->assertSame(3, (int) $dtVancouver->diffInHours($dtToronto), 'Midnight in Toronto is 3 hours from midnight in Vancouver'); + $this->assertSame(-3.0, $dtVancouver->diffInHours($dtToronto), 'Midnight in Toronto is 3 hours from midnight in Vancouver'); $dtToronto = Carbon::createFromDate(2012, 1, 1, 'America/Toronto'); - sleep(2); + usleep(2); $dtVancouver = Carbon::createFromDate(2012, 1, 1, 'America/Vancouver'); $this->assertSame(0, (int) $dtVancouver->diffInHours($dtToronto) % 24); @@ -452,7 +452,7 @@ public function testDiffInHoursWithTimezones() $dtToronto = Carbon::createMidnightDate(2012, 1, 1, 'America/Toronto'); $dtVancouver = Carbon::createMidnightDate(2012, 1, 1, 'America/Vancouver'); - $this->assertSame(3, (int) $dtVancouver->diffInHours($dtToronto), 'Midnight in Toronto is 3 hours from midnight in Vancouver'); + $this->assertSame(-3.0, $dtVancouver->diffInHours($dtToronto), 'Midnight in Toronto is 3 hours from midnight in Vancouver'); } public function testDiffInMinutesPositive() @@ -470,13 +470,13 @@ public function testDiffInMinutesPositiveALot() public function testDiffInMinutesNegativeWithSign() { $dt = Carbon::createFromDate(2000, 1, 1); - $this->assertSame(-58, (int) $dt->diffInMinutes($dt->copy()->subHour()->addMinutes(2), false)); + $this->assertSame(-58.0, $dt->diffInMinutes($dt->copy()->subHour()->addMinutes(2))); } public function testDiffInMinutesNegativeNoSign() { $dt = Carbon::createFromDate(2000, 1, 1); - $this->assertSame(58, (int) $dt->diffInMinutes($dt->copy()->subHour()->addMinutes(2))); + $this->assertSame(58.0, $dt->diffInMinutes($dt->copy()->subHour()->addMinutes(2), true)); } public function testDiffInMinutesVsDefaultNow() @@ -507,13 +507,13 @@ public function testDiffInSecondsPositiveALot() public function testDiffInSecondsNegativeWithSign() { $dt = Carbon::createFromDate(2000, 1, 1); - $this->assertSame(-58, (int) $dt->diffInSeconds($dt->copy()->subMinute()->addSeconds(2), false)); + $this->assertSame(-58.0, $dt->diffInSeconds($dt->copy()->subMinute()->addSeconds(2))); } public function testDiffInSecondsNegativeNoSign() { $dt = Carbon::createFromDate(2000, 1, 1); - $this->assertSame(58, (int) $dt->diffInSeconds($dt->copy()->subMinute()->addSeconds(2))); + $this->assertSame(58.0, $dt->diffInSeconds($dt->copy()->subMinute()->addSeconds(2), true)); } public function testDiffInSecondsVsDefaultNow() @@ -526,7 +526,7 @@ public function testDiffInSecondsVsDefaultNow() public function testDiffInSecondsEnsureIsTruncated() { $dt = Carbon::createFromDate(2000, 1, 1); - $this->assertSame(1.0, $dt->diffInSeconds($dt->copy()->addSeconds(1.9))); + $this->assertSame(1.0, $dt->diffInSeconds($dt->copy()->addSeconds((int) 1.9))); } public function testDiffInSecondsWithTimezones() @@ -683,14 +683,14 @@ public function testDiffForHumansNowAndMonth() public function testDiffForHumansNowAndMonths() { $this->wrapWithTestNow(function () { - $this->assertSame('2 months ago', Carbon::now()->subMonths(2)->diffForHumans()); + $this->assertSame('2 months ago', Carbon::now()->subMonthsNoOverflow(2)->diffForHumans()); }); } public function testDiffForHumansNowAndNearlyYear() { $this->wrapWithTestNow(function () { - $this->assertSame('11 months ago', Carbon::now()->subMonths(11)->diffForHumans()); + $this->assertSame('11 months ago', Carbon::now()->subMonthsNoOverflow(11)->diffForHumans()); }); } @@ -1133,14 +1133,14 @@ public function testDiffForHumansOtherAndFutureMonth() public function testDiffForHumansOtherAndFutureMonths() { $this->wrapWithTestNow(function () { - $this->assertSame('2 months after', Carbon::now()->diffForHumans(Carbon::now()->subMonths(2))); + $this->assertSame('2 months after', Carbon::now()->diffForHumans(Carbon::now()->subMonthsNoOverflow(2))); }); } public function testDiffForHumansOtherAndNearlyFutureYear() { $this->wrapWithTestNow(function () { - $this->assertSame('11 months after', Carbon::now()->diffForHumans(Carbon::now()->subMonths(11))); + $this->assertSame('11 months after', Carbon::now()->diffForHumans(Carbon::now()->subMonthsNoOverflow(11))); }); } @@ -1307,7 +1307,8 @@ public function testDiffWithString() { $dt1 = Carbon::createFromDate(2000, 1, 25)->endOfDay(); - $this->assertSame(384.0, round($dt1->diffInHours('2000-01-10'))); + $this->assertSame(384.0, round($dt1->diffInHours('2000-01-10', true))); + $this->assertSame(383.0, floor($dt1->diffInHours('2000-01-10', true))); } public function testDiffWithDateTime() @@ -1315,7 +1316,7 @@ public function testDiffWithDateTime() $dt1 = Carbon::createFromDate(2000, 1, 25)->endOfDay(); $dt2 = new DateTime('2000-01-10'); - $this->assertSame(384.0, round($dt1->diffInHours($dt2))); + $this->assertSame(-384.0, round($dt1->diffInHours($dt2))); } public function testDiffOptions() @@ -1324,6 +1325,7 @@ public function testDiffOptions() $this->assertSame(2, Carbon::JUST_NOW); $this->assertSame(4, Carbon::ONE_DAY_WORDS); $this->assertSame(8, Carbon::TWO_DAY_WORDS); + $this->assertSame(16, Carbon::SEQUENTIAL_PARTS_ONLY); $options = Carbon::getHumanDiffOptions(); $this->assertSame(0, $options); @@ -1345,8 +1347,10 @@ public function testDiffOptions() $this->assertSame('0 seconds before', $date->diffForHumans($date)); $this->assertSame('just now', Carbon::now()->diffForHumans()); - Carbon::setHumanDiffOptions(Carbon::ONE_DAY_WORDS | Carbon::TWO_DAY_WORDS | Carbon::NO_ZERO_DIFF); - $this->assertSame(13, Carbon::getHumanDiffOptions()); + Carbon::setHumanDiffOptions(Carbon::ONE_DAY_WORDS | Carbon::TWO_DAY_WORDS | Carbon::NO_ZERO_DIFF | Carbon::SEQUENTIAL_PARTS_ONLY); + $this->assertSame(29, Carbon::getHumanDiffOptions()); + + Carbon::disableHumanDiffOption(Carbon::SEQUENTIAL_PARTS_ONLY); $oneDayAfter = Carbon::create(2018, 3, 13, 2, 5, 6, 'UTC'); $oneDayBefore = Carbon::create(2018, 3, 11, 2, 5, 6, 'UTC'); @@ -1378,6 +1382,80 @@ public function testDiffOptions() Carbon::enableHumanDiffOption(Carbon::JUST_NOW); $this->assertSame(7, Carbon::getHumanDiffOptions()); + $origin = Carbon::create(2019, 1, 4, 0, 0, 0, 'UTC'); + $comparison = Carbon::create(2019, 2, 4, 0, 0, 0, 'UTC'); + $this->assertSame('1 month before', $origin->diffForHumans($comparison, [ + 'parts' => 2, + ])); + $this->assertSame('1 month before', $origin->diffForHumans($comparison, [ + 'parts' => 2, + 'options' => CarbonInterface::SEQUENTIAL_PARTS_ONLY, + ])); + + $origin = Carbon::create(2019, 1, 4, 0, 0, 0, 'UTC'); + $comparison = Carbon::create(2019, 2, 11, 0, 0, 0, 'UTC'); + $this->assertSame('1 month 1 week before', $origin->diffForHumans($comparison, [ + 'parts' => 2, + ])); + $this->assertSame('1 month 1 week before', $origin->diffForHumans($comparison, [ + 'parts' => 2, + 'options' => CarbonInterface::SEQUENTIAL_PARTS_ONLY, + ])); + + $origin = Carbon::create(2019, 1, 4, 0, 0, 0, 'UTC'); + $comparison = Carbon::create(2019, 2, 12, 0, 0, 0, 'UTC'); + $this->assertSame('1 month 1 week before', $origin->diffForHumans($comparison, [ + 'parts' => 2, + ])); + $this->assertSame('1 month 1 week before', $origin->diffForHumans($comparison, [ + 'parts' => 2, + 'options' => CarbonInterface::SEQUENTIAL_PARTS_ONLY, + ])); + $this->assertSame('1 month 1 week 1 day before', $origin->diffForHumans($comparison, [ + 'parts' => 3, + ])); + $this->assertSame('1 month 1 week 1 day before', $origin->diffForHumans($comparison, [ + 'parts' => 3, + 'options' => CarbonInterface::SEQUENTIAL_PARTS_ONLY, + ])); + + $origin = Carbon::create(2019, 1, 4, 0, 0, 0, 'UTC'); + $comparison = Carbon::create(2020, 1, 11, 0, 0, 0, 'UTC'); + $this->assertSame('1 year 1 week before', $origin->diffForHumans($comparison, [ + 'parts' => 2, + ])); + $this->assertSame('1 year before', $origin->diffForHumans($comparison, [ + 'parts' => 2, + 'options' => CarbonInterface::SEQUENTIAL_PARTS_ONLY, + ])); + + $origin = Carbon::create(2019, 1, 4, 0, 0, 0, 'UTC'); + $comparison = Carbon::create(2019, 2, 5, 0, 0, 0, 'UTC'); + $this->assertSame('1 month 1 day before', $origin->diffForHumans($comparison, [ + 'parts' => 2, + ])); + $this->assertSame('1 month before', $origin->diffForHumans($comparison, [ + 'parts' => 2, + 'options' => CarbonInterface::SEQUENTIAL_PARTS_ONLY, + ])); + + $origin = Carbon::create(2019, 1, 4, 0, 0, 0, 'UTC'); + $comparison = Carbon::create(2019, 1, 12, 0, 1, 0, 'UTC'); + $this->assertSame('1 week 1 day before', $origin->diffForHumans($comparison, [ + 'parts' => 2, + ])); + $this->assertSame('1 week 1 day before', $origin->diffForHumans($comparison, [ + 'parts' => 2, + 'options' => CarbonInterface::SEQUENTIAL_PARTS_ONLY, + ])); + $this->assertSame('1 week 1 day 1 minute before', $origin->diffForHumans($comparison, [ + 'parts' => 3, + ])); + $this->assertSame('1 week 1 day before', $origin->diffForHumans($comparison, [ + 'parts' => 3, + 'options' => CarbonInterface::SEQUENTIAL_PARTS_ONLY, + ])); + Carbon::setHumanDiffOptions($options); } @@ -1552,9 +1630,6 @@ public function testDiffForHumansWithIncorrectDateTimeStringWhichIsNotACarbonIns $mar13->diffForHumans('2018-04-13-08:00:00'); } - /** - * @group issue1490 - */ public function testFloatDiff() { date_default_timezone_set('UTC'); @@ -1563,80 +1638,79 @@ public function testFloatDiff() $this->assertSame(1.0006944444444443, Carbon::parse('2018-12-01 00:00')->floatDiffInDays(Carbon::parse('2018-12-02 00:01'))); + $this->assertSame(0.9714742503779745, Carbon::parse('2018-03-13 20:55:12.321456')->floatDiffInMonths(Carbon::parse('2018-04-12 14:24:58.987421'), true)); + $this->assertSame(0.9714742503779745, Carbon::parse('2018-04-12 14:24:58.987421')->floatDiffInMonths(Carbon::parse('2018-03-13 20:55:12.321456'), true)); $this->assertSame(0.9714742503779745, Carbon::parse('2018-03-13 20:55:12.321456')->floatDiffInMonths(Carbon::parse('2018-04-12 14:24:58.987421'))); - $this->assertSame(0.9714742503779745, Carbon::parse('2018-04-12 14:24:58.987421')->floatDiffInMonths(Carbon::parse('2018-03-13 20:55:12.321456'))); - $this->assertSame(0.9714742503779745, Carbon::parse('2018-03-13 20:55:12.321456')->floatDiffInMonths(Carbon::parse('2018-04-12 14:24:58.987421'), false)); - $this->assertSame(-0.9714742503779745, Carbon::parse('2018-04-12 14:24:58.987421')->floatDiffInMonths(Carbon::parse('2018-03-13 20:55:12.321456'), false)); + $this->assertSame(-0.9714742503779745, Carbon::parse('2018-04-12 14:24:58.987421')->floatDiffInMonths(Carbon::parse('2018-03-13 20:55:12.321456'))); - $this->assertSame(16.557633744585264, Carbon::parse('2018-02-13 20:55:12.321456')->floatDiffInMonths(Carbon::parse('2019-06-30 14:24:58.987421'))); + $this->assertSame(16.557633744585264, Carbon::parse('2018-02-13 20:55:12.321456')->floatDiffInMonths(Carbon::parse('2019-06-30 14:24:58.987421'), true)); + $this->assertSame(15.971474250377973, Carbon::parse('2018-02-13 20:55:12.321456')->floatDiffInMonths(Carbon::parse('2019-06-12 14:24:58.987421'), true)); + $this->assertSame(15.971474250377973, Carbon::parse('2019-06-12 14:24:58.987421')->floatDiffInMonths(Carbon::parse('2018-02-13 20:55:12.321456'), true)); $this->assertSame(15.971474250377973, Carbon::parse('2018-02-13 20:55:12.321456')->floatDiffInMonths(Carbon::parse('2019-06-12 14:24:58.987421'))); - $this->assertSame(15.971474250377973, Carbon::parse('2019-06-12 14:24:58.987421')->floatDiffInMonths(Carbon::parse('2018-02-13 20:55:12.321456'))); - $this->assertSame(15.971474250377973, Carbon::parse('2018-02-13 20:55:12.321456')->floatDiffInMonths(Carbon::parse('2019-06-12 14:24:58.987421'), false)); - $this->assertSame(-15.971474250377973, Carbon::parse('2019-06-12 14:24:58.987421')->floatDiffInMonths(Carbon::parse('2018-02-13 20:55:12.321456'), false)); + $this->assertSame(-15.971474250377973, Carbon::parse('2019-06-12 14:24:58.987421')->floatDiffInMonths(Carbon::parse('2018-02-13 20:55:12.321456'))); - $this->assertSame(1.0, Carbon::parse('2018-02-13 20:55:12.321456')->floatDiffInYears(Carbon::parse('2019-02-13 20:55:12.321456'))); - $this->assertSame(1.3746000338015283, Carbon::parse('2018-02-13 20:55:12.321456')->floatDiffInYears(Carbon::parse('2019-06-30 14:24:58.987421'))); - $this->assertSame(0.9609014036645421, Carbon::parse('2018-02-13 20:55:12.321456')->floatDiffInYears(Carbon::parse('2019-01-30 14:24:58.987421'))); + $this->assertSame(1.0, Carbon::parse('2018-02-13 20:55:12.321456')->floatDiffInYears(Carbon::parse('2019-02-13 20:55:12.321456'), true)); + $this->assertSame(1.3746000338015283, Carbon::parse('2018-02-13 20:55:12.321456')->floatDiffInYears(Carbon::parse('2019-06-30 14:24:58.987421'), true)); + $this->assertSame(0.9609014036645421, Carbon::parse('2018-02-13 20:55:12.321456')->floatDiffInYears(Carbon::parse('2019-01-30 14:24:58.987421'), true)); + $this->assertSame(1.3252849653083778, Carbon::parse('2018-02-13 20:55:12.321456')->floatDiffInYears(Carbon::parse('2019-06-12 14:24:58.987421'), true)); + $this->assertSame(1.3252849653083778, Carbon::parse('2019-06-12 14:24:58.987421')->floatDiffInYears(Carbon::parse('2018-02-13 20:55:12.321456'), true)); $this->assertSame(1.3252849653083778, Carbon::parse('2018-02-13 20:55:12.321456')->floatDiffInYears(Carbon::parse('2019-06-12 14:24:58.987421'))); - $this->assertSame(1.3252849653083778, Carbon::parse('2019-06-12 14:24:58.987421')->floatDiffInYears(Carbon::parse('2018-02-13 20:55:12.321456'))); - $this->assertSame(1.3252849653083778, Carbon::parse('2018-02-13 20:55:12.321456')->floatDiffInYears(Carbon::parse('2019-06-12 14:24:58.987421'), false)); - $this->assertSame(-1.3252849653083778, Carbon::parse('2019-06-12 14:24:58.987421')->floatDiffInYears(Carbon::parse('2018-02-13 20:55:12.321456'), false)); + $this->assertSame(-1.3252849653083778, Carbon::parse('2019-06-12 14:24:58.987421')->floatDiffInYears(Carbon::parse('2018-02-13 20:55:12.321456'))); + $this->assertSame(5.325284965308378, Carbon::parse('2018-02-13 20:55:12.321456')->floatDiffInYears(Carbon::parse('2023-06-12 14:24:58.987421'), true)); + $this->assertSame(5.325284965308378, Carbon::parse('2023-06-12 14:24:58.987421')->floatDiffInYears(Carbon::parse('2018-02-13 20:55:12.321456'), true)); $this->assertSame(5.325284965308378, Carbon::parse('2018-02-13 20:55:12.321456')->floatDiffInYears(Carbon::parse('2023-06-12 14:24:58.987421'))); - $this->assertSame(5.325284965308378, Carbon::parse('2023-06-12 14:24:58.987421')->floatDiffInYears(Carbon::parse('2018-02-13 20:55:12.321456'))); - $this->assertSame(5.325284965308378, Carbon::parse('2018-02-13 20:55:12.321456')->floatDiffInYears(Carbon::parse('2023-06-12 14:24:58.987421'), false)); - $this->assertSame(-5.325284965308378, Carbon::parse('2023-06-12 14:24:58.987421')->floatDiffInYears(Carbon::parse('2018-02-13 20:55:12.321456'), false)); + $this->assertSame(-5.325284965308378, Carbon::parse('2023-06-12 14:24:58.987421')->floatDiffInYears(Carbon::parse('2018-02-13 20:55:12.321456'))); - $this->assertSame(1.0, Carbon::parse('2018-10-01 00:00:00', 'Europe/Paris')->floatDiffInMonths(Carbon::parse('2018-11-01 00:00:00', 'Europe/Paris'))); - $this->assertSame(1.0, Carbon::parse('2018-10-28 00:00:00')->floatDiffInMonths(Carbon::parse('2018-11-28 00:00:00'))); - $this->assertSame(1.0, Carbon::parse('2018-10-28 00:00:00', 'Europe/Paris')->floatDiffInMonths(Carbon::parse('2018-11-28 00:00:00', 'Europe/Paris'))); + $this->assertSame(1.0, Carbon::parse('2018-10-01 00:00:00', 'Europe/Paris')->floatDiffInMonths(Carbon::parse('2018-11-01 00:00:00', 'Europe/Paris'), true)); + $this->assertSame(1.0, Carbon::parse('2018-10-28 00:00:00')->floatDiffInMonths(Carbon::parse('2018-11-28 00:00:00'), true)); + $this->assertSame(1.0, Carbon::parse('2018-10-28 00:00:00', 'Europe/Paris')->floatDiffInMonths(Carbon::parse('2018-11-28 00:00:00', 'Europe/Paris'), true)); - $this->assertSame(-1.0, Carbon::parse('2018-11-01 00:00:00', 'Europe/Paris')->floatDiffInMonths(Carbon::parse('2018-10-01 00:00:00', 'Europe/Paris'), false)); - $this->assertSame(-1.0, Carbon::parse('2018-11-28 00:00:00')->floatDiffInMonths(Carbon::parse('2018-10-28 00:00:00'), false)); - $this->assertSame(-1.0, Carbon::parse('2018-11-28 00:00:00', 'Europe/Paris')->floatDiffInMonths(Carbon::parse('2018-10-28 00:00:00', 'Europe/Paris'), false)); + $this->assertSame(-1.0, Carbon::parse('2018-11-01 00:00:00', 'Europe/Paris')->floatDiffInMonths(Carbon::parse('2018-10-01 00:00:00', 'Europe/Paris'))); + $this->assertSame(-1.0, Carbon::parse('2018-11-28 00:00:00')->floatDiffInMonths(Carbon::parse('2018-10-28 00:00:00'))); + $this->assertSame(-1.0, Carbon::parse('2018-11-28 00:00:00', 'Europe/Paris')->floatDiffInMonths(Carbon::parse('2018-10-28 00:00:00', 'Europe/Paris'))); } public function testFloatDiffWithRealUnits() { date_default_timezone_set('UTC'); + $this->assertSame(1.0006944444444443, Carbon::parse('2018-12-01 00:00')->floatDiffInRealDays(Carbon::parse('2018-12-02 00:01'), true)); - $this->assertSame(1.0006944444444443, Carbon::parse('2018-12-01 00:00')->floatDiffInRealDays(Carbon::parse('2018-12-02 00:01'))); - + $this->assertSame(0.9714742503779745, Carbon::parse('2018-03-13 20:55:12.321456')->floatDiffInRealMonths(Carbon::parse('2018-04-12 14:24:58.987421'), true)); + $this->assertSame(0.9714742503779745, Carbon::parse('2018-04-12 14:24:58.987421')->floatDiffInRealMonths(Carbon::parse('2018-03-13 20:55:12.321456'), true)); $this->assertSame(0.9714742503779745, Carbon::parse('2018-03-13 20:55:12.321456')->floatDiffInRealMonths(Carbon::parse('2018-04-12 14:24:58.987421'))); - $this->assertSame(0.9714742503779745, Carbon::parse('2018-04-12 14:24:58.987421')->floatDiffInRealMonths(Carbon::parse('2018-03-13 20:55:12.321456'))); - $this->assertSame(0.9714742503779745, Carbon::parse('2018-03-13 20:55:12.321456')->floatDiffInRealMonths(Carbon::parse('2018-04-12 14:24:58.987421'), false)); - $this->assertSame(-0.9714742503779745, Carbon::parse('2018-04-12 14:24:58.987421')->floatDiffInRealMonths(Carbon::parse('2018-03-13 20:55:12.321456'), false)); + $this->assertSame(-0.9714742503779745, Carbon::parse('2018-04-12 14:24:58.987421')->floatDiffInRealMonths(Carbon::parse('2018-03-13 20:55:12.321456'))); - $this->assertSame(16.557633744585264, Carbon::parse('2018-02-13 20:55:12.321456')->floatDiffInRealMonths(Carbon::parse('2019-06-30 14:24:58.987421'))); + $this->assertSame(16.557633744585264, Carbon::parse('2018-02-13 20:55:12.321456')->floatDiffInRealMonths(Carbon::parse('2019-06-30 14:24:58.987421'), true)); + $this->assertSame(15.971474250377973, Carbon::parse('2018-02-13 20:55:12.321456')->floatDiffInRealMonths(Carbon::parse('2019-06-12 14:24:58.987421'), true)); + $this->assertSame(15.971474250377973, Carbon::parse('2019-06-12 14:24:58.987421')->floatDiffInRealMonths(Carbon::parse('2018-02-13 20:55:12.321456'), true)); $this->assertSame(15.971474250377973, Carbon::parse('2018-02-13 20:55:12.321456')->floatDiffInRealMonths(Carbon::parse('2019-06-12 14:24:58.987421'))); - $this->assertSame(15.971474250377973, Carbon::parse('2019-06-12 14:24:58.987421')->floatDiffInRealMonths(Carbon::parse('2018-02-13 20:55:12.321456'))); - $this->assertSame(15.971474250377973, Carbon::parse('2018-02-13 20:55:12.321456')->floatDiffInRealMonths(Carbon::parse('2019-06-12 14:24:58.987421'), false)); - $this->assertSame(-15.971474250377973, Carbon::parse('2019-06-12 14:24:58.987421')->floatDiffInRealMonths(Carbon::parse('2018-02-13 20:55:12.321456'), false)); + $this->assertSame(-15.971474250377973, Carbon::parse('2019-06-12 14:24:58.987421')->floatDiffInRealMonths(Carbon::parse('2018-02-13 20:55:12.321456'))); - $this->assertSame(1.0, Carbon::parse('2018-02-13 20:55:12.321456')->floatDiffInRealYears(Carbon::parse('2019-02-13 20:55:12.321456'))); - $this->assertSame(1.3746000338015283, Carbon::parse('2018-02-13 20:55:12.321456')->floatDiffInRealYears(Carbon::parse('2019-06-30 14:24:58.987421'))); - $this->assertSame(0.9609014036645421, Carbon::parse('2018-02-13 20:55:12.321456')->floatDiffInRealYears(Carbon::parse('2019-01-30 14:24:58.987421'))); + $this->assertSame(1.0, Carbon::parse('2018-02-13 20:55:12.321456')->floatDiffInRealYears(Carbon::parse('2019-02-13 20:55:12.321456'), true)); + $this->assertSame(1.3746000338015283, Carbon::parse('2018-02-13 20:55:12.321456')->floatDiffInRealYears(Carbon::parse('2019-06-30 14:24:58.987421'), true)); + $this->assertSame(0.9609014036645421, Carbon::parse('2018-02-13 20:55:12.321456')->floatDiffInRealYears(Carbon::parse('2019-01-30 14:24:58.987421'), true)); + $this->assertSame(1.3252849653083778, Carbon::parse('2018-02-13 20:55:12.321456')->floatDiffInRealYears(Carbon::parse('2019-06-12 14:24:58.987421'), true)); + $this->assertSame(1.3252849653083778, Carbon::parse('2019-06-12 14:24:58.987421')->floatDiffInRealYears(Carbon::parse('2018-02-13 20:55:12.321456'), true)); $this->assertSame(1.3252849653083778, Carbon::parse('2018-02-13 20:55:12.321456')->floatDiffInRealYears(Carbon::parse('2019-06-12 14:24:58.987421'))); - $this->assertSame(1.3252849653083778, Carbon::parse('2019-06-12 14:24:58.987421')->floatDiffInRealYears(Carbon::parse('2018-02-13 20:55:12.321456'))); - $this->assertSame(1.3252849653083778, Carbon::parse('2018-02-13 20:55:12.321456')->floatDiffInRealYears(Carbon::parse('2019-06-12 14:24:58.987421'), false)); - $this->assertSame(-1.3252849653083778, Carbon::parse('2019-06-12 14:24:58.987421')->floatDiffInRealYears(Carbon::parse('2018-02-13 20:55:12.321456'), false)); + $this->assertSame(-1.3252849653083778, Carbon::parse('2019-06-12 14:24:58.987421')->floatDiffInRealYears(Carbon::parse('2018-02-13 20:55:12.321456'))); + $this->assertSame(5.325284965308378, Carbon::parse('2018-02-13 20:55:12.321456')->floatDiffInRealYears(Carbon::parse('2023-06-12 14:24:58.987421'), true)); + $this->assertSame(5.325284965308378, Carbon::parse('2023-06-12 14:24:58.987421')->floatDiffInRealYears(Carbon::parse('2018-02-13 20:55:12.321456'), true)); $this->assertSame(5.325284965308378, Carbon::parse('2018-02-13 20:55:12.321456')->floatDiffInRealYears(Carbon::parse('2023-06-12 14:24:58.987421'))); - $this->assertSame(5.325284965308378, Carbon::parse('2023-06-12 14:24:58.987421')->floatDiffInRealYears(Carbon::parse('2018-02-13 20:55:12.321456'))); - $this->assertSame(5.325284965308378, Carbon::parse('2018-02-13 20:55:12.321456')->floatDiffInRealYears(Carbon::parse('2023-06-12 14:24:58.987421'), false)); - $this->assertSame(-5.325284965308378, Carbon::parse('2023-06-12 14:24:58.987421')->floatDiffInRealYears(Carbon::parse('2018-02-13 20:55:12.321456'), false)); + $this->assertSame(-5.325284965308378, Carbon::parse('2023-06-12 14:24:58.987421')->floatDiffInRealYears(Carbon::parse('2018-02-13 20:55:12.321456'))); - $this->assertSame(1.0, Carbon::parse('2018-10-01 00:00:00', 'Europe/Paris')->floatDiffInRealMonths(Carbon::parse('2018-11-01 00:00:00', 'Europe/Paris'))); - $this->assertSame(1.0, Carbon::parse('2018-10-28 00:00:00')->floatDiffInRealMonths(Carbon::parse('2018-11-28 00:00:00'))); - $this->assertSame(1.0, Carbon::parse('2018-10-28 00:00:00', 'Europe/Paris')->floatDiffInRealMonths(Carbon::parse('2018-11-28 00:00:00', 'Europe/Paris'))); + $this->assertSame(1.0, Carbon::parse('2018-10-01 00:00:00', 'Europe/Paris')->floatDiffInRealMonths(Carbon::parse('2018-11-01 00:00:00', 'Europe/Paris'), true)); + $this->assertSame(1.0, Carbon::parse('2018-10-28 00:00:00')->floatDiffInRealMonths(Carbon::parse('2018-11-28 00:00:00'), true)); + $this->assertSame(1.0, Carbon::parse('2018-10-28 00:00:00', 'Europe/Paris')->floatDiffInRealMonths(Carbon::parse('2018-11-28 00:00:00', 'Europe/Paris'), true)); - $this->assertSame(-1.0, Carbon::parse('2018-11-01 00:00:00', 'Europe/Paris')->floatDiffInRealMonths(Carbon::parse('2018-10-01 00:00:00', 'Europe/Paris'), false)); - $this->assertSame(-1.0, Carbon::parse('2018-11-28 00:00:00')->floatDiffInRealMonths(Carbon::parse('2018-10-28 00:00:00'), false)); - $this->assertSame(-1.0, Carbon::parse('2018-11-28 00:00:00', 'Europe/Paris')->floatDiffInRealMonths(Carbon::parse('2018-10-28 00:00:00', 'Europe/Paris'), false)); + $this->assertSame(-1.0, Carbon::parse('2018-11-01 00:00:00', 'Europe/Paris')->floatDiffInRealMonths(Carbon::parse('2018-10-01 00:00:00', 'Europe/Paris'))); + $this->assertSame(-1.0, Carbon::parse('2018-11-28 00:00:00')->floatDiffInRealMonths(Carbon::parse('2018-10-28 00:00:00'))); + $this->assertSame(-1.0, Carbon::parse('2018-11-28 00:00:00', 'Europe/Paris')->floatDiffInRealMonths(Carbon::parse('2018-10-28 00:00:00', 'Europe/Paris'))); } /** @@ -1665,7 +1739,7 @@ public function testPhpBug77007() $this->assertSame(1.677234, $startDate->diffInSeconds($endDate, true)); $this->assertSame('- 00-00-00 00:00:01.677234', $startDate->diff($endDate)->format('%R %Y-%M-%D %H:%I:%S.%F')); - $this->assertSame(-1.677234, $startDate->diffInSeconds($endDate)); + $this->assertSame(-1.677234, $startDate->diffInSeconds($endDate, false)); $startDate = Carbon::parse('2018-10-11 20:59:06.914653'); $endDate = Carbon::parse('2018-10-11 20:59:06.237419'); @@ -1674,7 +1748,7 @@ public function testPhpBug77007() $this->assertSame(0.677234, $startDate->diffInSeconds($endDate, true)); $this->assertSame('- 00-00-00 00:00:00.677234', $startDate->diff($endDate)->format('%R %Y-%M-%D %H:%I:%S.%F')); - $this->assertSame(-0.677234, $startDate->diffInSeconds($endDate)); + $this->assertSame(-0.677234, $startDate->diffInSeconds($endDate, false)); $startDate = Carbon::parse('2017-12-31 23:59:59.914653'); $endDate = Carbon::parse('2018-01-01 00:00:00.237419'); @@ -1717,7 +1791,7 @@ public function testExactMonthDiffInSeconds() $d1 = Carbon::make('2019-01-23 12:00:00'); $d2 = Carbon::make('2019-02-23 12:00:00'); - $this->assertSame(2678400.0, $d2->diffInSeconds($d1)); + $this->assertSame(-2678400.0, $d2->diffInSeconds($d1)); } public function testDiffInUnit() diff --git a/tests/CarbonImmutable/DiffTest.php b/tests/CarbonImmutable/DiffTest.php index b2ac05db07..cfc4e6b7d1 100644 --- a/tests/CarbonImmutable/DiffTest.php +++ b/tests/CarbonImmutable/DiffTest.php @@ -14,7 +14,11 @@ use Carbon\CarbonImmutable as Carbon; use Carbon\CarbonInterface; use Carbon\CarbonInterval; +use Carbon\Exceptions\UnknownUnitException; use Closure; +use DateTime; +use Exception; +use InvalidArgumentException; use Tests\AbstractTestCase; class DiffTest extends AbstractTestCase @@ -28,6 +32,7 @@ public function testDiffAsCarbonInterval() { $dt = Carbon::createFromDate(2000, 1, 1); $this->assertCarbonInterval($dt->diff($dt->copy()->addYear()), 1, 0, 0, 0, 0, 0); + $this->assertTrue($dt->diff($dt)->isEmpty()); } public function testDiffInYearsPositive() @@ -39,13 +44,13 @@ public function testDiffInYearsPositive() public function testDiffInYearsNegativeWithSign() { $dt = Carbon::createFromDate(2000, 1, 1); - $this->assertSame(-1.0, $dt->diffInYears($dt->copy()->subYear(), false)); + $this->assertSame(-1.0, $dt->diffInYears($dt->copy()->subYear())); } public function testDiffInYearsNegativeNoSign() { $dt = Carbon::createFromDate(2000, 1, 1); - $this->assertSame(1.0, $dt->diffInYears($dt->copy()->subYear())); + $this->assertSame(1.0, $dt->diffInYears($dt->copy()->subYear(), true)); } public function testDiffInYearsVsDefaultNow() @@ -61,6 +66,37 @@ public function testDiffInYearsEnsureIsTruncated() $this->assertSame(1, (int) $dt->diffInYears($dt->copy()->addYear()->addMonths(7))); } + public function testDiffInQuartersPositive() + { + $dt = Carbon::createFromDate(2000, 1, 1); + $this->assertSame(1, (int) $dt->diffInQuarters($dt->copy()->addQuarter()->addDay())); + } + + public function testDiffInQuartersNegativeWithSign() + { + $dt = Carbon::createFromDate(2000, 1, 1); + $this->assertSame(-4, (int) $dt->diffInQuarters($dt->copy()->subQuarters(4))); + } + + public function testDiffInQuartersNegativeWithNoSign() + { + $dt = Carbon::createFromDate(2000, 1, 1); + $this->assertSame(4, (int) $dt->diffInQuarters($dt->copy()->subQuarters(4), true)); + } + + public function testDiffInQuartersVsDefaultNow() + { + $this->wrapWithTestNow(function () { + $this->assertSame(4, (int) Carbon::now()->subYear()->diffInQuarters()); + }); + } + + public function testDiffInQuartersEnsureIsTruncated() + { + $dt = Carbon::createFromDate(2000, 1, 1); + $this->assertSame(1, (int) $dt->diffInQuarters($dt->copy()->addQuarter()->addDays(12))); + } + public function testDiffInMonthsPositive() { $dt = Carbon::createFromDate(2000, 1, 1); @@ -70,13 +106,13 @@ public function testDiffInMonthsPositive() public function testDiffInMonthsNegativeWithSign() { $dt = Carbon::createFromDate(2000, 1, 1); - $this->assertSame(-11, (int) $dt->diffInMonths($dt->copy()->subYear()->addMonth(), false)); + $this->assertSame(-11, (int) $dt->diffInMonths($dt->copy()->subYear()->addMonth())); } public function testDiffInMonthsNegativeNoSign() { $dt = Carbon::createFromDate(2000, 1, 1); - $this->assertSame(11, (int) $dt->diffInMonths($dt->copy()->subYear()->addMonth())); + $this->assertSame(11, (int) $dt->diffInMonths($dt->copy()->subYear()->addMonth(), true)); } public function testDiffInMonthsVsDefaultNow() @@ -101,13 +137,13 @@ public function testDiffInDaysPositive() public function testDiffInDaysNegativeWithSign() { $dt = Carbon::createFromDate(2000, 1, 1); - $this->assertSame(-365.0, $dt->diffInDays($dt->copy()->subYear(), false)); + $this->assertSame(-365.0, $dt->diffInDays($dt->copy()->subYear())); } public function testDiffInDaysNegativeNoSign() { $dt = Carbon::createFromDate(2000, 1, 1); - $this->assertSame(365.0, $dt->diffInDays($dt->copy()->subYear())); + $this->assertSame(365.0, $dt->diffInDays($dt->copy()->subYear(), true)); } public function testDiffInDaysVsDefaultNow() @@ -144,7 +180,7 @@ public function testDiffInDaysFilteredPositiveWithSecondObject() public function testDiffInDaysFilteredNegativeNoSignWithMutated() { $dt = Carbon::createFromDate(2000, 1, 31); - $this->assertSame(5, $dt->diffInDaysFiltered(function (Carbon $date) { + $this->assertSame(-5, $dt->diffInDaysFiltered(function (Carbon $date) { return $date->dayOfWeek === Carbon::SUNDAY; }, $dt->copy()->startOfMonth())); } @@ -156,7 +192,7 @@ public function testDiffInDaysFilteredNegativeNoSignWithSecondObject() $this->assertSame(5, $dt1->diffInDaysFiltered(function (Carbon $date) { return $date->dayOfWeek === Carbon::SUNDAY; - }, $dt2)); + }, $dt2, true)); } public function testDiffInDaysFilteredNegativeWithSignWithMutated() @@ -164,7 +200,7 @@ public function testDiffInDaysFilteredNegativeWithSignWithMutated() $dt = Carbon::createFromDate(2000, 1, 31); $this->assertSame(-5, $dt->diffInDaysFiltered(function (Carbon $date) { return $date->dayOfWeek === 1; - }, $dt->copy()->startOfMonth(), false)); + }, $dt->copy()->startOfMonth())); } public function testDiffInDaysFilteredNegativeWithSignWithSecondObject() @@ -174,7 +210,7 @@ public function testDiffInDaysFilteredNegativeWithSignWithSecondObject() $this->assertSame(-5, $dt1->diffInDaysFiltered(function (Carbon $date) { return $date->dayOfWeek === Carbon::SUNDAY; - }, $dt2, false)); + }, $dt2)); } public function testDiffInHoursFiltered() @@ -182,7 +218,7 @@ public function testDiffInHoursFiltered() $dt1 = Carbon::createFromDate(2000, 1, 31)->endOfDay(); $dt2 = Carbon::createFromDate(2000, 1, 1)->startOfDay(); - $this->assertSame(31, $dt1->diffInHoursFiltered(function (Carbon $date) { + $this->assertSame(-31, $dt1->diffInHoursFiltered(function (Carbon $date) { return $date->hour === 9; }, $dt2)); } @@ -194,7 +230,7 @@ public function testDiffInHoursFilteredNegative() $this->assertSame(-31, $dt1->diffInHoursFiltered(function (Carbon $date) { return $date->hour === 9; - }, $dt2, false)); + }, $dt2)); } public function testDiffInHoursFilteredWorkHoursPerWeek() @@ -202,7 +238,7 @@ public function testDiffInHoursFilteredWorkHoursPerWeek() $dt1 = Carbon::createFromDate(2000, 1, 5)->endOfDay(); $dt2 = Carbon::createFromDate(2000, 1, 1)->startOfDay(); - $this->assertSame(40, $dt1->diffInHoursFiltered(function (Carbon $date) { + $this->assertSame(-40, $dt1->diffInHoursFiltered(function (Carbon $date) { return $date->hour > 8 && $date->hour < 17; }, $dt2)); } @@ -229,7 +265,7 @@ public function testDiffFilteredNegativeNoSignWithMutated() { $dt = Carbon::createFromDate(2000, 1, 31); - $this->assertSame(2, $dt->diffFiltered(CarbonInterval::days(2), function (Carbon $date) { + $this->assertSame(-2, $dt->diffFiltered(CarbonInterval::days(2), function (Carbon $date) { return $date->dayOfWeek === Carbon::SUNDAY; }, $dt->copy()->startOfMonth())); } @@ -239,7 +275,7 @@ public function testDiffFilteredNegativeNoSignWithSecondObject() $dt1 = Carbon::createFromDate(2006, 1, 31); $dt2 = Carbon::createFromDate(2000, 1, 1); - $this->assertSame(7, $dt1->diffFiltered(CarbonInterval::year(), function (Carbon $date) { + $this->assertSame(-7, $dt1->diffFiltered(CarbonInterval::year(), function (Carbon $date) { return $date->month === 1; }, $dt2)); } @@ -308,13 +344,13 @@ public function testDiffInWeekdaysPositive() public function testDiffInWeekdaysNegativeNoSign() { $dt = Carbon::createFromDate(2000, 1, 31); - $this->assertSame(21, $dt->diffInWeekdays($dt->copy()->startOfMonth())); + $this->assertSame(21, $dt->diffInWeekdays($dt->copy()->startOfMonth(), true)); } public function testDiffInWeekdaysNegativeWithSign() { $dt = Carbon::createFromDate(2000, 1, 31); - $this->assertSame(-21, $dt->diffInWeekdays($dt->copy()->startOfMonth(), false)); + $this->assertSame(-21, $dt->diffInWeekdays($dt->copy()->startOfMonth())); } public function testDiffInWeekendDaysPositive() @@ -326,13 +362,13 @@ public function testDiffInWeekendDaysPositive() public function testDiffInWeekendDaysNegativeNoSign() { $dt = Carbon::createFromDate(2000, 1, 31); - $this->assertSame(10, $dt->diffInWeekendDays($dt->copy()->startOfMonth())); + $this->assertSame(10, $dt->diffInWeekendDays($dt->copy()->startOfMonth(), true)); } public function testDiffInWeekendDaysNegativeWithSign() { $dt = Carbon::createFromDate(2000, 1, 31); - $this->assertSame(-10, $dt->diffInWeekendDays($dt->copy()->startOfMonth(), false)); + $this->assertSame(-10, $dt->diffInWeekendDays($dt->copy()->startOfMonth())); } public function testDiffInWeeksPositive() @@ -344,13 +380,13 @@ public function testDiffInWeeksPositive() public function testDiffInWeeksNegativeWithSign() { $dt = Carbon::createFromDate(2000, 1, 1); - $this->assertSame(-52, (int) $dt->diffInWeeks($dt->copy()->subYear(), false)); + $this->assertSame(-52, (int) $dt->diffInWeeks($dt->copy()->subYear())); } public function testDiffInWeeksNegativeNoSign() { $dt = Carbon::createFromDate(2000, 1, 1); - $this->assertSame(52, (int) $dt->diffInWeeks($dt->copy()->subYear())); + $this->assertSame(52, (int) $dt->diffInWeeks($dt->copy()->subYear(), true)); } public function testDiffInWeeksVsDefaultNow() @@ -375,13 +411,13 @@ public function testDiffInHoursPositive() public function testDiffInHoursNegativeWithSign() { $dt = Carbon::createFromDate(2000, 1, 1); - $this->assertSame(-22.0, $dt->diffInHours($dt->copy()->subDay()->addHours(2), false)); + $this->assertSame(-22.0, $dt->diffInHours($dt->copy()->subDay()->addHours(2))); } public function testDiffInHoursNegativeNoSign() { $dt = Carbon::createFromDate(2000, 1, 1); - $this->assertSame(22.0, $dt->diffInHours($dt->copy()->subDay()->addHours(2))); + $this->assertSame(22.0, $dt->diffInHours($dt->copy()->subDay()->addHours(2), true)); } public function testDiffInHoursVsDefaultNow() @@ -405,10 +441,10 @@ public function testDiffInHoursWithTimezones() $dtToronto = Carbon::create(2012, 1, 1, 0, 0, 0, 'America/Toronto'); $dtVancouver = Carbon::create(2012, 1, 1, 0, 0, 0, 'America/Vancouver'); - $this->assertSame(3.0, $dtVancouver->diffInHours($dtToronto), 'Midnight in Toronto is 3 hours from midnight in Vancouver'); + $this->assertSame(-3.0, $dtVancouver->diffInHours($dtToronto), 'Midnight in Toronto is 3 hours from midnight in Vancouver'); $dtToronto = Carbon::createFromDate(2012, 1, 1, 'America/Toronto'); - sleep(2); + usleep(2); $dtVancouver = Carbon::createFromDate(2012, 1, 1, 'America/Vancouver'); $this->assertSame(0, $dtVancouver->diffInHours($dtToronto) % 24); @@ -416,7 +452,7 @@ public function testDiffInHoursWithTimezones() $dtToronto = Carbon::createMidnightDate(2012, 1, 1, 'America/Toronto'); $dtVancouver = Carbon::createMidnightDate(2012, 1, 1, 'America/Vancouver'); - $this->assertSame(3.0, $dtVancouver->diffInHours($dtToronto), 'Midnight in Toronto is 3 hours from midnight in Vancouver'); + $this->assertSame(-3.0, $dtVancouver->diffInHours($dtToronto), 'Midnight in Toronto is 3 hours from midnight in Vancouver'); } public function testDiffInMinutesPositive() @@ -434,13 +470,13 @@ public function testDiffInMinutesPositiveALot() public function testDiffInMinutesNegativeWithSign() { $dt = Carbon::createFromDate(2000, 1, 1); - $this->assertSame(-58.0, $dt->diffInMinutes($dt->copy()->subHour()->addMinutes(2), false)); + $this->assertSame(-58.0, $dt->diffInMinutes($dt->copy()->subHour()->addMinutes(2))); } public function testDiffInMinutesNegativeNoSign() { $dt = Carbon::createFromDate(2000, 1, 1); - $this->assertSame(58.0, $dt->diffInMinutes($dt->copy()->subHour()->addMinutes(2))); + $this->assertSame(58.0, $dt->diffInMinutes($dt->copy()->subHour()->addMinutes(2), true)); } public function testDiffInMinutesVsDefaultNow() @@ -471,13 +507,13 @@ public function testDiffInSecondsPositiveALot() public function testDiffInSecondsNegativeWithSign() { $dt = Carbon::createFromDate(2000, 1, 1); - $this->assertSame(-58.0, $dt->diffInSeconds($dt->copy()->subMinute()->addSeconds(2), false)); + $this->assertSame(-58.0, $dt->diffInSeconds($dt->copy()->subMinute()->addSeconds(2))); } public function testDiffInSecondsNegativeNoSign() { $dt = Carbon::createFromDate(2000, 1, 1); - $this->assertSame(58.0, $dt->diffInSeconds($dt->copy()->subMinute()->addSeconds(2))); + $this->assertSame(58.0, $dt->diffInSeconds($dt->copy()->subMinute()->addSeconds(2), true)); } public function testDiffInSecondsVsDefaultNow() @@ -490,7 +526,7 @@ public function testDiffInSecondsVsDefaultNow() public function testDiffInSecondsEnsureIsTruncated() { $dt = Carbon::createFromDate(2000, 1, 1); - $this->assertSame(1.0, $dt->diffInSeconds($dt->copy()->addSeconds(1.9))); + $this->assertSame(1.0, $dt->diffInSeconds($dt->copy()->addSeconds((int) 1.9))); } public function testDiffInSecondsWithTimezones() @@ -516,6 +552,20 @@ public function testDiffForHumansNowAndSecond() }); } + /** + * @see https://github.com/briannesbitt/Carbon/issues/2136 + */ + public function testDiffInTheFuture() + { + Carbon::setTestNow('2020-07-22 09:15'); + + $this->assertSame( + '1 week from now', + Carbon::parse('2020-07-30 13:51:15') + ->diffForHumans(['options' => CarbonInterface::ROUND]) + ); + } + public function testDiffForHumansNowAndSecondWithTimezone() { $vanNow = Carbon::now('America/Vancouver'); @@ -1186,6 +1236,47 @@ public function testDiffForHumansWithOptions() }); } + public function testDiffForHumansWithMagicMethods() + { + $this->wrapWithTestNow(function () { + $this->assertSame('1 year', Carbon::now()->longAbsoluteDiffForHumans(Carbon::now()->subYears(1)->subMonth())); + $this->assertSame('1 year 1 month', Carbon::now()->longAbsoluteDiffForHumans(2, Carbon::now()->subYears(1)->subMonth())); + $this->assertSame('1 year 1 month', Carbon::now()->longAbsoluteDiffForHumans(Carbon::now()->subYears(1)->subMonth(), 2)); + $this->assertSame('1 year', Carbon::now()->longAbsoluteDiffForHumans(Carbon::now()->subYears(1))); + $this->assertSame('1 year', Carbon::now()->longAbsoluteDiffForHumans(Carbon::now()->addYears(1))); + $this->assertSame('1yr', Carbon::now()->shortAbsoluteDiffForHumans(Carbon::now()->subYears(1))); + $this->assertSame('1yr', Carbon::now()->shortAbsoluteDiffForHumans(Carbon::now()->addYears(1))); + $this->assertSame('1 year after', Carbon::now()->longRelativeDiffForHumans(Carbon::now()->subYears(1))); + $this->assertSame('1 year before', Carbon::now()->longRelativeDiffForHumans(Carbon::now()->addYears(1))); + $this->assertSame('1yr after', Carbon::now()->shortRelativeDiffForHumans(Carbon::now()->subYears(1))); + $this->assertSame('1yr before', Carbon::now()->shortRelativeDiffForHumans(Carbon::now()->addYears(1))); + $this->assertSame('1 year from now', Carbon::now()->longRelativeToNowDiffForHumans(Carbon::now()->subYears(1))); + $this->assertSame('1 year ago', Carbon::now()->longRelativeToNowDiffForHumans(Carbon::now()->addYears(1))); + $this->assertSame('1yr from now', Carbon::now()->shortRelativeToNowDiffForHumans(Carbon::now()->subYears(1))); + $this->assertSame('1yr ago', Carbon::now()->shortRelativeToNowDiffForHumans(Carbon::now()->addYears(1))); + $this->assertSame('1 year after', Carbon::now()->longRelativeToOtherDiffForHumans(Carbon::now()->subYears(1))); + $this->assertSame('1 year before', Carbon::now()->longRelativeToOtherDiffForHumans(Carbon::now()->addYears(1))); + $this->assertSame('1yr after', Carbon::now()->shortRelativeToOtherDiffForHumans(Carbon::now()->subYears(1))); + $this->assertSame('1yr before', Carbon::now()->shortRelativeToOtherDiffForHumans(Carbon::now()->addYears(1))); + $this->assertSame('1 year', Carbon::now()->subYears(1)->longAbsoluteDiffForHumans()); + $this->assertSame('1 year', Carbon::now()->addYears(1)->longAbsoluteDiffForHumans()); + $this->assertSame('1yr', Carbon::now()->subYears(1)->shortAbsoluteDiffForHumans()); + $this->assertSame('1yr', Carbon::now()->addYears(1)->shortAbsoluteDiffForHumans()); + $this->assertSame('1 year ago', Carbon::now()->subYears(1)->longRelativeDiffForHumans()); + $this->assertSame('1 year from now', Carbon::now()->addYears(1)->longRelativeDiffForHumans()); + $this->assertSame('1yr ago', Carbon::now()->subYears(1)->shortRelativeDiffForHumans()); + $this->assertSame('1yr from now', Carbon::now()->addYears(1)->shortRelativeDiffForHumans()); + $this->assertSame('1 year ago', Carbon::now()->subYears(1)->longRelativeToNowDiffForHumans()); + $this->assertSame('1 year from now', Carbon::now()->addYears(1)->longRelativeToNowDiffForHumans()); + $this->assertSame('1yr ago', Carbon::now()->subYears(1)->shortRelativeToNowDiffForHumans()); + $this->assertSame('1yr from now', Carbon::now()->addYears(1)->shortRelativeToNowDiffForHumans()); + $this->assertSame('1 year before', Carbon::now()->subYears(1)->longRelativeToOtherDiffForHumans()); + $this->assertSame('1 year after', Carbon::now()->addYears(1)->longRelativeToOtherDiffForHumans()); + $this->assertSame('1yr before', Carbon::now()->subYears(1)->shortRelativeToOtherDiffForHumans()); + $this->assertSame('1yr after', Carbon::now()->addYears(1)->shortRelativeToOtherDiffForHumans()); + }); + } + public function testDiffForHumansWithShorterMonthShouldStillBeAMonth() { $feb15 = Carbon::parse('2015-02-15'); @@ -1195,7 +1286,7 @@ public function testDiffForHumansWithShorterMonthShouldStillBeAMonth() public function testDiffForHumansWithDateTimeInstance() { - $feb15 = new \DateTime('2015-02-15'); + $feb15 = new DateTime('2015-02-15'); $mar15 = Carbon::parse('2015-03-15'); $this->assertSame('1 month after', $mar15->diffForHumans($feb15)); } @@ -1216,16 +1307,16 @@ public function testDiffWithString() { $dt1 = Carbon::createFromDate(2000, 1, 25)->endOfDay(); - $this->assertSame(384.0, round($dt1->diffInHours('2000-01-10'))); - $this->assertSame(383.0, floor($dt1->diffInHours('2000-01-10'))); + $this->assertSame(384.0, round($dt1->diffInHours('2000-01-10', true))); + $this->assertSame(383.0, floor($dt1->diffInHours('2000-01-10', true))); } public function testDiffWithDateTime() { $dt1 = Carbon::createFromDate(2000, 1, 25)->endOfDay(); - $dt2 = new \DateTime('2000-01-10'); + $dt2 = new DateTime('2000-01-10'); - $this->assertSame(383, (int) $dt1->diffInHours($dt2)); + $this->assertSame(-384.0, round($dt1->diffInHours($dt2))); } public function testDiffOptions() @@ -1372,6 +1463,10 @@ public function testDiffForHumansArrayParameter() { Carbon::setTestNow('2000-01-01 00:00:00'); $date = Carbon::now()->subtract('2 days, 3 hours and 40 minutes'); + $this->assertSame('2 days ago', $date->diffForHumans([ + 'parts' => 1, + 'join' => true, + ])); $this->assertSame('2 days and 3 hours ago', $date->diffForHumans([ 'parts' => 2, 'join' => true, @@ -1506,7 +1601,7 @@ public function testUntil() public function testDiffWithInvalidType() { - $this->expectException(\InvalidArgumentException::class); + $this->expectException(InvalidArgumentException::class); $this->expectExceptionMessage( 'Expected null, string, DateTime or DateTimeInterface, integer given' ); @@ -1516,7 +1611,7 @@ public function testDiffWithInvalidType() public function testDiffWithInvalidObject() { - $this->expectException(\InvalidArgumentException::class); + $this->expectException(InvalidArgumentException::class); $this->expectExceptionMessage( 'Expected null, string, DateTime or DateTimeInterface, Carbon\CarbonInterval given' ); @@ -1526,7 +1621,7 @@ public function testDiffWithInvalidObject() public function testDiffForHumansWithIncorrectDateTimeStringWhichIsNotACarbonInstance() { - $this->expectException(\Exception::class); + $this->expectException(Exception::class); $this->expectExceptionMessage( 'Failed to parse time string (2018-04-13-08:00:00) at position 16' ); @@ -1543,80 +1638,80 @@ public function testFloatDiff() $this->assertSame(1.0006944444444443, Carbon::parse('2018-12-01 00:00')->floatDiffInDays(Carbon::parse('2018-12-02 00:01'))); + $this->assertSame(0.9714742503779745, Carbon::parse('2018-03-13 20:55:12.321456')->floatDiffInMonths(Carbon::parse('2018-04-12 14:24:58.987421'), true)); + $this->assertSame(0.9714742503779745, Carbon::parse('2018-04-12 14:24:58.987421')->floatDiffInMonths(Carbon::parse('2018-03-13 20:55:12.321456'), true)); $this->assertSame(0.9714742503779745, Carbon::parse('2018-03-13 20:55:12.321456')->floatDiffInMonths(Carbon::parse('2018-04-12 14:24:58.987421'))); - $this->assertSame(0.9714742503779745, Carbon::parse('2018-04-12 14:24:58.987421')->floatDiffInMonths(Carbon::parse('2018-03-13 20:55:12.321456'))); - $this->assertSame(0.9714742503779745, Carbon::parse('2018-03-13 20:55:12.321456')->floatDiffInMonths(Carbon::parse('2018-04-12 14:24:58.987421'), false)); - $this->assertSame(-0.9714742503779745, Carbon::parse('2018-04-12 14:24:58.987421')->floatDiffInMonths(Carbon::parse('2018-03-13 20:55:12.321456'), false)); + $this->assertSame(-0.9714742503779745, Carbon::parse('2018-04-12 14:24:58.987421')->floatDiffInMonths(Carbon::parse('2018-03-13 20:55:12.321456'))); - $this->assertSame(16.557633744585264, Carbon::parse('2018-02-13 20:55:12.321456')->floatDiffInMonths(Carbon::parse('2019-06-30 14:24:58.987421'))); + $this->assertSame(16.557633744585264, Carbon::parse('2018-02-13 20:55:12.321456')->floatDiffInMonths(Carbon::parse('2019-06-30 14:24:58.987421'), true)); + $this->assertSame(15.971474250377973, Carbon::parse('2018-02-13 20:55:12.321456')->floatDiffInMonths(Carbon::parse('2019-06-12 14:24:58.987421'), true)); + $this->assertSame(15.971474250377973, Carbon::parse('2019-06-12 14:24:58.987421')->floatDiffInMonths(Carbon::parse('2018-02-13 20:55:12.321456'), true)); $this->assertSame(15.971474250377973, Carbon::parse('2018-02-13 20:55:12.321456')->floatDiffInMonths(Carbon::parse('2019-06-12 14:24:58.987421'))); - $this->assertSame(15.971474250377973, Carbon::parse('2019-06-12 14:24:58.987421')->floatDiffInMonths(Carbon::parse('2018-02-13 20:55:12.321456'))); - $this->assertSame(15.971474250377973, Carbon::parse('2018-02-13 20:55:12.321456')->floatDiffInMonths(Carbon::parse('2019-06-12 14:24:58.987421'), false)); - $this->assertSame(-15.971474250377973, Carbon::parse('2019-06-12 14:24:58.987421')->floatDiffInMonths(Carbon::parse('2018-02-13 20:55:12.321456'), false)); + $this->assertSame(-15.971474250377973, Carbon::parse('2019-06-12 14:24:58.987421')->floatDiffInMonths(Carbon::parse('2018-02-13 20:55:12.321456'))); - $this->assertSame(1.0, Carbon::parse('2018-02-13 20:55:12.321456')->floatDiffInYears(Carbon::parse('2019-02-13 20:55:12.321456'))); - $this->assertSame(1.3746000338015283, Carbon::parse('2018-02-13 20:55:12.321456')->floatDiffInYears(Carbon::parse('2019-06-30 14:24:58.987421'))); - $this->assertSame(0.9609014036645421, Carbon::parse('2018-02-13 20:55:12.321456')->floatDiffInYears(Carbon::parse('2019-01-30 14:24:58.987421'))); + $this->assertSame(1.0, Carbon::parse('2018-02-13 20:55:12.321456')->floatDiffInYears(Carbon::parse('2019-02-13 20:55:12.321456'), true)); + $this->assertSame(1.3746000338015283, Carbon::parse('2018-02-13 20:55:12.321456')->floatDiffInYears(Carbon::parse('2019-06-30 14:24:58.987421'), true)); + $this->assertSame(0.9609014036645421, Carbon::parse('2018-02-13 20:55:12.321456')->floatDiffInYears(Carbon::parse('2019-01-30 14:24:58.987421'), true)); + $this->assertSame(1.3252849653083778, Carbon::parse('2018-02-13 20:55:12.321456')->floatDiffInYears(Carbon::parse('2019-06-12 14:24:58.987421'), true)); + $this->assertSame(1.3252849653083778, Carbon::parse('2019-06-12 14:24:58.987421')->floatDiffInYears(Carbon::parse('2018-02-13 20:55:12.321456'), true)); $this->assertSame(1.3252849653083778, Carbon::parse('2018-02-13 20:55:12.321456')->floatDiffInYears(Carbon::parse('2019-06-12 14:24:58.987421'))); - $this->assertSame(1.3252849653083778, Carbon::parse('2019-06-12 14:24:58.987421')->floatDiffInYears(Carbon::parse('2018-02-13 20:55:12.321456'))); - $this->assertSame(1.3252849653083778, Carbon::parse('2018-02-13 20:55:12.321456')->floatDiffInYears(Carbon::parse('2019-06-12 14:24:58.987421'), false)); - $this->assertSame(-1.3252849653083778, Carbon::parse('2019-06-12 14:24:58.987421')->floatDiffInYears(Carbon::parse('2018-02-13 20:55:12.321456'), false)); + $this->assertSame(-1.3252849653083778, Carbon::parse('2019-06-12 14:24:58.987421')->floatDiffInYears(Carbon::parse('2018-02-13 20:55:12.321456'))); + $this->assertSame(5.325284965308378, Carbon::parse('2018-02-13 20:55:12.321456')->floatDiffInYears(Carbon::parse('2023-06-12 14:24:58.987421'), true)); + $this->assertSame(5.325284965308378, Carbon::parse('2023-06-12 14:24:58.987421')->floatDiffInYears(Carbon::parse('2018-02-13 20:55:12.321456'), true)); $this->assertSame(5.325284965308378, Carbon::parse('2018-02-13 20:55:12.321456')->floatDiffInYears(Carbon::parse('2023-06-12 14:24:58.987421'))); - $this->assertSame(5.325284965308378, Carbon::parse('2023-06-12 14:24:58.987421')->floatDiffInYears(Carbon::parse('2018-02-13 20:55:12.321456'))); - $this->assertSame(5.325284965308378, Carbon::parse('2018-02-13 20:55:12.321456')->floatDiffInYears(Carbon::parse('2023-06-12 14:24:58.987421'), false)); - $this->assertSame(-5.325284965308378, Carbon::parse('2023-06-12 14:24:58.987421')->floatDiffInYears(Carbon::parse('2018-02-13 20:55:12.321456'), false)); + $this->assertSame(-5.325284965308378, Carbon::parse('2023-06-12 14:24:58.987421')->floatDiffInYears(Carbon::parse('2018-02-13 20:55:12.321456'))); - $this->assertSame(1.0, Carbon::parse('2018-10-01 00:00:00', 'Europe/Paris')->floatDiffInMonths(Carbon::parse('2018-11-01 00:00:00', 'Europe/Paris'))); - $this->assertSame(1.0, Carbon::parse('2018-10-28 00:00:00')->floatDiffInMonths(Carbon::parse('2018-11-28 00:00:00'))); - $this->assertSame(1.0, Carbon::parse('2018-10-28 00:00:00', 'Europe/Paris')->floatDiffInMonths(Carbon::parse('2018-11-28 00:00:00', 'Europe/Paris'))); + $this->assertSame(1.0, Carbon::parse('2018-10-01 00:00:00', 'Europe/Paris')->floatDiffInMonths(Carbon::parse('2018-11-01 00:00:00', 'Europe/Paris'), true)); + $this->assertSame(1.0, Carbon::parse('2018-10-28 00:00:00')->floatDiffInMonths(Carbon::parse('2018-11-28 00:00:00'), true)); + $this->assertSame(1.0, Carbon::parse('2018-10-28 00:00:00', 'Europe/Paris')->floatDiffInMonths(Carbon::parse('2018-11-28 00:00:00', 'Europe/Paris'), true)); - $this->assertSame(-1.0, Carbon::parse('2018-11-01 00:00:00', 'Europe/Paris')->floatDiffInMonths(Carbon::parse('2018-10-01 00:00:00', 'Europe/Paris'), false)); - $this->assertSame(-1.0, Carbon::parse('2018-11-28 00:00:00')->floatDiffInMonths(Carbon::parse('2018-10-28 00:00:00'), false)); - $this->assertSame(-1.0, Carbon::parse('2018-11-28 00:00:00', 'Europe/Paris')->floatDiffInMonths(Carbon::parse('2018-10-28 00:00:00', 'Europe/Paris'), false)); + $this->assertSame(-1.0, Carbon::parse('2018-11-01 00:00:00', 'Europe/Paris')->floatDiffInMonths(Carbon::parse('2018-10-01 00:00:00', 'Europe/Paris'))); + $this->assertSame(-1.0, Carbon::parse('2018-11-28 00:00:00')->floatDiffInMonths(Carbon::parse('2018-10-28 00:00:00'))); + $this->assertSame(-1.0, Carbon::parse('2018-11-28 00:00:00', 'Europe/Paris')->floatDiffInMonths(Carbon::parse('2018-10-28 00:00:00', 'Europe/Paris'))); } public function testFloatDiffWithRealUnits() { date_default_timezone_set('UTC'); - $this->assertSame(1.0006944444444443, Carbon::parse('2018-12-01 00:00')->floatDiffInRealDays(Carbon::parse('2018-12-02 00:01'))); + $this->assertSame(1.0006944444444443, Carbon::parse('2018-12-01 00:00')->floatDiffInRealDays(Carbon::parse('2018-12-02 00:01'), true)); + $this->assertSame(0.9714742503779745, Carbon::parse('2018-03-13 20:55:12.321456')->floatDiffInRealMonths(Carbon::parse('2018-04-12 14:24:58.987421'), true)); + $this->assertSame(0.9714742503779745, Carbon::parse('2018-04-12 14:24:58.987421')->floatDiffInRealMonths(Carbon::parse('2018-03-13 20:55:12.321456'), true)); $this->assertSame(0.9714742503779745, Carbon::parse('2018-03-13 20:55:12.321456')->floatDiffInRealMonths(Carbon::parse('2018-04-12 14:24:58.987421'))); - $this->assertSame(0.9714742503779745, Carbon::parse('2018-04-12 14:24:58.987421')->floatDiffInRealMonths(Carbon::parse('2018-03-13 20:55:12.321456'))); - $this->assertSame(0.9714742503779745, Carbon::parse('2018-03-13 20:55:12.321456')->floatDiffInRealMonths(Carbon::parse('2018-04-12 14:24:58.987421'), false)); - $this->assertSame(-0.9714742503779745, Carbon::parse('2018-04-12 14:24:58.987421')->floatDiffInRealMonths(Carbon::parse('2018-03-13 20:55:12.321456'), false)); + $this->assertSame(-0.9714742503779745, Carbon::parse('2018-04-12 14:24:58.987421')->floatDiffInRealMonths(Carbon::parse('2018-03-13 20:55:12.321456'))); - $this->assertSame(16.557633744585264, Carbon::parse('2018-02-13 20:55:12.321456')->floatDiffInRealMonths(Carbon::parse('2019-06-30 14:24:58.987421'))); + $this->assertSame(16.557633744585264, Carbon::parse('2018-02-13 20:55:12.321456')->floatDiffInRealMonths(Carbon::parse('2019-06-30 14:24:58.987421'), true)); + $this->assertSame(15.971474250377973, Carbon::parse('2018-02-13 20:55:12.321456')->floatDiffInRealMonths(Carbon::parse('2019-06-12 14:24:58.987421'), true)); + $this->assertSame(15.971474250377973, Carbon::parse('2019-06-12 14:24:58.987421')->floatDiffInRealMonths(Carbon::parse('2018-02-13 20:55:12.321456'), true)); $this->assertSame(15.971474250377973, Carbon::parse('2018-02-13 20:55:12.321456')->floatDiffInRealMonths(Carbon::parse('2019-06-12 14:24:58.987421'))); - $this->assertSame(15.971474250377973, Carbon::parse('2019-06-12 14:24:58.987421')->floatDiffInRealMonths(Carbon::parse('2018-02-13 20:55:12.321456'))); - $this->assertSame(15.971474250377973, Carbon::parse('2018-02-13 20:55:12.321456')->floatDiffInRealMonths(Carbon::parse('2019-06-12 14:24:58.987421'), false)); - $this->assertSame(-15.971474250377973, Carbon::parse('2019-06-12 14:24:58.987421')->floatDiffInRealMonths(Carbon::parse('2018-02-13 20:55:12.321456'), false)); + $this->assertSame(-15.971474250377973, Carbon::parse('2019-06-12 14:24:58.987421')->floatDiffInRealMonths(Carbon::parse('2018-02-13 20:55:12.321456'))); - $this->assertSame(1.0, Carbon::parse('2018-02-13 20:55:12.321456')->floatDiffInRealYears(Carbon::parse('2019-02-13 20:55:12.321456'))); - $this->assertSame(1.3746000338015283, Carbon::parse('2018-02-13 20:55:12.321456')->floatDiffInRealYears(Carbon::parse('2019-06-30 14:24:58.987421'))); - $this->assertSame(0.9609014036645421, Carbon::parse('2018-02-13 20:55:12.321456')->floatDiffInRealYears(Carbon::parse('2019-01-30 14:24:58.987421'))); + $this->assertSame(1.0, Carbon::parse('2018-02-13 20:55:12.321456')->floatDiffInRealYears(Carbon::parse('2019-02-13 20:55:12.321456'), true)); + $this->assertSame(1.3746000338015283, Carbon::parse('2018-02-13 20:55:12.321456')->floatDiffInRealYears(Carbon::parse('2019-06-30 14:24:58.987421'), true)); + $this->assertSame(0.9609014036645421, Carbon::parse('2018-02-13 20:55:12.321456')->floatDiffInRealYears(Carbon::parse('2019-01-30 14:24:58.987421'), true)); + $this->assertSame(1.3252849653083778, Carbon::parse('2018-02-13 20:55:12.321456')->floatDiffInRealYears(Carbon::parse('2019-06-12 14:24:58.987421'), true)); + $this->assertSame(1.3252849653083778, Carbon::parse('2019-06-12 14:24:58.987421')->floatDiffInRealYears(Carbon::parse('2018-02-13 20:55:12.321456'), true)); $this->assertSame(1.3252849653083778, Carbon::parse('2018-02-13 20:55:12.321456')->floatDiffInRealYears(Carbon::parse('2019-06-12 14:24:58.987421'))); - $this->assertSame(1.3252849653083778, Carbon::parse('2019-06-12 14:24:58.987421')->floatDiffInRealYears(Carbon::parse('2018-02-13 20:55:12.321456'))); - $this->assertSame(1.3252849653083778, Carbon::parse('2018-02-13 20:55:12.321456')->floatDiffInRealYears(Carbon::parse('2019-06-12 14:24:58.987421'), false)); - $this->assertSame(-1.3252849653083778, Carbon::parse('2019-06-12 14:24:58.987421')->floatDiffInRealYears(Carbon::parse('2018-02-13 20:55:12.321456'), false)); + $this->assertSame(-1.3252849653083778, Carbon::parse('2019-06-12 14:24:58.987421')->floatDiffInRealYears(Carbon::parse('2018-02-13 20:55:12.321456'))); + $this->assertSame(5.325284965308378, Carbon::parse('2018-02-13 20:55:12.321456')->floatDiffInRealYears(Carbon::parse('2023-06-12 14:24:58.987421'), true)); + $this->assertSame(5.325284965308378, Carbon::parse('2023-06-12 14:24:58.987421')->floatDiffInRealYears(Carbon::parse('2018-02-13 20:55:12.321456'), true)); $this->assertSame(5.325284965308378, Carbon::parse('2018-02-13 20:55:12.321456')->floatDiffInRealYears(Carbon::parse('2023-06-12 14:24:58.987421'))); - $this->assertSame(5.325284965308378, Carbon::parse('2023-06-12 14:24:58.987421')->floatDiffInRealYears(Carbon::parse('2018-02-13 20:55:12.321456'))); - $this->assertSame(5.325284965308378, Carbon::parse('2018-02-13 20:55:12.321456')->floatDiffInRealYears(Carbon::parse('2023-06-12 14:24:58.987421'), false)); - $this->assertSame(-5.325284965308378, Carbon::parse('2023-06-12 14:24:58.987421')->floatDiffInRealYears(Carbon::parse('2018-02-13 20:55:12.321456'), false)); + $this->assertSame(-5.325284965308378, Carbon::parse('2023-06-12 14:24:58.987421')->floatDiffInRealYears(Carbon::parse('2018-02-13 20:55:12.321456'))); - $this->assertSame(1.0, Carbon::parse('2018-10-01 00:00:00', 'Europe/Paris')->floatDiffInRealMonths(Carbon::parse('2018-11-01 00:00:00', 'Europe/Paris'))); - $this->assertSame(1.0, Carbon::parse('2018-10-28 00:00:00')->floatDiffInRealMonths(Carbon::parse('2018-11-28 00:00:00'))); - $this->assertSame(1.0, Carbon::parse('2018-10-28 00:00:00', 'Europe/Paris')->floatDiffInRealMonths(Carbon::parse('2018-11-28 00:00:00', 'Europe/Paris'))); + $this->assertSame(1.0, Carbon::parse('2018-10-01 00:00:00', 'Europe/Paris')->floatDiffInRealMonths(Carbon::parse('2018-11-01 00:00:00', 'Europe/Paris'), true)); + $this->assertSame(1.0, Carbon::parse('2018-10-28 00:00:00')->floatDiffInRealMonths(Carbon::parse('2018-11-28 00:00:00'), true)); + $this->assertSame(1.0, Carbon::parse('2018-10-28 00:00:00', 'Europe/Paris')->floatDiffInRealMonths(Carbon::parse('2018-11-28 00:00:00', 'Europe/Paris'), true)); - $this->assertSame(-1.0, Carbon::parse('2018-11-01 00:00:00', 'Europe/Paris')->floatDiffInRealMonths(Carbon::parse('2018-10-01 00:00:00', 'Europe/Paris'), false)); - $this->assertSame(-1.0, Carbon::parse('2018-11-28 00:00:00')->floatDiffInRealMonths(Carbon::parse('2018-10-28 00:00:00'), false)); - $this->assertSame(-1.0, Carbon::parse('2018-11-28 00:00:00', 'Europe/Paris')->floatDiffInRealMonths(Carbon::parse('2018-10-28 00:00:00', 'Europe/Paris'), false)); + $this->assertSame(-1.0, Carbon::parse('2018-11-01 00:00:00', 'Europe/Paris')->floatDiffInRealMonths(Carbon::parse('2018-10-01 00:00:00', 'Europe/Paris'))); + $this->assertSame(-1.0, Carbon::parse('2018-11-28 00:00:00')->floatDiffInRealMonths(Carbon::parse('2018-10-28 00:00:00'))); + $this->assertSame(-1.0, Carbon::parse('2018-11-28 00:00:00', 'Europe/Paris')->floatDiffInRealMonths(Carbon::parse('2018-10-28 00:00:00', 'Europe/Paris'))); } /** @@ -1625,7 +1720,7 @@ public function testFloatDiffWithRealUnits() */ public function testPhpBug77007() { - $this->assertSame(3.0, Carbon::now()->addMinutes(3)->diffInMinutes()); + $this->assertSame(-3.0, Carbon::now()->addMinutes(3)->diffInMinutes()); $startDate = Carbon::parse('2018-10-11 20:59:06.914653'); $endDate = Carbon::parse('2018-10-11 20:59:07.237419'); @@ -1642,7 +1737,7 @@ public function testPhpBug77007() $endDate = Carbon::parse('2018-10-11 20:59:05.237419'); $this->assertSame('+ 00-00-00 00:00:01.677234', $startDate->diff($endDate, true)->format('%R %Y-%M-%D %H:%I:%S.%F')); - $this->assertSame(1.677234, $startDate->diffInSeconds($endDate)); + $this->assertSame(1.677234, $startDate->diffInSeconds($endDate, true)); $this->assertSame('- 00-00-00 00:00:01.677234', $startDate->diff($endDate)->format('%R %Y-%M-%D %H:%I:%S.%F')); $this->assertSame(-1.677234, $startDate->diffInSeconds($endDate, false)); @@ -1651,7 +1746,7 @@ public function testPhpBug77007() $endDate = Carbon::parse('2018-10-11 20:59:06.237419'); $this->assertSame('+ 00-00-00 00:00:00.677234', $startDate->diff($endDate, true)->format('%R %Y-%M-%D %H:%I:%S.%F')); - $this->assertSame(0.677234, $startDate->diffInSeconds($endDate)); + $this->assertSame(0.677234, $startDate->diffInSeconds($endDate, true)); $this->assertSame('- 00-00-00 00:00:00.677234', $startDate->diff($endDate)->format('%R %Y-%M-%D %H:%I:%S.%F')); $this->assertSame(-0.677234, $startDate->diffInSeconds($endDate, false)); @@ -1668,12 +1763,12 @@ public function testDiffWithZeroAndNonZeroMicroseconds() $requestTime = new Carbon('2018-11-14 18:23:12.0 +00:00'); $serverTime = new Carbon('2018-11-14 18:23:12.307628 +00:00'); - $this->assertSame(0.307628, $serverTime->diffInSeconds($requestTime)); + $this->assertSame(-0.307628, $serverTime->diffInSeconds($requestTime)); $requestTime = new Carbon('2019-02-10 18:23:12.0 +00:00'); $serverTime = new Carbon('2019-02-10 18:23:12.307628 +00:00'); - $this->assertSame(0.307628, $serverTime->diffInSeconds($requestTime)); + $this->assertSame(-0.307628, $serverTime->diffInSeconds($requestTime)); } public function testNearlyFullDayDiffInSeconds() @@ -1681,7 +1776,7 @@ public function testNearlyFullDayDiffInSeconds() $d1 = Carbon::parse('2019-06-15 12:34:56.123456'); $d2 = Carbon::parse('2019-06-16 12:34:56.123455'); - $this->assertSame(86399.999999, $d2->diffInSeconds($d1)); + $this->assertSame(-86399.999999, $d2->diffInSeconds($d1)); } public function testNearlyFullDayDiffInMicroseconds() @@ -1689,7 +1784,7 @@ public function testNearlyFullDayDiffInMicroseconds() $d1 = Carbon::parse('2019-06-15 12:34:56.123456'); $d2 = Carbon::parse('2019-06-16 12:34:56.123455'); - $this->assertSame(86399999999.0, $d2->diffInMicroseconds($d1)); + $this->assertSame(-86399999999.0, $d2->diffInMicroseconds($d1)); } public function testExactMonthDiffInSeconds() @@ -1697,6 +1792,18 @@ public function testExactMonthDiffInSeconds() $d1 = Carbon::make('2019-01-23 12:00:00'); $d2 = Carbon::make('2019-02-23 12:00:00'); - $this->assertSame(2678400.0, $d2->diffInSeconds($d1)); + $this->assertSame(-2678400.0, $d2->diffInSeconds($d1)); + } + + public function testDiffInUnit() + { + $this->assertSame(5.5, Carbon::make('2020-08-13 05:00')->diffInUnit('hour', '2020-08-13 10:30')); + } + + public function testDiffInUnitException() + { + $this->expectException(UnknownUnitException::class); + $this->expectExceptionMessage("Unknown unit 'moons'."); + $this->assertSame(5.5, Carbon::make('2020-08-13 05:00')->diffInUnit('moon', '2020-08-13 10:30')); } } From 1320852f0a22d1b84bf24f5db579f9049c3a0d26 Mon Sep 17 00:00:00 2001 From: KyleKatarn Date: Thu, 13 Aug 2020 18:31:53 +0200 Subject: [PATCH 38/38] Fix unit tests --- tests/Carbon/ModifyTest.php | 14 +++++++------- tests/CarbonImmutable/ModifyTest.php | 14 +++++++------- 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/tests/Carbon/ModifyTest.php b/tests/Carbon/ModifyTest.php index 33614de649..5230b3c4cc 100644 --- a/tests/Carbon/ModifyTest.php +++ b/tests/Carbon/ModifyTest.php @@ -31,13 +31,13 @@ public function testTimezoneModify() $a = new Carbon('2014-03-30 00:00:00', 'Europe/London'); $b = $a->copy(); $b->addHours(24); - $this->assertSame(23.0, $a->diffInRealHours($b, false)); - $this->assertSame(23.0, $b->diffInRealHours($a)); - $this->assertSame(-23.0, $b->diffInRealHours($a, false)); - $this->assertSame(-23.0 * 60, $b->diffInRealMinutes($a, false)); - $this->assertSame(-23.0 * 60 * 60, $b->diffInRealSeconds($a, false)); - $this->assertSame(-23.0 * 60 * 60 * 1000, $b->diffInRealMilliseconds($a, false)); - $this->assertSame(-23.0 * 60 * 60 * 1000000, $b->diffInRealMicroseconds($a, false)); + $this->assertSame(23.0, $a->diffInRealHours($b)); + $this->assertSame(23.0, $b->diffInRealHours($a, true)); + $this->assertSame(-23.0, $b->diffInRealHours($a)); + $this->assertSame(-23.0 * 60, $b->diffInRealMinutes($a)); + $this->assertSame(-23.0 * 60 * 60, $b->diffInRealSeconds($a)); + $this->assertSame(-23.0 * 60 * 60 * 1000, $b->diffInRealMilliseconds($a)); + $this->assertSame(-23.0 * 60 * 60 * 1000000, $b->diffInRealMicroseconds($a)); $a = new Carbon('2014-03-30 00:00:00', 'Europe/London'); $b = $a->copy(); diff --git a/tests/CarbonImmutable/ModifyTest.php b/tests/CarbonImmutable/ModifyTest.php index d902b15353..b5211d4891 100644 --- a/tests/CarbonImmutable/ModifyTest.php +++ b/tests/CarbonImmutable/ModifyTest.php @@ -29,13 +29,13 @@ public function testTimezoneModify() $a = new Carbon('2014-03-30 00:00:00', 'Europe/London'); $b = $a->addHours(24); - $this->assertSame(23.0, $a->diffInHours($b, false)); - $this->assertSame(23.0, $b->diffInHours($a)); - $this->assertSame(-23.0, $b->diffInHours($a, false)); - $this->assertSame(-23.0 * 60, $b->diffInMinutes($a, false)); - $this->assertSame(-23.0 * 60 * 60, $b->diffInSeconds($a, false)); - $this->assertSame(-23.0 * 60 * 60 * 1000, $b->diffInMilliseconds($a, false)); - $this->assertSame(-23.0 * 60 * 60 * 1000000, $b->diffInMicroseconds($a, false)); + $this->assertSame(23.0, $a->diffInHours($b)); + $this->assertSame(23.0, $b->diffInHours($a, true)); + $this->assertSame(-23.0, $b->diffInHours($a)); + $this->assertSame(-23.0 * 60, $b->diffInMinutes($a)); + $this->assertSame(-23.0 * 60 * 60, $b->diffInSeconds($a)); + $this->assertSame(-23.0 * 60 * 60 * 1000, $b->diffInMilliseconds($a)); + $this->assertSame(-23.0 * 60 * 60 * 1000000, $b->diffInMicroseconds($a)); $a = new Carbon('2014-03-30 00:00:00', 'Europe/London'); $b = $a->addRealHours(24);