[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
This commit is contained in:
Julien Mougenot
2023-05-26 11:40:46 +02:00
parent ddc98a8cd8
commit a563edc696
+6 -48
View File
@@ -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
//-----------------------------------------------------------------------------