Skip to content

Commit 3ae3144

Browse files
author
Wanasit Tanakitrungruang
committedJun 1, 2024·
Fix: Time expression parsing after date with number-ending
1 parent fdcfde0 commit 3ae3144

File tree

5 files changed

+63
-4
lines changed

5 files changed

+63
-4
lines changed
 

‎src/common/parsers/AbstractTimeExpressionParser.ts

+16-2
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,13 @@ export abstract class AbstractTimeExpressionParser implements Parser {
8080
extract(context: ParsingContext, match: RegExpMatchArray): ParsingResult {
8181
const startComponents = this.extractPrimaryTimeComponents(context, match);
8282
if (!startComponents) {
83+
// If the match seem like a year e.g. "2013.12:...",
84+
// then skips the year part and try matching again.
85+
if (match[0].match(/^\d{4}/)) {
86+
match.index += 4; // Skip over potential overlapping pattern
87+
return null;
88+
}
89+
8390
match.index += match[0].length; // Skip over potential overlapping pattern
8491
return null;
8592
}
@@ -94,8 +101,15 @@ export abstract class AbstractTimeExpressionParser implements Parser {
94101
const followingMatch = followingPattern.exec(remainingText);
95102

96103
// Pattern "456-12", "2022-12" should not be time without proper context
97-
if (text.match(/^\d{3,4}/) && followingMatch && followingMatch[0].match(/^\s*([+-])\s*\d{2,4}$/)) {
98-
return null;
104+
if (text.match(/^\d{3,4}/) && followingMatch) {
105+
// e.g. "2022-12"
106+
if (followingMatch[0].match(/^\s*([+-])\s*\d{2,4}$/)) {
107+
return null;
108+
}
109+
// e.g. "2022-12:01..."
110+
if (followingMatch[0].match(/^\s*([+-])\s*\d{2}\W\d{2}/)) {
111+
return null;
112+
}
99113
}
100114

101115
if (

‎src/locales/en/parsers/ENMonthNameLittleEndianParser.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ const PATTERN = new RegExp(
1919
`(${matchAnyPattern(MONTH_DICTIONARY)})` +
2020
"(?:" +
2121
`(?:-|/|,?\\s{0,3})` +
22-
`(${YEAR_PATTERN}(?![^\\s]\\d))` +
22+
`(${YEAR_PATTERN}(?!\\w))` +
2323
")?" +
2424
"(?=\\W|$)",
2525
"i"

‎src/locales/en/refiners/ENMergeDateTimeRefiner.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@ import AbstractMergeDateTimeRefiner from "../../../common/refiners/AbstractMerge
88
*/
99
export default class ENMergeDateTimeRefiner extends AbstractMergeDateTimeRefiner {
1010
patternBetween(): RegExp {
11-
return new RegExp("^\\s*(T|at|after|before|on|of|,|-)?\\s*$");
11+
return new RegExp("^\\s*(T|at|after|before|on|of|,|-|\\.|:)?\\s*$");
1212
}
1313
}

‎test/en/en_slash.test.ts

+15
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,21 @@ test("Test - Single Expression Little-Endian with Month name", function () {
136136

137137
expect(result.start).toBeDate(new Date(2012, 10 - 1, 8, 12));
138138
});
139+
140+
testSingleCase(chrono.strict, "06/Nov/2023", (result) => {
141+
expect(result.text).toBe("06/Nov/2023");
142+
expect(result).toBeDate(new Date(2023, 11 - 1, 6, 12));
143+
});
144+
145+
testSingleCase(chrono.strict, "06/Nov/2023:06:36:02", (result) => {
146+
expect(result.text).toBe("06/Nov/2023:06:36:02");
147+
expect(result).toBeDate(new Date(2023, 11 - 1, 6, 6, 36, 2));
148+
});
149+
150+
testSingleCase(chrono.strict, "06/Nov/2023:06:36:02 +0200", (result) => {
151+
expect(result.text).toBe("06/Nov/2023:06:36:02 +0200");
152+
expect(result).toBeDate(new Date("Mon Nov 06 2023 06:36:02 GMT+0200"));
153+
});
139154
});
140155

141156
test("Test - Range Expression", function () {

‎test/en/en_time_exp.test.ts

+30
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,36 @@ test("Test - Time expression", function () {
2424
});
2525
});
2626

27+
test("Test - Time expression after date", function () {
28+
testSingleCase(chrono, "05/31/2024 14:15", new Date(2016, 10 - 1, 1, 8), (result, text) => {
29+
expect(result.text).toBe(text);
30+
expect(result.start.get("hour")).toBe(14);
31+
expect(result.start.get("minute")).toBe(15);
32+
expect(result.start.get("meridiem")).toBe(Meridiem.PM);
33+
});
34+
35+
testSingleCase(chrono, "05/31/2024.14:15", new Date(2016, 10 - 1, 1, 8), (result, text) => {
36+
expect(result.text).toBe(text);
37+
expect(result.start.get("hour")).toBe(14);
38+
expect(result.start.get("minute")).toBe(15);
39+
expect(result.start.get("meridiem")).toBe(Meridiem.PM);
40+
});
41+
42+
testSingleCase(chrono, "05/31/2024:14:15", new Date(2016, 10 - 1, 1, 8), (result, text) => {
43+
expect(result.text).toBe(text);
44+
expect(result.start.get("hour")).toBe(14);
45+
expect(result.start.get("minute")).toBe(15);
46+
expect(result.start.get("meridiem")).toBe(Meridiem.PM);
47+
});
48+
49+
testSingleCase(chrono, "05/31/2024-14:15", new Date(2016, 10 - 1, 1, 8), (result, text) => {
50+
expect(result.text).toBe(text);
51+
expect(result.start.get("hour")).toBe(14);
52+
expect(result.start.get("minute")).toBe(15);
53+
expect(result.start.get("meridiem")).toBe(Meridiem.PM);
54+
});
55+
});
56+
2757
test("Test - Time range expression", function () {
2858
testSingleCase(chrono, "10:00:00 - 21:45:00", new Date(2016, 10 - 1, 1, 8), (result, text) => {
2959
expect(result.text).toBe(text);

0 commit comments

Comments
 (0)
Please sign in to comment.