@formkit/tempo 0.1.2 → 1.1.0
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 +22 -1
- package/dist/add.d.ts +11 -0
- package/dist/add.mjs +50 -0
- package/dist/add.mjs.map +1 -0
- package/dist/addDay.d.ts +4 -3
- package/dist/addDay.mjs.map +1 -1
- package/dist/addHour.d.ts +4 -3
- package/dist/addHour.mjs.map +1 -1
- package/dist/addMillisecond.d.ts +10 -0
- package/dist/addMillisecond.mjs +11 -0
- package/dist/addMillisecond.mjs.map +1 -0
- package/dist/addMinute.d.ts +5 -4
- package/dist/addMinute.mjs.map +1 -1
- package/dist/addMonth.d.ts +5 -5
- package/dist/addMonth.mjs +2 -12
- package/dist/addMonth.mjs.map +1 -1
- package/dist/addSecond.d.ts +4 -3
- package/dist/addSecond.mjs.map +1 -1
- package/dist/addYear.d.ts +5 -5
- package/dist/addYear.mjs +6 -12
- package/dist/addYear.mjs.map +1 -1
- package/dist/ap.mjs +1 -2
- package/dist/ap.mjs.map +1 -1
- package/dist/applyOffset.d.ts +5 -5
- package/dist/applyOffset.mjs +5 -11
- package/dist/applyOffset.mjs.map +1 -1
- package/dist/bundle.d.ts +365 -115
- package/dist/bundle.mjs +364 -180
- package/dist/bundle.mjs.map +1 -1
- package/dist/common.d.ts +27 -7
- package/dist/common.mjs +47 -42
- package/dist/common.mjs.map +1 -1
- package/dist/date.d.ts +2 -2
- package/dist/date.mjs +1 -3
- package/dist/date.mjs.map +1 -1
- package/dist/dayEnd.d.ts +3 -3
- package/dist/dayEnd.mjs.map +1 -1
- package/dist/dayOfYear.d.ts +3 -3
- package/dist/dayOfYear.mjs.map +1 -1
- package/dist/dayStart.d.ts +3 -3
- package/dist/dayStart.mjs +1 -1
- package/dist/dayStart.mjs.map +1 -1
- package/dist/diff.d.ts +38 -0
- package/dist/diff.mjs +86 -0
- package/dist/diff.mjs.map +1 -0
- package/dist/diffDays.d.ts +12 -5
- package/dist/diffDays.mjs +1 -0
- package/dist/diffDays.mjs.map +1 -1
- package/dist/diffHours.d.ts +12 -5
- package/dist/diffHours.mjs +1 -0
- package/dist/diffHours.mjs.map +1 -1
- package/dist/diffMilliseconds.d.ts +10 -4
- package/dist/diffMilliseconds.mjs.map +1 -1
- package/dist/diffMinutes.d.ts +11 -3
- package/dist/diffMinutes.mjs +5 -1
- package/dist/diffMinutes.mjs.map +1 -1
- package/dist/diffMonths.d.ts +10 -4
- package/dist/diffMonths.mjs.map +1 -1
- package/dist/diffSeconds.d.ts +12 -5
- package/dist/diffSeconds.mjs +5 -1
- package/dist/diffSeconds.mjs.map +1 -1
- package/dist/diffWeeks.d.ts +10 -3
- package/dist/diffWeeks.mjs.map +1 -1
- package/dist/diffYears.d.ts +10 -4
- package/dist/diffYears.mjs +4 -1
- package/dist/diffYears.mjs.map +1 -1
- package/dist/format.mjs +2 -3
- package/dist/format.mjs.map +1 -1
- package/dist/handleDateOverflow.d.ts +12 -0
- package/dist/handleDateOverflow.mjs +18 -0
- package/dist/handleDateOverflow.mjs.map +1 -0
- package/dist/hourEnd.d.ts +3 -3
- package/dist/hourEnd.mjs.map +1 -1
- package/dist/hourStart.d.ts +3 -3
- package/dist/hourStart.mjs +1 -1
- package/dist/hourStart.mjs.map +1 -1
- package/dist/index.cjs +380 -183
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +365 -115
- package/dist/index.d.ts +14 -1
- package/dist/index.mjs +26 -0
- package/dist/index.mjs.map +1 -1
- package/dist/isAfter.d.ts +6 -6
- package/dist/isAfter.mjs.map +1 -1
- package/dist/isBefore.d.ts +5 -5
- package/dist/isBefore.mjs.map +1 -1
- package/dist/isEqual.d.ts +13 -4
- package/dist/isEqual.mjs.map +1 -1
- package/dist/isFuture.d.ts +10 -0
- package/dist/isFuture.mjs +9 -0
- package/dist/isFuture.mjs.map +1 -0
- package/dist/isPast.d.ts +10 -0
- package/dist/isPast.mjs +9 -0
- package/dist/isPast.mjs.map +1 -0
- package/dist/iso8601.d.ts +1 -1
- package/dist/iso8601.mjs +4 -7
- package/dist/iso8601.mjs.map +1 -1
- package/dist/minuteEnd.d.ts +3 -3
- package/dist/minuteEnd.mjs.map +1 -1
- package/dist/minuteStart.d.ts +3 -3
- package/dist/minuteStart.mjs +1 -1
- package/dist/minuteStart.mjs.map +1 -1
- package/dist/monthDays.d.ts +3 -3
- package/dist/monthDays.mjs.map +1 -1
- package/dist/monthEnd.d.ts +3 -3
- package/dist/monthEnd.mjs.map +1 -1
- package/dist/monthStart.d.ts +3 -3
- package/dist/monthStart.mjs +1 -1
- package/dist/monthStart.mjs.map +1 -1
- package/dist/nearestDay.d.ts +3 -3
- package/dist/nearestDay.mjs +2 -4
- package/dist/nearestDay.mjs.map +1 -1
- package/dist/offset.d.ts +5 -5
- package/dist/offset.mjs +16 -16
- package/dist/offset.mjs.map +1 -1
- package/dist/parse.mjs +33 -23
- package/dist/parse.mjs.map +1 -1
- package/dist/parts.mjs +14 -24
- package/dist/parts.mjs.map +1 -1
- package/dist/range.mjs +1 -2
- package/dist/range.mjs.map +1 -1
- package/dist/removeOffset.d.ts +4 -4
- package/dist/removeOffset.mjs.map +1 -1
- package/dist/sameDay.d.ts +8 -2
- package/dist/sameDay.mjs.map +1 -1
- package/dist/sameHour.d.ts +8 -2
- package/dist/sameHour.mjs.map +1 -1
- package/dist/sameMillisecond.d.ts +16 -0
- package/dist/sameMillisecond.mjs +11 -0
- package/dist/sameMillisecond.mjs.map +1 -0
- package/dist/sameMinute.d.ts +8 -2
- package/dist/sameMinute.mjs.map +1 -1
- package/dist/sameMonth.d.ts +16 -0
- package/dist/sameMonth.mjs +11 -0
- package/dist/sameMonth.mjs.map +1 -0
- package/dist/sameSecond.d.ts +8 -2
- package/dist/sameSecond.mjs.map +1 -1
- package/dist/sameYear.d.ts +8 -2
- package/dist/sameYear.mjs.map +1 -1
- package/dist/setDayOfMonth.d.ts +11 -0
- package/dist/setDayOfMonth.mjs +16 -0
- package/dist/setDayOfMonth.mjs.map +1 -0
- package/dist/setHour.d.ts +10 -0
- package/dist/setHour.mjs +11 -0
- package/dist/setHour.mjs.map +1 -0
- package/dist/setMilliseconds.d.ts +10 -0
- package/dist/setMilliseconds.mjs +11 -0
- package/dist/setMilliseconds.mjs.map +1 -0
- package/dist/setMinutes.d.ts +10 -0
- package/dist/setMinutes.mjs +11 -0
- package/dist/setMinutes.mjs.map +1 -0
- package/dist/setMonth.d.ts +11 -0
- package/dist/setMonth.mjs +9 -0
- package/dist/setMonth.mjs.map +1 -0
- package/dist/setSeconds.d.ts +10 -0
- package/dist/setSeconds.mjs +11 -0
- package/dist/setSeconds.mjs.map +1 -0
- package/dist/setYear.d.ts +11 -0
- package/dist/setYear.mjs +9 -0
- package/dist/setYear.mjs.map +1 -0
- package/dist/types.d.ts +29 -7
- package/dist/tzDate.d.ts +5 -3
- package/dist/tzDate.mjs.map +1 -1
- package/dist/weekEnd.d.ts +4 -4
- package/dist/weekEnd.mjs +1 -1
- package/dist/weekEnd.mjs.map +1 -1
- package/dist/weekStart.d.ts +4 -4
- package/dist/weekStart.mjs +2 -3
- package/dist/weekStart.mjs.map +1 -1
- package/dist/yearDays.d.ts +3 -3
- package/dist/yearDays.mjs.map +1 -1
- package/dist/yearEnd.d.ts +3 -3
- package/dist/yearEnd.mjs +1 -2
- package/dist/yearEnd.mjs.map +1 -1
- package/dist/yearStart.d.ts +3 -3
- package/dist/yearStart.mjs +2 -3
- package/dist/yearStart.mjs.map +1 -1
- package/package.json +32 -23
package/dist/bundle.mjs
CHANGED
|
@@ -1,20 +1,17 @@
|
|
|
1
1
|
// src/iso8601.ts
|
|
2
|
-
var iso8601Match = /^([0-9]{4})-([0-1][0-9])(?:-([0-3][0-9]))?(?:[T ]?([0-2][0-9])(?::([0-5][0-9]))?(?::([0-5][0-9]))?)?(?:\.[0-9]+)?(Z|(?:\+|\-)[0-9]{2}:?[0-9]{2})?$/;
|
|
2
|
+
var iso8601Match = /^([0-9]{4})-([0-1][0-9])(?:-([0-3][0-9]))?(?:[T ]?([0-2][0-9])(?::([0-5][0-9]))?(?::([0-5][0-9]))?)?(?:\.[0-9]+)?(Z|(?:\+|\-)[0-9]{2}:?[0-9]{2}(?::?[0-9]{2})?)?$/;
|
|
3
3
|
function iso8601(date2) {
|
|
4
4
|
const matches = date2.match(iso8601Match);
|
|
5
5
|
if (matches) {
|
|
6
6
|
const month = Number(matches[2]);
|
|
7
|
-
if (month < 1 || month > 12)
|
|
8
|
-
return false;
|
|
7
|
+
if (month < 1 || month > 12) return false;
|
|
9
8
|
if (typeof matches[3] !== void 0) {
|
|
10
9
|
const date3 = Number(matches[3]);
|
|
11
|
-
if (date3 < 1 || date3 > 31)
|
|
12
|
-
return false;
|
|
10
|
+
if (date3 < 1 || date3 > 31) return false;
|
|
13
11
|
}
|
|
14
12
|
if (typeof matches[4] !== void 0) {
|
|
15
13
|
const hours = Number(matches[4]);
|
|
16
|
-
if (hours < 0 || hours > 23)
|
|
17
|
-
return false;
|
|
14
|
+
if (hours < 0 || hours > 23) return false;
|
|
18
15
|
}
|
|
19
16
|
return true;
|
|
20
17
|
}
|
|
@@ -34,9 +31,7 @@ function date(date2) {
|
|
|
34
31
|
date2 = /* @__PURE__ */ new Date();
|
|
35
32
|
}
|
|
36
33
|
if (date2 instanceof Date) {
|
|
37
|
-
|
|
38
|
-
d.setMilliseconds(0);
|
|
39
|
-
return d;
|
|
34
|
+
return new Date(date2);
|
|
40
35
|
}
|
|
41
36
|
date2 = date2.trim();
|
|
42
37
|
if (iso8601(date2)) {
|
|
@@ -52,6 +47,27 @@ function addDay(inputDate, count = 1) {
|
|
|
52
47
|
return d;
|
|
53
48
|
}
|
|
54
49
|
|
|
50
|
+
// src/addHour.ts
|
|
51
|
+
function addHour(inputDate, count = 1) {
|
|
52
|
+
const d = date(inputDate);
|
|
53
|
+
d.setHours(d.getHours() + count);
|
|
54
|
+
return d;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
// src/addMillisecond.ts
|
|
58
|
+
function addMillisecond(inputDate, count = 1) {
|
|
59
|
+
const d = date(inputDate);
|
|
60
|
+
d.setMilliseconds(d.getMilliseconds() + count);
|
|
61
|
+
return d;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
// src/addMinute.ts
|
|
65
|
+
function addMinute(inputDate, count = 1) {
|
|
66
|
+
const d = date(inputDate);
|
|
67
|
+
d.setMinutes(d.getMinutes() + count);
|
|
68
|
+
return d;
|
|
69
|
+
}
|
|
70
|
+
|
|
55
71
|
// src/monthEnd.ts
|
|
56
72
|
function monthEnd(inputDate) {
|
|
57
73
|
const d = date(inputDate);
|
|
@@ -67,13 +83,12 @@ function monthDays(inputDate) {
|
|
|
67
83
|
return d.getDate();
|
|
68
84
|
}
|
|
69
85
|
|
|
70
|
-
// src/
|
|
71
|
-
function
|
|
86
|
+
// src/handleDateOverflow.ts
|
|
87
|
+
function handleOverflow(inputDate, action, dateOverflow = false) {
|
|
72
88
|
const d = date(inputDate);
|
|
73
89
|
const dayOfMonth = d.getDate();
|
|
74
|
-
if (!dateOverflow)
|
|
75
|
-
|
|
76
|
-
d.setMonth(d.getMonth() + count);
|
|
90
|
+
if (!dateOverflow) d.setDate(1);
|
|
91
|
+
action(d);
|
|
77
92
|
if (!dateOverflow) {
|
|
78
93
|
const daysInMonth = monthDays(d);
|
|
79
94
|
d.setDate(daysInMonth < dayOfMonth ? daysInMonth : dayOfMonth);
|
|
@@ -81,38 +96,63 @@ function addMonth(inputDate, count = 1, dateOverflow = false) {
|
|
|
81
96
|
return d;
|
|
82
97
|
}
|
|
83
98
|
|
|
84
|
-
// src/
|
|
85
|
-
function
|
|
86
|
-
|
|
87
|
-
const dayOfMonth = d.getDate();
|
|
88
|
-
if (!dateOverflow)
|
|
89
|
-
d.setDate(1);
|
|
90
|
-
d.setFullYear(d.getFullYear() + count);
|
|
91
|
-
if (!dateOverflow) {
|
|
92
|
-
const daysInMonth = monthDays(d);
|
|
93
|
-
d.setDate(daysInMonth < dayOfMonth ? daysInMonth : dayOfMonth);
|
|
94
|
-
}
|
|
95
|
-
return d;
|
|
99
|
+
// src/addMonth.ts
|
|
100
|
+
function addMonth(inputDate, count = 1, dateOverflow = false) {
|
|
101
|
+
return handleOverflow(inputDate, (d) => d.setMonth(d.getMonth() + count), dateOverflow);
|
|
96
102
|
}
|
|
97
103
|
|
|
98
|
-
// src/
|
|
99
|
-
function
|
|
104
|
+
// src/addSecond.ts
|
|
105
|
+
function addSecond(inputDate, count = 1) {
|
|
100
106
|
const d = date(inputDate);
|
|
101
|
-
d.
|
|
107
|
+
d.setSeconds(d.getSeconds() + count);
|
|
102
108
|
return d;
|
|
103
109
|
}
|
|
104
110
|
|
|
105
|
-
// src/
|
|
106
|
-
function
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
111
|
+
// src/addYear.ts
|
|
112
|
+
function addYear(inputDate, count = 1, dateOverflow = false) {
|
|
113
|
+
return handleOverflow(
|
|
114
|
+
inputDate,
|
|
115
|
+
(d) => d.setFullYear(d.getFullYear() + count),
|
|
116
|
+
dateOverflow
|
|
117
|
+
);
|
|
110
118
|
}
|
|
111
119
|
|
|
112
|
-
// src/
|
|
113
|
-
function
|
|
114
|
-
|
|
115
|
-
d
|
|
120
|
+
// src/add.ts
|
|
121
|
+
function add(inputDate, duration, dateOverflow = false) {
|
|
122
|
+
var _a, _b;
|
|
123
|
+
let d = date(inputDate);
|
|
124
|
+
const applyFixedUnits = () => {
|
|
125
|
+
if (duration.weeks) {
|
|
126
|
+
d = addDay(d, duration.weeks * 7);
|
|
127
|
+
}
|
|
128
|
+
if (duration.days) {
|
|
129
|
+
d = addDay(d, duration.days);
|
|
130
|
+
}
|
|
131
|
+
if (duration.hours) {
|
|
132
|
+
d = addHour(d, duration.hours);
|
|
133
|
+
}
|
|
134
|
+
if (duration.minutes) {
|
|
135
|
+
d = addMinute(d, duration.minutes);
|
|
136
|
+
}
|
|
137
|
+
if (duration.seconds) {
|
|
138
|
+
d = addSecond(d, duration.seconds);
|
|
139
|
+
}
|
|
140
|
+
if (duration.milliseconds) {
|
|
141
|
+
d = addMillisecond(d, duration.milliseconds);
|
|
142
|
+
}
|
|
143
|
+
};
|
|
144
|
+
const applyCalendarUnits = () => {
|
|
145
|
+
if (duration.months) {
|
|
146
|
+
d = addMonth(d, duration.months, dateOverflow);
|
|
147
|
+
}
|
|
148
|
+
if (duration.years) {
|
|
149
|
+
d = addYear(d, duration.years, dateOverflow);
|
|
150
|
+
}
|
|
151
|
+
};
|
|
152
|
+
const calendarFirst = ((_a = duration.months) != null ? _a : 0) < 0 || ((_b = duration.years) != null ? _b : 0) < 0;
|
|
153
|
+
if (calendarFirst) applyCalendarUnits();
|
|
154
|
+
applyFixedUnits();
|
|
155
|
+
if (!calendarFirst) applyCalendarUnits();
|
|
116
156
|
return d;
|
|
117
157
|
}
|
|
118
158
|
|
|
@@ -148,6 +188,9 @@ var clock12 = [
|
|
|
148
188
|
["a", { dayPeriod: "narrow" }],
|
|
149
189
|
["A", { dayPeriod: "narrow" }]
|
|
150
190
|
];
|
|
191
|
+
var fractionalSeconds = [
|
|
192
|
+
["SSS", { fractionalSecond: "3-digit" }]
|
|
193
|
+
];
|
|
151
194
|
var fixedLength = {
|
|
152
195
|
DD: 2,
|
|
153
196
|
HH: 2,
|
|
@@ -159,6 +202,12 @@ var fixedLength = {
|
|
|
159
202
|
ss: 2
|
|
160
203
|
};
|
|
161
204
|
function fixedLengthByOffset(offsetString) {
|
|
205
|
+
if (/^[+-]\d{2}:\d{2}:\d{2}/.test(offsetString)) {
|
|
206
|
+
return 9;
|
|
207
|
+
}
|
|
208
|
+
if (/^[+-]\d{6}/.test(offsetString)) {
|
|
209
|
+
return 8;
|
|
210
|
+
}
|
|
162
211
|
if (/^[+-]\d{2}:\d{2}/.test(offsetString)) {
|
|
163
212
|
return 6;
|
|
164
213
|
}
|
|
@@ -169,17 +218,12 @@ function fixedLengthByOffset(offsetString) {
|
|
|
169
218
|
}
|
|
170
219
|
var genitiveTokens = ["MMMM", "MMM", "dddd", "ddd"];
|
|
171
220
|
var tokens = /* @__PURE__ */ new Map(
|
|
172
|
-
/* @__PURE__ */ [...clockAgnostic, ...clock24, ...clock12].map((format2) => {
|
|
221
|
+
/* @__PURE__ */ [...clockAgnostic, ...clock24, ...clock12, ...fractionalSeconds].map((format2) => {
|
|
173
222
|
return [format2[0], format2];
|
|
174
223
|
})
|
|
175
224
|
);
|
|
176
225
|
var dayPeriodMap = /* @__PURE__ */ new Map();
|
|
177
|
-
var styles = [
|
|
178
|
-
"full",
|
|
179
|
-
"long",
|
|
180
|
-
"medium",
|
|
181
|
-
"short"
|
|
182
|
-
];
|
|
226
|
+
var styles = ["full", "long", "medium", "short"];
|
|
183
227
|
var two = (n) => String(n).padStart(2, "0");
|
|
184
228
|
var four = (n) => String(n).padStart(2, "0");
|
|
185
229
|
function normStr(part) {
|
|
@@ -192,8 +236,7 @@ function fill(inputDate, parts2, locale, genitive = false, offset2 = null) {
|
|
|
192
236
|
const partMap = createPartMap(inputDate, parts2, locale, genitive);
|
|
193
237
|
const d = date(inputDate);
|
|
194
238
|
function value({ partName, partValue, token }) {
|
|
195
|
-
if (partName === "literal")
|
|
196
|
-
return partValue;
|
|
239
|
+
if (partName === "literal") return partValue;
|
|
197
240
|
const value2 = partMap[partName];
|
|
198
241
|
if (partName === "hour" && token === "H") {
|
|
199
242
|
return value2.replace(/^0/, "") || "0";
|
|
@@ -205,6 +248,9 @@ function fill(inputDate, parts2, locale, genitive = false, offset2 = null) {
|
|
|
205
248
|
const p = ap(d.getUTCHours() < 12 ? "am" : "pm", locale);
|
|
206
249
|
return token === "A" ? p.toUpperCase() : p.toLowerCase();
|
|
207
250
|
}
|
|
251
|
+
if (partName === "fractionalSecond") {
|
|
252
|
+
return String(d.getUTCMilliseconds()).padStart(3, "0");
|
|
253
|
+
}
|
|
208
254
|
if (partName === "timeZoneName") {
|
|
209
255
|
return offset2 != null ? offset2 : minsToOffset(-1 * d.getTimezoneOffset(), token);
|
|
210
256
|
}
|
|
@@ -230,8 +276,7 @@ function createPartMap(inputDate, parts2, locale, genitive = false) {
|
|
|
230
276
|
preciseLocale,
|
|
231
277
|
requestedParts.reduce(
|
|
232
278
|
(options, part) => {
|
|
233
|
-
if (part.partName === "literal")
|
|
234
|
-
return options;
|
|
279
|
+
if (part.partName === "literal") return options;
|
|
235
280
|
if (genitive && genitiveTokens.includes(part.token)) {
|
|
236
281
|
genitiveParts.push(part);
|
|
237
282
|
}
|
|
@@ -258,9 +303,7 @@ function createPartMap(inputDate, parts2, locale, genitive = false) {
|
|
|
258
303
|
}).formatToParts(d).map(normStr);
|
|
259
304
|
break;
|
|
260
305
|
}
|
|
261
|
-
const genitiveFormattedPart = formattedParts.find(
|
|
262
|
-
(p) => p.type === part.partName
|
|
263
|
-
);
|
|
306
|
+
const genitiveFormattedPart = formattedParts.find((p) => p.type === part.partName);
|
|
264
307
|
const index = valueParts.findIndex((p) => p.type === part.partName);
|
|
265
308
|
if (genitiveFormattedPart && index > -1) {
|
|
266
309
|
valueParts[index] = genitiveFormattedPart;
|
|
@@ -268,50 +311,48 @@ function createPartMap(inputDate, parts2, locale, genitive = false) {
|
|
|
268
311
|
}
|
|
269
312
|
}
|
|
270
313
|
}
|
|
271
|
-
if (hour12.length)
|
|
272
|
-
|
|
273
|
-
if (hour24.length)
|
|
274
|
-
addValues(hour24);
|
|
314
|
+
if (hour12.length) addValues(hour12, true);
|
|
315
|
+
if (hour24.length) addValues(hour24);
|
|
275
316
|
return valueParts.reduce((map, part) => {
|
|
276
317
|
map[part.type] = part.value;
|
|
277
318
|
return map;
|
|
278
319
|
}, {});
|
|
279
320
|
}
|
|
280
|
-
function
|
|
281
|
-
const
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
);
|
|
285
|
-
const
|
|
286
|
-
const sign = timeDiffInMins < 0 ? "-" : "+";
|
|
321
|
+
function secsToOffset(totalSecs, token = "Z") {
|
|
322
|
+
const sign = totalSecs < 0 ? "-" : "+";
|
|
323
|
+
const absSecs = Math.abs(totalSecs);
|
|
324
|
+
const hours = String(Math.floor(absSecs / 3600)).padStart(2, "0");
|
|
325
|
+
const mins = String(Math.floor(absSecs % 3600 / 60)).padStart(2, "0");
|
|
326
|
+
const secs = Math.round(absSecs % 60);
|
|
287
327
|
if (token === "ZZ") {
|
|
288
|
-
return `${sign}${hours}${mins}`;
|
|
328
|
+
return secs === 0 ? `${sign}${hours}${mins}` : `${sign}${hours}${mins}${String(secs).padStart(2, "0")}`;
|
|
289
329
|
}
|
|
290
|
-
return `${sign}${hours}:${mins}`;
|
|
330
|
+
return secs === 0 ? `${sign}${hours}:${mins}` : `${sign}${hours}:${mins}:${String(secs).padStart(2, "0")}`;
|
|
331
|
+
}
|
|
332
|
+
function minsToOffset(timeDiffInMins, token = "Z") {
|
|
333
|
+
return secsToOffset(timeDiffInMins * 60, token);
|
|
291
334
|
}
|
|
292
|
-
function
|
|
335
|
+
function offsetToSecs(offset2, token) {
|
|
293
336
|
validOffset(offset2, token);
|
|
294
|
-
const
|
|
295
|
-
|
|
296
|
-
);
|
|
297
|
-
|
|
298
|
-
return sign === "+" ? offsetInMins : -offsetInMins;
|
|
337
|
+
const match = offset2.match(/([+-])([0-3][0-9]):?([0-5][0-9])(?::?([0-5][0-9]))?/);
|
|
338
|
+
const [_, sign, hours, mins, secs = "0"] = match;
|
|
339
|
+
const totalSecs = Number(hours) * 3600 + Number(mins) * 60 + Number(secs);
|
|
340
|
+
return sign === "+" ? totalSecs : -totalSecs;
|
|
299
341
|
}
|
|
300
342
|
function validOffset(offset2, token = "Z") {
|
|
301
343
|
const valid = ((token2) => {
|
|
302
344
|
switch (token2) {
|
|
303
345
|
case "Z":
|
|
304
|
-
return /^([+-])[0-3][0-9]:[0-
|
|
346
|
+
return /^([+-])[0-3][0-9]:[0-5][0-9](?::[0-5][0-9])?$/.test(offset2);
|
|
305
347
|
case "ZZ":
|
|
306
|
-
return /^([+-])[0-3][0-9][0-
|
|
348
|
+
return /^([+-])[0-3][0-9][0-5][0-9](?:[0-5][0-9])?$/.test(offset2);
|
|
307
349
|
}
|
|
308
350
|
})(token);
|
|
309
|
-
if (!valid)
|
|
310
|
-
throw new Error(`Invalid offset: ${offset2}`);
|
|
351
|
+
if (!valid) throw new Error(`Invalid offset: ${offset2}`);
|
|
311
352
|
return offset2;
|
|
312
353
|
}
|
|
313
354
|
function escapeTokens(str) {
|
|
314
|
-
return clockAgnostic.concat(clock24).concat(clock12).sort((a, b) => a[0].length > b[0].length ? 1 : -1).reduce((target, part) => {
|
|
355
|
+
return clockAgnostic.concat(clock24).concat(clock12).concat(fractionalSeconds).sort((a, b) => a[0].length > b[0].length ? 1 : -1).reduce((target, part) => {
|
|
315
356
|
return target.replace(part[0], `\\${part[0]}`);
|
|
316
357
|
}, str);
|
|
317
358
|
}
|
|
@@ -325,10 +366,8 @@ function validate(parts2) {
|
|
|
325
366
|
throw new Error(`Numbers in format (${part.partValue}).`);
|
|
326
367
|
}
|
|
327
368
|
if (lastPart && lastPart.partName !== "literal" && part.partName !== "literal") {
|
|
328
|
-
if (!(lastPart.token in fixedLength) && !(part.token in fixedLength) && !(isNumeric(lastPart) && part.token.toLowerCase() === "a")) {
|
|
329
|
-
throw new Error(
|
|
330
|
-
`Illegal adjacent tokens (${lastPart.token}, ${part.token})`
|
|
331
|
-
);
|
|
369
|
+
if (!(lastPart.token in fixedLength) && !(part.token in fixedLength) && !(isNumeric(lastPart) && part.token.toLowerCase() === "a") && lastPart.token !== "SSS") {
|
|
370
|
+
throw new Error(`Illegal adjacent tokens (${lastPart.token}, ${part.token})`);
|
|
332
371
|
}
|
|
333
372
|
}
|
|
334
373
|
lastPart = part;
|
|
@@ -345,8 +384,7 @@ function getOffsetFormat(format2) {
|
|
|
345
384
|
// src/ap.ts
|
|
346
385
|
function ap(ampm, locale) {
|
|
347
386
|
const l = dayPeriodMap.get(locale);
|
|
348
|
-
if (l && l[ampm])
|
|
349
|
-
return l[ampm];
|
|
387
|
+
if (l && l[ampm]) return l[ampm];
|
|
350
388
|
const specimen = new Date(specDate);
|
|
351
389
|
specimen.setUTCHours(ampm === "am" ? 5 : 20);
|
|
352
390
|
const subparts = new Intl.DateTimeFormat(locale, {
|
|
@@ -369,16 +407,10 @@ function ap(ampm, locale) {
|
|
|
369
407
|
// src/applyOffset.ts
|
|
370
408
|
function applyOffset(dateInput, offset2 = "+00:00") {
|
|
371
409
|
const d = date(dateInput);
|
|
372
|
-
const
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
case 6:
|
|
377
|
-
return "Z";
|
|
378
|
-
}
|
|
379
|
-
})();
|
|
380
|
-
const timeDiffInMins = offsetToMins(offset2, token);
|
|
381
|
-
return new Date(d.getTime() + timeDiffInMins * 1e3 * 60);
|
|
410
|
+
const len = fixedLengthByOffset(offset2);
|
|
411
|
+
const token = len === 5 || len === 8 ? "ZZ" : "Z";
|
|
412
|
+
const timeDiffInSecs = offsetToSecs(offset2, token);
|
|
413
|
+
return new Date(d.getTime() + timeDiffInSecs * 1e3);
|
|
382
414
|
}
|
|
383
415
|
|
|
384
416
|
// src/deviceTZ.ts
|
|
@@ -389,22 +421,24 @@ function deviceTZ() {
|
|
|
389
421
|
// src/offset.ts
|
|
390
422
|
function relativeTime(d, timeZone) {
|
|
391
423
|
const utcParts = new Intl.DateTimeFormat("en-US", {
|
|
424
|
+
era: "short",
|
|
392
425
|
year: "numeric",
|
|
393
|
-
month: "
|
|
394
|
-
day: "
|
|
395
|
-
hour: "
|
|
396
|
-
minute: "
|
|
397
|
-
second: "
|
|
426
|
+
month: "numeric",
|
|
427
|
+
day: "numeric",
|
|
428
|
+
hour: "numeric",
|
|
429
|
+
minute: "numeric",
|
|
430
|
+
second: "numeric",
|
|
398
431
|
timeZone,
|
|
399
432
|
hourCycle: "h23"
|
|
400
|
-
}).formatToParts(d)
|
|
401
|
-
const
|
|
433
|
+
}).formatToParts(d);
|
|
434
|
+
const p = {};
|
|
402
435
|
utcParts.forEach((part) => {
|
|
403
|
-
|
|
436
|
+
if (part.type !== "literal") p[part.type] = part.value;
|
|
404
437
|
});
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
);
|
|
438
|
+
const year = p.era === "BC" ? 1 - Number(p.year) : Number(p.year);
|
|
439
|
+
const result = new Date(Date.UTC(0, 0, 1, Number(p.hour), Number(p.minute), Number(p.second)));
|
|
440
|
+
result.setUTCFullYear(year, Number(p.month) - 1, Number(p.day));
|
|
441
|
+
return result;
|
|
408
442
|
}
|
|
409
443
|
function offset(utcTime, tzA = "UTC", tzB = "device", timeZoneToken = "Z") {
|
|
410
444
|
var _a;
|
|
@@ -412,10 +446,8 @@ function offset(utcTime, tzA = "UTC", tzB = "device", timeZoneToken = "Z") {
|
|
|
412
446
|
const d = date(utcTime);
|
|
413
447
|
const timeA = relativeTime(d, tzA);
|
|
414
448
|
const timeB = relativeTime(d, tzB);
|
|
415
|
-
const
|
|
416
|
-
|
|
417
|
-
);
|
|
418
|
-
return minsToOffset(timeDiffInMins, timeZoneToken);
|
|
449
|
+
const timeDiffInSecs = Math.round((timeB.getTime() - timeA.getTime()) / 1e3);
|
|
450
|
+
return secsToOffset(timeDiffInSecs, timeZoneToken);
|
|
419
451
|
}
|
|
420
452
|
|
|
421
453
|
// src/tzDate.ts
|
|
@@ -442,7 +474,7 @@ function dayEnd(inputDate) {
|
|
|
442
474
|
// src/dayStart.ts
|
|
443
475
|
function dayStart(inputDate) {
|
|
444
476
|
const d = date(inputDate);
|
|
445
|
-
d.setHours(0, 0, 0);
|
|
477
|
+
d.setHours(0, 0, 0, 0);
|
|
446
478
|
return d;
|
|
447
479
|
}
|
|
448
480
|
|
|
@@ -454,13 +486,11 @@ function parts(format2, locale) {
|
|
|
454
486
|
let f = format2;
|
|
455
487
|
let match = 0;
|
|
456
488
|
const testPattern = (pattern) => {
|
|
457
|
-
if (!pattern[2])
|
|
458
|
-
pattern[2] = new RegExp(`(.)?(${pattern[0]})`, "g");
|
|
489
|
+
if (!pattern[2]) pattern[2] = new RegExp(`(.)?(${pattern[0]})`, "g");
|
|
459
490
|
if (pattern[2].test(f)) {
|
|
460
491
|
let didAdd = 0;
|
|
461
492
|
f = f.replace(pattern[2], (_, prefix, actualMatch) => {
|
|
462
|
-
if (prefix === "\\")
|
|
463
|
-
return actualMatch;
|
|
493
|
+
if (prefix === "\\") return actualMatch;
|
|
464
494
|
return `${typeof prefix === "string" ? prefix : ""}{!${didAdd++ ? match : match++}!}`;
|
|
465
495
|
});
|
|
466
496
|
return !!didAdd;
|
|
@@ -487,7 +517,7 @@ function parts(format2, locale) {
|
|
|
487
517
|
hour12
|
|
488
518
|
};
|
|
489
519
|
}
|
|
490
|
-
const found24Patterns = clockAgnostic.filter(testPattern).concat(clock24.filter(testPattern)).map(createPart.bind(null, false));
|
|
520
|
+
const found24Patterns = clockAgnostic.filter(testPattern).concat(clock24.filter(testPattern)).concat(fractionalSeconds.filter(testPattern)).map(createPart.bind(null, false));
|
|
491
521
|
const parts2 = validate2(
|
|
492
522
|
found24Patterns.concat(
|
|
493
523
|
clock12.filter(testPattern).map(createPart.bind(null, true))
|
|
@@ -516,10 +546,8 @@ function styleParts(format2, locale) {
|
|
|
516
546
|
if (typeof format2 === "string") {
|
|
517
547
|
options.dateStyle = format2;
|
|
518
548
|
} else {
|
|
519
|
-
if ("date" in format2)
|
|
520
|
-
|
|
521
|
-
if ("time" in format2)
|
|
522
|
-
options.timeStyle = format2.time;
|
|
549
|
+
if ("date" in format2) options.dateStyle = format2.date;
|
|
550
|
+
if ("time" in format2) options.timeStyle = format2.time;
|
|
523
551
|
}
|
|
524
552
|
const formatter = new Intl.DateTimeFormat(locale, options);
|
|
525
553
|
const segments = formatter.formatToParts(new Date(specDate)).map(normStr);
|
|
@@ -535,11 +563,9 @@ function styleParts(format2, locale) {
|
|
|
535
563
|
part.type === "hour" ? hourType : void 0,
|
|
536
564
|
options
|
|
537
565
|
);
|
|
538
|
-
if (formatPattern === void 0)
|
|
539
|
-
return;
|
|
566
|
+
if (formatPattern === void 0) return;
|
|
540
567
|
const partValue = formatPattern[1][partName];
|
|
541
|
-
if (!partValue)
|
|
542
|
-
return;
|
|
568
|
+
if (!partValue) return;
|
|
543
569
|
if (!formatPattern[2])
|
|
544
570
|
formatPattern[2] = new RegExp(`${formatPattern[0]}`, "g");
|
|
545
571
|
return {
|
|
@@ -560,8 +586,7 @@ function guessPattern(partName, partValue, locale, hour, options) {
|
|
|
560
586
|
case "year":
|
|
561
587
|
return l === 2 ? tokens.get("YY") : tokens.get("YYYY");
|
|
562
588
|
case "month":
|
|
563
|
-
if (n)
|
|
564
|
-
return l === 1 ? tokens.get("M") : tokens.get("MM");
|
|
589
|
+
if (n) return l === 1 ? tokens.get("M") : tokens.get("MM");
|
|
565
590
|
style = partStyle(locale, partName, partValue);
|
|
566
591
|
switch (style) {
|
|
567
592
|
case "long":
|
|
@@ -582,8 +607,7 @@ function guessPattern(partName, partValue, locale, hour, options) {
|
|
|
582
607
|
return tokens.get("dddd");
|
|
583
608
|
}
|
|
584
609
|
case "hour":
|
|
585
|
-
if (hour === 12)
|
|
586
|
-
return l === 1 ? tokens.get("h") : tokens.get("hh");
|
|
610
|
+
if (hour === 12) return l === 1 ? tokens.get("h") : tokens.get("hh");
|
|
587
611
|
return l === 1 ? tokens.get("H") : tokens.get("HH");
|
|
588
612
|
case "minute":
|
|
589
613
|
return l === 1 ? tokens.get("m") : tokens.get("mm");
|
|
@@ -608,8 +632,7 @@ function partStyle(locale, part, value) {
|
|
|
608
632
|
const formats2 = {};
|
|
609
633
|
for (let i = 0; i < 12; i++) {
|
|
610
634
|
date2.setMonth(0 + i);
|
|
611
|
-
if (i in weekdays)
|
|
612
|
-
date2.setDate(weekdays[i]);
|
|
635
|
+
if (i in weekdays) date2.setDate(weekdays[i]);
|
|
613
636
|
date2.setUTCHours(8 + i);
|
|
614
637
|
for (const style of partStyles) {
|
|
615
638
|
const segments = new Intl.DateTimeFormat(
|
|
@@ -628,12 +651,10 @@ function partStyle(locale, part, value) {
|
|
|
628
651
|
(part2) => part2.type === "month"
|
|
629
652
|
);
|
|
630
653
|
const index = segments.findIndex((part2) => part2.type === "month");
|
|
631
|
-
if (index > -1 && genitiveMonth)
|
|
632
|
-
segments[index] = genitiveMonth;
|
|
654
|
+
if (index > -1 && genitiveMonth) segments[index] = genitiveMonth;
|
|
633
655
|
}
|
|
634
656
|
segments.forEach((part2) => {
|
|
635
|
-
if (part2.type === "literal")
|
|
636
|
-
return;
|
|
657
|
+
if (part2.type === "literal") return;
|
|
637
658
|
const type = part2.type;
|
|
638
659
|
formats2[type] = Object.assign(formats2[type] || {}, {
|
|
639
660
|
[part2.value]: style
|
|
@@ -675,8 +696,7 @@ function format(inputDateOrOptions, format2 = "long", locale = "device", genitiv
|
|
|
675
696
|
tz
|
|
676
697
|
} = inputDateOrOptions);
|
|
677
698
|
}
|
|
678
|
-
if (format2 === "ISO8601")
|
|
679
|
-
return date(inputDateOrOptions).toISOString();
|
|
699
|
+
if (format2 === "ISO8601") return date(inputDateOrOptions).toISOString();
|
|
680
700
|
if (tz) {
|
|
681
701
|
forceOffset = offset(inputDateOrOptions, "utc", tz, getOffsetFormat(format2));
|
|
682
702
|
}
|
|
@@ -692,7 +712,7 @@ function format(inputDateOrOptions, format2 = "long", locale = "device", genitiv
|
|
|
692
712
|
}
|
|
693
713
|
return fill(
|
|
694
714
|
inputDateOrOptions,
|
|
695
|
-
parts(format2, locale).filter(partFilter != null ? partFilter : () => true),
|
|
715
|
+
parts(format2, locale).filter(partFilter != null ? partFilter : (() => true)),
|
|
696
716
|
locale,
|
|
697
717
|
genitive,
|
|
698
718
|
forceOffset
|
|
@@ -726,7 +746,7 @@ function hourEnd(inputDate) {
|
|
|
726
746
|
// src/hourStart.ts
|
|
727
747
|
function hourStart(inputDate) {
|
|
728
748
|
const d = date(inputDate);
|
|
729
|
-
d.setMinutes(0, 0);
|
|
749
|
+
d.setMinutes(0, 0, 0);
|
|
730
750
|
return d;
|
|
731
751
|
}
|
|
732
752
|
|
|
@@ -740,7 +760,7 @@ function minuteEnd(inputDate) {
|
|
|
740
760
|
// src/minuteStart.ts
|
|
741
761
|
function minuteStart(inputDate) {
|
|
742
762
|
const d = date(inputDate);
|
|
743
|
-
d.setSeconds(0);
|
|
763
|
+
d.setSeconds(0, 0);
|
|
744
764
|
return d;
|
|
745
765
|
}
|
|
746
766
|
|
|
@@ -748,7 +768,7 @@ function minuteStart(inputDate) {
|
|
|
748
768
|
function monthStart(inputDate) {
|
|
749
769
|
const d = date(inputDate);
|
|
750
770
|
d.setDate(1);
|
|
751
|
-
d.setHours(0, 0, 0);
|
|
771
|
+
d.setHours(0, 0, 0, 0);
|
|
752
772
|
return d;
|
|
753
773
|
}
|
|
754
774
|
|
|
@@ -784,13 +804,11 @@ function nearestDay(inputDate, search, constraint = 7) {
|
|
|
784
804
|
for (let i = 0; i <= increments || i < decrements; i++) {
|
|
785
805
|
if (i <= increments) {
|
|
786
806
|
const next = addDay(d, i);
|
|
787
|
-
if (search(next))
|
|
788
|
-
return next;
|
|
807
|
+
if (search(next)) return next;
|
|
789
808
|
}
|
|
790
809
|
if (i && i <= decrements) {
|
|
791
810
|
const prev = addDay(d, -i);
|
|
792
|
-
if (search(prev))
|
|
793
|
-
return prev;
|
|
811
|
+
if (search(prev)) return prev;
|
|
794
812
|
}
|
|
795
813
|
}
|
|
796
814
|
return null;
|
|
@@ -799,8 +817,7 @@ function nearestDay(inputDate, search, constraint = 7) {
|
|
|
799
817
|
// src/range.ts
|
|
800
818
|
function range(token, locale = "en", genitive = false) {
|
|
801
819
|
const r = (n, c) => Array(n).fill("").map((_, i) => `${c(i)}`);
|
|
802
|
-
if (token === "M")
|
|
803
|
-
return r(12, (i) => i + 1);
|
|
820
|
+
if (token === "M") return r(12, (i) => i + 1);
|
|
804
821
|
if (token === "MM")
|
|
805
822
|
return r(12, (i) => {
|
|
806
823
|
const m = i + 1;
|
|
@@ -858,19 +875,16 @@ function parse(dateStrOrOptions, format2 = "ISO8601", locale = "device") {
|
|
|
858
875
|
} else {
|
|
859
876
|
dateStr = dateStrOrOptions;
|
|
860
877
|
}
|
|
861
|
-
if (!dateStr)
|
|
862
|
-
throw new Error("parse() requires a date string.");
|
|
878
|
+
if (!dateStr) throw new Error("parse() requires a date string.");
|
|
863
879
|
const invalid = () => {
|
|
864
880
|
throw new Error(
|
|
865
881
|
`Date (${dateStr}) does not match format (${formatStr(format2, locale)})`
|
|
866
882
|
);
|
|
867
883
|
};
|
|
868
|
-
if (format2 === "ISO8601")
|
|
869
|
-
return date(dateStr);
|
|
884
|
+
if (format2 === "ISO8601") return date(dateStr);
|
|
870
885
|
const genitive = styles.includes(format2) || typeof format2 === "object";
|
|
871
886
|
const formatParts = validate(parts(format2, locale).filter(partFilter));
|
|
872
|
-
if (!formatParts.length)
|
|
873
|
-
throw new Error("parse() requires a pattern.");
|
|
887
|
+
if (!formatParts.length) throw new Error("parse() requires a pattern.");
|
|
874
888
|
let parsedParts;
|
|
875
889
|
try {
|
|
876
890
|
parsedParts = parseParts(dateStr, formatParts);
|
|
@@ -884,17 +898,19 @@ function parse(dateStrOrOptions, format2 = "ISO8601", locale = "device") {
|
|
|
884
898
|
["DD", now.getDate()],
|
|
885
899
|
["HH", 0],
|
|
886
900
|
["mm", 0],
|
|
887
|
-
["ss", 0]
|
|
901
|
+
["ss", 0],
|
|
902
|
+
["SSS", 0]
|
|
888
903
|
]);
|
|
889
904
|
let a = null;
|
|
890
905
|
let offset2 = "";
|
|
891
906
|
parsedParts.forEach((part) => {
|
|
892
|
-
if (part.partName === "literal")
|
|
893
|
-
|
|
894
|
-
if (part.token === part.value)
|
|
895
|
-
return invalid();
|
|
907
|
+
if (part.partName === "literal") return;
|
|
908
|
+
if (part.token === part.value) return invalid();
|
|
896
909
|
const v = Number(part.value);
|
|
897
|
-
if (
|
|
910
|
+
if (part.token === "SSS") {
|
|
911
|
+
const digits = part.value.padEnd(3, "0").slice(0, 3);
|
|
912
|
+
parsed.set("SSS", Number(digits));
|
|
913
|
+
} else if (parsed.has(part.token)) {
|
|
898
914
|
parsed.set(part.token, v);
|
|
899
915
|
} else if (part.token === "YY") {
|
|
900
916
|
parsed.set("YYYY", fourDigitYear(part.value));
|
|
@@ -934,17 +950,24 @@ function parse(dateStrOrOptions, format2 = "ISO8601", locale = "device") {
|
|
|
934
950
|
parsed.set("HH", 0);
|
|
935
951
|
}
|
|
936
952
|
parsed.set("MM", (parsed.get("MM") || 1) - 1);
|
|
937
|
-
let [Y, M, D, h, m, s] = Array.from(parsed.values());
|
|
953
|
+
let [Y, M, D, h, m, s, ms] = Array.from(parsed.values());
|
|
938
954
|
const maxDaysInMonth = monthDays(/* @__PURE__ */ new Date(`${four(Y)}-${two(M + 1)}-10`));
|
|
939
955
|
if (maxDaysInMonth < D && dateOverflow === "throw")
|
|
940
956
|
throw new Error(`Invalid date ${four(Y)}-${two(M + 1)}-${two(D)}`);
|
|
941
957
|
D = dateOverflow === "backward" ? Math.min(D, maxDaysInMonth) : D;
|
|
942
|
-
const
|
|
943
|
-
|
|
944
|
-
|
|
958
|
+
const msStr = String(ms).padStart(3, "0");
|
|
959
|
+
if (offset2) {
|
|
960
|
+
const isoStringUtc = `${four(Y)}-${two(M + 1)}-${two(D)}T${two(h)}:${two(m)}:${two(s)}.${msStr}Z`;
|
|
961
|
+
const d2 = new Date(isoStringUtc);
|
|
962
|
+
if (!isFinite(+d2)) return invalid();
|
|
963
|
+
const len = fixedLengthByOffset(offset2);
|
|
964
|
+
const token = len === 5 || len === 8 ? "ZZ" : "Z";
|
|
965
|
+
const offsetSecs = offsetToSecs(offset2, token);
|
|
966
|
+
return new Date(d2.getTime() - offsetSecs * 1e3);
|
|
967
|
+
}
|
|
968
|
+
const isoString = `${four(Y)}-${two(M + 1)}-${two(D)}T${two(h)}:${two(m)}:${two(s)}.${msStr}`;
|
|
945
969
|
const d = new Date(isoString);
|
|
946
|
-
if (isFinite(+d))
|
|
947
|
-
return d;
|
|
970
|
+
if (isFinite(+d)) return d;
|
|
948
971
|
return invalid();
|
|
949
972
|
}
|
|
950
973
|
function parseParts(dateStr, formatParts) {
|
|
@@ -964,13 +987,18 @@ function parseParts(dateStr, formatParts) {
|
|
|
964
987
|
len = current.partValue.length;
|
|
965
988
|
} else if (current.partName === "timeZoneName") {
|
|
966
989
|
len = fixedLengthByOffset(dateStr.substring(pos));
|
|
990
|
+
} else if (current.token === "SSS") {
|
|
991
|
+
let end = pos;
|
|
992
|
+
while (end < dateStr.length && /\d/.test(dateStr.charAt(end))) {
|
|
993
|
+
end++;
|
|
994
|
+
}
|
|
995
|
+
len = end - pos;
|
|
967
996
|
} else if (current.token in fixedLength) {
|
|
968
997
|
len = fixedLength[current.token];
|
|
969
998
|
} else if (next) {
|
|
970
999
|
if (next.partName === "literal") {
|
|
971
1000
|
len = dateStr.indexOf(next.partValue, pos) - pos;
|
|
972
|
-
if (len < 0)
|
|
973
|
-
throw new Error();
|
|
1001
|
+
if (len < 0) throw new Error();
|
|
974
1002
|
} else if (next.partName === "dayPeriod") {
|
|
975
1003
|
for (let i2 = 1; i2 <= 4; i2++) {
|
|
976
1004
|
if (isNaN(Number(dateStr.charAt(pos + i2)))) {
|
|
@@ -980,8 +1008,7 @@ function parseParts(dateStr, formatParts) {
|
|
|
980
1008
|
}
|
|
981
1009
|
} else {
|
|
982
1010
|
const nextChar = dateStr.substring(pos).search(/\d/);
|
|
983
|
-
if (nextChar !== -1)
|
|
984
|
-
len = pos + nextChar;
|
|
1011
|
+
if (nextChar !== -1) len = pos + nextChar;
|
|
985
1012
|
}
|
|
986
1013
|
} else {
|
|
987
1014
|
len = dateStr.length;
|
|
@@ -1006,6 +1033,13 @@ function sameSecond(inputDateA, inputDateB) {
|
|
|
1006
1033
|
return a.getSeconds() === b.getSeconds();
|
|
1007
1034
|
}
|
|
1008
1035
|
|
|
1036
|
+
// src/sameMillisecond.ts
|
|
1037
|
+
function sameMillisecond(inputDateA, inputDateB) {
|
|
1038
|
+
const a = date(inputDateA);
|
|
1039
|
+
const b = date(inputDateB);
|
|
1040
|
+
return a.getMilliseconds() === b.getMilliseconds();
|
|
1041
|
+
}
|
|
1042
|
+
|
|
1009
1043
|
// src/sameMinute.ts
|
|
1010
1044
|
function sameMinute(inputDateA, inputDateB) {
|
|
1011
1045
|
const a = date(inputDateA);
|
|
@@ -1027,14 +1061,62 @@ function sameYear(inputDateA, inputDateB) {
|
|
|
1027
1061
|
return a.getFullYear() === b.getFullYear();
|
|
1028
1062
|
}
|
|
1029
1063
|
|
|
1064
|
+
// src/setMilliseconds.ts
|
|
1065
|
+
function setMilliseconds(inputDate, ms) {
|
|
1066
|
+
const d = date(inputDate);
|
|
1067
|
+
d.setMilliseconds(ms);
|
|
1068
|
+
return d;
|
|
1069
|
+
}
|
|
1070
|
+
|
|
1071
|
+
// src/setSeconds.ts
|
|
1072
|
+
function setSeconds(inputDate, second) {
|
|
1073
|
+
const d = date(inputDate);
|
|
1074
|
+
d.setSeconds(second);
|
|
1075
|
+
return d;
|
|
1076
|
+
}
|
|
1077
|
+
|
|
1078
|
+
// src/setMinutes.ts
|
|
1079
|
+
function setMinutes(inputDate, minute) {
|
|
1080
|
+
const d = date(inputDate);
|
|
1081
|
+
d.setMinutes(minute);
|
|
1082
|
+
return d;
|
|
1083
|
+
}
|
|
1084
|
+
|
|
1085
|
+
// src/setHour.ts
|
|
1086
|
+
function setHour(inputDate, hour) {
|
|
1087
|
+
const d = date(inputDate);
|
|
1088
|
+
d.setHours(hour);
|
|
1089
|
+
return d;
|
|
1090
|
+
}
|
|
1091
|
+
|
|
1092
|
+
// src/setDayOfMonth.ts
|
|
1093
|
+
function setDayOfMonth(inputDate, day, dateOverflow = false) {
|
|
1094
|
+
const d = date(inputDate);
|
|
1095
|
+
const daysInMonth = monthDays(d);
|
|
1096
|
+
if (!dateOverflow) {
|
|
1097
|
+
day = day > daysInMonth ? daysInMonth : day;
|
|
1098
|
+
}
|
|
1099
|
+
d.setDate(day);
|
|
1100
|
+
return d;
|
|
1101
|
+
}
|
|
1102
|
+
|
|
1103
|
+
// src/setMonth.ts
|
|
1104
|
+
function setMonth(inputDate, month, dateOverflow = false) {
|
|
1105
|
+
return handleOverflow(inputDate, (d) => d.setMonth(month), dateOverflow);
|
|
1106
|
+
}
|
|
1107
|
+
|
|
1108
|
+
// src/setYear.ts
|
|
1109
|
+
function setYear(inputDate, year, dateOverflow = false) {
|
|
1110
|
+
return handleOverflow(inputDate, (d) => d.setFullYear(year), dateOverflow);
|
|
1111
|
+
}
|
|
1112
|
+
|
|
1030
1113
|
// src/weekStart.ts
|
|
1031
1114
|
function weekStart(inputDate, startOfWeekDay = 0) {
|
|
1032
1115
|
const d = date(inputDate);
|
|
1033
|
-
let
|
|
1034
|
-
if (
|
|
1035
|
-
|
|
1036
|
-
d.
|
|
1037
|
-
d.setHours(0, 0, 0);
|
|
1116
|
+
let diff2 = startOfWeekDay - d.getDay();
|
|
1117
|
+
if (diff2 > 0) diff2 = diff2 - 7;
|
|
1118
|
+
d.setDate(d.getDate() + diff2);
|
|
1119
|
+
d.setHours(0, 0, 0, 0);
|
|
1038
1120
|
return d;
|
|
1039
1121
|
}
|
|
1040
1122
|
|
|
@@ -1042,24 +1124,22 @@ function weekStart(inputDate, startOfWeekDay = 0) {
|
|
|
1042
1124
|
function weekEnd(inputDate, startOfWeekDay = 0) {
|
|
1043
1125
|
const d = weekStart(inputDate, startOfWeekDay);
|
|
1044
1126
|
d.setDate(d.getDate() + 6);
|
|
1045
|
-
d.setHours(23, 59, 59);
|
|
1127
|
+
d.setHours(23, 59, 59, 999);
|
|
1046
1128
|
return d;
|
|
1047
1129
|
}
|
|
1048
1130
|
|
|
1049
1131
|
// src/yearStart.ts
|
|
1050
1132
|
function yearStart(inputDate) {
|
|
1051
1133
|
const d = date(inputDate);
|
|
1052
|
-
d.setMonth(0);
|
|
1053
|
-
d.
|
|
1054
|
-
d.setHours(0, 0, 0);
|
|
1134
|
+
d.setMonth(0, 1);
|
|
1135
|
+
d.setHours(0, 0, 0, 0);
|
|
1055
1136
|
return d;
|
|
1056
1137
|
}
|
|
1057
1138
|
|
|
1058
1139
|
// src/yearEnd.ts
|
|
1059
1140
|
function yearEnd(inputDate) {
|
|
1060
1141
|
const d = date(inputDate);
|
|
1061
|
-
d.setMonth(11);
|
|
1062
|
-
d.setDate(31);
|
|
1142
|
+
d.setMonth(11, 31);
|
|
1063
1143
|
d.setHours(23, 59, 59, 999);
|
|
1064
1144
|
return d;
|
|
1065
1145
|
}
|
|
@@ -1085,6 +1165,16 @@ function isEqual(dateLeft, dateRight) {
|
|
|
1085
1165
|
return +_dateLeft === +_dateRight;
|
|
1086
1166
|
}
|
|
1087
1167
|
|
|
1168
|
+
// src/isPast.ts
|
|
1169
|
+
function isPast(inputDate) {
|
|
1170
|
+
return isBefore(inputDate);
|
|
1171
|
+
}
|
|
1172
|
+
|
|
1173
|
+
// src/isFuture.ts
|
|
1174
|
+
function isFuture(inputDate) {
|
|
1175
|
+
return isAfter(inputDate);
|
|
1176
|
+
}
|
|
1177
|
+
|
|
1088
1178
|
// src/diffMilliseconds.ts
|
|
1089
1179
|
function diffMilliseconds(dateA, dateB) {
|
|
1090
1180
|
const left = date(dateA);
|
|
@@ -1100,17 +1190,26 @@ function diffRound(value, method = "trunc") {
|
|
|
1100
1190
|
|
|
1101
1191
|
// src/diffSeconds.ts
|
|
1102
1192
|
function diffSeconds(dateA, dateB, roundingMethod) {
|
|
1103
|
-
return diffRound(
|
|
1193
|
+
return diffRound(
|
|
1194
|
+
// @ts-ignore
|
|
1195
|
+
diffMilliseconds(dateA, dateB) / 1e3,
|
|
1196
|
+
roundingMethod
|
|
1197
|
+
);
|
|
1104
1198
|
}
|
|
1105
1199
|
|
|
1106
1200
|
// src/diffMinutes.ts
|
|
1107
1201
|
function diffMinutes(dateA, dateB, roundingMethod) {
|
|
1108
|
-
return diffRound(
|
|
1202
|
+
return diffRound(
|
|
1203
|
+
//@ts-ignore
|
|
1204
|
+
diffMilliseconds(dateA, dateB) / 6e4,
|
|
1205
|
+
roundingMethod
|
|
1206
|
+
);
|
|
1109
1207
|
}
|
|
1110
1208
|
|
|
1111
1209
|
// src/diffHours.ts
|
|
1112
1210
|
function diffHours(dateA, dateB, roundingMethod) {
|
|
1113
1211
|
return diffRound(
|
|
1212
|
+
//@ts-ignore
|
|
1114
1213
|
diffMilliseconds(dateA, dateB) / 36e5,
|
|
1115
1214
|
// 1000 * 60 * 60
|
|
1116
1215
|
roundingMethod
|
|
@@ -1120,6 +1219,7 @@ function diffHours(dateA, dateB, roundingMethod) {
|
|
|
1120
1219
|
// src/diffDays.ts
|
|
1121
1220
|
function diffDays(dateA, dateB, roundingMethod) {
|
|
1122
1221
|
return diffRound(
|
|
1222
|
+
// @ts-ignore
|
|
1123
1223
|
diffMilliseconds(dateA, dateB) / 864e5,
|
|
1124
1224
|
// hour * 24
|
|
1125
1225
|
roundingMethod
|
|
@@ -1157,12 +1257,85 @@ function diffMonths(dateA, dateB) {
|
|
|
1157
1257
|
|
|
1158
1258
|
// src/diffYears.ts
|
|
1159
1259
|
function diffYears(dateA, dateB) {
|
|
1160
|
-
const r = Math.trunc(
|
|
1260
|
+
const r = Math.trunc(
|
|
1261
|
+
//@ts-ignore
|
|
1262
|
+
diffMonths(dateA, dateB) / 12
|
|
1263
|
+
);
|
|
1161
1264
|
return r == 0 ? 0 : r;
|
|
1162
1265
|
}
|
|
1266
|
+
|
|
1267
|
+
// src/diff.ts
|
|
1268
|
+
function negateDuration(duration) {
|
|
1269
|
+
const negated = {};
|
|
1270
|
+
for (const unit of Object.keys(duration)) {
|
|
1271
|
+
negated[unit] = -duration[unit];
|
|
1272
|
+
}
|
|
1273
|
+
return negated;
|
|
1274
|
+
}
|
|
1275
|
+
function calendarDiff(current, target, diffUnit, addUnit) {
|
|
1276
|
+
let amount = diffUnit(current, target);
|
|
1277
|
+
let next = addUnit(current, -amount);
|
|
1278
|
+
while (amount > 0 && next < target) {
|
|
1279
|
+
amount--;
|
|
1280
|
+
next = addUnit(current, -amount);
|
|
1281
|
+
}
|
|
1282
|
+
return [amount, next];
|
|
1283
|
+
}
|
|
1284
|
+
function diff(dateA, dateB, options) {
|
|
1285
|
+
let a = date(dateA);
|
|
1286
|
+
let b = date(dateB);
|
|
1287
|
+
if (a < b) {
|
|
1288
|
+
const duration2 = diff(b, a, options);
|
|
1289
|
+
return (options == null ? void 0 : options.abs) ? duration2 : negateDuration(duration2);
|
|
1290
|
+
}
|
|
1291
|
+
const skip = new Set(options == null ? void 0 : options.skip);
|
|
1292
|
+
const duration = {};
|
|
1293
|
+
if (!skip.has("years")) {
|
|
1294
|
+
const [years, next] = calendarDiff(a, b, diffYears, addYear);
|
|
1295
|
+
a = next;
|
|
1296
|
+
if (years) duration.years = years;
|
|
1297
|
+
}
|
|
1298
|
+
if (!skip.has("months")) {
|
|
1299
|
+
const [months, next] = calendarDiff(a, b, diffMonths, addMonth);
|
|
1300
|
+
a = next;
|
|
1301
|
+
if (months) duration.months = months;
|
|
1302
|
+
}
|
|
1303
|
+
if (!skip.has("weeks")) {
|
|
1304
|
+
const weeks = diffWeeks(a, b);
|
|
1305
|
+
a = addDay(a, -(weeks * 7));
|
|
1306
|
+
if (weeks) duration.weeks = weeks;
|
|
1307
|
+
}
|
|
1308
|
+
if (!skip.has("days")) {
|
|
1309
|
+
const days = diffDays(a, b);
|
|
1310
|
+
a = addDay(a, -days);
|
|
1311
|
+
if (days) duration.days = days;
|
|
1312
|
+
}
|
|
1313
|
+
if (!skip.has("hours")) {
|
|
1314
|
+
const hours = diffHours(a, b);
|
|
1315
|
+
a = addHour(a, -hours);
|
|
1316
|
+
if (hours) duration.hours = hours;
|
|
1317
|
+
}
|
|
1318
|
+
if (!skip.has("minutes")) {
|
|
1319
|
+
const minutes = diffMinutes(a, b);
|
|
1320
|
+
a = addMinute(a, -minutes);
|
|
1321
|
+
if (minutes) duration.minutes = minutes;
|
|
1322
|
+
}
|
|
1323
|
+
if (!skip.has("seconds")) {
|
|
1324
|
+
const seconds = diffSeconds(a, b);
|
|
1325
|
+
a = addSecond(a, -seconds);
|
|
1326
|
+
if (seconds) duration.seconds = seconds;
|
|
1327
|
+
}
|
|
1328
|
+
if (!skip.has("milliseconds")) {
|
|
1329
|
+
const ms = diffMilliseconds(a, b);
|
|
1330
|
+
if (ms) duration.milliseconds = ms;
|
|
1331
|
+
}
|
|
1332
|
+
return duration;
|
|
1333
|
+
}
|
|
1163
1334
|
export {
|
|
1335
|
+
add,
|
|
1164
1336
|
addDay,
|
|
1165
1337
|
addHour,
|
|
1338
|
+
addMillisecond,
|
|
1166
1339
|
addMinute,
|
|
1167
1340
|
addMonth,
|
|
1168
1341
|
addSecond,
|
|
@@ -1173,6 +1346,7 @@ export {
|
|
|
1173
1346
|
dayEnd,
|
|
1174
1347
|
dayOfYear,
|
|
1175
1348
|
dayStart,
|
|
1349
|
+
diff,
|
|
1176
1350
|
diffDays,
|
|
1177
1351
|
diffHours,
|
|
1178
1352
|
diffMilliseconds,
|
|
@@ -1189,6 +1363,8 @@ export {
|
|
|
1189
1363
|
isAfter,
|
|
1190
1364
|
isBefore,
|
|
1191
1365
|
isEqual,
|
|
1366
|
+
isFuture,
|
|
1367
|
+
isPast,
|
|
1192
1368
|
iso8601,
|
|
1193
1369
|
minuteEnd,
|
|
1194
1370
|
minuteStart,
|
|
@@ -1204,9 +1380,17 @@ export {
|
|
|
1204
1380
|
removeOffset,
|
|
1205
1381
|
sameDay,
|
|
1206
1382
|
sameHour,
|
|
1383
|
+
sameMillisecond,
|
|
1207
1384
|
sameMinute,
|
|
1208
1385
|
sameSecond,
|
|
1209
1386
|
sameYear,
|
|
1387
|
+
setDayOfMonth,
|
|
1388
|
+
setHour,
|
|
1389
|
+
setMilliseconds,
|
|
1390
|
+
setMinutes,
|
|
1391
|
+
setMonth,
|
|
1392
|
+
setSeconds,
|
|
1393
|
+
setYear,
|
|
1210
1394
|
tzDate,
|
|
1211
1395
|
weekEnd,
|
|
1212
1396
|
weekStart,
|