[JS] Try to guess what the date is when it doesn't follow the given format (issue #15490)

We use the format to guess in which order we can find month, day, ... we get the numbers
in the date and consider them as month, day, ...
This commit is contained in:
Calixte Denizet 2022-09-22 13:25:52 +02:00
parent 547fa3ed2c
commit 9e40938a29
2 changed files with 100 additions and 3 deletions

View file

@ -38,6 +38,7 @@ class AForm {
"m/d/yy HH:MM",
];
this._timeFormats = ["HH:MM", "h:MM tt", "HH:MM:ss", "h:MM:ss tt"];
this._dateActionsCache = new Map();
// The e-mail address regex below originates from:
// https://html.spec.whatwg.org/multipage/input.html#valid-e-mail-address
@ -52,6 +53,93 @@ class AForm {
return event.target ? `[ ${event.target.name} ]` : "";
}
_tryToGuessDate(cFormat, cDate) {
// We use the format to know the order of day, month, year, ...
let actions = this._dateActionsCache.get(cFormat);
if (!actions) {
actions = [];
this._dateActionsCache.set(cFormat, actions);
cFormat.replace(
/(d+)|(m+)|(y+)|(H+)|(M+)|(s+)/g,
function (match, d, m, y, H, M, s) {
if (d) {
actions.push((n, date) => {
if (n >= 1 && n <= 31) {
date.setDate(n);
return true;
}
return false;
});
} else if (m) {
actions.push((n, date) => {
if (n >= 1 && n <= 12) {
date.setMonth(n - 1);
return true;
}
return false;
});
} else if (y) {
actions.push((n, date) => {
if (n < 50) {
n += 2000;
} else if (n < 100) {
n += 1900;
}
date.setYear(n);
return true;
});
} else if (H) {
actions.push((n, date) => {
if (n >= 0 && n <= 23) {
date.setHours(n);
return true;
}
return false;
});
} else if (M) {
actions.push((n, date) => {
if (n >= 0 && n <= 59) {
date.setMinutes(n);
return true;
}
return false;
});
} else if (s) {
actions.push((n, date) => {
if (n >= 0 && n <= 59) {
date.setSeconds(n);
return true;
}
return false;
});
}
return "";
}
);
}
const number = /\d+/g;
let i = 0;
let array;
const date = new Date();
while ((array = number.exec(cDate)) !== null) {
if (i < actions.length) {
if (!actions[i++](parseInt(array[0]), date)) {
return null;
}
} else {
break;
}
}
if (i === 0) {
return null;
}
return date;
}
_parseDate(cFormat, cDate) {
let date = null;
try {
@ -60,7 +148,7 @@ class AForm {
if (!date) {
date = Date.parse(cDate);
if (isNaN(date)) {
date = null;
date = this._tryToGuessDate(cFormat, cDate);
} else {
date = new Date(date);
}