From 2ac6492cc29b7300cf1ab1635222d3b96ea5507d Mon Sep 17 00:00:00 2001 From: Bruno Boi Date: Wed, 12 May 2021 14:07:05 +0000 Subject: [FIX] web: parseDateTime util optimizations - `parseDateTime` util: we now caches "without zero" date and time formats Taskid: 2412172 --- addons/web/static/src/js/core/time.js | 29 ++++++++++++++++++++++++++ addons/web/static/src/js/fields/field_utils.js | 8 +++---- 2 files changed, 33 insertions(+), 4 deletions(-) (limited to 'addons/web/static/src/js') diff --git a/addons/web/static/src/js/core/time.js b/addons/web/static/src/js/core/time.js index 3e677c4201a..b9f41813030 100644 --- a/addons/web/static/src/js/core/time.js +++ b/addons/web/static/src/js/core/time.js @@ -300,7 +300,34 @@ function getLangDatetimeFormat() { return strftime_to_moment_format(_t.database.parameters.date_format + " " + _t.database.parameters.time_format); } +const dateFormatWoZeroCache = {}; +/** + * Get date format of the user's language - allows non padded + */ +function getLangDateFormatWoZero() { + const dateFormat = getLangDateFormat(); + if (!dateFormat in dateFormatWoZeroCache) { + dateFormatWoZeroCache[dateFormat] = dateFormat + .replace('MM', 'M') + .replace('DD', 'D'); + } + return dateFormatWoZeroCache[dateFormat]; +} +const timeFormatWoZeroCache = {}; +/** + * Get time format of the user's language - allows non padded + */ +function getLangTimeFormatWoZero() { + const timeFormat = getLangTimeFormat(); + if (!timeFormat in timeFormatWoZeroCache) { + timeFormatWoZeroCache[timeFormat] = timeFormat + .replace('HH', 'H') + .replace('mm', 'm') + .replace('ss', 's'); + } + return timeFormatWoZeroCache[timeFormat]; +} return { date_to_utc: date_to_utc, @@ -316,6 +343,8 @@ return { moment_to_strftime_format: moment_to_strftime_format, getLangDateFormat: getLangDateFormat, getLangTimeFormat: getLangTimeFormat, + getLangDateFormatWoZero: getLangDateFormatWoZero, + getLangTimeFormatWoZero: getLangTimeFormatWoZero, getLangDatetimeFormat: getLangDatetimeFormat, }; diff --git a/addons/web/static/src/js/fields/field_utils.js b/addons/web/static/src/js/fields/field_utils.js index 0407da1b265..7dfb92b3d19 100644 --- a/addons/web/static/src/js/fields/field_utils.js +++ b/addons/web/static/src/js/fields/field_utils.js @@ -511,10 +511,10 @@ function parseDateTime(value, field, options) { if (!value) { return false; } - var datePattern = time.getLangDateFormat(), - timePattern = time.getLangTimeFormat(); - var datePatternWoZero = datePattern.replace('MM','M').replace('DD','D'), - timePatternWoZero = timePattern.replace('HH','H').replace('mm','m').replace('ss','s'); + 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; -- cgit v1.2.3