chrono-node 2.3.3 → 2.3.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -13,7 +13,9 @@ function now(reference) {
13
13
  const component = new results_1.ParsingComponents(reference, {});
14
14
  dayjs_2.assignSimilarDate(component, targetDate);
15
15
  dayjs_2.assignSimilarTime(component, targetDate);
16
- component.assign("timezoneOffset", targetDate.utcOffset());
16
+ if (reference.timezoneOffset !== null) {
17
+ component.assign("timezoneOffset", targetDate.utcOffset());
18
+ }
17
19
  return component;
18
20
  }
19
21
  exports.now = now;
package/dist/results.js CHANGED
@@ -120,17 +120,20 @@ class ParsingComponents {
120
120
  }
121
121
  date() {
122
122
  const date = this.dateWithoutTimezoneAdjustment();
123
- return new Date(date.getTime() + this.getSystemTimezoneAdjustmentMinute() * 60000);
123
+ return new Date(date.getTime() + this.getSystemTimezoneAdjustmentMinute(date) * 60000);
124
124
  }
125
125
  dateWithoutTimezoneAdjustment() {
126
126
  const date = new Date(this.get("year"), this.get("month") - 1, this.get("day"), this.get("hour"), this.get("minute"), this.get("second"), this.get("millisecond"));
127
127
  date.setFullYear(this.get("year"));
128
128
  return date;
129
129
  }
