From 7e2aaf7ce653cfb4b8ba40943bc8bc12a7c002e9 Mon Sep 17 00:00:00 2001 From: XuYui Date: Thu, 22 Oct 2026 02:49:28 -0700 Subject: [PATCH] fix(en): don't read an out-of-range day before a year as a two-digit year "May 32, 2021" was rejected by ENMonthNameMiddleEndianParser (day > 31), but ENMonthNameParser then matched "May 32" as month + two-digit year and returned 2032-05-01, dropping the explicit ", 2021". Skip the two-digit year in ENMonthNameParser when it is followed by a comma and a four-digit year. Fixes #677 --- src/locales/en/parsers/ENMonthNameParser.ts | 7 +++++++ test/en/en_month_name_middle_endian.test.ts | 14 ++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/src/locales/en/parsers/ENMonthNameParser.ts b/src/locales/en/parsers/ENMonthNameParser.ts index cd6e89b97..23597437c 100644 --- a/src/locales/en/parsers/ENMonthNameParser.ts +++ b/src/locales/en/parsers/ENMonthNameParser.ts @@ -51,6 +51,13 @@ export default class ENMonthNameParser extends AbstractParserWithWordBoundaryChe result.start.assign("month", month); if (match[YEAR_GROUP]) { + // Skip the case where the two-digit "year" is an out-of-range day followed by + // the real year (ex: May 32, 2021) + const nextText = context.text.substring(match.index + match[0].length); + if (/^\d{2}$/.test(match[YEAR_GROUP]) && /^\s*,\s*\d{4}(?!\d)/.test(nextText)) { + return null; + } + const year = parseYear(match[YEAR_GROUP]); result.start.assign("year", year); } else { diff --git a/test/en/en_month_name_middle_endian.test.ts b/test/en/en_month_name_middle_endian.test.ts index 877da3873..22fb392a2 100644 --- a/test/en/en_month_name_middle_endian.test.ts +++ b/test/en/en_month_name_middle_endian.test.ts @@ -384,3 +384,17 @@ test("Test - Impossible Dates (Strict Mode)", () => { testUnexpectedResult(chrono.strict, "February 151998", new Date(2014, 7, 10)); }); + +test("Test - Impossible Dates (Casual Mode)", () => { + testUnexpectedResult(chrono, "January 32, 2021", new Date(2020, 0, 1)); + + testUnexpectedResult(chrono, "May 32, 2021", new Date(2020, 0, 1)); + + testUnexpectedResult(chrono, "May 99, 2021", new Date(2020, 0, 1)); + + testUnexpectedResult(chrono, "May 33,2021", new Date(2020, 0, 1)); + + testSingleCase(chrono, "The event is on May 32, 2021 now", new Date(2020, 0, 1), (result) => { + expect(result.text).toBe("now"); + }); +});