Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 38 additions & 2 deletions mathesar_ui/src/utils/date-time/DateTimeFormatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,33 @@ function parseKeywords(input: string): Dayjs | undefined {
}
}

function makeLooseVariants(formats: string[]): string[] {
const variants = new Set<string>();

for (const f of formats) {
variants.add(f);

if (f.includes('HH')) {
variants.add(f.replace(/HH/g, 'H'));
}

if (f.includes(':ss')) {
variants.add(f.replace(':ss', ''));
variants.add(f.replace(':ss.Z', ''));
variants.add(f.replace(':ss Z', ''));
variants.add(f.replace(':ssZZ', ''));
}

if (f.includes('T')) {
variants.add(f.replace('T', ' '));
} else if (f.includes(' ')) {
variants.add(f.replace(' ', 'T'));
}
}

return Array.from(variants);
}

function parseWithSpec(
input: string,
spec: DateTimeSpecification,
Expand All @@ -52,7 +79,9 @@ function parseWithSpec(
...canonicalFormats,
];

const strictResult = dayjs(input, allFormats, true);
const looseFormats = makeLooseVariants(allFormats);

const strictResult = dayjs(input, looseFormats, true);
if (strictResult.isValid()) return strictResult;

const canonicalResult = dayjs(input, canonicalFormats);
Expand All @@ -72,9 +101,16 @@ export default class DateTimeFormatter implements InputFormatter<string> {
* @param input could come from the user or from an API response
*/
parse(input: string): ParseResult<string> {
const dayjsValue =
let dayjsValue =
parseKeywords(input) ?? parseWithSpec(input, this.specification);

if (!dayjsValue) {
const permissive = dayjs(input);
if (permissive.isValid()) {
dayjsValue = permissive;
}
}

const value = (() => {
if (dayjsValue) {
// If we were able to parse the input, then we return the canonical
Expand Down
5 changes: 4 additions & 1 deletion mathesar_ui/src/utils/date-time/DateTimeSpecification.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,12 @@ const timeFormattingStringMap: Record<TimeFormat, string> = {
const commonTimeFormattingStrings = [
'hh:mm:ss a',
'HH:mm:ss',
'H:mm:ss',
'hh:mm a',
'HH:mm',
'H:mm',
];

const commonTimeWithTZFormattingStrings = [
...commonTimeFormattingStrings,
...combine(
Expand Down Expand Up @@ -139,7 +142,7 @@ export default class DateTimeSpecification {
if (this.type === 'timestamp') {
return combine(
allDateFormattingStrings,
commonTimeFormattingStrings,
['HH:mm:ss', 'H:mm:ss', 'HH:mm', 'H:mm', 'hh:mm:ss a', 'hh:mm a'],
(a, b) => `${a} ${b}`,
);
}
Expand Down
Loading