130
- getSystemTimezoneAdjustmentMinute() {
131
- var _a;
132
- const currentTimezoneOffset = -new Date().getTimezoneOffset();
133
- const targetTimezoneOffset = (_a = this.get("timezoneOffset")) !== null && _a !== void 0 ? _a : this.reference.timezoneOffset;
130
+ getSystemTimezoneAdjustmentMinute(date) {
131
+ var _a, _b;
132
+ if (!date || date.getTime() < 0) {
133
+ date = new Date();
134
+ }
135
+ const currentTimezoneOffset = -date.getTimezoneOffset();
136
+ const targetTimezoneOffset = (_b = (_a = this.get("timezoneOffset")) !== null && _a !== void 0 ? _a : this.reference.timezoneOffset) !== null && _b !== void 0 ? _b : currentTimezoneOffset;
134
137
  return currentTimezoneOffset - targetTimezoneOffset;
135
138
  }
136
139
  static createRelativeFromReference(reference, fragments) {
package/package.json CHANGED
@@ -15,7 +15,7 @@
15
15
  "preset": "ts-jest"
16
16
  },
17
17
  "license": "MIT",
18
- "version": "2.3.3",
18
+ "version": "2.3.4",
19
19
  "directories": {
20
20
  "source": "./src",
21
21
  "test": "./test"
@@ -8,7 +8,9 @@ export function now(reference: ReferenceWithTimezone): ParsingComponents {
8
8
  const component = new ParsingComponents(reference, {});
9
9
  assignSimilarDate(component, targetDate);
10
10
  assignSimilarTime(component, targetDate);
11
- component.assign("timezoneOffset", targetDate.utcOffset());
11
+ if (reference.timezoneOffset !== null) {
12
+ component.assign("timezoneOffset", targetDate.utcOffset());
13
+ }
12
14
  return component;
13
15
  }
14
16
 
package/src/results.ts CHANGED
@@ -142,7 +142,7 @@ export class ParsingComponents implements ParsedComponents {
142
142
 
143
143
  date(): Date {
144
144
  const date = this.dateWithoutTimezoneAdjustment();
145
- return new Date(date.getTime() + this.getSystemTimezoneAdjustmentMinute() * 60000);
145
+ return new Date(date.getTime() + this.getSystemTimezoneAdjustmentMinute(date) * 60000);
146
146
  }
147
147
 
148
148
  private dateWithoutTimezoneAdjustment() {
@@ -160,9 +160,16 @@ export class ParsingComponents implements ParsedComponents {
160
160
  return date;
161
161
  }
162
162
 
163
- private getSystemTimezoneAdjustmentMinute() {
164
- const currentTimezoneOffset = -new Date().getTimezoneOffset();
165
- const targetTimezoneOffset = this.get("timezoneOffset") ?? this.reference.timezoneOffset;
163
+ private getSystemTimezoneAdjustmentMinute(date?: Date) {
164
+ if (!date || date.getTime() < 0) {
165
+ // Javascript date timezone calculation got effect when the time epoch < 0
166
+ // e.g. new Date('Tue Feb 02 1300 00:00:00 GMT+0900 (JST)') => Tue Feb 02 1300 00:18:59 GMT+0918 (JST)
167
+ date = new Date();
168
+ }
169
+
170
+ const currentTimezoneOffset = -date.getTimezoneOffset();
171
+ const targetTimezoneOffset =
172
+ this.get("timezoneOffset") ?? this.reference.timezoneOffset ?? currentTimezoneOffset;
166
173
 
167
174
  return currentTimezoneOffset - targetTimezoneOffset;
168
175
  }
@@ -21,6 +21,17 @@ test("Test - Single Expression", () => {
21
21
  expect(result.start).toBeDate(new Date(2012, 7, 10, 8, 9, 10, 11));
22
22
  });
23
23
 
24
+ testSingleCase(
25
+ chrono.casual,
26
+ "The Deadline is now, without implicit local timezone",
27
+ { instant: new Date(1637674343000), timezone: null },
28
+ (result) => {
29
+ expect(result.text).toBe("now");
30
+ expect(result.start).toBeDate(new Date(1637674343000));
31
+ expect(result.start.isCertain("timezoneOffset")).toBe(false);
32
+ }
33
+ );
34
+
24
35
  testSingleCase(chrono.casual, "The Deadline is today", new Date(2012, 7, 10, 14, 12), (result) => {
25
36
  expect(result.index).toBe(16);
26
37
  expect(result.text).toBe("today");
@@ -81,20 +81,95 @@ test("Test - Parsing date with timezone abbreviation", function () {
81
81
  });
82
82
 
83
83
  test("Test - Not parsing timezone from relative time", function () {
84
- const refDate = new Date(2020, 11 - 1, 14, 13, 48, 22);
84
+ {
85
+ const refInstant = new Date("Sun Nov 29 2020 13:24:13 GMT+0900 (Japan Standard Time)");
86
+ const expectedInstant = new Date("Sun Nov 29 2020 14:24:13 GMT+0900 (Japan Standard Time)");
87
+
88
+ testSingleCase(chrono, "in 1 hour get eggs and milk", refInstant, (result, text) => {
89
+ expect(result.text).toBe("in 1 hour");
90
+ expect(result.start.get("timezoneOffset")).toBe(-refInstant.getTimezoneOffset());
91
+ expect(result.start).toBeDate(expectedInstant);
92
+ });
93
+ }
94
+
95
+ {
96
+ const refInstant = new Date("Sun Nov 29 2020 13:24:13 GMT+0900 (Japan Standard Time)");
97
+ const expectedInstant = new Date("Sun Nov 29 2020 14:24:13 GMT+0900 (Japan Standard Time)");
98
+
99
+ testSingleCase(chrono, "in 1 hour GMT", refInstant, (result, text) => {
100
+ expect(result.text).toBe("in 1 hour");
101
+ expect(result.start).toBeDate(expectedInstant);
102
+ });
103
+ }
104
+
105
+ {
106
+ const refInstant = new Date("Sun Nov 29 2020 13:24:13 GMT+0900 (Japan Standard Time)");
107
+ const expectedInstant = new Date("Sun Nov 29 2020 14:24:13 GMT+0900 (Japan Standard Time)");
108
+
109
+ testSingleCase(chrono, "in 1 hour GMT", { instant: refInstant, timezone: "JST" }, (result, text) => {
110
+ expect(result.text).toBe("in 1 hour");
111
+ expect(result.start).toBeDate(expectedInstant);
112
+ });
113
+ }
114
+
115
+ {
116
+ const refInstant = new Date("Sun Nov 29 2020 13:24:13 GMT+0900 (Japan Standard Time)");
117
+ const expectedInstant = new Date("Sun Nov 29 2020 14:24:13 GMT+0900 (Japan Standard Time)");
118
+
119
+ testSingleCase(chrono, "in 1 hour GMT", { instant: refInstant, timezone: "BST" }, (result, text) => {
120
+ expect(result.text).toBe("in 1 hour");
121
+ expect(result.start).toBeDate(expectedInstant);
122
+ });
123
+ }
124
+ });
85
125
 
86
- testSingleCase(chrono, "in 1 hour get eggs and milk", refDate, (result, text) => {
87
- expect(result.text).toBe("in 1 hour");
88
- expect(result.start.get("timezoneOffset")).toBe(-refDate.getTimezoneOffset());
126
+ test("Test - Relative time (Now) is not effected by timezone setting", function () {
127
+ const refInstant = new Date(1637674343000);
128
+
129
+ testSingleCase(chrono, "now", { instant: refInstant }, (result) => {
130
+ expect(result.text).toBe("now");
131
+ expect(result.start).toBeDate(refInstant);
132
+ });
133
+
134
+ testSingleCase(chrono, "now", { instant: refInstant, timezone: null }, (result) => {
135
+ expect(result.text).toBe("now");
136
+ expect(result.start).toBeDate(refInstant);
137
+ });
138
+
139
+ testSingleCase(chrono, "now", { instant: refInstant, timezone: "BST" }, (result) => {
140
+ expect(result.text).toBe("now");
141
+ expect(result.start).toBeDate(refInstant);
142
+ });
143
+
144
+ testSingleCase(chrono, "now", { instant: refInstant, timezone: "JST" }, (result) => {
145
+ expect(result.text).toBe("now");
146
+ expect(result.start).toBeDate(refInstant);
89
147
  });
148
+ });
90
149
 
91
- // This test fail when the system/assume timezone is exactly similar to the mentioned timezone
92
- // Temporary disable this test
150
+ test("Test - Relative time (2 hour later) is not effected by timezone setting", function () {
151
+ const refInstant = new Date(1637674343000);
152
+ const expectedInstant = new Date(1637674343000 + 2 * 60 * 60 * 1000);
93
153
 
94
- // testSingleCase(chrono, "in 3 hours GMT", refDate, (result, text) => {
95
- // expect(result.text).toBe("in 3 hours");
96
- // expect(result.start.get("timezoneOffset")).toBe(-refDate.getTimezoneOffset());
97
- // });
154
+ testSingleCase(chrono, "2 hour later", { instant: refInstant }, (result) => {
155
+ expect(result.text).toBe("2 hour later");
156
+ expect(result.start).toBeDate(expectedInstant);
157
+ });
158
+
159
+ testSingleCase(chrono, "2 hour later", { instant: refInstant, timezone: null }, (result) => {
160
+ expect(result.text).toBe("2 hour later");
161
+ expect(result.start).toBeDate(expectedInstant);
162
+ });
163
+
164
+ testSingleCase(chrono, "2 hour later", { instant: refInstant, timezone: "BST" }, (result) => {
165
+ expect(result.text).toBe("2 hour later");
166
+ expect(result.start).toBeDate(expectedInstant);
167
+ });
168
+
169
+ testSingleCase(chrono, "2 hour later", { instant: refInstant, timezone: "JST" }, (result) => {
170
+ expect(result.text).toBe("2 hour later");
171
+ expect(result.start).toBeDate(expectedInstant);
172
+ });
98
173
  });
99
174
 
100
175
  test("Test - Parsing timezone from relative date when valid", function () {