fix parsing am/pm times

This commit is contained in:
2024-05-23 19:45:12 -05:00
parent e397500d07
commit 3d13e784ea
+31 -15
View File
@@ -45,7 +45,7 @@ function checkAmPm(value: string) {
* @description Utility function to check if a string contain h / m / s indicators * @description Utility function to check if a string contain h / m / s indicators
* @param {string} value * @param {string} value
*/ */
function checkMatchers(value: string) { function checkMatchers(value: string): number | null {
const hoursMatch = /(\d+)h/i.exec(value); const hoursMatch = /(\d+)h/i.exec(value);
const hoursMatchValue = hoursMatch ? parse(hoursMatch[1]) : 0; const hoursMatchValue = hoursMatch ? parse(hoursMatch[1]) : 0;
@@ -60,7 +60,7 @@ function checkMatchers(value: string) {
hoursMatchValue * MILLIS_PER_HOUR + minutesMatchValue * MILLIS_PER_MINUTE + secondsMatchValue * MILLIS_PER_SECOND hoursMatchValue * MILLIS_PER_HOUR + minutesMatchValue * MILLIS_PER_MINUTE + secondsMatchValue * MILLIS_PER_SECOND
); );
} }
return { hoursMatchValue }; return null;
} }
/** /**
@@ -85,8 +85,8 @@ function inferSeparators(value: string, isAM: boolean, isPM: boolean) {
} }
} else if (length === 2) { } else if (length === 2) {
if (isPM || isAM) { if (isPM || isAM) {
inferredMillis = parse(value) * inferredMillis; inferredMillis = parse(value) * MILLIS_PER_HOUR;
if (isAM) { if (isAM || (isPM && value === '12')) {
// this ensures we dont add 12 hours in the end // this ensures we dont add 12 hours in the end
addAM = 12; addAM = 12;
} }
@@ -112,43 +112,59 @@ function inferSeparators(value: string, isAM: boolean, isPM: boolean) {
* @returns {number} - time string in millis * @returns {number} - time string in millis
*/ */
export const forgivingStringToMillis = (value: string): number => { export const forgivingStringToMillis = (value: string): number => {
if (value === '12am') { if (value === '12am' || value === '12a') {
return 0; return 0;
} }
const { isAM, isPM, value: parsingValue } = checkAmPm(value); const { isAM, isPM, value: parsingValue } = checkAmPm(value);
const maybeMillisFromMatchers = checkMatchers(parsingValue); const maybeMillisFromMatchers = checkMatchers(parsingValue);
if (typeof maybeMillisFromMatchers === 'number') { if (maybeMillisFromMatchers !== null) {
return maybeMillisFromMatchers; return maybeMillisFromMatchers;
} }
let { hoursMatchValue } = maybeMillisFromMatchers;
let millis = 0;
// split string at known separators : , . // split string at known separators : , .
const separatorRegex = /[\s,:.]+/; const separatorRegex = /[\s,:.]+/;
const [first, second, third] = parsingValue.split(separatorRegex); const [first, second, third] = parsingValue.split(separatorRegex);
let addTwelve = isPM;
let millis = 0;
if (first != null && second != null && third != null) { if (first != null && second != null && third != null) {
// if string has three sections, treat as [hours] [minutes] [seconds] // if string has three sections, treat as [hours] [minutes] [seconds]
millis = parse(first) * MILLIS_PER_HOUR; let hours = parse(first);
if (hours === 12) {
if (isAM) {
hours = 0;
}
addTwelve = false;
}
millis = hours * MILLIS_PER_HOUR;
millis += parse(second) * MILLIS_PER_MINUTE; millis += parse(second) * MILLIS_PER_MINUTE;
millis += parse(third) * MILLIS_PER_SECOND; millis += parse(third) * MILLIS_PER_SECOND;
} else if (first != null && second == null && third == null) { } else if (first != null && second == null && third == null) {
// we only have one section, infer separators // we only have one section, infer separators
const { inferredMillis, addAM } = inferSeparators(first, isAM, isPM); const { inferredMillis, addAM } = inferSeparators(first, isAM, isPM);
millis = inferredMillis; millis = inferredMillis;
hoursMatchValue = addAM; addTwelve = addAM < 12 && isPM;
} }
if (first != null && second != null && third == null) { if (first != null && second != null && third == null) {
// if string has two sections, treat as [hours] [minutes] // if string has two sections, treat as [hours] [minutes]
millis = parse(first) * MILLIS_PER_HOUR; let hours = parse(first);
if (hours === 12) {
if (isAM) {
hours = 0;
}
addTwelve = false;
}
millis = hours * MILLIS_PER_HOUR;
millis += parse(second) * MILLIS_PER_MINUTE; millis += parse(second) * MILLIS_PER_MINUTE;
} }
// Add 12 hours if it is PM // Add 12 hours if needed
if (isPM && hoursMatchValue < 12) { if (addTwelve) {
millis += 12 * MILLIS_PER_HOUR; millis += 12 * MILLIS_PER_HOUR;
} }