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
5 changes: 5 additions & 0 deletions .changeset/datetime-format-quoted-literals.md
Original file line number Diff line number Diff line change
@@ -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.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add Fixes #2906 to this summary so the issue closes when the changeset is released (.github/instructions/code-review.instructions.md).

Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This no longer merges. #2883 moved this file to packages/core/src/components/utils/datetime/luxon-datetime-format-masks.ts. The spec stayed in packages/core/src/components/utils/test/ and already imports ../datetime/luxon-datetime-format-masks.

Please rebase onto main and replay this change onto the new path.

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;
}

/**
Expand All @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
);
});
});
Loading