chrono-node
Advanced tools
Comparing version 2.3.3 to 2.3.4
@@ -16,3 +16,5 @@ "use strict"; | ||
dayjs_2.assignSimilarTime(component, targetDate); | ||
component.assign("timezoneOffset", targetDate.utcOffset()); | ||
if (reference.timezoneOffset !== null) { | ||
component.assign("timezoneOffset", targetDate.utcOffset()); | ||
} | ||
return component; | ||
@@ -19,0 +21,0 @@ } |
@@ -123,3 +123,3 @@ "use strict"; | ||
const date = this.dateWithoutTimezoneAdjustment(); | ||
return new Date(date.getTime() + this.getSystemTimezoneAdjustmentMinute() * 60000); | ||
return new Date(date.getTime() + this.getSystemTimezoneAdjustmentMinute(date) * 60000); | ||
} | ||
@@ -131,6 +131,9 @@ dateWithoutTimezoneAdjustment() { | ||
} | ||
getSystemTimezoneAdjustmentMinute() { | ||
var _a; | ||
const currentTimezoneOffset = -new Date().getTimezoneOffset(); | ||
const targetTimezoneOffset = (_a = this.get("timezoneOffset")) !== null && _a !== void 0 ? _a : this.reference.timezoneOffset; | ||
getSystemTimezoneAdjustmentMinute(date) { | ||
var _a, _b; | ||
if (!date || date.getTime() < 0) { | ||
date = new Date(); | ||
} | ||
const currentTimezoneOffset = -date.getTimezoneOffset(); | ||
const targetTimezoneOffset = (_b = (_a = this.get("timezoneOffset")) !== null && _a !== void 0 ? _a : this.reference.timezoneOffset) !== null && _b !== void 0 ? _b : currentTimezoneOffset; | ||
return currentTimezoneOffset - targetTimezoneOffset; | ||
@@ -137,0 +140,0 @@ } |
@@ -18,3 +18,3 @@ { | ||
"license": "MIT", | ||
"version": "2.3.3", | ||
"version": "2.3.4", | ||
"directories": { | ||
@@ -21,0 +21,0 @@ "source": "./src", |
@@ -11,3 +11,5 @@ import { ParsingComponents, ReferenceWithTimezone } from "../results"; | ||
assignSimilarTime(component, targetDate); | ||
component.assign("timezoneOffset", targetDate.utcOffset()); | ||
if (reference.timezoneOffset !== null) { | ||
component.assign("timezoneOffset", targetDate.utcOffset()); | ||
} | ||
return component; | ||
@@ -14,0 +16,0 @@ } |
@@ -145,3 +145,3 @@ import { Component, ParsedComponents, ParsedResult, ParsingReference } from "./index"; | ||
const date = this.dateWithoutTimezoneAdjustment(); | ||
return new Date(date.getTime() + this.getSystemTimezoneAdjustmentMinute() * 60000); | ||
return new Date(date.getTime() + this.getSystemTimezoneAdjustmentMinute(date) * 60000); | ||
} | ||
@@ -164,6 +164,13 @@ | ||
private getSystemTimezoneAdjustmentMinute() { | ||
const currentTimezoneOffset = -new Date().getTimezoneOffset(); | ||
const targetTimezoneOffset = this.get("timezoneOffset") ?? this.reference.timezoneOffset; | ||
private getSystemTimezoneAdjustmentMinute(date?: Date) { | ||
if (!date || date.getTime() < 0) { | ||
// Javascript date timezone calculation got effect when the time epoch < 0 | ||
// e.g. new Date('Tue Feb 02 1300 00:00:00 GMT+0900 (JST)') => Tue Feb 02 1300 00:18:59 GMT+0918 (JST) | ||
date = new Date(); | ||
} | ||
const currentTimezoneOffset = -date.getTimezoneOffset(); | ||
const targetTimezoneOffset = | ||
this.get("timezoneOffset") ?? this.reference.timezoneOffset ?? currentTimezoneOffset; | ||
return currentTimezoneOffset - targetTimezoneOffset; | ||
@@ -170,0 +177,0 @@ } |
@@ -24,2 +24,13 @@ import * as chrono from "../../src/"; | ||
testSingleCase( | ||
chrono.casual, | ||
"The Deadline is now, without implicit local timezone", | ||
{ instant: new Date(1637674343000), timezone: null }, | ||
(result) => { | ||
expect(result.text).toBe("now"); | ||
expect(result.start).toBeDate(new Date(1637674343000)); | ||
expect(result.start.isCertain("timezoneOffset")).toBe(false); | ||
} | ||
); | ||
testSingleCase(chrono.casual, "The Deadline is today", new Date(2012, 7, 10, 14, 12), (result) => { | ||
@@ -26,0 +37,0 @@ expect(result.index).toBe(16); |
@@ -84,18 +84,93 @@ import * as chrono from "../../src"; | ||
test("Test - Not parsing timezone from relative time", function () { | ||
const refDate = new Date(2020, 11 - 1, 14, 13, 48, 22); | ||
{ | ||
const refInstant = new Date("Sun Nov 29 2020 13:24:13 GMT+0900 (Japan Standard Time)"); | ||
const expectedInstant = new Date("Sun Nov 29 2020 14:24:13 GMT+0900 (Japan Standard Time)"); | ||
testSingleCase(chrono, "in 1 hour get eggs and milk", refDate, (result, text) => { | ||
expect(result.text).toBe("in 1 hour"); | ||
expect(result.start.get("timezoneOffset")).toBe(-refDate.getTimezoneOffset()); | ||
testSingleCase(chrono, "in 1 hour get eggs and milk", refInstant, (result, text) => { | ||
expect(result.text).toBe("in 1 hour"); | ||
expect(result.start.get("timezoneOffset")).toBe(-refInstant.getTimezoneOffset()); | ||
expect(result.start).toBeDate(expectedInstant); | ||
}); | ||
} | ||
{ | ||
const refInstant = new Date("Sun Nov 29 2020 13:24:13 GMT+0900 (Japan Standard Time)"); | ||
const expectedInstant = new Date("Sun Nov 29 2020 14:24:13 GMT+0900 (Japan Standard Time)"); | ||
testSingleCase(chrono, "in 1 hour GMT", refInstant, (result, text) => { | ||
expect(result.text).toBe("in 1 hour"); | ||
expect(result.start).toBeDate(expectedInstant); | ||
}); | ||
} | ||
{ | ||
const refInstant = new Date("Sun Nov 29 2020 13:24:13 GMT+0900 (Japan Standard Time)"); | ||
const expectedInstant = new Date("Sun Nov 29 2020 14:24:13 GMT+0900 (Japan Standard Time)"); | ||
testSingleCase(chrono, "in 1 hour GMT", { instant: refInstant, timezone: "JST" }, (result, text) => { | ||
expect(result.text).toBe("in 1 hour"); | ||
expect(result.start).toBeDate(expectedInstant); | ||
}); | ||
} | ||
{ | ||
const refInstant = new Date("Sun Nov 29 2020 13:24:13 GMT+0900 (Japan Standard Time)"); | ||
const expectedInstant = new Date("Sun Nov 29 2020 14:24:13 GMT+0900 (Japan Standard Time)"); | ||
testSingleCase(chrono, "in 1 hour GMT", { instant: refInstant, timezone: "BST" }, (result, text) => { | ||
expect(result.text).toBe("in 1 hour"); | ||
expect(result.start).toBeDate(expectedInstant); | ||
}); | ||
} | ||
}); | ||
test("Test - Relative time (Now) is not effected by timezone setting", function () { | ||
const refInstant = new Date(1637674343000); | ||
testSingleCase(chrono, "now", { instant: refInstant }, (result) => { | ||
expect(result.text).toBe("now"); | ||
expect(result.start).toBeDate(refInstant); | ||
}); | ||
// This test fail when the system/assume timezone is exactly similar to the mentioned timezone | ||
// Temporary disable this test | ||
testSingleCase(chrono, "now", { instant: refInstant, timezone: null }, (result) => { | ||
expect(result.text).toBe("now"); | ||
expect(result.start).toBeDate(refInstant); | ||
}); | ||
// testSingleCase(chrono, "in 3 hours GMT", refDate, (result, text) => { | ||
// expect(result.text).toBe("in 3 hours"); | ||
// expect(result.start.get("timezoneOffset")).toBe(-refDate.getTimezoneOffset()); | ||
// }); | ||
testSingleCase(chrono, "now", { instant: refInstant, timezone: "BST" }, (result) => { | ||
expect(result.text).toBe("now"); | ||
expect(result.start).toBeDate(refInstant); | ||
}); | ||
testSingleCase(chrono, "now", { instant: refInstant, timezone: "JST" }, (result) => { | ||
expect(result.text).toBe("now"); | ||
expect(result.start).toBeDate(refInstant); | ||
}); | ||
}); | ||
test("Test - Relative time (2 hour later) is not effected by timezone setting", function () { | ||
const refInstant = new Date(1637674343000); | ||
const expectedInstant = new Date(1637674343000 + 2 * 60 * 60 * 1000); | ||
testSingleCase(chrono, "2 hour later", { instant: refInstant }, (result) => { | ||
expect(result.text).toBe("2 hour later"); | ||
expect(result.start).toBeDate(expectedInstant); | ||
}); | ||
testSingleCase(chrono, "2 hour later", { instant: refInstant, timezone: null }, (result) => { | ||
expect(result.text).toBe("2 hour later"); | ||
expect(result.start).toBeDate(expectedInstant); | ||
}); | ||
testSingleCase(chrono, "2 hour later", { instant: refInstant, timezone: "BST" }, (result) => { | ||
expect(result.text).toBe("2 hour later"); | ||
expect(result.start).toBeDate(expectedInstant); | ||
}); | ||
testSingleCase(chrono, "2 hour later", { instant: refInstant, timezone: "JST" }, (result) => { | ||
expect(result.text).toBe("2 hour later"); | ||
expect(result.start).toBeDate(expectedInstant); | ||
}); | ||
}); | ||
test("Test - Parsing timezone from relative date when valid", function () { | ||
@@ -102,0 +177,0 @@ const refDate = new Date(2020, 11 - 1, 14, 13, 48, 22); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
1352244
26599