From 9a3f8b5f55f72619d82b8aacbee4ac3b95b84fee Mon Sep 17 00:00:00 2001 From: Sebastian Turner Date: Sun, 18 Oct 2026 20:55:42 -0700 Subject: [PATCH] fix(core/datetime-input): ignore quoted literals when splitting format Signed-off-by: Sebastian Turner --- .changeset/datetime-format-quoted-literals.md | 5 +++ .../utils/luxon-datetime-format-masks.ts | 32 +++++++++++++++++-- .../test/luxon-datetime-format-masks.spec.ts | 20 ++++++++++++ 3 files changed, 54 insertions(+), 3 deletions(-) create mode 100644 .changeset/datetime-format-quoted-literals.md diff --git a/.changeset/datetime-format-quoted-literals.md b/.changeset/datetime-format-quoted-literals.md new file mode 100644 index 00000000000..19ce651224d --- /dev/null +++ b/.changeset/datetime-format-quoted-literals.md @@ -0,0 +1,5 @@ +--- +'@siemens/ix': patch +--- + +Fix `ix-datetime-input` splitting its `format` into date and time parts when a quoted literal contains time token letters (for example `dd.MM.yyyy 'um' HH:mm` or `LL/dd/yyyy 'at' hh:mm a`). Previously the literal was cut in half, so selecting a value in the picker could commit a wrong hour. diff --git a/packages/core/src/components/utils/luxon-datetime-format-masks.ts b/packages/core/src/components/utils/luxon-datetime-format-masks.ts index a31b015cebe..42b7ce1c9d8 100644 --- a/packages/core/src/components/utils/luxon-datetime-format-masks.ts +++ b/packages/core/src/components/utils/luxon-datetime-format-masks.ts @@ -10,8 +10,24 @@ export const LUXON_DEFAULT_DATE_ONLY_FORMAT = 'yyyy/LL/dd'; /** Used when a format string has no time tokens. */ export const LUXON_DEFAULT_TIME_ONLY_FORMAT = 'HH:mm:ss'; +/** + * Index of the first time token in `format`, ignoring text inside single + * quotes, which Luxon treats as literal (e.g. `'at'`, `'um'`). + */ function firstLuxonTimeTokenIndex(format: string): number { - return format.search(/[HhmsaSZ]/); + let inQuote = false; + + for (let i = 0; i < format.length; i++) { + const char = format[i]; + + if (char === "'") { + inQuote = !inQuote; + } else if (!inQuote && /[HhmsaSZ]/.test(char)) { + return i; + } + } + + return -1; } /** @@ -29,8 +45,18 @@ export function getLuxonDateOnlyFormatMask( } let end = timeTokenIndex; - while (end > 0 && " \t'T".includes(format[end - 1])) { - end--; + while (end > 0) { + const char = format[end - 1]; + + if (char === "'") { + // Drop the whole quoted literal that joins date and time, e.g. 'T' or 'at' + const openingQuote = format.lastIndexOf("'", end - 2); + end = openingQuote === -1 ? end - 1 : openingQuote; + } else if (' \tT'.includes(char)) { + end--; + } else { + break; + } } const prefix = format.slice(0, end).trimEnd(); diff --git a/packages/core/src/components/utils/test/luxon-datetime-format-masks.spec.ts b/packages/core/src/components/utils/test/luxon-datetime-format-masks.spec.ts index 00125432b3e..12649772482 100644 --- a/packages/core/src/components/utils/test/luxon-datetime-format-masks.spec.ts +++ b/packages/core/src/components/utils/test/luxon-datetime-format-masks.spec.ts @@ -34,6 +34,18 @@ describe('getLuxonDateOnlyFormatMask', () => { ); }); + it('ignores time token letters inside quoted literals', () => { + expect(getLuxonDateOnlyFormatMask("dd.MM.yyyy 'um' HH:mm")).toBe( + 'dd.MM.yyyy' + ); + expect(getLuxonDateOnlyFormatMask("LL/dd/yyyy 'at' hh:mm a")).toBe( + 'LL/dd/yyyy' + ); + expect(getLuxonDateOnlyFormatMask("'Date:' yyyy/LL/dd HH:mm")).toBe( + "'Date:' yyyy/LL/dd" + ); + }); + it('uses fallback when prefix is empty', () => { expect(getLuxonDateOnlyFormatMask('HH:mm:ss')).toBe( LUXON_DEFAULT_DATE_ONLY_FORMAT @@ -55,4 +67,12 @@ describe('getLuxonTimeFormatMask', () => { expect(getLuxonTimeFormatMask("yyyy-MM-dd'T'HH:mm:ss")).toBe('HH:mm:ss'); expect(getLuxonTimeFormatMask('yyyy-MM-dd HH:mm')).toBe('HH:mm'); }); + + it('ignores time token letters inside quoted literals', () => { + expect(getLuxonTimeFormatMask("dd.MM.yyyy 'um' HH:mm")).toBe('HH:mm'); + expect(getLuxonTimeFormatMask("LL/dd/yyyy 'at' hh:mm a")).toBe('hh:mm a'); + expect(getLuxonTimeFormatMask("'Date:' yyyy/LL/dd")).toBe( + LUXON_DEFAULT_TIME_ONLY_FORMAT + ); + }); });