chrono-node 2.8.2 → 2.8.3
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.
- package/README.md +1 -1
- package/dist/cjs/calculation/duration.d.ts +8 -0
- package/dist/cjs/calculation/duration.js +124 -0
- package/dist/cjs/calculation/duration.js.map +1 -0
- package/dist/cjs/{common/calculation → calculation}/weekdays.d.ts +2 -2
- package/dist/cjs/{common/calculation → calculation}/weekdays.js +3 -3
- package/dist/cjs/calculation/weekdays.js.map +1 -0
- package/dist/cjs/common/casualReferences.js +1 -1
- package/dist/cjs/common/casualReferences.js.map +1 -1
- package/dist/cjs/common/refiners/ForwardDateRefiner.js.map +1 -1
- package/dist/cjs/common/refiners/UnlikelyFormatFilter.js +0 -6
- package/dist/cjs/common/refiners/UnlikelyFormatFilter.js.map +1 -1
- package/dist/cjs/index.d.ts +2 -2
- package/dist/cjs/index.js +2 -1
- package/dist/cjs/index.js.map +1 -1
- package/dist/cjs/locales/de/parsers/DEWeekdayParser.js +1 -1
- package/dist/cjs/locales/de/parsers/DEWeekdayParser.js.map +1 -1
- package/dist/cjs/locales/en/parsers/ENTimeUnitAgoFormatParser.js +4 -5
- package/dist/cjs/locales/en/parsers/ENTimeUnitAgoFormatParser.js.map +1 -1
- package/dist/cjs/locales/en/parsers/ENTimeUnitCasualRelativeFormatParser.js +5 -5
- package/dist/cjs/locales/en/parsers/ENTimeUnitCasualRelativeFormatParser.js.map +1 -1
- package/dist/cjs/locales/en/parsers/ENTimeUnitWithinFormatParser.js +4 -0
- package/dist/cjs/locales/en/parsers/ENTimeUnitWithinFormatParser.js.map +1 -1
- package/dist/cjs/locales/en/parsers/ENWeekdayParser.js +1 -1
- package/dist/cjs/locales/en/parsers/ENWeekdayParser.js.map +1 -1
- package/dist/cjs/locales/en/refiners/ENMergeRelativeFollowByDateRefiner.js +4 -4
- package/dist/cjs/locales/en/refiners/ENMergeRelativeFollowByDateRefiner.js.map +1 -1
- package/dist/cjs/locales/es/parsers/ESWeekdayParser.js +1 -1
- package/dist/cjs/locales/es/parsers/ESWeekdayParser.js.map +1 -1
- package/dist/cjs/locales/fr/parsers/FRWeekdayParser.js +1 -1
- package/dist/cjs/locales/fr/parsers/FRWeekdayParser.js.map +1 -1
- package/dist/cjs/locales/it/parsers/ITRelativeDateFormatParser.d.ts +1 -1
- package/dist/cjs/locales/it/parsers/ITRelativeDateFormatParser.js +2 -2
- package/dist/cjs/locales/it/parsers/ITWeekdayParser.js +1 -1
- package/dist/cjs/locales/it/parsers/ITWeekdayParser.js.map +1 -1
- package/dist/cjs/locales/ja/parsers/JPWeekdayParser.js +1 -1
- package/dist/cjs/locales/ja/parsers/JPWeekdayParser.js.map +1 -1
- package/dist/cjs/locales/ja/parsers/JPWeekdayWithParenthesesParser.js +1 -1
- package/dist/cjs/locales/ja/parsers/JPWeekdayWithParenthesesParser.js.map +1 -1
- package/dist/cjs/locales/nl/parsers/NLWeekdayParser.js +1 -1
- package/dist/cjs/locales/nl/parsers/NLWeekdayParser.js.map +1 -1
- package/dist/cjs/locales/pt/parsers/PTWeekdayParser.js +1 -1
- package/dist/cjs/locales/pt/parsers/PTWeekdayParser.js.map +1 -1
- package/dist/cjs/locales/ru/parsers/RUWeekdayParser.js +1 -1
- package/dist/cjs/locales/ru/parsers/RUWeekdayParser.js.map +1 -1
- package/dist/cjs/locales/uk/parsers/UKWeekdayParser.js +1 -1
- package/dist/cjs/locales/uk/parsers/UKWeekdayParser.js.map +1 -1
- package/dist/cjs/results.d.ts +3 -4
- package/dist/cjs/results.js +26 -27
- package/dist/cjs/results.js.map +1 -1
- package/dist/cjs/types.d.ts +1 -0
- package/dist/cjs/types.js.map +1 -1
- package/dist/cjs/utils/dates.d.ts +2 -0
- package/dist/cjs/utils/dates.js +16 -0
- package/dist/cjs/utils/dates.js.map +1 -1
- package/dist/cjs/utils/timeunits.js.map +1 -1
- package/dist/esm/calculation/duration.d.ts +8 -0
- package/dist/esm/calculation/duration.js +119 -0
- package/dist/esm/calculation/duration.js.map +1 -0
- package/dist/esm/{common/calculation → calculation}/weekdays.d.ts +2 -2
- package/dist/esm/{common/calculation → calculation}/weekdays.js +3 -3
- package/dist/esm/calculation/weekdays.js.map +1 -0
- package/dist/esm/common/casualReferences.js +1 -1
- package/dist/esm/common/casualReferences.js.map +1 -1
- package/dist/esm/common/refiners/ForwardDateRefiner.js.map +1 -1
- package/dist/esm/common/refiners/UnlikelyFormatFilter.js +0 -6
- package/dist/esm/common/refiners/UnlikelyFormatFilter.js.map +1 -1
- package/dist/esm/index.d.ts +2 -2
- package/dist/esm/index.js +2 -2
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/locales/de/parsers/DEWeekdayParser.js +1 -1
- package/dist/esm/locales/de/parsers/DEWeekdayParser.js.map +1 -1
- package/dist/esm/locales/en/parsers/ENTimeUnitAgoFormatParser.js +4 -5
- package/dist/esm/locales/en/parsers/ENTimeUnitAgoFormatParser.js.map +1 -1
- package/dist/esm/locales/en/parsers/ENTimeUnitCasualRelativeFormatParser.js +5 -5
- package/dist/esm/locales/en/parsers/ENTimeUnitCasualRelativeFormatParser.js.map +1 -1
- package/dist/esm/locales/en/parsers/ENTimeUnitWithinFormatParser.js +4 -0
- package/dist/esm/locales/en/parsers/ENTimeUnitWithinFormatParser.js.map +1 -1
- package/dist/esm/locales/en/parsers/ENWeekdayParser.js +1 -1
- package/dist/esm/locales/en/parsers/ENWeekdayParser.js.map +1 -1
- package/dist/esm/locales/en/refiners/ENMergeRelativeFollowByDateRefiner.js +4 -4
- package/dist/esm/locales/en/refiners/ENMergeRelativeFollowByDateRefiner.js.map +1 -1
- package/dist/esm/locales/es/parsers/ESWeekdayParser.js +1 -1
- package/dist/esm/locales/es/parsers/ESWeekdayParser.js.map +1 -1
- package/dist/esm/locales/fr/parsers/FRWeekdayParser.js +1 -1
- package/dist/esm/locales/fr/parsers/FRWeekdayParser.js.map +1 -1
- package/dist/esm/locales/it/index.js +2 -2
- package/dist/esm/locales/it/parsers/ITRelativeDateFormatParser.d.ts +1 -1
- package/dist/esm/locales/it/parsers/ITRelativeDateFormatParser.js +1 -1
- package/dist/esm/locales/it/parsers/ITWeekdayParser.js +1 -1
- package/dist/esm/locales/it/parsers/ITWeekdayParser.js.map +1 -1
- package/dist/esm/locales/ja/parsers/JPWeekdayParser.js +1 -1
- package/dist/esm/locales/ja/parsers/JPWeekdayParser.js.map +1 -1
- package/dist/esm/locales/ja/parsers/JPWeekdayWithParenthesesParser.js +1 -1
- package/dist/esm/locales/ja/parsers/JPWeekdayWithParenthesesParser.js.map +1 -1
- package/dist/esm/locales/nl/parsers/NLWeekdayParser.js +1 -1
- package/dist/esm/locales/nl/parsers/NLWeekdayParser.js.map +1 -1
- package/dist/esm/locales/pt/parsers/PTWeekdayParser.js +1 -1
- package/dist/esm/locales/pt/parsers/PTWeekdayParser.js.map +1 -1
- package/dist/esm/locales/ru/parsers/RUWeekdayParser.js +1 -1
- package/dist/esm/locales/ru/parsers/RUWeekdayParser.js.map +1 -1
- package/dist/esm/locales/uk/parsers/UKWeekdayParser.js +1 -1
- package/dist/esm/locales/uk/parsers/UKWeekdayParser.js.map +1 -1
- package/dist/esm/results.d.ts +3 -4
- package/dist/esm/results.js +23 -24
- package/dist/esm/results.js.map +1 -1
- package/dist/esm/types.d.ts +1 -0
- package/dist/esm/types.js.map +1 -1
- package/dist/esm/utils/dates.d.ts +2 -0
- package/dist/esm/utils/dates.js +14 -0
- package/dist/esm/utils/dates.js.map +1 -1
- package/dist/esm/utils/timeunits.js.map +1 -1
- package/package.json +1 -1
- package/src/calculation/duration.ts +143 -0
- package/src/{common/calculation → calculation}/weekdays.ts +3 -3
- package/src/common/casualReferences.ts +1 -1
- package/src/common/refiners/ForwardDateRefiner.ts +0 -1
- package/src/common/refiners/UnlikelyFormatFilter.ts +0 -8
- package/src/index.ts +2 -2
- package/src/locales/de/parsers/DEWeekdayParser.ts +1 -1
- package/src/locales/en/parsers/ENTimeUnitAgoFormatParser.ts +4 -5
- package/src/locales/en/parsers/ENTimeUnitCasualRelativeFormatParser.ts +5 -5
- package/src/locales/en/parsers/ENTimeUnitWithinFormatParser.ts +5 -0
- package/src/locales/en/parsers/ENWeekdayParser.ts +1 -1
- package/src/locales/en/refiners/ENMergeRelativeFollowByDateRefiner.ts +4 -4
- package/src/locales/es/parsers/ESWeekdayParser.ts +1 -1
- package/src/locales/fr/parsers/FRWeekdayParser.ts +1 -1
- package/src/locales/it/index.ts +2 -2
- package/src/locales/it/parsers/ITRelativeDateFormatParser.ts +1 -1
- package/src/locales/it/parsers/ITWeekdayParser.ts +1 -1
- package/src/locales/ja/parsers/JPWeekdayParser.ts +1 -1
- package/src/locales/ja/parsers/JPWeekdayWithParenthesesParser.ts +1 -1
- package/src/locales/nl/parsers/NLWeekdayParser.ts +1 -1
- package/src/locales/pt/parsers/PTWeekdayParser.ts +1 -1
- package/src/locales/ru/parsers/RUWeekdayParser.ts +1 -1
- package/src/locales/uk/parsers/UKWeekdayParser.ts +1 -1
- package/src/results.ts +23 -27
- package/src/types.ts +2 -0
- package/src/utils/dates.ts +26 -0
- package/src/utils/timeunits.ts +9 -0
- package/test/calculation_duration.test.ts +165 -0
- package/test/{calculation.test.ts → calculation_weekdays.test.ts} +1 -1
- package/test/en/en_month_name_little_endian.test.ts +19 -0
- package/test/en/en_time_exp.test.ts +14 -0
- package/test/en/en_year.test.ts +38 -0
- package/test/en/negative_cases.test.ts +5 -0
- package/dist/cjs/common/calculation/weekdays.js.map +0 -1
- package/dist/esm/common/calculation/weekdays.js.map +0 -1
package/dist/esm/utils/dates.js
CHANGED
|
@@ -1,3 +1,16 @@
|
|
|
1
|
+
import { Meridiem } from "../types.js";
|
|
2
|
+
export function assignSimilarDate(component, target) {
|
|
3
|
+
component.assign("day", target.getDate());
|
|
4
|
+
component.assign("month", target.getMonth() + 1);
|
|
5
|
+
component.assign("year", target.getFullYear());
|
|
6
|
+
}
|
|
7
|
+
export function assignSimilarTime(component, target) {
|
|
8
|
+
component.assign("hour", target.getHours());
|
|
9
|
+
component.assign("minute", target.getMinutes());
|
|
10
|
+
component.assign("second", target.getSeconds());
|
|
11
|
+
component.assign("millisecond", target.getMilliseconds());
|
|
12
|
+
component.assign("meridiem", target.getHours() < 12 ? Meridiem.AM : Meridiem.PM);
|
|
13
|
+
}
|
|
1
14
|
export function implySimilarDate(component, target) {
|
|
2
15
|
component.imply("day", target.getDate());
|
|
3
16
|
component.imply("month", target.getMonth() + 1);
|
|
@@ -8,5 +21,6 @@ export function implySimilarTime(component, target) {
|
|
|
8
21
|
component.imply("minute", target.getMinutes());
|
|
9
22
|
component.imply("second", target.getSeconds());
|
|
10
23
|
component.imply("millisecond", target.getMilliseconds());
|
|
24
|
+
component.imply("meridiem", target.getHours() < 12 ? Meridiem.AM : Meridiem.PM);
|
|
11
25
|
}
|
|
12
26
|
//# sourceMappingURL=dates.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"dates.js","sourceRoot":"","sources":["../../../src/utils/dates.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"dates.js","sourceRoot":"","sources":["../../../src/utils/dates.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,QAAQ,EAAE,MAAM,UAAU,CAAC;AAOpC,MAAM,UAAU,iBAAiB,CAAC,SAA4B,EAAE,MAAY;IACxE,SAAS,CAAC,MAAM,CAAC,KAAK,EAAE,MAAM,CAAC,OAAO,EAAE,CAAC,CAAC;IAC1C,SAAS,CAAC,MAAM,CAAC,OAAO,EAAE,MAAM,CAAC,QAAQ,EAAE,GAAG,CAAC,CAAC,CAAC;IACjD,SAAS,CAAC,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,WAAW,EAAE,CAAC,CAAC;AACnD,CAAC;AAOD,MAAM,UAAU,iBAAiB,CAAC,SAA4B,EAAE,MAAY;IACxE,SAAS,CAAC,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,QAAQ,EAAE,CAAC,CAAC;IAC5C,SAAS,CAAC,MAAM,CAAC,QAAQ,EAAE,MAAM,CAAC,UAAU,EAAE,CAAC,CAAC;IAChD,SAAS,CAAC,MAAM,CAAC,QAAQ,EAAE,MAAM,CAAC,UAAU,EAAE,CAAC,CAAC;IAChD,SAAS,CAAC,MAAM,CAAC,aAAa,EAAE,MAAM,CAAC,eAAe,EAAE,CAAC,CAAC;IAC1D,SAAS,CAAC,MAAM,CAAC,UAAU,EAAE,MAAM,CAAC,QAAQ,EAAE,GAAG,EAAE,CAAC,CAAC,CAAC,QAAQ,CAAC,EAAE,CAAC,CAAC,CAAC,QAAQ,CAAC,EAAE,CAAC,CAAC;AACrF,CAAC;AAOD,MAAM,UAAU,gBAAgB,CAAC,SAA4B,EAAE,MAAY;IACvE,SAAS,CAAC,KAAK,CAAC,KAAK,EAAE,MAAM,CAAC,OAAO,EAAE,CAAC,CAAC;IACzC,SAAS,CAAC,KAAK,CAAC,OAAO,EAAE,MAAM,CAAC,QAAQ,EAAE,GAAG,CAAC,CAAC,CAAC;IAChD,SAAS,CAAC,KAAK,CAAC,MAAM,EAAE,MAAM,CAAC,WAAW,EAAE,CAAC,CAAC;AAClD,CAAC;AAOD,MAAM,UAAU,gBAAgB,CAAC,SAA4B,EAAE,MAAY;IACvE,SAAS,CAAC,KAAK,CAAC,MAAM,EAAE,MAAM,CAAC,QAAQ,EAAE,CAAC,CAAC;IAC3C,SAAS,CAAC,KAAK,CAAC,QAAQ,EAAE,MAAM,CAAC,UAAU,EAAE,CAAC,CAAC;IAC/C,SAAS,CAAC,KAAK,CAAC,QAAQ,EAAE,MAAM,CAAC,UAAU,EAAE,CAAC,CAAC;IAC/C,SAAS,CAAC,KAAK,CAAC,aAAa,EAAE,MAAM,CAAC,eAAe,EAAE,CAAC,CAAC;IACzD,SAAS,CAAC,KAAK,CAAC,UAAU,EAAE,MAAM,CAAC,QAAQ,EAAE,GAAG,EAAE,CAAC,CAAC,CAAC,QAAQ,CAAC,EAAE,CAAC,CAAC,CAAC,QAAQ,CAAC,EAAE,CAAC,CAAC;AACpF,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"timeunits.js","sourceRoot":"","sources":["../../../src/utils/timeunits.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"timeunits.js","sourceRoot":"","sources":["../../../src/utils/timeunits.ts"],"names":[],"mappings":"AAWA,MAAM,UAAU,gBAAgB,CAAC,SAAoB;IACjD,MAAM,QAAQ,GAAG,EAAE,CAAC;IACpB,KAAK,MAAM,GAAG,IAAI,SAAS,EAAE,CAAC;QAE1B,QAAQ,CAAC,GAAG,CAAC,GAAG,CAAC,SAAS,CAAC,GAAG,CAAC,CAAC;IACpC,CAAC;IAED,OAAO,QAAqB,CAAC;AACjC,CAAC;AAKD,MAAM,UAAU,mBAAmB,CAAC,UAA6B,EAAE,SAAoB;IACnF,MAAM,MAAM,GAAG,UAAU,CAAC,KAAK,EAAE,CAAC;IAElC,IAAI,IAAI,GAAG,UAAU,CAAC,KAAK,EAAE,CAAC;IAC9B,KAAK,MAAM,GAAG,IAAI,SAAS,EAAE,CAAC;QAE1B,IAAI,GAAG,IAAI,CAAC,GAAG,CAAC,SAAS,CAAC,GAAG,CAAC,EAAE,GAAgB,CAAC,CAAC;IACtD,CAAC;IAED,IAAI,KAAK,IAAI,SAAS,IAAI,GAAG,IAAI,SAAS,IAAI,MAAM,IAAI,SAAS,IAAI,OAAO,IAAI,SAAS,IAAI,MAAM,IAAI,SAAS,EAAE,CAAC;QAC/G,MAAM,CAAC,KAAK,CAAC,KAAK,EAAE,IAAI,CAAC,IAAI,EAAE,CAAC,CAAC;QACjC,MAAM,CAAC,KAAK,CAAC,OAAO,EAAE,IAAI,CAAC,KAAK,EAAE,GAAG,CAAC,CAAC,CAAC;QACxC,MAAM,CAAC,KAAK,CAAC,MAAM,EAAE,IAAI,CAAC,IAAI,EAAE,CAAC,CAAC;IACtC,CAAC;IAED,IAAI,QAAQ,IAAI,SAAS,IAAI,QAAQ,IAAI,SAAS,IAAI,MAAM,IAAI,SAAS,EAAE,CAAC;QACxE,MAAM,CAAC,KAAK,CAAC,QAAQ,EAAE,IAAI,CAAC,MAAM,EAAE,CAAC,CAAC;QACtC,MAAM,CAAC,KAAK,CAAC,QAAQ,EAAE,IAAI,CAAC,MAAM,EAAE,CAAC,CAAC;QACtC,MAAM,CAAC,KAAK,CAAC,MAAM,EAAE,IAAI,CAAC,IAAI,EAAE,CAAC,CAAC;IACtC,CAAC;IAED,OAAO,MAAM,CAAC;AAClB,CAAC"}
|
package/package.json
CHANGED
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
import { Timeunit } from "../types";
|
|
2
|
+
|
|
3
|
+
export type TimeunitShorten = "y" | "mo" | "M" | "w" | "d" | "h" | "m" | "s" | "ms";
|
|
4
|
+
export type TimeunitSpecial = "quarter";
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* A type represent a directed time duration as a set of values by timeunits.
|
|
8
|
+
* The positive values mean the time duration into the future.
|
|
9
|
+
*/
|
|
10
|
+
export type Duration = { [c in Timeunit | TimeunitSpecial | TimeunitShorten]?: number };
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Returns the date after adding the given `duration` to `ref`.
|
|
14
|
+
* @param ref
|
|
15
|
+
* @param duration
|
|
16
|
+
*/
|
|
17
|
+
export function addDuration(ref: Date, duration: Duration): Date {
|
|
18
|
+
let date = new Date(ref);
|
|
19
|
+
|
|
20
|
+
// Replace short timeunit keys with full timeunit keys
|
|
21
|
+
if (duration["y"]) {
|
|
22
|
+
duration["year"] = duration["y"];
|
|
23
|
+
delete duration["y"];
|
|
24
|
+
}
|
|
25
|
+
if (duration["mo"]) {
|
|
26
|
+
duration["month"] = duration["mo"];
|
|
27
|
+
delete duration["mo"];
|
|
28
|
+
}
|
|
29
|
+
if (duration["M"]) {
|
|
30
|
+
duration["month"] = duration["M"];
|
|
31
|
+
delete duration["M"];
|
|
32
|
+
}
|
|
33
|
+
if (duration["w"]) {
|
|
34
|
+
duration["week"] = duration["w"];
|
|
35
|
+
delete duration["w"];
|
|
36
|
+
}
|
|
37
|
+
if (duration["d"]) {
|
|
38
|
+
duration["day"] = duration["d"];
|
|
39
|
+
delete duration["d"];
|
|
40
|
+
}
|
|
41
|
+
if (duration["h"]) {
|
|
42
|
+
duration["hour"] = duration["h"];
|
|
43
|
+
delete duration["h"];
|
|
44
|
+
}
|
|
45
|
+
if (duration["m"]) {
|
|
46
|
+
duration["minute"] = duration["m"];
|
|
47
|
+
delete duration["m"];
|
|
48
|
+
}
|
|
49
|
+
if (duration["s"]) {
|
|
50
|
+
duration["second"] = duration["s"];
|
|
51
|
+
delete duration["s"];
|
|
52
|
+
}
|
|
53
|
+
if (duration["ms"]) {
|
|
54
|
+
duration["millisecond"] = duration["ms"];
|
|
55
|
+
delete duration["ms"];
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
if ("year" in duration) {
|
|
59
|
+
const floor = Math.floor(duration["year"]);
|
|
60
|
+
date.setFullYear(date.getFullYear() + floor);
|
|
61
|
+
const remainingFraction = duration["year"] - floor;
|
|
62
|
+
if (remainingFraction > 0) {
|
|
63
|
+
duration.month = duration?.month ?? 0;
|
|
64
|
+
duration.month += remainingFraction * 12;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
if ("quarter" in duration) {
|
|
68
|
+
const floor = Math.floor(duration["quarter"]);
|
|
69
|
+
date.setMonth(date.getMonth() + floor * 3);
|
|
70
|
+
}
|
|
71
|
+
if ("month" in duration) {
|
|
72
|
+
const floor = Math.floor(duration["month"]);
|
|
73
|
+
date.setMonth(date.getMonth() + floor);
|
|
74
|
+
const remainingFraction = duration["month"] - floor;
|
|
75
|
+
if (remainingFraction > 0) {
|
|
76
|
+
duration.week = duration?.week ?? 0;
|
|
77
|
+
duration.week += remainingFraction * 4;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
if ("week" in duration) {
|
|
81
|
+
const floor = Math.floor(duration["week"]);
|
|
82
|
+
date.setDate(date.getDate() + floor * 7);
|
|
83
|
+
const remainingFraction = duration["week"] - floor;
|
|
84
|
+
if (remainingFraction > 0) {
|
|
85
|
+
duration.day = duration?.day ?? 0;
|
|
86
|
+
duration.day += Math.round(remainingFraction * 7);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
if ("day" in duration) {
|
|
90
|
+
const floor = Math.floor(duration["day"]);
|
|
91
|
+
date.setDate(date.getDate() + floor);
|
|
92
|
+
const remainingFraction = duration["day"] - floor;
|
|
93
|
+
if (remainingFraction > 0) {
|
|
94
|
+
duration.hour = duration?.hour ?? 0;
|
|
95
|
+
duration.hour += Math.round(remainingFraction * 24);
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
if ("hour" in duration) {
|
|
99
|
+
const floor = Math.floor(duration["hour"]);
|
|
100
|
+
date.setHours(date.getHours() + floor);
|
|
101
|
+
const remainingFraction = duration["hour"] - floor;
|
|
102
|
+
if (remainingFraction > 0) {
|
|
103
|
+
duration.minute = duration?.minute ?? 0;
|
|
104
|
+
duration.minute += Math.round(remainingFraction * 60);
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
if ("minute" in duration) {
|
|
108
|
+
const floor = Math.floor(duration["minute"]);
|
|
109
|
+
date.setMinutes(date.getMinutes() + floor);
|
|
110
|
+
const remainingFraction = duration["minute"] - floor;
|
|
111
|
+
if (remainingFraction > 0) {
|
|
112
|
+
duration.second = duration?.second ?? 0;
|
|
113
|
+
duration.second += Math.round(remainingFraction * 60);
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
if ("second" in duration) {
|
|
117
|
+
const floor = Math.floor(duration["second"]);
|
|
118
|
+
date.setSeconds(date.getSeconds() + floor);
|
|
119
|
+
const remainingFraction = duration["second"] - floor;
|
|
120
|
+
if (remainingFraction > 0) {
|
|
121
|
+
duration.millisecond = duration?.millisecond ?? 0;
|
|
122
|
+
duration.millisecond += Math.round(remainingFraction * 1000);
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
if ("millisecond" in duration) {
|
|
126
|
+
const floor = Math.floor(duration["millisecond"]);
|
|
127
|
+
date.setMilliseconds(date.getMilliseconds() + floor);
|
|
128
|
+
}
|
|
129
|
+
return date;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* Return the reversed duration (e.g. back into the past, instead of future)
|
|
134
|
+
* @param duration
|
|
135
|
+
*/
|
|
136
|
+
export function reverseDuration(duration: Duration): Duration {
|
|
137
|
+
const reversed = {};
|
|
138
|
+
for (const key in duration) {
|
|
139
|
+
// noinspection JSUnfilteredForInLoop
|
|
140
|
+
reversed[key] = -duration[key];
|
|
141
|
+
}
|
|
142
|
+
return reversed as Duration;
|
|
143
|
+
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { Weekday } from "
|
|
2
|
-
import { ParsingComponents, ReferenceWithTimezone } from "
|
|
3
|
-
import { addImpliedTimeUnits } from "
|
|
1
|
+
import { Weekday } from "../types";
|
|
2
|
+
import { ParsingComponents, ReferenceWithTimezone } from "../results";
|
|
3
|
+
import { addImpliedTimeUnits } from "../utils/timeunits";
|
|
4
4
|
|
|
5
5
|
/**
|
|
6
6
|
* Returns the parsing components at the weekday (considering the modifier). The time and timezone is assume to be
|
|
@@ -137,7 +137,7 @@ export function afternoon(reference: ReferenceWithTimezone, implyHour = 15): Par
|
|
|
137
137
|
export function noon(reference: ReferenceWithTimezone): ParsingComponents {
|
|
138
138
|
const component = new ParsingComponents(reference, {});
|
|
139
139
|
component.imply("meridiem", Meridiem.AM);
|
|
140
|
-
component.
|
|
140
|
+
component.assign("hour", 12);
|
|
141
141
|
component.imply("minute", 0);
|
|
142
142
|
component.imply("second", 0);
|
|
143
143
|
component.imply("millisecond", 0);
|
|
@@ -7,7 +7,6 @@
|
|
|
7
7
|
import { ParsingContext, Refiner } from "../../chrono";
|
|
8
8
|
import { ParsingResult } from "../../results";
|
|
9
9
|
import dayjs from "dayjs";
|
|
10
|
-
import { implySimilarDate } from "../../utils/dayjs";
|
|
11
10
|
import * as dates from "../../utils/dates";
|
|
12
11
|
|
|
13
12
|
export default class ForwardDateRefiner implements Refiner {
|
|
@@ -47,14 +47,6 @@ export default class UnlikelyFormatFilter extends Filter {
|
|
|
47
47
|
return false;
|
|
48
48
|
}
|
|
49
49
|
|
|
50
|
-
if (result.start.isOnlyTime() && (!result.start.isCertain("hour") || !result.start.isCertain("minute"))) {
|
|
51
|
-
context.debug(() => {
|
|
52
|
-
console.log(`(Strict) Removing uncertain time component: ${result} (${result.end})`);
|
|
53
|
-
});
|
|
54
|
-
|
|
55
|
-
return false;
|
|
56
|
-
}
|
|
57
|
-
|
|
58
50
|
return true;
|
|
59
51
|
}
|
|
60
52
|
}
|
package/src/index.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import * as en from "./locales/en";
|
|
2
|
-
import { Chrono, Parser, Refiner } from "./chrono";
|
|
2
|
+
import { Chrono, Parser, ParsingContext, Refiner } from "./chrono";
|
|
3
3
|
import { ParsingResult, ParsingComponents, ReferenceWithTimezone } from "./results";
|
|
4
4
|
import { Component, ParsedComponents, ParsedResult, ParsingOption, ParsingReference, Meridiem, Weekday } from "./types";
|
|
5
5
|
|
|
6
|
-
export { en, Chrono, Parser, Refiner, ParsingResult, ParsingComponents, ReferenceWithTimezone };
|
|
6
|
+
export { en, Chrono, Parser, ParsingContext, Refiner, ParsingResult, ParsingComponents, ReferenceWithTimezone };
|
|
7
7
|
export { Component, ParsedComponents, ParsedResult, ParsingOption, ParsingReference, Meridiem, Weekday };
|
|
8
8
|
|
|
9
9
|
// Export all locales
|
|
@@ -3,7 +3,7 @@ import { ParsingComponents } from "../../../results";
|
|
|
3
3
|
import { WEEKDAY_DICTIONARY } from "../constants";
|
|
4
4
|
import { matchAnyPattern } from "../../../utils/pattern";
|
|
5
5
|
import { AbstractParserWithWordBoundaryChecking } from "../../../common/parsers/AbstractParserWithWordBoundary";
|
|
6
|
-
import { createParsingComponentsAtWeekday } from "../../../
|
|
6
|
+
import { createParsingComponentsAtWeekday } from "../../../calculation/weekdays";
|
|
7
7
|
|
|
8
8
|
const PATTERN = new RegExp(
|
|
9
9
|
"(?:(?:\\,|\\(|\\()\\s*)?" +
|
|
@@ -2,7 +2,7 @@ import { ParsingContext } from "../../../chrono";
|
|
|
2
2
|
import { parseTimeUnits, TIME_UNITS_NO_ABBR_PATTERN, TIME_UNITS_PATTERN } from "../constants";
|
|
3
3
|
import { ParsingComponents } from "../../../results";
|
|
4
4
|
import { AbstractParserWithWordBoundaryChecking } from "../../../common/parsers/AbstractParserWithWordBoundary";
|
|
5
|
-
import {
|
|
5
|
+
import { reverseDuration } from "../../../calculation/duration";
|
|
6
6
|
|
|
7
7
|
const PATTERN = new RegExp(`(${TIME_UNITS_PATTERN})\\s{0,5}(?:ago|before|earlier)(?=\\W|$)`, "i");
|
|
8
8
|
const STRICT_PATTERN = new RegExp(`(${TIME_UNITS_NO_ABBR_PATTERN})\\s{0,5}(?:ago|before|earlier)(?=\\W|$)`, "i");
|
|
@@ -17,11 +17,10 @@ export default class ENTimeUnitAgoFormatParser extends AbstractParserWithWordBou
|
|
|
17
17
|
}
|
|
18
18
|
|
|
19
19
|
innerExtract(context: ParsingContext, match: RegExpMatchArray) {
|
|
20
|
-
const
|
|
21
|
-
if (!
|
|
20
|
+
const duration = parseTimeUnits(match[1]);
|
|
21
|
+
if (!duration) {
|
|
22
22
|
return null;
|
|
23
23
|
}
|
|
24
|
-
|
|
25
|
-
return ParsingComponents.createRelativeFromReference(context.reference, outputTimeUnits);
|
|
24
|
+
return ParsingComponents.createRelativeFromReference(context.reference, reverseDuration(duration));
|
|
26
25
|
}
|
|
27
26
|
}
|
|
@@ -2,7 +2,7 @@ import { TIME_UNITS_PATTERN, parseTimeUnits, TIME_UNITS_NO_ABBR_PATTERN } from "
|
|
|
2
2
|
import { ParsingContext } from "../../../chrono";
|
|
3
3
|
import { ParsingComponents } from "../../../results";
|
|
4
4
|
import { AbstractParserWithWordBoundaryChecking } from "../../../common/parsers/AbstractParserWithWordBoundary";
|
|
5
|
-
import {
|
|
5
|
+
import { reverseDuration } from "../../../calculation/duration";
|
|
6
6
|
|
|
7
7
|
const PATTERN = new RegExp(`(this|last|past|next|after|\\+|-)\\s*(${TIME_UNITS_PATTERN})(?=\\W|$)`, "i");
|
|
8
8
|
const PATTERN_NO_ABBR = new RegExp(
|
|
@@ -21,17 +21,17 @@ export default class ENTimeUnitCasualRelativeFormatParser extends AbstractParser
|
|
|
21
21
|
|
|
22
22
|
innerExtract(context: ParsingContext, match: RegExpMatchArray) {
|
|
23
23
|
const prefix = match[1].toLowerCase();
|
|
24
|
-
let
|
|
25
|
-
if (!
|
|
24
|
+
let duration = parseTimeUnits(match[2]);
|
|
25
|
+
if (!duration) {
|
|
26
26
|
return null;
|
|
27
27
|
}
|
|
28
28
|
switch (prefix) {
|
|
29
29
|
case "last":
|
|
30
30
|
case "past":
|
|
31
31
|
case "-":
|
|
32
|
-
|
|
32
|
+
duration = reverseDuration(duration);
|
|
33
33
|
break;
|
|
34
34
|
}
|
|
35
|
-
return ParsingComponents.createRelativeFromReference(context.reference,
|
|
35
|
+
return ParsingComponents.createRelativeFromReference(context.reference, duration);
|
|
36
36
|
}
|
|
37
37
|
}
|
|
@@ -42,6 +42,11 @@ export default class ENTimeUnitWithinFormatParser extends AbstractParserWithWord
|
|
|
42
42
|
if (!timeUnits) {
|
|
43
43
|
return null;
|
|
44
44
|
}
|
|
45
|
+
context.debug(() => {
|
|
46
|
+
console.log(timeUnits);
|
|
47
|
+
console.log(ParsingComponents.createRelativeFromReference(context.reference, timeUnits));
|
|
48
|
+
});
|
|
49
|
+
|
|
45
50
|
return ParsingComponents.createRelativeFromReference(context.reference, timeUnits);
|
|
46
51
|
}
|
|
47
52
|
}
|
|
@@ -3,7 +3,7 @@ import { ParsingComponents } from "../../../results";
|
|
|
3
3
|
import { WEEKDAY_DICTIONARY } from "../constants";
|
|
4
4
|
import { matchAnyPattern } from "../../../utils/pattern";
|
|
5
5
|
import { AbstractParserWithWordBoundaryChecking } from "../../../common/parsers/AbstractParserWithWordBoundary";
|
|
6
|
-
import { createParsingComponentsAtWeekday } from "../../../
|
|
6
|
+
import { createParsingComponentsAtWeekday } from "../../../calculation/weekdays";
|
|
7
7
|
import { Weekday } from "../../../types";
|
|
8
8
|
|
|
9
9
|
const PATTERN = new RegExp(
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { MergingRefiner } from "../../../common/abstractRefiners";
|
|
2
2
|
import { ParsingComponents, ParsingResult, ReferenceWithTimezone } from "../../../results";
|
|
3
3
|
import { parseTimeUnits } from "../constants";
|
|
4
|
-
import {
|
|
4
|
+
import { reverseDuration } from "../../../calculation/duration";
|
|
5
5
|
|
|
6
6
|
function hasImpliedEarlierReferenceDate(result: ParsingResult): boolean {
|
|
7
7
|
return result.text.match(/\s+(before|from)$/i) != null;
|
|
@@ -38,14 +38,14 @@ export default class ENMergeRelativeFollowByDateRefiner extends MergingRefiner {
|
|
|
38
38
|
}
|
|
39
39
|
|
|
40
40
|
mergeResults(textBetween: string, currentResult: ParsingResult, nextResult: ParsingResult): ParsingResult {
|
|
41
|
-
let
|
|
41
|
+
let duration = parseTimeUnits(currentResult.text);
|
|
42
42
|
if (hasImpliedEarlierReferenceDate(currentResult)) {
|
|
43
|
-
|
|
43
|
+
duration = reverseDuration(duration);
|
|
44
44
|
}
|
|
45
45
|
|
|
46
46
|
const components = ParsingComponents.createRelativeFromReference(
|
|
47
47
|
new ReferenceWithTimezone(nextResult.start.date()),
|
|
48
|
-
|
|
48
|
+
duration
|
|
49
49
|
);
|
|
50
50
|
|
|
51
51
|
return new ParsingResult(
|
|
@@ -3,7 +3,7 @@ import { ParsingComponents } from "../../../results";
|
|
|
3
3
|
import { WEEKDAY_DICTIONARY } from "../constants";
|
|
4
4
|
import { matchAnyPattern } from "../../../utils/pattern";
|
|
5
5
|
import { AbstractParserWithWordBoundaryChecking } from "../../../common/parsers/AbstractParserWithWordBoundary";
|
|
6
|
-
import { createParsingComponentsAtWeekday } from "../../../
|
|
6
|
+
import { createParsingComponentsAtWeekday } from "../../../calculation/weekdays";
|
|
7
7
|
|
|
8
8
|
const PATTERN = new RegExp(
|
|
9
9
|
"(?:(?:\\,|\\(|\\()\\s*)?" +
|
|
@@ -3,7 +3,7 @@ import { ParsingComponents } from "../../../results";
|
|
|
3
3
|
import { WEEKDAY_DICTIONARY } from "../constants";
|
|
4
4
|
import { matchAnyPattern } from "../../../utils/pattern";
|
|
5
5
|
import { AbstractParserWithWordBoundaryChecking } from "../../../common/parsers/AbstractParserWithWordBoundary";
|
|
6
|
-
import { createParsingComponentsAtWeekday } from "../../../
|
|
6
|
+
import { createParsingComponentsAtWeekday } from "../../../calculation/weekdays";
|
|
7
7
|
|
|
8
8
|
const PATTERN = new RegExp(
|
|
9
9
|
"(?:(?:\\,|\\(|\\()\\s*)?" +
|
package/src/locales/it/index.ts
CHANGED
|
@@ -20,7 +20,7 @@ import { includeCommonConfiguration } from "../../configurations";
|
|
|
20
20
|
import ENCasualDateParser from "./parsers/ITCasualDateParser";
|
|
21
21
|
import ENCasualTimeParser from "./parsers/ITCasualTimeParser";
|
|
22
22
|
import ENWeekdayParser from "./parsers/ITWeekdayParser";
|
|
23
|
-
import
|
|
23
|
+
import ITRelativeDateFormatParser from "./parsers/ITRelativeDateFormatParser";
|
|
24
24
|
|
|
25
25
|
import { ParsedResult, ParsingOption } from "../../index";
|
|
26
26
|
import { Chrono, Configuration } from "../../chrono";
|
|
@@ -66,7 +66,7 @@ export function createCasualConfiguration(littleEndian = false): Configuration {
|
|
|
66
66
|
option.parsers.unshift(new ENCasualDateParser());
|
|
67
67
|
option.parsers.unshift(new ENCasualTimeParser());
|
|
68
68
|
option.parsers.unshift(new ENMonthNameParser());
|
|
69
|
-
option.parsers.unshift(new
|
|
69
|
+
option.parsers.unshift(new ITRelativeDateFormatParser());
|
|
70
70
|
option.parsers.unshift(new ENTimeUnitCasualRelativeFormatParser());
|
|
71
71
|
return option;
|
|
72
72
|
}
|
|
@@ -15,7 +15,7 @@ const PATTERN = new RegExp(
|
|
|
15
15
|
const MODIFIER_WORD_GROUP = 1;
|
|
16
16
|
const RELATIVE_WORD_GROUP = 2;
|
|
17
17
|
|
|
18
|
-
export default class
|
|
18
|
+
export default class ITRelativeDateFormatParser extends AbstractParserWithWordBoundaryChecking {
|
|
19
19
|
innerPattern(): RegExp {
|
|
20
20
|
return PATTERN;
|
|
21
21
|
}
|
|
@@ -3,7 +3,7 @@ import { ParsingComponents } from "../../../results";
|
|
|
3
3
|
import { WEEKDAY_DICTIONARY } from "../constants";
|
|
4
4
|
import { matchAnyPattern } from "../../../utils/pattern";
|
|
5
5
|
import { AbstractParserWithWordBoundaryChecking } from "../../../common/parsers/AbstractParserWithWordBoundary";
|
|
6
|
-
import { createParsingComponentsAtWeekday } from "../../../
|
|
6
|
+
import { createParsingComponentsAtWeekday } from "../../../calculation/weekdays";
|
|
7
7
|
|
|
8
8
|
const PATTERN = new RegExp(
|
|
9
9
|
"(?:(?:\\,|\\(|\\()\\s*)?" +
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { ParsingContext, Parser } from "../../../chrono";
|
|
2
2
|
import { ParsingComponents } from "../../../results";
|
|
3
3
|
import { WEEKDAY_OFFSET } from "../constants";
|
|
4
|
-
import { createParsingComponentsAtWeekday } from "../../../
|
|
4
|
+
import { createParsingComponentsAtWeekday } from "../../../calculation/weekdays";
|
|
5
5
|
|
|
6
6
|
const PATTERN = new RegExp(
|
|
7
7
|
"((?<prefix>前の|次の|今週))?(?<weekday>" + Object.keys(WEEKDAY_OFFSET).join("|") + ")(?:曜日|曜)",
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { ParsingContext, Parser } from "../../../chrono";
|
|
2
2
|
import { ParsingComponents } from "../../../results";
|
|
3
3
|
import { WEEKDAY_OFFSET } from "../constants";
|
|
4
|
-
import { createParsingComponentsAtWeekday } from "../../../
|
|
4
|
+
import { createParsingComponentsAtWeekday } from "../../../calculation/weekdays";
|
|
5
5
|
|
|
6
6
|
const PATTERN = new RegExp("(?:\\(|\\()(?<weekday>" + Object.keys(WEEKDAY_OFFSET).join("|") + ")(?:\\)|\\))", "i");
|
|
7
7
|
|
|
@@ -3,7 +3,7 @@ import { ParsingComponents } from "../../../results";
|
|
|
3
3
|
import { WEEKDAY_DICTIONARY } from "../../nl/constants";
|
|
4
4
|
import { matchAnyPattern } from "../../../utils/pattern";
|
|
5
5
|
import { AbstractParserWithWordBoundaryChecking } from "../../../common/parsers/AbstractParserWithWordBoundary";
|
|
6
|
-
import { createParsingComponentsAtWeekday } from "../../../
|
|
6
|
+
import { createParsingComponentsAtWeekday } from "../../../calculation/weekdays";
|
|
7
7
|
|
|
8
8
|
const PATTERN = new RegExp(
|
|
9
9
|
"(?:(?:\\,|\\(|\\()\\s*)?" +
|
|
@@ -3,7 +3,7 @@ import { ParsingComponents } from "../../../results";
|
|
|
3
3
|
import { WEEKDAY_DICTIONARY } from "../constants";
|
|
4
4
|
import { matchAnyPattern } from "../../../utils/pattern";
|
|
5
5
|
import { AbstractParserWithWordBoundaryChecking } from "../../../common/parsers/AbstractParserWithWordBoundary";
|
|
6
|
-
import { createParsingComponentsAtWeekday } from "../../../
|
|
6
|
+
import { createParsingComponentsAtWeekday } from "../../../calculation/weekdays";
|
|
7
7
|
|
|
8
8
|
const PATTERN = new RegExp(
|
|
9
9
|
"(?:(?:\\,|\\(|\\()\\s*)?" +
|
|
@@ -2,7 +2,7 @@ import { ParsingContext } from "../../../chrono";
|
|
|
2
2
|
import { ParsingComponents } from "../../../results";
|
|
3
3
|
import { WEEKDAY_DICTIONARY } from "../constants";
|
|
4
4
|
import { matchAnyPattern } from "../../../utils/pattern";
|
|
5
|
-
import { createParsingComponentsAtWeekday } from "../../../
|
|
5
|
+
import { createParsingComponentsAtWeekday } from "../../../calculation/weekdays";
|
|
6
6
|
import { AbstractParserWithLeftRightBoundaryChecking } from "./AbstractParserWithWordBoundaryChecking";
|
|
7
7
|
|
|
8
8
|
const PREFIX_GROUP = 1;
|
|
@@ -3,7 +3,7 @@ import { ParsingComponents } from "../../../results";
|
|
|
3
3
|
// TODO: ADD REGEX_PARTS below
|
|
4
4
|
import { WEEKDAY_DICTIONARY } from "../constants";
|
|
5
5
|
import { matchAnyPattern } from "../../../utils/pattern";
|
|
6
|
-
import { createParsingComponentsAtWeekday } from "../../../
|
|
6
|
+
import { createParsingComponentsAtWeekday } from "../../../calculation/weekdays";
|
|
7
7
|
import { AbstractParserWithLeftRightBoundaryChecking } from "./AbstractParserWithWordBoundaryChecking";
|
|
8
8
|
|
|
9
9
|
const PREFIX_GROUP = 1;
|
package/src/results.ts
CHANGED
|
@@ -2,8 +2,9 @@ import { Component, ParsedComponents, ParsedResult, ParsingReference } from "./t
|
|
|
2
2
|
|
|
3
3
|
import quarterOfYear from "dayjs/plugin/quarterOfYear";
|
|
4
4
|
import dayjs, { QUnitType } from "dayjs";
|
|
5
|
-
import { assignSimilarDate, assignSimilarTime, implySimilarTime } from "./utils/
|
|
5
|
+
import { assignSimilarDate, assignSimilarTime, implySimilarTime } from "./utils/dates";
|
|
6
6
|
import { toTimezoneOffset } from "./timezone";
|
|
7
|
+
import { addDuration, Duration } from "./calculation/duration";
|
|
7
8
|
dayjs.extend(quarterOfYear);
|
|
8
9
|
|
|
9
10
|
export class ReferenceWithTimezone {
|
|
@@ -215,18 +216,12 @@ export class ParsingComponents implements ParsedComponents {
|
|
|
215
216
|
return date;
|
|
216
217
|
}
|
|
217
218
|
|
|
218
|
-
static createRelativeFromReference(
|
|
219
|
-
reference
|
|
220
|
-
fragments: { [c in QUnitType]?: number }
|
|
221
|
-
): ParsingComponents {
|
|
222
|
-
let date = dayjs(reference.getDateWithAdjustedTimezone());
|
|
223
|
-
for (const key in fragments) {
|
|
224
|
-
date = date.add(fragments[key as QUnitType], key as QUnitType);
|
|
225
|
-
}
|
|
219
|
+
static createRelativeFromReference(reference: ReferenceWithTimezone, duration: Duration): ParsingComponents {
|
|
220
|
+
let date = addDuration(reference.getDateWithAdjustedTimezone(), duration);
|
|
226
221
|
|
|
227
222
|
const components = new ParsingComponents(reference);
|
|
228
223
|
components.addTag("result/relativeDate");
|
|
229
|
-
if (
|
|
224
|
+
if (duration["hour"] || duration["minute"] || duration["second"]) {
|
|
230
225
|
components.addTag("result/relativeDateAndTime");
|
|
231
226
|
assignSimilarTime(components, date);
|
|
232
227
|
assignSimilarDate(components, date);
|
|
@@ -235,26 +230,27 @@ export class ParsingComponents implements ParsedComponents {
|
|
|
235
230
|
implySimilarTime(components, date);
|
|
236
231
|
components.imply("timezoneOffset", reference.getTimezoneOffset());
|
|
237
232
|
|
|
238
|
-
if (
|
|
239
|
-
components.assign("day", date.
|
|
240
|
-
components.assign("month", date.
|
|
241
|
-
components.assign("year", date.
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
components.assign("
|
|
245
|
-
components.assign("
|
|
246
|
-
components.
|
|
233
|
+
if (duration["day"]) {
|
|
234
|
+
components.assign("day", date.getDate());
|
|
235
|
+
components.assign("month", date.getMonth() + 1);
|
|
236
|
+
components.assign("year", date.getFullYear());
|
|
237
|
+
components.assign("weekday", date.getDay());
|
|
238
|
+
} else if (duration["week"]) {
|
|
239
|
+
components.assign("day", date.getDate());
|
|
240
|
+
components.assign("month", date.getMonth() + 1);
|
|
241
|
+
components.assign("year", date.getFullYear());
|
|
242
|
+
components.imply("weekday", date.getDay());
|
|
247
243
|
} else {
|
|
248
|
-
components.imply("day", date.
|
|
249
|
-
if (
|
|
250
|
-
components.assign("month", date.
|
|
251
|
-
components.assign("year", date.
|
|
244
|
+
components.imply("day", date.getDate());
|
|
245
|
+
if (duration["month"]) {
|
|
246
|
+
components.assign("month", date.getMonth() + 1);
|
|
247
|
+
components.assign("year", date.getFullYear());
|
|
252
248
|
} else {
|
|
253
|
-
components.imply("month", date.
|
|
254
|
-
if (
|
|
255
|
-
components.assign("year", date.
|
|
249
|
+
components.imply("month", date.getMonth() + 1);
|
|
250
|
+
if (duration["year"]) {
|
|
251
|
+
components.assign("year", date.getFullYear());
|
|
256
252
|
} else {
|
|
257
|
-
components.imply("year", date.
|
|
253
|
+
components.imply("year", date.getFullYear());
|
|
258
254
|
}
|
|
259
255
|
}
|
|
260
256
|
}
|
package/src/types.ts
CHANGED
package/src/utils/dates.ts
CHANGED
|
@@ -1,4 +1,29 @@
|
|
|
1
1
|
import { ParsingComponents } from "../results";
|
|
2
|
+
import { Meridiem } from "../types";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Assign (force update) the parsing component to the same day as the `target`.
|
|
6
|
+
* @param component the component to be updated.
|
|
7
|
+
* @param target the target date with timezone adjusted.
|
|
8
|
+
*/
|
|
9
|
+
export function assignSimilarDate(component: ParsingComponents, target: Date) {
|
|
10
|
+
component.assign("day", target.getDate());
|
|
11
|
+
component.assign("month", target.getMonth() + 1);
|
|
12
|
+
component.assign("year", target.getFullYear());
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Assign (force update) the parsing component to the same time as the `target`.
|
|
17
|
+
* @param component the component to be updated.
|
|
18
|
+
* @param target the target date with timezone adjusted.
|
|
19
|
+
*/
|
|
20
|
+
export function assignSimilarTime(component: ParsingComponents, target: Date) {
|
|
21
|
+
component.assign("hour", target.getHours());
|
|
22
|
+
component.assign("minute", target.getMinutes());
|
|
23
|
+
component.assign("second", target.getSeconds());
|
|
24
|
+
component.assign("millisecond", target.getMilliseconds());
|
|
25
|
+
component.assign("meridiem", target.getHours() < 12 ? Meridiem.AM : Meridiem.PM);
|
|
26
|
+
}
|
|
2
27
|
|
|
3
28
|
/**
|
|
4
29
|
* Imply (weakly update) the parsing component to the same day as the `target`.
|
|
@@ -21,4 +46,5 @@ export function implySimilarTime(component: ParsingComponents, target: Date) {
|
|
|
21
46
|
component.imply("minute", target.getMinutes());
|
|
22
47
|
component.imply("second", target.getSeconds());
|
|
23
48
|
component.imply("millisecond", target.getMilliseconds());
|
|
49
|
+
component.imply("meridiem", target.getHours() < 12 ? Meridiem.AM : Meridiem.PM);
|
|
24
50
|
}
|