Skip to content

Commit

Permalink
Fix: Time expression parsing after date with number-ending
Browse files Browse the repository at this point in the history
  • Loading branch information
Wanasit Tanakitrungruang committed Jun 1, 2024
1 parent fdcfde0 commit 3ae3144
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 4 deletions.
18 changes: 16 additions & 2 deletions src/common/parsers/AbstractTimeExpressionParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,13 @@ export abstract class AbstractTimeExpressionParser implements Parser {
extract(context: ParsingContext, match: RegExpMatchArray): ParsingResult {
const startComponents = this.extractPrimaryTimeComponents(context, match);
if (!startComponents) {
// If the match seem like a year e.g. "2013.12:...",
// then skips the year part and try matching again.
if (match[0].match(/^\d{4}/)) {
match.index += 4; // Skip over potential overlapping pattern
return null;
}

match.index += match[0].length; // Skip over potential overlapping pattern
return null;
}
Expand All @@ -94,8 +101,15 @@ export abstract class AbstractTimeExpressionParser implements Parser {
const followingMatch = followingPattern.exec(remainingText);

// Pattern "456-12", "2022-12" should not be time without proper context
if (text.match(/^\d{3,4}/) && followingMatch && followingMatch[0].match(/^\s*([+-])\s*\d{2,4}$/)) {
return null;
if (text.match(/^\d{3,4}/) && followingMatch) {
// e.g. "2022-12"
if (followingMatch[0].match(/^\s*([+-])\s*\d{2,4}$/)) {
return null;
}
// e.g. "2022-12:01..."
if (followingMatch[0].match(/^\s*([+-])\s*\d{2}\W\d{2}/)) {
return null;
}
}

if (
Expand Down
2 changes: 1 addition & 1 deletion src/locales/en/parsers/ENMonthNameLittleEndianParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const PATTERN = new RegExp(
`(${matchAnyPattern(MONTH_DICTIONARY)})` +
"(?:" +
`(?:-|/|,?\\s{0,3})` +
`(${YEAR_PATTERN}(?![^\\s]\\d))` +
`(${YEAR_PATTERN}(?!\\w))` +
")?" +
"(?=\\W|$)",
"i"
Expand Down
2 changes: 1 addition & 1 deletion src/locales/en/refiners/ENMergeDateTimeRefiner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ import AbstractMergeDateTimeRefiner from "../../../common/refiners/AbstractMerge
*/
export default class ENMergeDateTimeRefiner extends AbstractMergeDateTimeRefiner {
patternBetween(): RegExp {
return new RegExp("^\\s*(T|at|after|before|on|of|,|-)?\\s*$");
return new RegExp("^\\s*(T|at|after|before|on|of|,|-|\\.|:)?\\s*$");
}
}
15 changes: 15 additions & 0 deletions test/en/en_slash.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,21 @@ test("Test - Single Expression Little-Endian with Month name", function () {

expect(result.start).toBeDate(new Date(2012, 10 - 1, 8, 12));
});

testSingleCase(chrono.strict, "06/Nov/2023", (result) => {
expect(result.text).toBe("06/Nov/2023");
expect(result).toBeDate(new Date(2023, 11 - 1, 6, 12));
});

testSingleCase(chrono.strict, "06/Nov/2023:06:36:02", (result) => {
expect(result.text).toBe("06/Nov/2023:06:36:02");
expect(result).toBeDate(new Date(2023, 11 - 1, 6, 6, 36, 2));
});

testSingleCase(chrono.strict, "06/Nov/2023:06:36:02 +0200", (result) => {
expect(result.text).toBe("06/Nov/2023:06:36:02 +0200");
expect(result).toBeDate(new Date("Mon Nov 06 2023 06:36:02 GMT+0200"));
});
});

test("Test - Range Expression", function () {
Expand Down
30 changes: 30 additions & 0 deletions test/en/en_time_exp.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,36 @@ test("Test - Time expression", function () {
});
});

test("Test - Time expression after date", function () {
testSingleCase(chrono, "05/31/2024 14:15", new Date(2016, 10 - 1, 1, 8), (result, text) => {
expect(result.text).toBe(text);
expect(result.start.get("hour")).toBe(14);
expect(result.start.get("minute")).toBe(15);
expect(result.start.get("meridiem")).toBe(Meridiem.PM);
});

testSingleCase(chrono, "05/31/2024.14:15", new Date(2016, 10 - 1, 1, 8), (result, text) => {
expect(result.text).toBe(text);
expect(result.start.get("hour")).toBe(14);
expect(result.start.get("minute")).toBe(15);
expect(result.start.get("meridiem")).toBe(Meridiem.PM);
});

testSingleCase(chrono, "05/31/2024:14:15", new Date(2016, 10 - 1, 1, 8), (result, text) => {
expect(result.text).toBe(text);
expect(result.start.get("hour")).toBe(14);
expect(result.start.get("minute")).toBe(15);
expect(result.start.get("meridiem")).toBe(Meridiem.PM);
});

testSingleCase(chrono, "05/31/2024-14:15", new Date(2016, 10 - 1, 1, 8), (result, text) => {
expect(result.text).toBe(text);
expect(result.start.get("hour")).toBe(14);
expect(result.start.get("minute")).toBe(15);
expect(result.start.get("meridiem")).toBe(Meridiem.PM);
});
});

test("Test - Time range expression", function () {
testSingleCase(chrono, "10:00:00 - 21:45:00", new Date(2016, 10 - 1, 1, 8), (result, text) => {
expect(result.text).toBe(text);
Expand Down

0 comments on commit 3ae3144

Please sign in to comment.