Skip to content

Commit f036345

Browse files
author
Wanasit Tanakitrungruang
committedMay 30, 2021
Improvement: timezone case-check when apply to date
1 parent 77fbd83 commit f036345

File tree

2 files changed

+47
-13
lines changed

2 files changed

+47
-13
lines changed
 

‎src/common/refiners/ExtractTimezoneAbbrRefiner.ts

+13-5
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import { ParsingContext, Refiner } from "../../chrono";
33
import { ParsingResult } from "../../results";
44

5-
const TIMEZONE_NAME_PATTERN = new RegExp("^\\s*\\(?([A-Z]{2,4})\\)?(?=\\W|$)", "i");
5+
const TIMEZONE_NAME_PATTERN = new RegExp("^\\s*,?\\s*\\(?([A-Z]{2,4})\\)?(?=\\W|$)", "i");
66
const DEFAULT_TIMEZONE_ABBR_MAP = {
77
ACDT: 630,
88
ACST: 570,
@@ -224,12 +224,20 @@ export default class ExtractTimezoneAbbrRefiner implements Refiner {
224224
console.log(`Extracting timezone: '${timezoneAbbr}' into : ${extractedTimezoneOffset}`);
225225
});
226226

227-
// We may already have extracted the offset e.g. "11 am GMT+0900 (JST)"
228-
// - if they are equal, we also want to take the abbreviation text into result
229-
// - if they are not equal, we trust the offset more
230227
const currentTimezoneOffset = result.start.get("timezoneOffset");
231228
if (currentTimezoneOffset !== null && extractedTimezoneOffset != currentTimezoneOffset) {
232-
return;
229+
// We may already have extracted the timezone offset e.g. "11 am GMT+0900 (JST)"
230+
// - if they are equal, we also want to take the abbreviation text into result
231+
// - if they are not equal, we trust the offset more
232+
if (result.start.isCertain("timezoneOffset")) {
233+
return;
234+
}
235+
236+
// This is often because it's relative time with inferred timezone (e.g. in 1 hour, tomorrow)
237+
// Then, we want to double check the abbr case (e.g. "GET" not "get")
238+
if (timezoneAbbr != match[1]) {
239+
return;
240+
}
233241
}
234242

235243
result.text += match[0];

‎test/en/en_timezone_exp.test.ts

+34-8
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as chrono from "../../src";
22
import { testSingleCase } from "../test_util";
33

4-
test("Test - Parsing date with UTC offset", function () {
4+
test("Test - Parsing date/time with UTC offset", function () {
55
testSingleCase(chrono, "wednesday, september 16, 2020 at 11 am utc+02:45 ", (result, text) => {
66
expect(result.text).toBe("wednesday, september 16, 2020 at 11 am utc+02:45");
77

@@ -27,7 +27,7 @@ test("Test - Parsing date with UTC offset", function () {
2727
});
2828
});
2929

30-
test("Test - Parsing date with GMT offset", function () {
30+
test("Test - Parsing date/time with GMT offset", function () {
3131
testSingleCase(chrono, "wednesday, september 16, 2020 at 11 am GMT -08:45 ", (result, text) => {
3232
expect(result.text).toBe("wednesday, september 16, 2020 at 11 am GMT -08:45");
3333

@@ -45,7 +45,7 @@ test("Test - Parsing date with GMT offset", function () {
4545
});
4646
});
4747

48-
test("Test - Parsing date with timezone abbreviation", function () {
48+
test("Test - Parsing date/time with timezone abbreviation", function () {
4949
testSingleCase(chrono, "wednesday, september 16, 2020 at 11 am", (result, text) => {
5050
expect(result.text).toBe(text);
5151

@@ -73,21 +73,47 @@ test("Test - Parsing date with timezone abbreviation", function () {
7373
});
7474
});
7575

76-
test("Test - Not parsing timezone from relative date", function () {
76+
test("Test - Parsing date with timezone abbreviation", function () {
77+
testSingleCase(chrono, "Wednesday, September 16, 2020, EST", (result, text) => {
78+
expect(result.text).toBe(text);
79+
expect(result.start.get("timezoneOffset")).toBe(-300);
80+
});
81+
});
82+
83+
test("Test - Not parsing timezone from relative time", function () {
7784
const refDate = new Date(2020, 11 - 1, 14, 13, 48, 22);
7885

7986
testSingleCase(chrono, "in 1 hour get eggs and milk", refDate, (result, text) => {
8087
expect(result.text).toBe("in 1 hour");
8188
expect(result.start.get("timezoneOffset")).toBe(-refDate.getTimezoneOffset());
8289
});
8390

84-
// testSingleCase(chrono, "in 1 hour GMT", refDate, (result, text) => {
85-
// expect(result.text).toBe("in 1 hour");
86-
// expect(result.start.get("timezoneOffset")).toBe(-refDate.getTimezoneOffset());
87-
// });
91+
testSingleCase(chrono, "in 3 hours GMT", refDate, (result, text) => {
92+
expect(result.text).toBe("in 3 hours");
93+
expect(result.start.get("timezoneOffset")).toBe(-refDate.getTimezoneOffset());
94+
});
95+
});
96+
97+
test("Test - Parsing timezone from relative date when valid", function () {
98+
const refDate = new Date(2020, 11 - 1, 14, 13, 48, 22);
8899

89100
testSingleCase(chrono, "in 1 day get eggs and milk", refDate, (result, text) => {
90101
expect(result.text).toBe("in 1 day");
91102
expect(result.start.get("timezoneOffset")).toBe(-refDate.getTimezoneOffset());
92103
});
104+
105+
testSingleCase(chrono, "in 1 day GET", refDate, (result, text) => {
106+
expect(result.text).toBe("in 1 day GET");
107+
expect(result.start.get("timezoneOffset")).toBe(240);
108+
});
109+
110+
testSingleCase(chrono, "today EST", (result, text) => {
111+
expect(result.text).toBe(text);
112+
expect(result.start.get("timezoneOffset")).toBe(-300);
113+
});
114+
115+
testSingleCase(chrono, "next week EST", (result, text) => {
116+
expect(result.text).toBe(text);
117+
expect(result.start.get("timezoneOffset")).toBe(-300);
118+
});
93119
});

0 commit comments

Comments
 (0)
Please sign in to comment.