chrono-node 2.3.3 → 2.3.7
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 +2 -2
- package/dist/common/casualReferences.js +3 -1
- package/dist/common/parsers/AbstractParserWithWordBoundary.js +2 -1
- package/dist/common/refiners/ExtractTimezoneAbbrRefiner.js +5 -0
- package/dist/index.d.ts +2 -1
- package/dist/index.js +3 -1
- package/dist/locales/en/index.js +2 -1
- package/dist/locales/en/parsers/ENRelativeDateFormatParser.js +2 -2
- package/dist/locales/en/parsers/ENTimeUnitCasualRelativeFormatParser.js +1 -1
- package/dist/locales/en/refiners/ENMergeRelativeDateRefiner.d.ts +7 -0
- package/dist/locales/en/refiners/ENMergeRelativeDateRefiner.js +35 -0
- package/dist/locales/zh/hans/constants.d.ts +27 -0
- package/dist/locales/zh/hans/constants.js +51 -0
- package/dist/locales/zh/hans/index.d.ts +9 -0
- package/dist/locales/zh/hans/index.js +49 -0
- package/dist/locales/zh/hans/parsers/ZHHansCasualDateParser.d.ts +7 -0
- package/dist/locales/zh/hans/parsers/ZHHansCasualDateParser.js +138 -0
- package/dist/locales/zh/hans/parsers/ZHHansDateParser.d.ts +6 -0
- package/dist/locales/zh/hans/parsers/ZHHansDateParser.js +72 -0
- package/dist/locales/zh/hans/parsers/ZHHansDeadlineFormatParser.d.ts +6 -0
- package/dist/locales/zh/hans/parsers/ZHHansDeadlineFormatParser.js +78 -0
- package/dist/locales/zh/hans/parsers/ZHHansRelationWeekdayParser.d.ts +7 -0
- package/dist/locales/zh/hans/parsers/ZHHansRelationWeekdayParser.js +70 -0
- package/dist/locales/zh/hans/parsers/ZHHansTimeExpressionParser.d.ts +6 -0
- package/dist/locales/zh/hans/parsers/ZHHansTimeExpressionParser.js +438 -0
- package/dist/locales/zh/hans/parsers/ZHHansWeekdayParser.d.ts +7 -0
- package/dist/locales/zh/hans/parsers/ZHHansWeekdayParser.js +46 -0
- package/dist/locales/zh/hans/refiners/ZHHansMergeDateRangeRefiner.d.ts +4 -0
- package/dist/locales/zh/hans/refiners/ZHHansMergeDateRangeRefiner.js +12 -0
- package/dist/locales/zh/hans/refiners/ZHHansMergeDateTimeRefiner.d.ts +4 -0
- package/dist/locales/zh/hans/refiners/ZHHansMergeDateTimeRefiner.js +12 -0
- package/dist/locales/zh/hant/constants.d.ts +28 -0
- package/dist/locales/zh/hant/constants.js +52 -0
- package/dist/locales/zh/hant/index.d.ts +9 -0
- package/dist/locales/zh/hant/index.js +49 -0
- package/dist/locales/zh/hant/parsers/ZHHantCasualDateParser.d.ts +7 -0
- package/dist/locales/zh/hant/parsers/ZHHantCasualDateParser.js +138 -0
- package/dist/locales/zh/hant/parsers/ZHHantDateParser.d.ts +6 -0
- package/dist/locales/zh/hant/parsers/ZHHantDateParser.js +64 -0
- package/dist/locales/zh/hant/parsers/ZHHantDeadlineFormatParser.d.ts +6 -0
- package/dist/locales/zh/hant/parsers/ZHHantDeadlineFormatParser.js +78 -0
- package/dist/locales/zh/hant/parsers/ZHHantRelationWeekdayParser.d.ts +7 -0
- package/dist/locales/zh/hant/parsers/ZHHantRelationWeekdayParser.js +70 -0
- package/dist/locales/zh/hant/parsers/ZHHantTimeExpressionParser.d.ts +6 -0
- package/dist/locales/zh/hant/parsers/ZHHantTimeExpressionParser.js +438 -0
- package/dist/locales/zh/hant/parsers/ZHHantWeekdayParser.d.ts +7 -0
- package/dist/locales/zh/hant/parsers/ZHHantWeekdayParser.js +46 -0
- package/dist/locales/zh/hant/refiners/ZHHantMergeDateRangeRefiner.d.ts +4 -0
- package/dist/locales/zh/hant/refiners/ZHHantMergeDateRangeRefiner.js +12 -0
- package/dist/locales/zh/hant/refiners/ZHHantMergeDateTimeRefiner.d.ts +4 -0
- package/dist/locales/zh/hant/refiners/ZHHantMergeDateTimeRefiner.js +12 -0
- package/dist/locales/zh/index.d.ts +2 -0
- package/dist/locales/zh/index.js +16 -0
- package/dist/results.js +8 -5
- package/dist/timezone.js +1 -1
- package/dist/utils/dayjs.js +7 -0
- package/package.json +1 -1
- package/src/common/casualReferences.ts +3 -1
- package/src/common/parsers/AbstractParserWithWordBoundary.ts +1 -1
- package/src/common/refiners/ExtractTimezoneAbbrRefiner.ts +8 -0
- package/src/index.ts +2 -1
- package/src/locales/en/index.ts +2 -1
- package/src/locales/en/parsers/ENMonthNameLittleEndianParser.ts +0 -2
- package/src/locales/en/parsers/ENRelativeDateFormatParser.ts +2 -2
- package/src/locales/en/parsers/ENTimeUnitCasualRelativeFormatParser.ts +1 -1
- package/src/locales/en/refiners/ENMergeRelativeDateRefiner.ts +58 -0
- package/src/locales/zh/hans/constants.ts +52 -0
- package/src/locales/zh/hans/index.ts +62 -0
- package/src/locales/zh/hans/parsers/ZHHansCasualDateParser.ts +128 -0
- package/src/locales/zh/hans/parsers/ZHHansDateParser.ts +75 -0
- package/src/locales/zh/hans/parsers/ZHHansDeadlineFormatParser.ts +81 -0
- package/src/locales/zh/hans/parsers/ZHHansRelationWeekdayParser.ts +69 -0
- package/src/locales/zh/hans/parsers/ZHHansTimeExpressionParser.ts +424 -0
- package/src/locales/zh/hans/parsers/ZHHansWeekdayParser.ts +46 -0
- package/src/locales/zh/hans/refiners/ZHHansMergeDateRangeRefiner.ts +7 -0
- package/src/locales/zh/hans/refiners/ZHHansMergeDateTimeRefiner.ts +7 -0
- package/src/locales/zh/hant/constants.ts +53 -0
- package/src/locales/zh/hant/index.ts +63 -0
- package/src/locales/zh/hant/parsers/ZHHantCasualDateParser.ts +128 -0
- package/src/locales/zh/hant/parsers/ZHHantDateParser.ts +68 -0
- package/src/locales/zh/hant/parsers/ZHHantDeadlineFormatParser.ts +81 -0
- package/src/locales/zh/hant/parsers/ZHHantRelationWeekdayParser.ts +69 -0
- package/src/locales/zh/hant/parsers/ZHHantTimeExpressionParser.ts +424 -0
- package/src/locales/zh/hant/parsers/ZHHantWeekdayParser.ts +46 -0
- package/src/locales/zh/hant/refiners/ZHHantMergeDateRangeRefiner.ts +7 -0
- package/src/locales/zh/hant/refiners/ZHHantMergeDateTimeRefiner.ts +7 -0
- package/src/locales/zh/index.ts +2 -0
- package/src/results.ts +11 -4
- package/src/timezone.ts +1 -1
- package/src/utils/dayjs.ts +6 -0
- package/test/en/en_casual.test.ts +11 -0
- package/test/en/en_merging_relative_dates.test.ts +51 -0
- package/test/en/en_month.test.ts +13 -0
- package/test/en/en_relative.test.ts +20 -0
- package/test/en/en_time_units_ago.test.ts +16 -0
- package/test/en/en_time_units_casual_relative.test.ts +16 -0
- package/test/en/en_time_units_later.test.ts +6 -0
- package/test/en/en_timezone_exp.test.ts +85 -10
- package/test/zh/zh.test.ts +18 -0
- package/test/zh/zh_hans_casual.test.ts +228 -0
- package/test/zh/zh_hans_date.test.ts +102 -0
- package/test/zh/zh_hans_deadline.test.ts +136 -0
- package/test/zh/zh_hans_time_exp.test.ts +182 -0
- package/test/zh/zh_hans_weekday.test.ts +132 -0
- package/test/zh/zh_hant_casual.test.ts +246 -0
- package/test/zh/zh_hant_date.test.ts +102 -0
- package/test/zh/zh_hant_deadline.test.ts +136 -0
- package/test/zh/zh_hant_time_exp.test.ts +182 -0
- package/test/zh/zh_hant_weekday.test.ts +132 -0
|
@@ -0,0 +1,438 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
const dayjs_1 = __importDefault(require("dayjs"));
|
|
7
|
+
const AbstractParserWithWordBoundary_1 = require("../../../../common/parsers/AbstractParserWithWordBoundary");
|
|
8
|
+
const constants_1 = require("../constants");
|
|
9
|
+
const FIRST_REG_PATTERN = new RegExp("(?:由|從|自)?" +
|
|
10
|
+
"(?:" +
|
|
11
|
+
"(今|明|前|大前|後|大後|聽|昨|尋|琴)(早|朝|晚)|" +
|
|
12
|
+
"(上(?:午|晝)|朝(?:早)|早(?:上)|下(?:午|晝)|晏(?:晝)|晚(?:上)|夜(?:晚)?|中(?:午)|凌(?:晨))|" +
|
|
13
|
+
"(今|明|前|大前|後|大後|聽|昨|尋|琴)(?:日|天)" +
|
|
14
|
+
"(?:[\\s,,]*)" +
|
|
15
|
+
"(?:(上(?:午|晝)|朝(?:早)|早(?:上)|下(?:午|晝)|晏(?:晝)|晚(?:上)|夜(?:晚)?|中(?:午)|凌(?:晨)))?" +
|
|
16
|
+
")?" +
|
|
17
|
+
"(?:[\\s,,]*)" +
|
|
18
|
+
"(?:(\\d+|[" +
|
|
19
|
+
Object.keys(constants_1.NUMBER).join("") +
|
|
20
|
+
"]+)(?:\\s*)(?:點|時|:|:)" +
|
|
21
|
+
"(?:\\s*)" +
|
|
22
|
+
"(\\d+|半|正|整|[" +
|
|
23
|
+
Object.keys(constants_1.NUMBER).join("") +
|
|
24
|
+
"]+)?(?:\\s*)(?:分|:|:)?" +
|
|
25
|
+
"(?:\\s*)" +
|
|
26
|
+
"(\\d+|[" +
|
|
27
|
+
Object.keys(constants_1.NUMBER).join("") +
|
|
28
|
+
"]+)?(?:\\s*)(?:秒)?)" +
|
|
29
|
+
"(?:\\s*(A.M.|P.M.|AM?|PM?))?", "i");
|
|
30
|
+
const SECOND_REG_PATTERN = new RegExp("(?:^\\s*(?:到|至|\\-|\\–|\\~|\\〜)\\s*)" +
|
|
31
|
+
"(?:" +
|
|
32
|
+
"(今|明|前|大前|後|大後|聽|昨|尋|琴)(早|朝|晚)|" +
|
|
33
|
+
"(上(?:午|晝)|朝(?:早)|早(?:上)|下(?:午|晝)|晏(?:晝)|晚(?:上)|夜(?:晚)?|中(?:午)|凌(?:晨))|" +
|
|
34
|
+
"(今|明|前|大前|後|大後|聽|昨|尋|琴)(?:日|天)" +
|
|
35
|
+
"(?:[\\s,,]*)" +
|
|
36
|
+
"(?:(上(?:午|晝)|朝(?:早)|早(?:上)|下(?:午|晝)|晏(?:晝)|晚(?:上)|夜(?:晚)?|中(?:午)|凌(?:晨)))?" +
|
|
37
|
+
")?" +
|
|
38
|
+
"(?:[\\s,,]*)" +
|
|
39
|
+
"(?:(\\d+|[" +
|
|
40
|
+
Object.keys(constants_1.NUMBER).join("") +
|
|
41
|
+
"]+)(?:\\s*)(?:點|時|:|:)" +
|
|
42
|
+
"(?:\\s*)" +
|
|
43
|
+
"(\\d+|半|正|整|[" +
|
|
44
|
+
Object.keys(constants_1.NUMBER).join("") +
|
|
45
|
+
"]+)?(?:\\s*)(?:分|:|:)?" +
|
|
46
|
+
"(?:\\s*)" +
|
|
47
|
+
"(\\d+|[" +
|
|
48
|
+
Object.keys(constants_1.NUMBER).join("") +
|
|
49
|
+
"]+)?(?:\\s*)(?:秒)?)" +
|
|
50
|
+
"(?:\\s*(A.M.|P.M.|AM?|PM?))?", "i");
|
|
51
|
+
const DAY_GROUP_1 = 1;
|
|
52
|
+
const ZH_AM_PM_HOUR_GROUP_1 = 2;
|
|
53
|
+
const ZH_AM_PM_HOUR_GROUP_2 = 3;
|
|
54
|
+
const DAY_GROUP_3 = 4;
|
|
55
|
+
const ZH_AM_PM_HOUR_GROUP_3 = 5;
|
|
56
|
+
const HOUR_GROUP = 6;
|
|
57
|
+
const MINUTE_GROUP = 7;
|
|
58
|
+
const SECOND_GROUP = 8;
|
|
59
|
+
const AM_PM_HOUR_GROUP = 9;
|
|
60
|
+
class ZHHantTimeExpressionParser extends AbstractParserWithWordBoundary_1.AbstractParserWithWordBoundaryChecking {
|
|
61
|
+
innerPattern() {
|
|
62
|
+
return FIRST_REG_PATTERN;
|
|
63
|
+
}
|
|
64
|
+
innerExtract(context, match) {
|
|
65
|
+
if (match.index > 0 && context.text[match.index - 1].match(/\w/)) {
|
|
66
|
+
return null;
|
|
67
|
+
}
|
|
68
|
+
const refMoment = dayjs_1.default(context.refDate);
|
|
69
|
+
const result = context.createParsingResult(match.index, match[0]);
|
|
70
|
+
const startMoment = refMoment.clone();
|
|
71
|
+
if (match[DAY_GROUP_1]) {
|
|
72
|
+
var day1 = match[DAY_GROUP_1];
|
|
73
|
+
if (day1 == "明" || day1 == "聽") {
|
|
74
|
+
if (refMoment.hour() > 1) {
|
|
75
|
+
startMoment.add(1, "day");
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
else if (day1 == "昨" || day1 == "尋" || day1 == "琴") {
|
|
79
|
+
startMoment.add(-1, "day");
|
|
80
|
+
}
|
|
81
|
+
else if (day1 == "前") {
|
|
82
|
+
startMoment.add(-2, "day");
|
|
83
|
+
}
|
|
84
|
+
else if (day1 == "大前") {
|
|
85
|
+
startMoment.add(-3, "day");
|
|
86
|
+
}
|
|
87
|
+
else if (day1 == "後") {
|
|
88
|
+
startMoment.add(2, "day");
|
|
89
|
+
}
|
|
90
|
+
else if (day1 == "大後") {
|
|
91
|
+
startMoment.add(3, "day");
|
|
92
|
+
}
|
|
93
|
+
result.start.assign("day", startMoment.date());
|
|
94
|
+
result.start.assign("month", startMoment.month() + 1);
|
|
95
|
+
result.start.assign("year", startMoment.year());
|
|
96
|
+
}
|
|
97
|
+
else if (match[DAY_GROUP_3]) {
|
|
98
|
+
var day3 = match[DAY_GROUP_3];
|
|
99
|
+
if (day3 == "明" || day3 == "聽") {
|
|
100
|
+
startMoment.add(1, "day");
|
|
101
|
+
}
|
|
102
|
+
else if (day3 == "昨" || day3 == "尋" || day3 == "琴") {
|
|
103
|
+
startMoment.add(-1, "day");
|
|
104
|
+
}
|
|
105
|
+
else if (day3 == "前") {
|
|
106
|
+
startMoment.add(-2, "day");
|
|
107
|
+
}
|
|
108
|
+
else if (day3 == "大前") {
|
|
109
|
+
startMoment.add(-3, "day");
|
|
110
|
+
}
|
|
111
|
+
else if (day3 == "後") {
|
|
112
|
+
startMoment.add(2, "day");
|
|
113
|
+
}
|
|
114
|
+
else if (day3 == "大後") {
|
|
115
|
+
startMoment.add(3, "day");
|
|
116
|
+
}
|
|
117
|
+
result.start.assign("day", startMoment.date());
|
|
118
|
+
result.start.assign("month", startMoment.month() + 1);
|
|
119
|
+
result.start.assign("year", startMoment.year());
|
|
120
|
+
}
|
|
121
|
+
else {
|
|
122
|
+
result.start.imply("day", startMoment.date());
|
|
123
|
+
result.start.imply("month", startMoment.month() + 1);
|
|
124
|
+
result.start.imply("year", startMoment.year());
|
|
125
|
+
}
|
|
126
|
+
let hour = 0;
|
|
127
|
+
let minute = 0;
|
|
128
|
+
let meridiem = -1;
|
|
129
|
+
if (match[SECOND_GROUP]) {
|
|
130
|
+
var second = parseInt(match[SECOND_GROUP]);
|
|
131
|
+
if (isNaN(second)) {
|
|
132
|
+
second = constants_1.zhStringToNumber(match[SECOND_GROUP]);
|
|
133
|
+
}
|
|
134
|
+
if (second >= 60)
|
|
135
|
+
return null;
|
|
136
|
+
result.start.assign("second", second);
|
|
137
|
+
}
|
|
138
|
+
hour = parseInt(match[HOUR_GROUP]);
|
|
139
|
+
if (isNaN(hour)) {
|
|
140
|
+
hour = constants_1.zhStringToNumber(match[HOUR_GROUP]);
|
|
141
|
+
}
|
|
142
|
+
if (match[MINUTE_GROUP]) {
|
|
143
|
+
if (match[MINUTE_GROUP] == "半") {
|
|
144
|
+
minute = 30;
|
|
145
|
+
}
|
|
146
|
+
else if (match[MINUTE_GROUP] == "正" || match[MINUTE_GROUP] == "整") {
|
|
147
|
+
minute = 0;
|
|
148
|
+
}
|
|
149
|
+
else {
|
|
150
|
+
minute = parseInt(match[MINUTE_GROUP]);
|
|
151
|
+
if (isNaN(minute)) {
|
|
152
|
+
minute = constants_1.zhStringToNumber(match[MINUTE_GROUP]);
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
else if (hour > 100) {
|
|
157
|
+
minute = hour % 100;
|
|
158
|
+
hour = Math.floor(hour / 100);
|
|
159
|
+
}
|
|
160
|
+
if (minute >= 60) {
|
|
161
|
+
return null;
|
|
162
|
+
}
|
|
163
|
+
if (hour > 24) {
|
|
164
|
+
return null;
|
|
165
|
+
}
|
|
166
|
+
if (hour >= 12) {
|
|
167
|
+
meridiem = 1;
|
|
168
|
+
}
|
|
169
|
+
if (match[AM_PM_HOUR_GROUP]) {
|
|
170
|
+
if (hour > 12)
|
|
171
|
+
return null;
|
|
172
|
+
var ampm = match[AM_PM_HOUR_GROUP][0].toLowerCase();
|
|
173
|
+
if (ampm == "a") {
|
|
174
|
+
meridiem = 0;
|
|
175
|
+
if (hour == 12)
|
|
176
|
+
hour = 0;
|
|
177
|
+
}
|
|
178
|
+
if (ampm == "p") {
|
|
179
|
+
meridiem = 1;
|
|
180
|
+
if (hour != 12)
|
|
181
|
+
hour += 12;
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
else if (match[ZH_AM_PM_HOUR_GROUP_1]) {
|
|
185
|
+
var zhAMPMString1 = match[ZH_AM_PM_HOUR_GROUP_1];
|
|
186
|
+
var zhAMPM1 = zhAMPMString1[0];
|
|
187
|
+
if (zhAMPM1 == "朝" || zhAMPM1 == "早") {
|
|
188
|
+
meridiem = 0;
|
|
189
|
+
if (hour == 12)
|
|
190
|
+
hour = 0;
|
|
191
|
+
}
|
|
192
|
+
else if (zhAMPM1 == "晚") {
|
|
193
|
+
meridiem = 1;
|
|
194
|
+
if (hour != 12)
|
|
195
|
+
hour += 12;
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
else if (match[ZH_AM_PM_HOUR_GROUP_2]) {
|
|
199
|
+
var zhAMPMString2 = match[ZH_AM_PM_HOUR_GROUP_2];
|
|
200
|
+
var zhAMPM2 = zhAMPMString2[0];
|
|
201
|
+
if (zhAMPM2 == "上" || zhAMPM2 == "朝" || zhAMPM2 == "早" || zhAMPM2 == "凌") {
|
|
202
|
+
meridiem = 0;
|
|
203
|
+
if (hour == 12)
|
|
204
|
+
hour = 0;
|
|
205
|
+
}
|
|
206
|
+
else if (zhAMPM2 == "下" || zhAMPM2 == "晏" || zhAMPM2 == "晚") {
|
|
207
|
+
meridiem = 1;
|
|
208
|
+
if (hour != 12)
|
|
209
|
+
hour += 12;
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
else if (match[ZH_AM_PM_HOUR_GROUP_3]) {
|
|
213
|
+
var zhAMPMString3 = match[ZH_AM_PM_HOUR_GROUP_3];
|
|
214
|
+
var zhAMPM3 = zhAMPMString3[0];
|
|
215
|
+
if (zhAMPM3 == "上" || zhAMPM3 == "朝" || zhAMPM3 == "早" || zhAMPM3 == "凌") {
|
|
216
|
+
meridiem = 0;
|
|
217
|
+
if (hour == 12)
|
|
218
|
+
hour = 0;
|
|
219
|
+
}
|
|
220
|
+
else if (zhAMPM3 == "下" || zhAMPM3 == "晏" || zhAMPM3 == "晚") {
|
|
221
|
+
meridiem = 1;
|
|
222
|
+
if (hour != 12)
|
|
223
|
+
hour += 12;
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
result.start.assign("hour", hour);
|
|
227
|
+
result.start.assign("minute", minute);
|
|
228
|
+
if (meridiem >= 0) {
|
|
229
|
+
result.start.assign("meridiem", meridiem);
|
|
230
|
+
}
|
|
231
|
+
else {
|
|
232
|
+
if (hour < 12) {
|
|
233
|
+
result.start.imply("meridiem", 0);
|
|
234
|
+
}
|
|
235
|
+
else {
|
|
236
|
+
result.start.imply("meridiem", 1);
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
match = SECOND_REG_PATTERN.exec(context.text.substring(result.index + result.text.length));
|
|
240
|
+
if (!match) {
|
|
241
|
+
if (result.text.match(/^\d+$/)) {
|
|
242
|
+
return null;
|
|
243
|
+
}
|
|
244
|
+
return result;
|
|
245
|
+
}
|
|
246
|
+
const endMoment = startMoment.clone();
|
|
247
|
+
result.end = context.createParsingComponents();
|
|
248
|
+
if (match[DAY_GROUP_1]) {
|
|
249
|
+
var day1 = match[DAY_GROUP_1];
|
|
250
|
+
if (day1 == "明" || day1 == "聽") {
|
|
251
|
+
if (refMoment.hour() > 1) {
|
|
252
|
+
endMoment.add(1, "day");
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
else if (day1 == "昨" || day1 == "尋" || day1 == "琴") {
|
|
256
|
+
endMoment.add(-1, "day");
|
|
257
|
+
}
|
|
258
|
+
else if (day1 == "前") {
|
|
259
|
+
endMoment.add(-2, "day");
|
|
260
|
+
}
|
|
261
|
+
else if (day1 == "大前") {
|
|
262
|
+
endMoment.add(-3, "day");
|
|
263
|
+
}
|
|
264
|
+
else if (day1 == "後") {
|
|
265
|
+
endMoment.add(2, "day");
|
|
266
|
+
}
|
|
267
|
+
else if (day1 == "大後") {
|
|
268
|
+
endMoment.add(3, "day");
|
|
269
|
+
}
|
|
270
|
+
result.end.assign("day", endMoment.date());
|
|
271
|
+
result.end.assign("month", endMoment.month() + 1);
|
|
272
|
+
result.end.assign("year", endMoment.year());
|
|
273
|
+
}
|
|
274
|
+
else if (match[DAY_GROUP_3]) {
|
|
275
|
+
var day3 = match[DAY_GROUP_3];
|
|
276
|
+
if (day3 == "明" || day3 == "聽") {
|
|
277
|
+
endMoment.add(1, "day");
|
|
278
|
+
}
|
|
279
|
+
else if (day3 == "昨" || day3 == "尋" || day3 == "琴") {
|
|
280
|
+
endMoment.add(-1, "day");
|
|
281
|
+
}
|
|
282
|
+
else if (day3 == "前") {
|
|
283
|
+
endMoment.add(-2, "day");
|
|
284
|
+
}
|
|
285
|
+
else if (day3 == "大前") {
|
|
286
|
+
endMoment.add(-3, "day");
|
|
287
|
+
}
|
|
288
|
+
else if (day3 == "後") {
|
|
289
|
+
endMoment.add(2, "day");
|
|
290
|
+
}
|
|
291
|
+
else if (day3 == "大後") {
|
|
292
|
+
endMoment.add(3, "day");
|
|
293
|
+
}
|
|
294
|
+
result.end.assign("day", endMoment.date());
|
|
295
|
+
result.end.assign("month", endMoment.month() + 1);
|
|
296
|
+
result.end.assign("year", endMoment.year());
|
|
297
|
+
}
|
|
298
|
+
else {
|
|
299
|
+
result.end.imply("day", endMoment.date());
|
|
300
|
+
result.end.imply("month", endMoment.month() + 1);
|
|
301
|
+
result.end.imply("year", endMoment.year());
|
|
302
|
+
}
|
|
303
|
+
hour = 0;
|
|
304
|
+
minute = 0;
|
|
305
|
+
meridiem = -1;
|
|
306
|
+
if (match[SECOND_GROUP]) {
|
|
307
|
+
var second = parseInt(match[SECOND_GROUP]);
|
|
308
|
+
if (isNaN(second)) {
|
|
309
|
+
second = constants_1.zhStringToNumber(match[SECOND_GROUP]);
|
|
310
|
+
}
|
|
311
|
+
if (second >= 60)
|
|
312
|
+
return null;
|
|
313
|
+
result.end.assign("second", second);
|
|
314
|
+
}
|
|
315
|
+
hour = parseInt(match[HOUR_GROUP]);
|
|
316
|
+
if (isNaN(hour)) {
|
|
317
|
+
hour = constants_1.zhStringToNumber(match[HOUR_GROUP]);
|
|
318
|
+
}
|
|
319
|
+
if (match[MINUTE_GROUP]) {
|
|
320
|
+
if (match[MINUTE_GROUP] == "半") {
|
|
321
|
+
minute = 30;
|
|
322
|
+
}
|
|
323
|
+
else if (match[MINUTE_GROUP] == "正" || match[MINUTE_GROUP] == "整") {
|
|
324
|
+
minute = 0;
|
|
325
|
+
}
|
|
326
|
+
else {
|
|
327
|
+
minute = parseInt(match[MINUTE_GROUP]);
|
|
328
|
+
if (isNaN(minute)) {
|
|
329
|
+
minute = constants_1.zhStringToNumber(match[MINUTE_GROUP]);
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
else if (hour > 100) {
|
|
334
|
+
minute = hour % 100;
|
|
335
|
+
hour = Math.floor(hour / 100);
|
|
336
|
+
}
|
|
337
|
+
if (minute >= 60) {
|
|
338
|
+
return null;
|
|
339
|
+
}
|
|
340
|
+
if (hour > 24) {
|
|
341
|
+
return null;
|
|
342
|
+
}
|
|
343
|
+
if (hour >= 12) {
|
|
344
|
+
meridiem = 1;
|
|
345
|
+
}
|
|
346
|
+
if (match[AM_PM_HOUR_GROUP]) {
|
|
347
|
+
if (hour > 12)
|
|
348
|
+
return null;
|
|
349
|
+
var ampm = match[AM_PM_HOUR_GROUP][0].toLowerCase();
|
|
350
|
+
if (ampm == "a") {
|
|
351
|
+
meridiem = 0;
|
|
352
|
+
if (hour == 12)
|
|
353
|
+
hour = 0;
|
|
354
|
+
}
|
|
355
|
+
if (ampm == "p") {
|
|
356
|
+
meridiem = 1;
|
|
357
|
+
if (hour != 12)
|
|
358
|
+
hour += 12;
|
|
359
|
+
}
|
|
360
|
+
if (!result.start.isCertain("meridiem")) {
|
|
361
|
+
if (meridiem == 0) {
|
|
362
|
+
result.start.imply("meridiem", 0);
|
|
363
|
+
if (result.start.get("hour") == 12) {
|
|
364
|
+
result.start.assign("hour", 0);
|
|
365
|
+
}
|
|
366
|
+
}
|
|
367
|
+
else {
|
|
368
|
+
result.start.imply("meridiem", 1);
|
|
369
|
+
if (result.start.get("hour") != 12) {
|
|
370
|
+
result.start.assign("hour", result.start.get("hour") + 12);
|
|
371
|
+
}
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
}
|
|
375
|
+
else if (match[ZH_AM_PM_HOUR_GROUP_1]) {
|
|
376
|
+
var zhAMPMString1 = match[ZH_AM_PM_HOUR_GROUP_1];
|
|
377
|
+
var zhAMPM1 = zhAMPMString1[0];
|
|
378
|
+
if (zhAMPM1 == "朝" || zhAMPM1 == "早") {
|
|
379
|
+
meridiem = 0;
|
|
380
|
+
if (hour == 12)
|
|
381
|
+
hour = 0;
|
|
382
|
+
}
|
|
383
|
+
else if (zhAMPM1 == "晚") {
|
|
384
|
+
meridiem = 1;
|
|
385
|
+
if (hour != 12)
|
|
386
|
+
hour += 12;
|
|
387
|
+
}
|
|
388
|
+
}
|
|
389
|
+
else if (match[ZH_AM_PM_HOUR_GROUP_2]) {
|
|
390
|
+
var zhAMPMString2 = match[ZH_AM_PM_HOUR_GROUP_2];
|
|
391
|
+
var zhAMPM2 = zhAMPMString2[0];
|
|
392
|
+
if (zhAMPM2 == "上" || zhAMPM2 == "朝" || zhAMPM2 == "早" || zhAMPM2 == "凌") {
|
|
393
|
+
meridiem = 0;
|
|
394
|
+
if (hour == 12)
|
|
395
|
+
hour = 0;
|
|
396
|
+
}
|
|
397
|
+
else if (zhAMPM2 == "下" || zhAMPM2 == "晏" || zhAMPM2 == "晚") {
|
|
398
|
+
meridiem = 1;
|
|
399
|
+
if (hour != 12)
|
|
400
|
+
hour += 12;
|
|
401
|
+
}
|
|
402
|
+
}
|
|
403
|
+
else if (match[ZH_AM_PM_HOUR_GROUP_3]) {
|
|
404
|
+
var zhAMPMString3 = match[ZH_AM_PM_HOUR_GROUP_3];
|
|
405
|
+
var zhAMPM3 = zhAMPMString3[0];
|
|
406
|
+
if (zhAMPM3 == "上" || zhAMPM3 == "朝" || zhAMPM3 == "早" || zhAMPM3 == "凌") {
|
|
407
|
+
meridiem = 0;
|
|
408
|
+
if (hour == 12)
|
|
409
|
+
hour = 0;
|
|
410
|
+
}
|
|
411
|
+
else if (zhAMPM3 == "下" || zhAMPM3 == "晏" || zhAMPM3 == "晚") {
|
|
412
|
+
meridiem = 1;
|
|
413
|
+
if (hour != 12)
|
|
414
|
+
hour += 12;
|
|
415
|
+
}
|
|
416
|
+
}
|
|
417
|
+
result.text = result.text + match[0];
|
|
418
|
+
result.end.assign("hour", hour);
|
|
419
|
+
result.end.assign("minute", minute);
|
|
420
|
+
if (meridiem >= 0) {
|
|
421
|
+
result.end.assign("meridiem", meridiem);
|
|
422
|
+
}
|
|
423
|
+
else {
|
|
424
|
+
const startAtPM = result.start.isCertain("meridiem") && result.start.get("meridiem") == 1;
|
|
425
|
+
if (startAtPM && result.start.get("hour") > hour) {
|
|
426
|
+
result.end.imply("meridiem", 0);
|
|
427
|
+
}
|
|
428
|
+
else if (hour > 12) {
|
|
429
|
+
result.end.imply("meridiem", 1);
|
|
430
|
+
}
|
|
431
|
+
}
|
|
432
|
+
if (result.end.date().getTime() < result.start.date().getTime()) {
|
|
433
|
+
result.end.imply("day", result.end.get("day") + 1);
|
|
434
|
+
}
|
|
435
|
+
return result;
|
|
436
|
+
}
|
|
437
|
+
}
|
|
438
|
+
exports.default = ZHHantTimeExpressionParser;
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { ParsingContext } from "../../../../chrono";
|
|
2
|
+
import { AbstractParserWithWordBoundaryChecking } from "../../../../common/parsers/AbstractParserWithWordBoundary";
|
|
3
|
+
import { ParsingResult } from "../../../../results";
|
|
4
|
+
export default class ZHHantWeekdayParser extends AbstractParserWithWordBoundaryChecking {
|
|
5
|
+
innerPattern(): RegExp;
|
|
6
|
+
innerExtract(context: ParsingContext, match: RegExpMatchArray): ParsingResult;
|
|
7
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
const dayjs_1 = __importDefault(require("dayjs"));
|
|
7
|
+
const AbstractParserWithWordBoundary_1 = require("../../../../common/parsers/AbstractParserWithWordBoundary");
|
|
8
|
+
const constants_1 = require("../constants");
|
|
9
|
+
const PATTERN = new RegExp("(?:星期|禮拜|週)(?<weekday>" + Object.keys(constants_1.WEEKDAY_OFFSET).join("|") + ")");
|
|
10
|
+
class ZHHantWeekdayParser extends AbstractParserWithWordBoundary_1.AbstractParserWithWordBoundaryChecking {
|
|
11
|
+
innerPattern() {
|
|
12
|
+
return PATTERN;
|
|
13
|
+
}
|
|
14
|
+
innerExtract(context, match) {
|
|
15
|
+
const result = context.createParsingResult(match.index, match[0]);
|
|
16
|
+
const dayOfWeek = match.groups.weekday;
|
|
17
|
+
const offset = constants_1.WEEKDAY_OFFSET[dayOfWeek];
|
|
18
|
+
if (offset === undefined)
|
|
19
|
+
return null;
|
|
20
|
+
let startMoment = dayjs_1.default(context.refDate);
|
|
21
|
+
const startMomentFixed = false;
|
|
22
|
+
const refOffset = startMoment.day();
|
|
23
|
+
if (Math.abs(offset - 7 - refOffset) < Math.abs(offset - refOffset)) {
|
|
24
|
+
startMoment = startMoment.day(offset - 7);
|
|
25
|
+
}
|
|
26
|
+
else if (Math.abs(offset + 7 - refOffset) < Math.abs(offset - refOffset)) {
|
|
27
|
+
startMoment = startMoment.day(offset + 7);
|
|
28
|
+
}
|
|
29
|
+
else {
|
|
30
|
+
startMoment = startMoment.day(offset);
|
|
31
|
+
}
|
|
32
|
+
result.start.assign("weekday", offset);
|
|
33
|
+
if (startMomentFixed) {
|
|
34
|
+
result.start.assign("day", startMoment.date());
|
|
35
|
+
result.start.assign("month", startMoment.month() + 1);
|
|
36
|
+
result.start.assign("year", startMoment.year());
|
|
37
|
+
}
|
|
38
|
+
else {
|
|
39
|
+
result.start.imply("day", startMoment.date());
|
|
40
|
+
result.start.imply("month", startMoment.month() + 1);
|
|
41
|
+
result.start.imply("year", startMoment.year());
|
|
42
|
+
}
|
|
43
|
+
return result;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
exports.default = ZHHantWeekdayParser;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
const AbstractMergeDateRangeRefiner_1 = __importDefault(require("../../../../common/refiners/AbstractMergeDateRangeRefiner"));
|
|
7
|
+
class ZHHantMergeDateRangeRefiner extends AbstractMergeDateRangeRefiner_1.default {
|
|
8
|
+
patternBetween() {
|
|
9
|
+
return /^\s*(至|到|\-|\~|~|-|ー)\s*$/i;
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
exports.default = ZHHantMergeDateRangeRefiner;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
const AbstractMergeDateTimeRefiner_1 = __importDefault(require("../../../../common/refiners/AbstractMergeDateTimeRefiner"));
|
|
7
|
+
class ZHHantMergeDateTimeRefiner extends AbstractMergeDateTimeRefiner_1.default {
|
|
8
|
+
patternBetween() {
|
|
9
|
+
return /^\s*$/i;
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
exports.default = ZHHantMergeDateTimeRefiner;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
|
|
5
|
+
}) : (function(o, m, k, k2) {
|
|
6
|
+
if (k2 === undefined) k2 = k;
|
|
7
|
+
o[k2] = m[k];
|
|
8
|
+
}));
|
|
9
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
10
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
11
|
+
};
|
|
12
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
13
|
+
exports.hans = void 0;
|
|
14
|
+
__exportStar(require("./hant"), exports);
|
|
15
|
+
var hans_1 = require("./hans");
|
|
16
|
+
Object.defineProperty(exports, "hans", { enumerable: true, get: function () { return hans_1.hans; } });
|
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
|
-
|
|
133
|
-
|
|
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/dist/timezone.js
CHANGED
package/dist/utils/dayjs.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.implySimilarTime = exports.assignSimilarTime = exports.assignSimilarDate = exports.assignTheNextDay = void 0;
|
|
4
|
+
const index_1 = require("../index");
|
|
4
5
|
function assignTheNextDay(component, targetDayJs) {
|
|
5
6
|
targetDayJs = targetDayJs.add(1, "day");
|
|
6
7
|
assignSimilarDate(component, targetDayJs);
|
|
@@ -18,6 +19,12 @@ function assignSimilarTime(component, targetDayJs) {
|
|
|
18
19
|
component.assign("minute", targetDayJs.minute());
|
|
19
20
|
component.assign("second", targetDayJs.second());
|
|
20
21
|
component.assign("millisecond", targetDayJs.millisecond());
|
|
22
|
+
if (component.get("hour") < 12) {
|
|
23
|
+
component.assign("meridiem", index_1.Meridiem.AM);
|
|
24
|
+
}
|
|
25
|
+
else {
|
|
26
|
+
component.assign("meridiem", index_1.Meridiem.PM);
|
|
27
|
+
}
|
|
21
28
|
}
|
|
22
29
|
exports.assignSimilarTime = assignSimilarTime;
|
|
23
30
|
function implySimilarTime(component, targetDayJs) {
|
package/package.json
CHANGED
|
@@ -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
|
-
|
|
11
|
+
if (reference.timezoneOffset !== null) {
|
|
12
|
+
component.assign("timezoneOffset", targetDate.utcOffset());
|
|
13
|
+
}
|
|
12
14
|
return component;
|
|
13
15
|
}
|
|
14
16
|
|
|
@@ -27,7 +27,7 @@ export abstract class AbstractParserWithWordBoundaryChecking implements Parser {
|
|
|
27
27
|
}
|
|
28
28
|
|
|
29
29
|
extract(context: ParsingContext, match: RegExpMatchArray) {
|
|
30
|
-
const header = match[1];
|
|
30
|
+
const header = match[1] ?? "";
|
|
31
31
|
match.index = match.index + header.length;
|
|
32
32
|
match[0] = match[0].substring(header.length);
|
|
33
33
|
for (let i = 2; i < match.length; i++) {
|
|
@@ -240,6 +240,14 @@ export default class ExtractTimezoneAbbrRefiner implements Refiner {
|
|
|
240
240
|
}
|
|
241
241
|
}
|
|
242
242
|
|
|
243
|
+
if (result.start.isOnlyDate()) {
|
|
244
|
+
// If the time is not explicitly mentioned,
|
|
245
|
+
// Then, we also want to double check the abbr case (e.g. "GET" not "get")
|
|
246
|
+
if (timezoneAbbr != match[1]) {
|
|
247
|
+
return;
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
|
|
243
251
|
result.text += match[0];
|
|
244
252
|
|
|
245
253
|
if (!result.start.isCertain("timezoneOffset")) {
|
package/src/index.ts
CHANGED
|
@@ -105,7 +105,8 @@ import * as fr from "./locales/fr";
|
|
|
105
105
|
import * as ja from "./locales/ja";
|
|
106
106
|
import * as pt from "./locales/pt";
|
|
107
107
|
import * as nl from "./locales/nl";
|
|
108
|
-
|
|
108
|
+
import * as zh from "./locales/zh";
|
|
109
|
+
export { de, fr, ja, pt, nl, zh };
|
|
109
110
|
|
|
110
111
|
/**
|
|
111
112
|
* A shortcut for {@link en | chrono.en.strict}
|