From d35be5acb9ea5207650b9e1203e4373daa947c5d Mon Sep 17 00:00:00 2001 From: Sho-ki Date: Thu, 16 May 2024 00:37:25 -0700 Subject: [PATCH 1/2] add error handle when going over 12 when specifying 12-hour format --- src/impl/tokenParser.js | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/impl/tokenParser.js b/src/impl/tokenParser.js index 48a7595ed..202be01e7 100644 --- a/src/impl/tokenParser.js +++ b/src/impl/tokenParser.js @@ -451,10 +451,13 @@ export class TokenParser { if (!this.isValid) { return { input, tokens: this.tokens, invalidReason: this.invalidReason }; } else { - const [rawMatches, matches] = match(input, this.regex, this.handlers), - [result, zone, specificOffset] = matches - ? dateTimeFromMatches(matches) - : [null, null, undefined]; + const [rawMatches, matches] = match(input, this.regex, this.handlers); + if (hasOwnProperty(matches, "h") && matches.h > 12) { + throw new ConflictingSpecificationError("Can't go over 12 when specifying 12-hour format"); + } + const [result, zone, specificOffset] = matches + ? dateTimeFromMatches(matches) + : [null, null, undefined]; if (hasOwnProperty(matches, "a") && hasOwnProperty(matches, "H")) { throw new ConflictingSpecificationError( "Can't include meridiem when specifying 24-hour format" From f8ae8911e33e0bcca7cf0c9cb023ae809f4179bb Mon Sep 17 00:00:00 2001 From: Sho-ki Date: Thu, 16 May 2024 00:37:57 -0700 Subject: [PATCH 2/2] add test --- test/datetime/tokenParse.test.js | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/test/datetime/tokenParse.test.js b/test/datetime/tokenParse.test.js index 8b5c6a8d7..d8839846d 100644 --- a/test/datetime/tokenParse.test.js +++ b/test/datetime/tokenParse.test.js @@ -74,6 +74,12 @@ test("DateTime.fromFormat() throws if you specify meridiem with 24-hour time", ( expect(() => DateTime.fromFormat("930PM", "Hmma")).toThrow(ConflictingSpecificationError); }); +test("DateTime.fromFormat() throws if h is used with 24-hour time", () => { + expect(() => DateTime.fromFormat("18:30 AM", "h:mm a")).toThrowError( + ConflictingSpecificationError + ); +}); + // #714 test("DateTime.fromFormat() makes dots optional and handles non breakable spaces", () => { function parseMeridiem(input, isAM) {