[REF] *:replace moment usages with luxon
luxon and moment are both used in the solution, but these two libraries facilitate the manipulation of dates. It was decided to replace all uses of moment with luxon so we can then remove moment.js from the code and lighten the assets. task-3391739 closes odoo/odoo#134470 Related: odoo/enterprise#46987 Signed-off-by: Samuel Degueldre (sad) <sad@odoo.com>
This commit is contained in:
@@ -161,7 +161,6 @@
|
||||
'web/static/src/legacy/legacy_setup.js',
|
||||
'web/static/src/legacy/backend_utils.js',
|
||||
'web/static/src/legacy/utils.js',
|
||||
'web/static/src/legacy/js/core/dates.js',
|
||||
'web/static/src/legacy/js/env.js',
|
||||
'web/static/src/legacy/js/owl_compatibility.js',
|
||||
|
||||
|
||||
@@ -214,7 +214,6 @@
|
||||
'web/static/src/legacy/legacy_setup.js',
|
||||
'web/static/src/legacy/backend_utils.js',
|
||||
'web/static/src/legacy/utils.js',
|
||||
'web/static/src/legacy/js/core/dates.js',
|
||||
'web/static/src/legacy/js/env.js',
|
||||
'web/static/src/legacy/js/owl_compatibility.js',
|
||||
|
||||
|
||||
@@ -150,7 +150,6 @@ This module provides the core of the Odoo Web Client.
|
||||
'web/static/src/legacy/legacy_rpc_error_handler.js',
|
||||
'web/static/src/legacy/backend_utils.js',
|
||||
'web/static/src/legacy/utils.js',
|
||||
'web/static/src/legacy/js/core/dates.js',
|
||||
'web/static/src/legacy/js/env.js',
|
||||
'web/static/src/legacy/js/owl_compatibility.js',
|
||||
|
||||
@@ -267,7 +266,6 @@ This module provides the core of the Odoo Web Client.
|
||||
'web/static/src/libs/pdfjs.js',
|
||||
|
||||
'web/static/src/legacy/utils.js',
|
||||
'web/static/src/legacy/js/core/dates.js',
|
||||
'web/static/src/legacy/js/owl_compatibility.js',
|
||||
'web/static/src/legacy/js/public/public_env.js',
|
||||
'web/static/src/legacy/js/public/public_root.js',
|
||||
|
||||
@@ -1,192 +0,0 @@
|
||||
/** @odoo-module **/
|
||||
|
||||
import time from "@web/legacy/js/core/time";
|
||||
import { _t } from "@web/core/l10n/translation";
|
||||
|
||||
const { DateTime } = luxon;
|
||||
|
||||
function getTimezoneOffset(value) {
|
||||
return luxon.Settings.defaultZone.offset(value.valueOf());
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a string representing a date. If the value is false, then we return
|
||||
* an empty string. Note that this is dependant on the localization settings
|
||||
*
|
||||
* @param {Moment|false} value
|
||||
* @param {Object} [field]
|
||||
* a description of the field (note: this parameter is ignored)
|
||||
* @param {Object} [options] additional options
|
||||
* @param {boolean} [options.timezone=true] use the user timezone when formating the
|
||||
* date
|
||||
* @returns {string}
|
||||
*/
|
||||
export function formatDate(value, field, options) {
|
||||
if (value === false || isNaN(value)) {
|
||||
return "";
|
||||
}
|
||||
if (field && field.type === 'datetime') {
|
||||
if (!options || !('timezone' in options) || options.timezone) {
|
||||
value = value.clone().add(getTimezoneOffset(value), 'minutes');
|
||||
}
|
||||
}
|
||||
var date_format = time.getLangDateFormat();
|
||||
return value.format(date_format);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a string representing a datetime. If the value is false, then we
|
||||
* return an empty string. Note that this is dependant on the localization
|
||||
* settings
|
||||
*
|
||||
* @params {Moment|false}
|
||||
* @param {Object} [field]
|
||||
* a description of the field (note: this parameter is ignored)
|
||||
* @param {Object} [options] additional options
|
||||
* @param {boolean} [options.timezone=true] use the user timezone when formating the
|
||||
* date
|
||||
* @returns {string}
|
||||
*/
|
||||
export function formatDateTime(value, field, options) {
|
||||
if (value === false) {
|
||||
return "";
|
||||
}
|
||||
if (!options || !('timezone' in options) || options.timezone) {
|
||||
value = value.clone().add(getTimezoneOffset(value), 'minutes');
|
||||
}
|
||||
return value.format(time.getLangDatetimeFormat());
|
||||
}
|
||||
|
||||
/**
|
||||
* Smart date inputs are shortcuts to write dates quicker.
|
||||
* These shortcuts should respect the format ^[+-]\d+[dmwy]?$
|
||||
*
|
||||
* e.g.
|
||||
* "+1d" or "+1" will return now + 1 day
|
||||
* "-2w" will return now - 2 weeks
|
||||
* "+3m" will return now + 3 months
|
||||
* "-4y" will return now + 4 years
|
||||
*
|
||||
* @param {string} value
|
||||
* @returns {DateTime|false} luxon DateTime
|
||||
*/
|
||||
function parseSmartDateInput(value) {
|
||||
const units = {
|
||||
d: 'days',
|
||||
m: 'months',
|
||||
w: 'weeks',
|
||||
y: 'years',
|
||||
};
|
||||
const re = new RegExp(`^([+-])(\\d+)([${Object.keys(units).join('')}]?)$`);
|
||||
const match = re.exec(value);
|
||||
if (match) {
|
||||
let date = DateTime.now()
|
||||
const offset = parseInt(match[2], 10);
|
||||
const unit = units[match[3] || 'd'];
|
||||
if (match[1] === '+') {
|
||||
date.plus({[unit]: offset});
|
||||
} else {
|
||||
date.minus({[unit]: offset});
|
||||
}
|
||||
return date;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create an Date object
|
||||
* The method toJSON return the formated value to send value server side
|
||||
*
|
||||
* @param {string} value
|
||||
* @param {Object} [field]
|
||||
* a description of the field (note: this parameter is ignored)
|
||||
* @param {Object} [options] additional options
|
||||
* @param {boolean} [options.isUTC] the formatted date is utc
|
||||
* @param {boolean} [options.timezone=false] format the date after apply the timezone
|
||||
* offset
|
||||
* @returns {Moment|false} Moment date object
|
||||
*/
|
||||
export function parseDate(value, field, options) {
|
||||
if (!value) {
|
||||
return false;
|
||||
}
|
||||
var datePattern = time.getLangDateFormat();
|
||||
var datePatternWoZero = time.getLangDateFormatWoZero();
|
||||
var date;
|
||||
const smartDate = parseSmartDateInput(value);
|
||||
if (smartDate) {
|
||||
date = smartDate;
|
||||
} else {
|
||||
if (options && options.isUTC) {
|
||||
value = value.padStart(10, "0"); // server may send "932-10-10" for "0932-10-10" on some OS
|
||||
date = moment.utc(value);
|
||||
} else {
|
||||
date = moment.utc(value, [datePattern, datePatternWoZero, moment.ISO_8601]);
|
||||
}
|
||||
}
|
||||
if (date.isValid()) {
|
||||
if (date.year() === 0) {
|
||||
date.year(moment.utc().year());
|
||||
}
|
||||
if (date.year() >= 1000){
|
||||
date.toJSON = function () {
|
||||
return this.clone().locale('en').format('YYYY-MM-DD');
|
||||
};
|
||||
return date;
|
||||
}
|
||||
}
|
||||
throw new Error(_t("'%s' is not a correct date", value));
|
||||
}
|
||||
|
||||
/**
|
||||
* Create an Date object
|
||||
* The method toJSON return the formated value to send value server side
|
||||
*
|
||||
* @param {string} value
|
||||
* @param {Object} [field]
|
||||
* a description of the field (note: this parameter is ignored)
|
||||
* @param {Object} [options] additional options
|
||||
* @param {boolean} [options.isUTC] the formatted date is utc
|
||||
* @param {boolean} [options.timezone=false] format the date after apply the timezone
|
||||
* offset
|
||||
* @returns {Moment|false} Moment date object
|
||||
*/
|
||||
export function parseDateTime(value, field, options) {
|
||||
if (!value) {
|
||||
return false;
|
||||
}
|
||||
const datePattern = time.getLangDateFormat();
|
||||
const timePattern = time.getLangTimeFormat();
|
||||
const datePatternWoZero = time.getLangDateFormatWoZero();
|
||||
const timePatternWoZero = time.getLangTimeFormatWoZero();
|
||||
var pattern1 = datePattern + ' ' + timePattern;
|
||||
var pattern2 = datePatternWoZero + ' ' + timePatternWoZero;
|
||||
var datetime;
|
||||
const smartDate = parseSmartDateInput(value);
|
||||
if (smartDate) {
|
||||
datetime = smartDate;
|
||||
} else {
|
||||
if (options && options.isUTC) {
|
||||
value = value.padStart(19, "0"); // server may send "932-10-10" for "0932-10-10" on some OS
|
||||
// phatomjs crash if we don't use this format
|
||||
datetime = moment.utc(value.replace(' ', 'T') + 'Z');
|
||||
} else {
|
||||
datetime = moment.utc(value, [pattern1, pattern2, moment.ISO_8601]);
|
||||
if (options && options.timezone) {
|
||||
datetime.add(-getTimezoneOffset(datetime), 'minutes');
|
||||
}
|
||||
}
|
||||
}
|
||||
if (datetime.isValid()) {
|
||||
if (datetime.year() === 0) {
|
||||
datetime.year(moment.utc().year());
|
||||
}
|
||||
if (datetime.year() >= 1000) {
|
||||
datetime.toJSON = function () {
|
||||
return this.clone().locale('en').format('YYYY-MM-DD HH:mm:ss');
|
||||
};
|
||||
return datetime;
|
||||
}
|
||||
}
|
||||
throw new Error(_t("'%s' is not a correct datetime", value));
|
||||
}
|
||||
@@ -540,31 +540,4 @@ QUnit.module("Search", () => {
|
||||
"Quarter term should be translated and notation should be right to left"
|
||||
);
|
||||
});
|
||||
|
||||
QUnit.skip(
|
||||
"Moment.js localization does not affect formatted domain dates",
|
||||
async function (assert) {
|
||||
patchDate(2020, 5, 1, 13, 0, 0);
|
||||
const initialLocale = moment.locale();
|
||||
moment.defineLocale("addoneForTest", {
|
||||
postformat: function (string) {
|
||||
return string.replace(/\d/g, (match) => (1 + parseInt(match)) % 10);
|
||||
},
|
||||
});
|
||||
const referenceMoment = moment().locale("addoneForTest");
|
||||
assert.deepEqual(
|
||||
constructDateDomain(referenceMoment, "date_field", "date", [
|
||||
"this_month",
|
||||
"this_year",
|
||||
]),
|
||||
{
|
||||
domain: `["&", ["date_field", ">=", "2020-06-01"], ["date_field", "<=", "2020-06-30"]]`,
|
||||
description: "June 3131",
|
||||
},
|
||||
"Numbers in domain should not use addoneForTest locale"
|
||||
);
|
||||
moment.locale(initialLocale);
|
||||
moment.updateLocale("addoneForTest", null);
|
||||
}
|
||||
);
|
||||
});
|
||||
|
||||
@@ -153,7 +153,6 @@ Featuring
|
||||
'web/static/src/session.js',
|
||||
'web/static/src/legacy/js/core/cookie_utils.js',
|
||||
|
||||
'web/static/lib/moment/moment.js',
|
||||
'web/static/lib/luxon/luxon.js',
|
||||
'web/static/lib/owl/owl.js',
|
||||
'web/static/lib/owl/odoo_module.js',
|
||||
@@ -187,7 +186,6 @@ Featuring
|
||||
'web/static/src/legacy/js/libs/autocomplete.js',
|
||||
'web/static/src/legacy/js/libs/bootstrap.js',
|
||||
'web/static/src/legacy/js/libs/jquery.js',
|
||||
'web/static/src/legacy/js/libs/moment.js',
|
||||
'website/static/src/libs/zoomodoo/zoomodoo.js',
|
||||
'web/static/src/legacy/js/libs/jSignatureCustom.js',
|
||||
'web/static/src/legacy/js/core/ajax.js',
|
||||
|
||||
Reference in New Issue
Block a user