From a563edc6966e7bedf90fc1bee1497c76c7bc9746 Mon Sep 17 00:00:00 2001 From: Julien Mougenot Date: Mon, 22 May 2023 14:01:37 +0000 Subject: [PATCH] [REM] web: Remove local fast date comparison in date utils This commit removes a function from the `@web/core/l10n/dates` file which was designed to save some time when comparing 2 dates. The existence of this function was justified by the fact that Luxon would allegedly perform expensive and needless transformations to convert a date to a number (which is the case when comparing date objects). However it appeared that it was a mistake on my part, as I actually inspected the wrong class method: I checked the `valueOf` implementation of the `Duration` class, not the `DateTime` class. X-original-commit: 995a1ab49bdd9e63b4f1027bbd723037c5045d21 Part-of: odoo/odoo#122564 --- addons/web/static/src/core/l10n/dates.js | 54 +++--------------------- 1 file changed, 6 insertions(+), 48 deletions(-) diff --git a/addons/web/static/src/core/l10n/dates.js b/addons/web/static/src/core/l10n/dates.js index 826205baf36..0ef4789aee4 100644 --- a/addons/web/static/src/core/l10n/dates.js +++ b/addons/web/static/src/core/l10n/dates.js @@ -120,10 +120,10 @@ export function areDatesEqual(d1, d2) { * @param {DateTime} maxDate */ export function clampDate(desired, minDate, maxDate) { - if (_fastLocalDateCompare(maxDate, "<", desired)) { + if (maxDate < desired) { return maxDate; } - if (_fastLocalDateCompare(minDate, ">", desired)) { + if (minDate > desired) { return minDate; } return desired; @@ -142,6 +142,7 @@ export function is24HourFormat(format) { /** * @param {NullableDateTime | NullableDateRange} value * @param {NullableDateRange} range + * @returns {boolean} */ export function isInRange(value, range) { if (!value || !range) { @@ -153,16 +154,11 @@ export function isInRange(value, range) { return isInRange(actualValues[0], range); } return ( - (_fastLocalDateCompare(value[0], "<=", range[0]) && - _fastLocalDateCompare(range[0], "<=", value[1])) || - (_fastLocalDateCompare(range[0], "<=", value[0]) && - _fastLocalDateCompare(value[0], "<=", range[1])) + (value[0] <= range[0] && range[0] <= value[1]) || + (range[0] <= value[0] && value[0] <= range[1]) ); } else { - return ( - _fastLocalDateCompare(range[0], "<=", value) && - _fastLocalDateCompare(value, "<=", range[1]) - ); + return range[0] <= value && value <= range[1]; } } @@ -266,44 +262,6 @@ export function today() { return DateTime.local().startOf("day"); } -/** - * Fast comparison of local dates. - * This is faster than Luxon's internal number conversions but only works on DateTime - * objects sharing the exact same locale and timezone. - * - * @private - * @param {NullableDateTime} d1 - * @param {"<" | "<=" | ">" | ">=" | "=" | "!="} operator - * @param {NullableDateTime} d2 - */ -function _fastLocalDateCompare(d1, operator, d2) { - if (!d1 || !d2) { - return false; - } - const a = d1.ts; - const b = d2.ts; - switch (operator) { - case "<": { - return a < b; - } - case "<=": { - return a <= b; - } - case ">": { - return a > b; - } - case ">=": { - return a >= b; - } - case "=": { - return a === b; - } - case "!=": { - return a !== b; - } - } -} - //----------------------------------------------------------------------------- // Formatting //-----------------------------------------------------------------------------