@stacksjs/datetime 0.59.10 → 0.61.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/dist/index.js +200 -134
- package/package.json +2 -2
- package/dist/index.d.ts +0 -5
package/dist/index.js
CHANGED
|
@@ -2,16 +2,16 @@
|
|
|
2
2
|
// src/index.ts
|
|
3
3
|
import {useDateFormat, useNow} from "@stacksjs/utils";
|
|
4
4
|
|
|
5
|
-
// ../../../../node_modules/@formkit/tempo/dist/
|
|
6
|
-
var iso8601 = function(
|
|
7
|
-
const matches =
|
|
5
|
+
// ../../../../node_modules/@formkit/tempo/dist/iso8601.mjs
|
|
6
|
+
var iso8601 = function(date) {
|
|
7
|
+
const matches = date.match(iso8601Match);
|
|
8
8
|
if (matches) {
|
|
9
9
|
const month = Number(matches[2]);
|
|
10
10
|
if (month < 1 || month > 12)
|
|
11
11
|
return false;
|
|
12
12
|
if (typeof matches[3] !== undefined) {
|
|
13
|
-
const
|
|
14
|
-
if (
|
|
13
|
+
const date2 = Number(matches[3]);
|
|
14
|
+
if (date2 < 1 || date2 > 31)
|
|
15
15
|
return false;
|
|
16
16
|
}
|
|
17
17
|
if (typeof matches[4] !== undefined) {
|
|
@@ -23,6 +23,9 @@ var iso8601 = function(date2) {
|
|
|
23
23
|
}
|
|
24
24
|
return false;
|
|
25
25
|
};
|
|
26
|
+
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})?$/;
|
|
27
|
+
|
|
28
|
+
// ../../../../node_modules/@formkit/tempo/dist/date.mjs
|
|
26
29
|
var normalize = function(date2) {
|
|
27
30
|
const matches = date2.match(iso8601Match);
|
|
28
31
|
if (matches && typeof matches[4] === "undefined") {
|
|
@@ -45,6 +48,8 @@ var date = function(date2) {
|
|
|
45
48
|
}
|
|
46
49
|
throw new Error(`Non ISO 8601 compliant date (${date2}).`);
|
|
47
50
|
};
|
|
51
|
+
|
|
52
|
+
// ../../../../node_modules/@formkit/tempo/dist/monthEnd.mjs
|
|
48
53
|
var monthEnd = function(inputDate) {
|
|
49
54
|
const d = date(inputDate);
|
|
50
55
|
d.setDate(1);
|
|
@@ -52,49 +57,62 @@ var monthEnd = function(inputDate) {
|
|
|
52
57
|
d.setDate(0);
|
|
53
58
|
return d;
|
|
54
59
|
};
|
|
60
|
+
|
|
61
|
+
// ../../../../node_modules/@formkit/tempo/dist/monthDays.mjs
|
|
55
62
|
var monthDays = function(inputDate) {
|
|
56
63
|
const d = monthEnd(inputDate);
|
|
57
64
|
return d.getDate();
|
|
58
65
|
};
|
|
66
|
+
|
|
67
|
+
// ../../../../node_modules/@formkit/tempo/dist/common.mjs
|
|
68
|
+
var fixedLengthByOffset = function(offsetString) {
|
|
69
|
+
if (/^[+-]\d{2}:\d{2}/.test(offsetString)) {
|
|
70
|
+
return 6;
|
|
71
|
+
}
|
|
72
|
+
if (/^[+-]\d{4}/.test(offsetString)) {
|
|
73
|
+
return 5;
|
|
74
|
+
}
|
|
75
|
+
throw new Error("Invalid offset format");
|
|
76
|
+
};
|
|
59
77
|
var normStr = function(part) {
|
|
60
78
|
if (part.type === "literal") {
|
|
61
79
|
part.value = part.value.normalize("NFKC");
|
|
62
80
|
}
|
|
63
81
|
return part;
|
|
64
82
|
};
|
|
65
|
-
var fill = function(inputDate,
|
|
66
|
-
const partMap = createPartMap(inputDate,
|
|
83
|
+
var fill = function(inputDate, parts, locale, genitive = false, offset = null) {
|
|
84
|
+
const partMap = createPartMap(inputDate, parts, locale, genitive);
|
|
67
85
|
const d = date(inputDate);
|
|
68
86
|
function value({ partName, partValue, token }) {
|
|
69
87
|
if (partName === "literal")
|
|
70
88
|
return partValue;
|
|
71
89
|
const value2 = partMap[partName];
|
|
72
90
|
if (partName === "hour" && token === "H") {
|
|
73
|
-
return value2.replace(/^0/, "");
|
|
91
|
+
return value2.replace(/^0/, "") || "0";
|
|
74
92
|
}
|
|
75
|
-
if (
|
|
93
|
+
if (["mm", "ss", "MM"].includes(token) && value2.length === 1) {
|
|
76
94
|
return `0${value2}`;
|
|
77
95
|
}
|
|
78
96
|
if (partName === "dayPeriod") {
|
|
79
|
-
const p =
|
|
97
|
+
const p = ap2(d.getUTCHours() < 12 ? "am" : "pm", locale);
|
|
80
98
|
return token === "A" ? p.toUpperCase() : p.toLowerCase();
|
|
81
99
|
}
|
|
82
100
|
if (partName === "timeZoneName") {
|
|
83
|
-
return
|
|
101
|
+
return offset != null ? offset : minsToOffset(-1 * d.getTimezoneOffset(), token);
|
|
84
102
|
}
|
|
85
103
|
return value2;
|
|
86
104
|
}
|
|
87
|
-
return
|
|
105
|
+
return parts.map((part) => {
|
|
88
106
|
return {
|
|
89
107
|
...part,
|
|
90
108
|
value: value(part)
|
|
91
109
|
};
|
|
92
110
|
});
|
|
93
111
|
};
|
|
94
|
-
var createPartMap = function(inputDate,
|
|
112
|
+
var createPartMap = function(inputDate, parts, locale, genitive = false) {
|
|
95
113
|
const d = date(inputDate);
|
|
96
|
-
const hour12 =
|
|
97
|
-
const hour24 =
|
|
114
|
+
const hour12 = parts.filter((part) => part.hour12);
|
|
115
|
+
const hour24 = parts.filter((part) => !part.hour12);
|
|
98
116
|
const valueParts = [];
|
|
99
117
|
const genitiveParts = [];
|
|
100
118
|
function addValues(requestedParts, hour122 = false) {
|
|
@@ -141,23 +159,33 @@ var createPartMap = function(inputDate, parts2, locale, genitive = false) {
|
|
|
141
159
|
return map;
|
|
142
160
|
}, {});
|
|
143
161
|
};
|
|
144
|
-
var minsToOffset = function(timeDiffInMins) {
|
|
162
|
+
var minsToOffset = function(timeDiffInMins, token = "Z") {
|
|
145
163
|
const hours = String(Math.floor(Math.abs(timeDiffInMins / 60))).padStart(2, "0");
|
|
146
164
|
const mins = String(Math.abs(timeDiffInMins % 60)).padStart(2, "0");
|
|
147
165
|
const sign = timeDiffInMins < 0 ? "-" : "+";
|
|
148
|
-
|
|
166
|
+
if (token === "ZZ") {
|
|
167
|
+
return `${sign}${hours}${mins}`;
|
|
168
|
+
}
|
|
169
|
+
return `${sign}${hours}:${mins}`;
|
|
149
170
|
};
|
|
150
|
-
var offsetToMins = function(
|
|
151
|
-
validOffset(
|
|
152
|
-
const [_, sign, hours, mins] =
|
|
171
|
+
var offsetToMins = function(offset, token) {
|
|
172
|
+
validOffset(offset, token);
|
|
173
|
+
const [_, sign, hours, mins] = offset.match(/([+-])([0-3][0-9]):?([0-6][0-9])/);
|
|
153
174
|
const offsetInMins = Number(hours) * 60 + Number(mins);
|
|
154
175
|
return sign === "+" ? offsetInMins : -offsetInMins;
|
|
155
176
|
};
|
|
156
|
-
var validOffset = function(
|
|
157
|
-
const valid =
|
|
177
|
+
var validOffset = function(offset, token = "Z") {
|
|
178
|
+
const valid = ((token2) => {
|
|
179
|
+
switch (token2) {
|
|
180
|
+
case "Z":
|
|
181
|
+
return /^([+-])[0-3][0-9]:[0-6][0-9]$/.test(offset);
|
|
182
|
+
case "ZZ":
|
|
183
|
+
return /^([+-])[0-3][0-9][0-6][0-9]$/.test(offset);
|
|
184
|
+
}
|
|
185
|
+
})(token);
|
|
158
186
|
if (!valid)
|
|
159
|
-
throw new Error(`Invalid offset: ${
|
|
160
|
-
return
|
|
187
|
+
throw new Error(`Invalid offset: ${offset}`);
|
|
188
|
+
return offset;
|
|
161
189
|
};
|
|
162
190
|
var escapeTokens = function(str) {
|
|
163
191
|
return clockAgnostic.concat(clock24).concat(clock12).sort((a, b) => a[0].length > b[0].length ? 1 : -1).reduce((target, part) => {
|
|
@@ -167,9 +195,9 @@ var escapeTokens = function(str) {
|
|
|
167
195
|
var isNumeric = function(part) {
|
|
168
196
|
return ["numeric", "2-digit"].includes(part.partValue);
|
|
169
197
|
};
|
|
170
|
-
var validate = function(
|
|
198
|
+
var validate = function(parts) {
|
|
171
199
|
let lastPart = undefined;
|
|
172
|
-
for (const part of
|
|
200
|
+
for (const part of parts) {
|
|
173
201
|
if (part.partName === "literal" && !isNaN(parseFloat(part.partValue))) {
|
|
174
202
|
throw new Error(`Numbers in format (${part.partValue}).`);
|
|
175
203
|
}
|
|
@@ -180,9 +208,71 @@ var validate = function(parts2) {
|
|
|
180
208
|
}
|
|
181
209
|
lastPart = part;
|
|
182
210
|
}
|
|
183
|
-
return
|
|
211
|
+
return parts;
|
|
212
|
+
};
|
|
213
|
+
var getOffsetFormat = function(format) {
|
|
214
|
+
if (typeof format === "string") {
|
|
215
|
+
return format.includes("ZZ") ? "ZZ" : "Z";
|
|
216
|
+
}
|
|
217
|
+
return "time" in format && format.time === "full" ? "Z" : "ZZ";
|
|
218
|
+
};
|
|
219
|
+
var specDate = "1999-03-04T02:05:01.000Z";
|
|
220
|
+
var memoParts = new Map;
|
|
221
|
+
var clockAgnostic = [
|
|
222
|
+
["YYYY", { year: "numeric" }],
|
|
223
|
+
["YY", { year: "2-digit" }],
|
|
224
|
+
["MMMM", { month: "long" }],
|
|
225
|
+
["MMM", { month: "short" }],
|
|
226
|
+
["MM", { month: "2-digit" }],
|
|
227
|
+
["M", { month: "numeric" }],
|
|
228
|
+
["DD", { day: "2-digit" }],
|
|
229
|
+
["D", { day: "numeric" }],
|
|
230
|
+
["dddd", { weekday: "long" }],
|
|
231
|
+
["ddd", { weekday: "short" }],
|
|
232
|
+
["d", { weekday: "narrow" }],
|
|
233
|
+
["mm", { minute: "2-digit" }],
|
|
234
|
+
["m", { minute: "numeric" }],
|
|
235
|
+
["ss", { second: "2-digit" }],
|
|
236
|
+
["s", { second: "numeric" }],
|
|
237
|
+
["ZZ", { timeZoneName: "long" }],
|
|
238
|
+
["Z", { timeZoneName: "short" }]
|
|
239
|
+
];
|
|
240
|
+
var clock24 = [
|
|
241
|
+
["HH", { hour: "2-digit" }],
|
|
242
|
+
["H", { hour: "numeric" }]
|
|
243
|
+
];
|
|
244
|
+
var clock12 = [
|
|
245
|
+
["hh", { hour: "2-digit" }],
|
|
246
|
+
["h", { hour: "numeric" }],
|
|
247
|
+
["a", { dayPeriod: "narrow" }],
|
|
248
|
+
["A", { dayPeriod: "narrow" }]
|
|
249
|
+
];
|
|
250
|
+
var fixedLength = {
|
|
251
|
+
DD: 2,
|
|
252
|
+
HH: 2,
|
|
253
|
+
MM: 2,
|
|
254
|
+
YY: 2,
|
|
255
|
+
YYYY: 4,
|
|
256
|
+
hh: 2,
|
|
257
|
+
mm: 2,
|
|
258
|
+
ss: 2
|
|
184
259
|
};
|
|
185
|
-
var
|
|
260
|
+
var genitiveTokens = ["MMMM", "MMM", "dddd", "ddd"];
|
|
261
|
+
var tokens = new Map([...clockAgnostic, ...clock24, ...clock12].map((format) => {
|
|
262
|
+
return [format[0], format];
|
|
263
|
+
}));
|
|
264
|
+
var dayPeriodMap = new Map;
|
|
265
|
+
var styles = [
|
|
266
|
+
"full",
|
|
267
|
+
"long",
|
|
268
|
+
"medium",
|
|
269
|
+
"short"
|
|
270
|
+
];
|
|
271
|
+
var two = (n) => String(n).padStart(2, "0");
|
|
272
|
+
var four = (n) => String(n).padStart(2, "0");
|
|
273
|
+
|
|
274
|
+
// ../../../../node_modules/@formkit/tempo/dist/ap.mjs
|
|
275
|
+
var ap2 = function(ampm, locale) {
|
|
186
276
|
const l = dayPeriodMap.get(locale);
|
|
187
277
|
if (l && l[ampm])
|
|
188
278
|
return l[ampm];
|
|
@@ -201,14 +291,28 @@ var ap = function(ampm, locale) {
|
|
|
201
291
|
}
|
|
202
292
|
return ampm;
|
|
203
293
|
};
|
|
204
|
-
|
|
294
|
+
|
|
295
|
+
// ../../../../node_modules/@formkit/tempo/dist/applyOffset.mjs
|
|
296
|
+
var applyOffset = function(dateInput, offset = "+00:00") {
|
|
205
297
|
const d = date(dateInput);
|
|
206
|
-
const
|
|
298
|
+
const token = (() => {
|
|
299
|
+
switch (fixedLengthByOffset(offset)) {
|
|
300
|
+
case 5:
|
|
301
|
+
return "ZZ";
|
|
302
|
+
case 6:
|
|
303
|
+
return "Z";
|
|
304
|
+
}
|
|
305
|
+
})();
|
|
306
|
+
const timeDiffInMins = offsetToMins(offset, token);
|
|
207
307
|
return new Date(d.getTime() + timeDiffInMins * 1000 * 60);
|
|
208
308
|
};
|
|
309
|
+
|
|
310
|
+
// ../../../../node_modules/@formkit/tempo/dist/deviceTZ.mjs
|
|
209
311
|
var deviceTZ = function() {
|
|
210
312
|
return Intl.DateTimeFormat().resolvedOptions().timeZone;
|
|
211
313
|
};
|
|
314
|
+
|
|
315
|
+
// ../../../../node_modules/@formkit/tempo/dist/offset.mjs
|
|
212
316
|
var relativeTime = function(d, timeZone) {
|
|
213
317
|
const utcParts = new Intl.DateTimeFormat("en-US", {
|
|
214
318
|
year: "numeric",
|
|
@@ -220,25 +324,28 @@ var relativeTime = function(d, timeZone) {
|
|
|
220
324
|
timeZone,
|
|
221
325
|
hourCycle: "h23"
|
|
222
326
|
}).formatToParts(d).map(normStr);
|
|
223
|
-
const
|
|
327
|
+
const parts = {};
|
|
224
328
|
utcParts.forEach((part) => {
|
|
225
|
-
|
|
329
|
+
parts[part.type] = part.value;
|
|
226
330
|
});
|
|
227
|
-
return new Date(`${
|
|
331
|
+
return new Date(`${parts.year}-${parts.month}-${parts.day}T${parts.hour}:${parts.minute}:${parts.second}Z`);
|
|
228
332
|
};
|
|
229
|
-
var offset = function(utcTime, tzA = "UTC", tzB = "device") {
|
|
230
|
-
|
|
333
|
+
var offset = function(utcTime, tzA = "UTC", tzB = "device", timeZoneToken = "Z") {
|
|
334
|
+
var _a;
|
|
335
|
+
tzB = tzB === "device" ? (_a = deviceTZ()) != null ? _a : "utc" : tzB;
|
|
231
336
|
const d = date(utcTime);
|
|
232
337
|
const timeA = relativeTime(d, tzA);
|
|
233
338
|
const timeB = relativeTime(d, tzB);
|
|
234
339
|
const timeDiffInMins = (timeB.getTime() - timeA.getTime()) / 1000 / 60;
|
|
235
|
-
return minsToOffset(timeDiffInMins);
|
|
340
|
+
return minsToOffset(timeDiffInMins, timeZoneToken);
|
|
236
341
|
};
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
342
|
+
|
|
343
|
+
// ../../../../node_modules/@formkit/tempo/dist/parts.mjs
|
|
344
|
+
var parts = function(format, locale) {
|
|
345
|
+
if (styles.includes(format) || typeof format === "object") {
|
|
346
|
+
return styleParts(format, locale);
|
|
240
347
|
}
|
|
241
|
-
let f =
|
|
348
|
+
let f = format;
|
|
242
349
|
let match = 0;
|
|
243
350
|
const testPattern = (pattern) => {
|
|
244
351
|
if (!pattern[2])
|
|
@@ -292,17 +399,17 @@ var parts = function(format2, locale) {
|
|
|
292
399
|
};
|
|
293
400
|
}).filter((part) => !(part.partName === "literal" && part.partValue === ""));
|
|
294
401
|
};
|
|
295
|
-
var styleParts = function(
|
|
402
|
+
var styleParts = function(format, locale) {
|
|
296
403
|
const options = {
|
|
297
404
|
timeZone: "UTC"
|
|
298
405
|
};
|
|
299
|
-
if (typeof
|
|
300
|
-
options.dateStyle =
|
|
406
|
+
if (typeof format === "string") {
|
|
407
|
+
options.dateStyle = format;
|
|
301
408
|
} else {
|
|
302
|
-
if ("date" in
|
|
303
|
-
options.dateStyle =
|
|
304
|
-
if ("time" in
|
|
305
|
-
options.timeStyle =
|
|
409
|
+
if ("date" in format)
|
|
410
|
+
options.dateStyle = format.date;
|
|
411
|
+
if ("time" in format)
|
|
412
|
+
options.timeStyle = format.time;
|
|
306
413
|
}
|
|
307
414
|
const formatter = new Intl.DateTimeFormat(locale, options);
|
|
308
415
|
const segments = formatter.formatToParts(new Date(specDate)).map(normStr);
|
|
@@ -311,7 +418,7 @@ var styleParts = function(format2, locale) {
|
|
|
311
418
|
const hourType = hourPart && hourPart.value === "23" ? 24 : 12;
|
|
312
419
|
return segments.map((part) => {
|
|
313
420
|
const partName = part.type;
|
|
314
|
-
const formatPattern = guessPattern(part.type, part.value, locale, part.type === "hour" ? hourType : undefined);
|
|
421
|
+
const formatPattern = guessPattern(part.type, part.value, locale, part.type === "hour" ? hourType : undefined, options);
|
|
315
422
|
if (formatPattern === undefined)
|
|
316
423
|
return;
|
|
317
424
|
const partValue = formatPattern[1][partName];
|
|
@@ -329,7 +436,7 @@ var styleParts = function(format2, locale) {
|
|
|
329
436
|
};
|
|
330
437
|
}).filter((part) => !!part);
|
|
331
438
|
};
|
|
332
|
-
var guessPattern = function(partName, partValue, locale, hour) {
|
|
439
|
+
var guessPattern = function(partName, partValue, locale, hour, options) {
|
|
333
440
|
const l = partValue.length;
|
|
334
441
|
const n = !isNaN(Number(partValue));
|
|
335
442
|
let style;
|
|
@@ -371,31 +478,30 @@ var guessPattern = function(partName, partValue, locale, hour) {
|
|
|
371
478
|
case "literal":
|
|
372
479
|
return [partValue, { literal: partValue }, new RegExp("")];
|
|
373
480
|
case "timeZoneName":
|
|
374
|
-
|
|
375
|
-
return offset2.length === 2 && offset2[1].length === 4 ? tokens.get("ZZ") : tokens.get("Z");
|
|
481
|
+
return options.timeStyle === "full" ? tokens.get("Z") : tokens.get("ZZ");
|
|
376
482
|
default:
|
|
377
483
|
return;
|
|
378
484
|
}
|
|
379
485
|
};
|
|
380
486
|
var partStyle = function(locale, part, value) {
|
|
381
487
|
if (!memoParts.has(locale)) {
|
|
382
|
-
const
|
|
488
|
+
const date16 = new Date(specDate);
|
|
383
489
|
const weekdays = [3, 8, 9, 7, 6, 4, 3];
|
|
384
490
|
const parts2 = ["weekday", "month", "dayPeriod"];
|
|
385
491
|
const partStyles = ["long", "short", "narrow"];
|
|
386
492
|
const formats2 = {};
|
|
387
493
|
for (let i = 0;i < 12; i++) {
|
|
388
|
-
|
|
494
|
+
date16.setMonth(0 + i);
|
|
389
495
|
if (i in weekdays)
|
|
390
|
-
|
|
391
|
-
|
|
496
|
+
date16.setDate(weekdays[i]);
|
|
497
|
+
date16.setUTCHours(8 + i);
|
|
392
498
|
for (const style of partStyles) {
|
|
393
|
-
const segments = new Intl.DateTimeFormat(locale, parts2.reduce((options, part2) => Object.assign(options, { [part2]: style }), { hour12: true, timeZone: "UTC" })).formatToParts(
|
|
499
|
+
const segments = new Intl.DateTimeFormat(locale, parts2.reduce((options, part2) => Object.assign(options, { [part2]: style }), { hour12: true, timeZone: "UTC" })).formatToParts(date16).map(normStr);
|
|
394
500
|
if (style === "long" || style === "short") {
|
|
395
501
|
const genitiveFormattedParts = new Intl.DateTimeFormat(locale, {
|
|
396
502
|
dateStyle: style === "short" ? "medium" : "long",
|
|
397
503
|
timeZone: "UTC"
|
|
398
|
-
}).formatToParts(
|
|
504
|
+
}).formatToParts(date16).map(normStr);
|
|
399
505
|
const genitiveMonth = genitiveFormattedParts.find((part2) => part2.type === "month");
|
|
400
506
|
const index = segments.findIndex((part2) => part2.type === "month");
|
|
401
507
|
if (index > -1 && genitiveMonth)
|
|
@@ -416,13 +522,19 @@ var partStyle = function(locale, part, value) {
|
|
|
416
522
|
const formats = memoParts.get(locale);
|
|
417
523
|
return formats ? formats[part][value] : undefined;
|
|
418
524
|
};
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
525
|
+
|
|
526
|
+
// ../../../../node_modules/@formkit/tempo/dist/removeOffset.mjs
|
|
527
|
+
var removeOffset = function(dateInput, offset3 = "+00:00") {
|
|
528
|
+
const positive = offset3.slice(0, 1) === "+";
|
|
529
|
+
return applyOffset(dateInput, offset3.replace(positive ? "+" : "-", positive ? "-" : "+"));
|
|
422
530
|
};
|
|
531
|
+
|
|
532
|
+
// ../../../../node_modules/@formkit/tempo/dist/deviceLocale.mjs
|
|
423
533
|
var deviceLocale = function() {
|
|
424
534
|
return Intl.DateTimeFormat().resolvedOptions().locale;
|
|
425
535
|
};
|
|
536
|
+
|
|
537
|
+
// ../../../../node_modules/@formkit/tempo/dist/format.mjs
|
|
426
538
|
var format = function(inputDateOrOptions, format2 = "long", locale = "device", genitive = false, partFilter) {
|
|
427
539
|
let tz, forceOffset;
|
|
428
540
|
if (typeof inputDateOrOptions === "object" && !(inputDateOrOptions instanceof Date)) {
|
|
@@ -438,10 +550,10 @@ var format = function(inputDateOrOptions, format2 = "long", locale = "device", g
|
|
|
438
550
|
if (format2 === "ISO8601")
|
|
439
551
|
return date(inputDateOrOptions).toISOString();
|
|
440
552
|
if (tz) {
|
|
441
|
-
forceOffset = offset(inputDateOrOptions, "utc", tz);
|
|
553
|
+
forceOffset = offset(inputDateOrOptions, "utc", tz, getOffsetFormat(format2));
|
|
442
554
|
}
|
|
443
555
|
tz != null || (tz = deviceTZ());
|
|
444
|
-
if (tz.toLowerCase() !== "utc") {
|
|
556
|
+
if ((tz == null ? undefined : tz.toLowerCase()) !== "utc") {
|
|
445
557
|
inputDateOrOptions = removeOffset(inputDateOrOptions, offset(inputDateOrOptions, tz, "utc"));
|
|
446
558
|
}
|
|
447
559
|
if (!locale || locale === "device") {
|
|
@@ -449,9 +561,13 @@ var format = function(inputDateOrOptions, format2 = "long", locale = "device", g
|
|
|
449
561
|
}
|
|
450
562
|
return fill(inputDateOrOptions, parts(format2, locale).filter(partFilter != null ? partFilter : () => true), locale, genitive, forceOffset).map((p) => p.value).join("");
|
|
451
563
|
};
|
|
564
|
+
|
|
565
|
+
// ../../../../node_modules/@formkit/tempo/dist/formatStr.mjs
|
|
452
566
|
var formatStr = function(format2, locale = "en", escapeLiterals = false, filterParts = () => true) {
|
|
453
567
|
return parts(format2, locale).filter(filterParts).reduce((f, p) => f += escapeLiterals && p.partName === "literal" ? escapeTokens(p.token) : p.token, "").normalize("NFKC");
|
|
454
568
|
};
|
|
569
|
+
|
|
570
|
+
// ../../../../node_modules/@formkit/tempo/dist/fourDigitYear.mjs
|
|
455
571
|
var fourDigitYear = function(value) {
|
|
456
572
|
const y = (new Date()).getFullYear();
|
|
457
573
|
const currentYear = y % 100;
|
|
@@ -459,6 +575,8 @@ var fourDigitYear = function(value) {
|
|
|
459
575
|
const parsedYear = Number(value);
|
|
460
576
|
return (century + (parsedYear > currentYear + 20 ? -1 : 0)) * 100 + parsedYear;
|
|
461
577
|
};
|
|
578
|
+
|
|
579
|
+
// ../../../../node_modules/@formkit/tempo/dist/range.mjs
|
|
462
580
|
var range = function(token, locale = "en", genitive = false) {
|
|
463
581
|
const r = (n, c) => Array(n).fill("").map((_, i) => `${c(i)}`);
|
|
464
582
|
if (token === "M")
|
|
@@ -473,9 +591,9 @@ var range = function(token, locale = "en", genitive = false) {
|
|
|
473
591
|
if (token.startsWith("d"))
|
|
474
592
|
return r(7, (i) => `0${i + 2}`).map((d) => format(`2022-10-${d}`, token, locale));
|
|
475
593
|
if (token === "a")
|
|
476
|
-
return [
|
|
594
|
+
return [ap2("am", locale).toLowerCase(), ap2("pm", locale).toLowerCase()];
|
|
477
595
|
if (token === "A")
|
|
478
|
-
return [
|
|
596
|
+
return [ap2("am", locale).toUpperCase(), ap2("pm", locale).toUpperCase()];
|
|
479
597
|
if (token.startsWith("Y")) {
|
|
480
598
|
const year = (new Date()).getFullYear();
|
|
481
599
|
return r(120, (i) => i + 1).reduce((ranges, i) => {
|
|
@@ -495,14 +613,16 @@ var range = function(token, locale = "en", genitive = false) {
|
|
|
495
613
|
return r(60, (i) => `${token.length > 1 && i < 10 ? "0" : ""}${i}`);
|
|
496
614
|
return [];
|
|
497
615
|
};
|
|
498
|
-
|
|
616
|
+
|
|
617
|
+
// ../../../../node_modules/@formkit/tempo/dist/parse.mjs
|
|
618
|
+
var parse = function(dateStrOrOptions, format3 = "ISO8601", locale = "device") {
|
|
499
619
|
let partFilter = () => true;
|
|
500
620
|
let dateStr;
|
|
501
621
|
let dateOverflow = "backward";
|
|
502
622
|
if (typeof dateStrOrOptions === "object") {
|
|
503
623
|
({
|
|
504
624
|
date: dateStr,
|
|
505
|
-
format:
|
|
625
|
+
format: format3 = "ISO8601",
|
|
506
626
|
locale = "device",
|
|
507
627
|
dateOverflow = "backward",
|
|
508
628
|
partFilter = () => true
|
|
@@ -513,12 +633,12 @@ var parse = function(dateStrOrOptions, format2 = "ISO8601", locale = "device") {
|
|
|
513
633
|
if (!dateStr)
|
|
514
634
|
throw new Error("parse() requires a date string.");
|
|
515
635
|
const invalid = () => {
|
|
516
|
-
throw new Error(`Date (${dateStr}) does not match format (${formatStr(
|
|
636
|
+
throw new Error(`Date (${dateStr}) does not match format (${formatStr(format3, locale)})`);
|
|
517
637
|
};
|
|
518
|
-
if (
|
|
638
|
+
if (format3 === "ISO8601")
|
|
519
639
|
return date(dateStr);
|
|
520
|
-
const genitive = styles.includes(
|
|
521
|
-
const formatParts = validate(parts(
|
|
640
|
+
const genitive = styles.includes(format3) || typeof format3 === "object";
|
|
641
|
+
const formatParts = validate(parts(format3, locale).filter(partFilter));
|
|
522
642
|
if (!formatParts.length)
|
|
523
643
|
throw new Error("parse() requires a pattern.");
|
|
524
644
|
let parsedParts;
|
|
@@ -537,7 +657,7 @@ var parse = function(dateStrOrOptions, format2 = "ISO8601", locale = "device") {
|
|
|
537
657
|
["ss", 0]
|
|
538
658
|
]);
|
|
539
659
|
let a = null;
|
|
540
|
-
let
|
|
660
|
+
let offset4 = "";
|
|
541
661
|
parsedParts.forEach((part) => {
|
|
542
662
|
if (part.partName === "literal")
|
|
543
663
|
return;
|
|
@@ -559,9 +679,9 @@ var parse = function(dateStrOrOptions, format2 = "ISO8601", locale = "device") {
|
|
|
559
679
|
} else if (t === "M") {
|
|
560
680
|
parsed.set("MM", v);
|
|
561
681
|
} else if (t === "a" || t === "A") {
|
|
562
|
-
a = part.value.toLowerCase() ===
|
|
563
|
-
} else if (t === "Z") {
|
|
564
|
-
|
|
682
|
+
a = part.value.toLowerCase() === ap2("am", locale).toLowerCase();
|
|
683
|
+
} else if (t === "Z" || t === "ZZ") {
|
|
684
|
+
offset4 = validOffset(part.value, t);
|
|
565
685
|
} else {
|
|
566
686
|
const values = range(t, locale, genitive);
|
|
567
687
|
const index = values.indexOf(part.value);
|
|
@@ -589,7 +709,7 @@ var parse = function(dateStrOrOptions, format2 = "ISO8601", locale = "device") {
|
|
|
589
709
|
if (maxDaysInMonth < D && dateOverflow === "throw")
|
|
590
710
|
throw new Error(`Invalid date ${four(Y)}-${two(M + 1)}-${two(D)}`);
|
|
591
711
|
D = dateOverflow === "backward" ? Math.min(D, maxDaysInMonth) : D;
|
|
592
|
-
const isoString = `${four(Y)}-${two(M + 1)}-${two(D)}T${two(h)}:${two(m)}:${two(s)}${
|
|
712
|
+
const isoString = `${four(Y)}-${two(M + 1)}-${two(D)}T${two(h)}:${two(m)}:${two(s)}${offset4}`;
|
|
593
713
|
const d = new Date(isoString);
|
|
594
714
|
if (isFinite(+d))
|
|
595
715
|
return d;
|
|
@@ -597,9 +717,9 @@ var parse = function(dateStrOrOptions, format2 = "ISO8601", locale = "device") {
|
|
|
597
717
|
};
|
|
598
718
|
var parseParts = function(dateStr, formatParts) {
|
|
599
719
|
let i = 0;
|
|
600
|
-
const advance = (
|
|
601
|
-
|
|
602
|
-
|
|
720
|
+
const advance = (parts22) => [
|
|
721
|
+
parts22[i++],
|
|
722
|
+
parts22[i]
|
|
603
723
|
];
|
|
604
724
|
let pos = 0;
|
|
605
725
|
const parsed = [];
|
|
@@ -610,6 +730,8 @@ var parseParts = function(dateStr, formatParts) {
|
|
|
610
730
|
let len = 1;
|
|
611
731
|
if (current.partName === "literal") {
|
|
612
732
|
len = current.partValue.length;
|
|
733
|
+
} else if (current.partName === "timeZoneName") {
|
|
734
|
+
len = fixedLengthByOffset(dateStr.substring(pos));
|
|
613
735
|
} else if (current.token in fixedLength) {
|
|
614
736
|
len = fixedLength[current.token];
|
|
615
737
|
} else if (next) {
|
|
@@ -637,62 +759,6 @@ var parseParts = function(dateStr, formatParts) {
|
|
|
637
759
|
} while (n);
|
|
638
760
|
return parsed;
|
|
639
761
|
};
|
|
640
|
-
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]{4})?$/;
|
|
641
|
-
var specDate = "1999-03-04T02:05:01.000Z";
|
|
642
|
-
var memoParts = new Map;
|
|
643
|
-
var clockAgnostic = [
|
|
644
|
-
["YYYY", { year: "numeric" }],
|
|
645
|
-
["YY", { year: "2-digit" }],
|
|
646
|
-
["MMMM", { month: "long" }],
|
|
647
|
-
["MMM", { month: "short" }],
|
|
648
|
-
["MM", { month: "2-digit" }],
|
|
649
|
-
["M", { month: "numeric" }],
|
|
650
|
-
["DD", { day: "2-digit" }],
|
|
651
|
-
["D", { day: "numeric" }],
|
|
652
|
-
["dddd", { weekday: "long" }],
|
|
653
|
-
["ddd", { weekday: "short" }],
|
|
654
|
-
["d", { weekday: "narrow" }],
|
|
655
|
-
["mm", { minute: "2-digit" }],
|
|
656
|
-
["m", { minute: "numeric" }],
|
|
657
|
-
["ss", { second: "2-digit" }],
|
|
658
|
-
["s", { second: "numeric" }],
|
|
659
|
-
["Z", { timeZoneName: "short" }]
|
|
660
|
-
];
|
|
661
|
-
var clock24 = [
|
|
662
|
-
["HH", { hour: "2-digit" }],
|
|
663
|
-
["H", { hour: "numeric" }]
|
|
664
|
-
];
|
|
665
|
-
var clock12 = [
|
|
666
|
-
["hh", { hour: "2-digit" }],
|
|
667
|
-
["h", { hour: "numeric" }],
|
|
668
|
-
["a", { dayPeriod: "narrow" }],
|
|
669
|
-
["A", { dayPeriod: "narrow" }]
|
|
670
|
-
];
|
|
671
|
-
var fixedLength = {
|
|
672
|
-
DD: 2,
|
|
673
|
-
HH: 2,
|
|
674
|
-
MM: 2,
|
|
675
|
-
YY: 2,
|
|
676
|
-
YYYY: 4,
|
|
677
|
-
hh: 2,
|
|
678
|
-
mm: 2,
|
|
679
|
-
ss: 2,
|
|
680
|
-
Z: 5
|
|
681
|
-
};
|
|
682
|
-
var genitiveTokens = ["MMMM", "MMM", "dddd", "ddd"];
|
|
683
|
-
var tokens = new Map([...clockAgnostic, ...clock24, ...clock12].map((format2) => {
|
|
684
|
-
return [format2[0], format2];
|
|
685
|
-
}));
|
|
686
|
-
var dayPeriodMap = new Map;
|
|
687
|
-
var styles = [
|
|
688
|
-
"full",
|
|
689
|
-
"long",
|
|
690
|
-
"medium",
|
|
691
|
-
"short"
|
|
692
|
-
];
|
|
693
|
-
var two = (n) => String(n).padStart(2, "0");
|
|
694
|
-
var four = (n) => String(n).padStart(2, "0");
|
|
695
|
-
|
|
696
762
|
// src/index.ts
|
|
697
763
|
var now = useNow;
|
|
698
764
|
var dateFormat = useDateFormat;
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@stacksjs/datetime",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.
|
|
4
|
+
"version": "0.61.0",
|
|
5
5
|
"description": "The Stacks datetime helpers.",
|
|
6
6
|
"author": "Chris Breuer",
|
|
7
7
|
"license": "MIT",
|
|
@@ -49,7 +49,7 @@
|
|
|
49
49
|
"prepublishOnly": "bun run build"
|
|
50
50
|
},
|
|
51
51
|
"dependencies": {
|
|
52
|
-
"@formkit/tempo": "^0.
|
|
52
|
+
"@formkit/tempo": "^0.1.1",
|
|
53
53
|
"@stacksjs/utils": "latest"
|
|
54
54
|
},
|
|
55
55
|
"devDependencies": {
|