Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return Invalid DateTime for using meridiem with 24-hour (#1549) #1562

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
8 changes: 6 additions & 2 deletions src/datetime.js
Original file line number Diff line number Diff line change
Expand Up @@ -901,9 +901,13 @@ export default class DateTime {
numberingSystem,
defaultToEN: true,
}),
[vals, parsedZone, specificOffset, invalid] = parseFromTokens(localeToUse, text, fmt);
[vals, parsedZone, specificOffset, invalid, explanation] = parseFromTokens(
localeToUse,
text,
fmt
);
if (invalid) {
return DateTime.invalid(invalid);
return DateTime.invalid(invalid, explanation);
} else {
return parseDataToDateTime(vals, parsedZone, opts, `format ${fmt}`, text, specificOffset);
}
Expand Down
20 changes: 18 additions & 2 deletions src/impl/tokenParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -450,14 +450,30 @@ export function explainFromTokens(locale, input, format) {
throw new ConflictingSpecificationError(
"Can't include meridiem when specifying 24-hour format"
);
} else if (
hasOwnProperty(matches, "h") &&
matches["h"] > 12 &&
tokens.find((t) => t.val === "h" || t.val === "hh")
) {
const hourValue = matches["h"];
return {
input,
tokens,
invalidReason: "unit out of range",
invalidExplanation: `you specified ${hourValue} (of type ${typeof hourValue}) as an hour along with a meridiem, which is invalid`,
};
}
return { input, tokens, regex, rawMatches, matches, result, zone, specificOffset };
}
}

export function parseFromTokens(locale, input, format) {
const { result, zone, specificOffset, invalidReason } = explainFromTokens(locale, input, format);
return [result, zone, specificOffset, invalidReason];
const { result, zone, specificOffset, invalidReason, invalidExplanation } = explainFromTokens(
locale,
input,
format
);
return [result, zone, specificOffset, invalidReason, invalidExplanation];
}

export function formatOptsToTokens(formatOpts, locale) {
Expand Down
32 changes: 31 additions & 1 deletion test/datetime/tokenParse.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,40 @@ test("DateTime.fromFormat() parses basic times", () => {
expect(i.millisecond).toBe(445);
});

test("DateTime.fromFormat() yields Invalid reason for invalid 12-hour time", () => {
const i = DateTime.fromFormat("11-08-2023 15:00 AM", "MM-dd-yyyy h:mm a");
expect(i.invalid).not.toBeNull();
expect(i.invalid.reason).toEqual("unit out of range");
expect(i.invalid.explanation).toEqual(
"you specified 15 (of type number) as an hour along with a meridiem, which is invalid"
);
});

test("DateTime.fromFormat() yields Invalid reason for invalid 12-hour time with padding", () => {
const i = DateTime.fromFormat("11-08-2023 15:00 AM", "MM-dd-yyyy hh:mm a");
expect(i.invalid).not.toBeNull();
expect(i.invalid.reason).toEqual("unit out of range");
expect(i.invalid.explanation).toEqual(
"you specified 15 (of type number) as an hour along with a meridiem, which is invalid"
);
});

test("DateTime.fromFormat() throws ConflictingSpecificationError for invalid format", () => {
expect(() => {
DateTime.fromFormat("11-08-2023 12:00 AM", "MM-dd-yyyy H:mm a");
}).toThrowError(
ConflictingSpecificationError,
"Can't include meridiem when specifying 24-hour format"
);
});

test("DateTime.fromFormat() yields Invalid reason 'unparseable' for incompatible formats", () => {
const i = DateTime.fromFormat("Mar 3, 2020", "MMM dd, yyyy");
expect(i.invalid).not.toBeNull;
expect(i.invalid).not.toBeNull();
expect(i.invalid.reason).toEqual("unparsable");
expect(i.invalid.explanation).toEqual(
'the input "Mar 3, 2020" can\'t be parsed as format MMM dd, yyyy'
);
});

test("DateTime.fromFormat() parses with variable-length input", () => {
Expand Down