@arkv/temporal 0.2.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 +492 -0
- package/dist/cjs/constants.js +7 -0
- package/dist/cjs/dayjs.js +319 -0
- package/dist/cjs/diff.js +60 -0
- package/dist/cjs/format.js +84 -0
- package/dist/cjs/index.js +49 -0
- package/dist/cjs/locale.js +73 -0
- package/dist/cjs/package.json +1 -0
- package/dist/cjs/parse.js +46 -0
- package/dist/cjs/start-end.js +111 -0
- package/dist/cjs/types.js +2 -0
- package/dist/cjs/units.js +56 -0
- package/dist/esm/constants.js +4 -0
- package/dist/esm/dayjs.js +315 -0
- package/dist/esm/diff.js +57 -0
- package/dist/esm/format.js +81 -0
- package/dist/esm/index.js +45 -0
- package/dist/esm/locale.js +66 -0
- package/dist/esm/package.json +1 -0
- package/dist/esm/parse.js +43 -0
- package/dist/esm/start-end.js +107 -0
- package/dist/esm/types.js +1 -0
- package/dist/esm/units.js +52 -0
- package/dist/types/constants.d.ts +3 -0
- package/dist/types/dayjs.d.ts +61 -0
- package/dist/types/diff.d.ts +2 -0
- package/dist/types/format.d.ts +2 -0
- package/dist/types/index.d.ts +16 -0
- package/dist/types/locale.d.ts +15 -0
- package/dist/types/parse.d.ts +2 -0
- package/dist/types/start-end.d.ts +4 -0
- package/dist/types/types.d.ts +12 -0
- package/dist/types/units.d.ts +4 -0
- package/package.json +60 -0
|
@@ -0,0 +1,319 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.TDayjs = exports.DEFAULT_TZ = void 0;
|
|
4
|
+
const constants_js_1 = require("./constants.js");
|
|
5
|
+
const diff_js_1 = require("./diff.js");
|
|
6
|
+
const format_js_1 = require("./format.js");
|
|
7
|
+
const locale_js_1 = require("./locale.js");
|
|
8
|
+
const parse_js_1 = require("./parse.js");
|
|
9
|
+
const start_end_js_1 = require("./start-end.js");
|
|
10
|
+
const units_js_1 = require("./units.js");
|
|
11
|
+
function addToZdt(zdt, n, key) {
|
|
12
|
+
switch (key) {
|
|
13
|
+
case 'years':
|
|
14
|
+
return zdt.add({ years: n });
|
|
15
|
+
case 'months':
|
|
16
|
+
return zdt.add({ months: n });
|
|
17
|
+
case 'weeks':
|
|
18
|
+
return zdt.add({ weeks: n });
|
|
19
|
+
case 'days':
|
|
20
|
+
return zdt.add({ days: n });
|
|
21
|
+
case 'hours':
|
|
22
|
+
return zdt.add({ hours: n });
|
|
23
|
+
case 'minutes':
|
|
24
|
+
return zdt.add({ minutes: n });
|
|
25
|
+
case 'seconds':
|
|
26
|
+
return zdt.add({ seconds: n });
|
|
27
|
+
case 'milliseconds':
|
|
28
|
+
return zdt.add({ milliseconds: n });
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
exports.DEFAULT_TZ = Temporal.Now.timeZoneId();
|
|
32
|
+
class TDayjs {
|
|
33
|
+
$zdt;
|
|
34
|
+
$L;
|
|
35
|
+
$tz;
|
|
36
|
+
constructor(cfg) {
|
|
37
|
+
this.$L = cfg.locale ?? (0, locale_js_1.getGlobalLocale)();
|
|
38
|
+
this.$tz = cfg.tz ?? exports.DEFAULT_TZ;
|
|
39
|
+
this.$zdt =
|
|
40
|
+
cfg.zdt !== undefined
|
|
41
|
+
? cfg.zdt
|
|
42
|
+
: (0, parse_js_1.parseInput)(cfg.date, this.$tz);
|
|
43
|
+
}
|
|
44
|
+
_clone(zdt) {
|
|
45
|
+
return new TDayjs({
|
|
46
|
+
zdt,
|
|
47
|
+
locale: this.$L,
|
|
48
|
+
tz: this.$tz,
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
isValid() {
|
|
52
|
+
return this.$zdt !== null;
|
|
53
|
+
}
|
|
54
|
+
clone() {
|
|
55
|
+
return this._clone(this.$zdt);
|
|
56
|
+
}
|
|
57
|
+
year(value) {
|
|
58
|
+
if (value === undefined) {
|
|
59
|
+
return this.$zdt?.year ?? NaN;
|
|
60
|
+
}
|
|
61
|
+
if (!this.$zdt)
|
|
62
|
+
return this._clone(null);
|
|
63
|
+
return this.add(value - this.$zdt.year, 'year');
|
|
64
|
+
}
|
|
65
|
+
month(value) {
|
|
66
|
+
if (value === undefined) {
|
|
67
|
+
return (this.$zdt?.month ?? 1) - 1;
|
|
68
|
+
}
|
|
69
|
+
if (!this.$zdt)
|
|
70
|
+
return this._clone(null);
|
|
71
|
+
// value is 0-indexed; $zdt.month is 1-indexed
|
|
72
|
+
return this.add(value - (this.$zdt.month - 1), 'month');
|
|
73
|
+
}
|
|
74
|
+
date(value) {
|
|
75
|
+
if (value === undefined) {
|
|
76
|
+
return this.$zdt?.day ?? NaN;
|
|
77
|
+
}
|
|
78
|
+
if (!this.$zdt)
|
|
79
|
+
return this._clone(null);
|
|
80
|
+
return this.add(value - this.$zdt.day, 'day');
|
|
81
|
+
}
|
|
82
|
+
day(value) {
|
|
83
|
+
if (value === undefined) {
|
|
84
|
+
return (this.$zdt?.dayOfWeek ?? 0) % 7;
|
|
85
|
+
}
|
|
86
|
+
if (!this.$zdt)
|
|
87
|
+
return this._clone(null);
|
|
88
|
+
const current = this.$zdt.dayOfWeek % 7;
|
|
89
|
+
return this._clone(this.$zdt.add({ days: value - current }));
|
|
90
|
+
}
|
|
91
|
+
hour(value) {
|
|
92
|
+
if (value === undefined) {
|
|
93
|
+
return this.$zdt?.hour ?? NaN;
|
|
94
|
+
}
|
|
95
|
+
if (!this.$zdt)
|
|
96
|
+
return this._clone(null);
|
|
97
|
+
return this.add(value - this.$zdt.hour, 'hour');
|
|
98
|
+
}
|
|
99
|
+
minute(value) {
|
|
100
|
+
if (value === undefined) {
|
|
101
|
+
return this.$zdt?.minute ?? NaN;
|
|
102
|
+
}
|
|
103
|
+
if (!this.$zdt)
|
|
104
|
+
return this._clone(null);
|
|
105
|
+
return this.add(value - this.$zdt.minute, 'minute');
|
|
106
|
+
}
|
|
107
|
+
second(value) {
|
|
108
|
+
if (value === undefined) {
|
|
109
|
+
return this.$zdt?.second ?? NaN;
|
|
110
|
+
}
|
|
111
|
+
if (!this.$zdt)
|
|
112
|
+
return this._clone(null);
|
|
113
|
+
return this.add(value - this.$zdt.second, 'second');
|
|
114
|
+
}
|
|
115
|
+
millisecond(value) {
|
|
116
|
+
if (value === undefined) {
|
|
117
|
+
return this.$zdt?.millisecond ?? NaN;
|
|
118
|
+
}
|
|
119
|
+
if (!this.$zdt)
|
|
120
|
+
return this._clone(null);
|
|
121
|
+
return this.add(value - this.$zdt.millisecond, 'millisecond');
|
|
122
|
+
}
|
|
123
|
+
get(unit) {
|
|
124
|
+
const u = (0, units_js_1.normalizeUnit)(unit);
|
|
125
|
+
return this._getByUnit(u);
|
|
126
|
+
}
|
|
127
|
+
_getByUnit(u) {
|
|
128
|
+
switch (u) {
|
|
129
|
+
case 'year':
|
|
130
|
+
return this.year();
|
|
131
|
+
case 'month':
|
|
132
|
+
return this.month();
|
|
133
|
+
case 'date':
|
|
134
|
+
return this.date();
|
|
135
|
+
case 'day':
|
|
136
|
+
return this.day();
|
|
137
|
+
case 'hour':
|
|
138
|
+
return this.hour();
|
|
139
|
+
case 'minute':
|
|
140
|
+
return this.minute();
|
|
141
|
+
case 'second':
|
|
142
|
+
return this.second();
|
|
143
|
+
case 'millisecond':
|
|
144
|
+
return this.millisecond();
|
|
145
|
+
case 'week':
|
|
146
|
+
return this.day();
|
|
147
|
+
case 'quarter':
|
|
148
|
+
return Math.ceil((this.month() + 1) / 3);
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
set(unit, value) {
|
|
152
|
+
const u = (0, units_js_1.normalizeUnit)(unit);
|
|
153
|
+
return this._setByUnit(u, value);
|
|
154
|
+
}
|
|
155
|
+
_setByUnit(u, value) {
|
|
156
|
+
switch (u) {
|
|
157
|
+
case 'year':
|
|
158
|
+
return this.year(value);
|
|
159
|
+
case 'month':
|
|
160
|
+
return this.month(value);
|
|
161
|
+
case 'date':
|
|
162
|
+
return this.date(value);
|
|
163
|
+
case 'day':
|
|
164
|
+
return this.day(value);
|
|
165
|
+
case 'hour':
|
|
166
|
+
return this.hour(value);
|
|
167
|
+
case 'minute':
|
|
168
|
+
return this.minute(value);
|
|
169
|
+
case 'second':
|
|
170
|
+
return this.second(value);
|
|
171
|
+
case 'millisecond':
|
|
172
|
+
return this.millisecond(value);
|
|
173
|
+
default:
|
|
174
|
+
return this.clone();
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
// --- Manipulation ---
|
|
178
|
+
add(value, unit) {
|
|
179
|
+
if (!this.$zdt)
|
|
180
|
+
return this._clone(null);
|
|
181
|
+
const n = Number(value);
|
|
182
|
+
const u = (0, units_js_1.normalizeUnit)(unit ?? 'millisecond');
|
|
183
|
+
if (u === 'quarter') {
|
|
184
|
+
return this.add(n * 3, 'month');
|
|
185
|
+
}
|
|
186
|
+
const key = units_js_1.DURATION_KEY[u];
|
|
187
|
+
if (!key)
|
|
188
|
+
return this._clone(this.$zdt);
|
|
189
|
+
return this._clone(addToZdt(this.$zdt, n, key));
|
|
190
|
+
}
|
|
191
|
+
subtract(value, unit) {
|
|
192
|
+
return this.add(-Number(value), unit);
|
|
193
|
+
}
|
|
194
|
+
startOf(unit) {
|
|
195
|
+
if (!this.$zdt)
|
|
196
|
+
return this._clone(null);
|
|
197
|
+
const u = (0, units_js_1.normalizeUnit)(unit);
|
|
198
|
+
return this._clone((0, start_end_js_1.startOfHelper)(this.$zdt, u, (0, locale_js_1.getLocaleObj)(this.$L)));
|
|
199
|
+
}
|
|
200
|
+
endOf(unit) {
|
|
201
|
+
if (!this.$zdt)
|
|
202
|
+
return this._clone(null);
|
|
203
|
+
const u = (0, units_js_1.normalizeUnit)(unit);
|
|
204
|
+
return this._clone((0, start_end_js_1.endOfHelper)(this.$zdt, u, (0, locale_js_1.getLocaleObj)(this.$L)));
|
|
205
|
+
}
|
|
206
|
+
// --- Comparison ---
|
|
207
|
+
_toZdt(d) {
|
|
208
|
+
if (d instanceof TDayjs)
|
|
209
|
+
return d;
|
|
210
|
+
return new TDayjs({ date: d, tz: this.$tz });
|
|
211
|
+
}
|
|
212
|
+
isBefore(date, unit) {
|
|
213
|
+
if (!this.$zdt)
|
|
214
|
+
return false;
|
|
215
|
+
const other = this._toZdt(date);
|
|
216
|
+
if (!other.isValid())
|
|
217
|
+
return false;
|
|
218
|
+
if (!unit) {
|
|
219
|
+
return (Temporal.ZonedDateTime.compare(this.$zdt, other.$zdt) < 0);
|
|
220
|
+
}
|
|
221
|
+
return this.endOf(unit).valueOf() < other.valueOf();
|
|
222
|
+
}
|
|
223
|
+
isAfter(date, unit) {
|
|
224
|
+
if (!this.$zdt)
|
|
225
|
+
return false;
|
|
226
|
+
const other = this._toZdt(date);
|
|
227
|
+
if (!other.isValid())
|
|
228
|
+
return false;
|
|
229
|
+
if (!unit) {
|
|
230
|
+
return (Temporal.ZonedDateTime.compare(this.$zdt, other.$zdt) > 0);
|
|
231
|
+
}
|
|
232
|
+
return other.valueOf() < this.startOf(unit).valueOf();
|
|
233
|
+
}
|
|
234
|
+
isSame(date, unit) {
|
|
235
|
+
if (!this.$zdt)
|
|
236
|
+
return false;
|
|
237
|
+
const other = this._toZdt(date);
|
|
238
|
+
if (!other.isValid())
|
|
239
|
+
return false;
|
|
240
|
+
if (!unit) {
|
|
241
|
+
return (Temporal.ZonedDateTime.compare(this.$zdt, other.$zdt) === 0);
|
|
242
|
+
}
|
|
243
|
+
const start = this.startOf(unit).valueOf();
|
|
244
|
+
const end = this.endOf(unit).valueOf();
|
|
245
|
+
const v = other.valueOf();
|
|
246
|
+
return start <= v && v <= end;
|
|
247
|
+
}
|
|
248
|
+
// --- Display ---
|
|
249
|
+
format(template) {
|
|
250
|
+
if (!this.$zdt)
|
|
251
|
+
return constants_js_1.INVALID_DATE;
|
|
252
|
+
const loc = (0, locale_js_1.getLocaleObj)(this.$L);
|
|
253
|
+
return (0, format_js_1.formatDate)(this.$zdt, template ?? constants_js_1.FORMAT_DEFAULT, loc);
|
|
254
|
+
}
|
|
255
|
+
diff(date, unit, float = false) {
|
|
256
|
+
if (!this.$zdt)
|
|
257
|
+
return NaN;
|
|
258
|
+
const other = this._toZdt(date ?? undefined);
|
|
259
|
+
if (!other.$zdt)
|
|
260
|
+
return NaN;
|
|
261
|
+
const u = (0, units_js_1.normalizeUnit)(unit ?? 'millisecond');
|
|
262
|
+
// dayjs: this.diff(other) = this - other
|
|
263
|
+
// diffHelper(from, to) = from.until(to) = to - from
|
|
264
|
+
// so pass (other, this) to get this - other
|
|
265
|
+
return (0, diff_js_1.diffHelper)(other.$zdt, this.$zdt, u, float);
|
|
266
|
+
}
|
|
267
|
+
valueOf() {
|
|
268
|
+
return this.$zdt?.epochMilliseconds ?? NaN;
|
|
269
|
+
}
|
|
270
|
+
unix() {
|
|
271
|
+
return Math.floor(this.valueOf() / 1_000);
|
|
272
|
+
}
|
|
273
|
+
daysInMonth() {
|
|
274
|
+
return this.$zdt?.daysInMonth ?? NaN;
|
|
275
|
+
}
|
|
276
|
+
utcOffset() {
|
|
277
|
+
if (!this.$zdt)
|
|
278
|
+
return 0;
|
|
279
|
+
return this.$zdt.offsetNanoseconds / 60_000_000_000;
|
|
280
|
+
}
|
|
281
|
+
// --- Conversion ---
|
|
282
|
+
toDate() {
|
|
283
|
+
return new Date(this.valueOf());
|
|
284
|
+
}
|
|
285
|
+
toISOString() {
|
|
286
|
+
if (!this.$zdt)
|
|
287
|
+
return constants_js_1.INVALID_DATE;
|
|
288
|
+
return this.$zdt.toInstant().toString();
|
|
289
|
+
}
|
|
290
|
+
toJSON() {
|
|
291
|
+
return this.isValid() ? this.toISOString() : null;
|
|
292
|
+
}
|
|
293
|
+
toString() {
|
|
294
|
+
return this.isValid()
|
|
295
|
+
? this.toDate().toUTCString()
|
|
296
|
+
: constants_js_1.INVALID_DATE;
|
|
297
|
+
}
|
|
298
|
+
locale(preset, object) {
|
|
299
|
+
if (preset === undefined)
|
|
300
|
+
return this.$L;
|
|
301
|
+
let name;
|
|
302
|
+
if (typeof preset === 'string') {
|
|
303
|
+
name = preset.toLowerCase();
|
|
304
|
+
if (object) {
|
|
305
|
+
(0, locale_js_1.registerLocale)({ ...object, name });
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
else {
|
|
309
|
+
(0, locale_js_1.registerLocale)(preset);
|
|
310
|
+
name = preset.name;
|
|
311
|
+
}
|
|
312
|
+
return new TDayjs({
|
|
313
|
+
zdt: this.$zdt,
|
|
314
|
+
locale: name,
|
|
315
|
+
tz: this.$tz,
|
|
316
|
+
});
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
exports.TDayjs = TDayjs;
|
package/dist/cjs/diff.js
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.diffHelper = diffHelper;
|
|
4
|
+
function monthDiff(from, to, float) {
|
|
5
|
+
const dur = from.until(to, {
|
|
6
|
+
largestUnit: 'months',
|
|
7
|
+
});
|
|
8
|
+
const whole = dur.months;
|
|
9
|
+
if (!float)
|
|
10
|
+
return whole;
|
|
11
|
+
const anchor = from.add({ months: whole });
|
|
12
|
+
const remaining = to.epochMilliseconds - anchor.epochMilliseconds;
|
|
13
|
+
const nextMs = anchor.add({ months: 1 }).epochMilliseconds -
|
|
14
|
+
anchor.epochMilliseconds;
|
|
15
|
+
return whole + remaining / nextMs;
|
|
16
|
+
}
|
|
17
|
+
function dayDiff(from, to, float) {
|
|
18
|
+
const dur = from.until(to, { largestUnit: 'days' });
|
|
19
|
+
const total = dur.days +
|
|
20
|
+
dur.hours / 24 +
|
|
21
|
+
dur.minutes / 1440 +
|
|
22
|
+
dur.seconds / 86_400 +
|
|
23
|
+
dur.milliseconds / 86_400_000;
|
|
24
|
+
return float ? total : Math.trunc(total);
|
|
25
|
+
}
|
|
26
|
+
function msDiff(from, to, unit, float) {
|
|
27
|
+
const ms = to.epochMilliseconds - from.epochMilliseconds;
|
|
28
|
+
const divisors = {
|
|
29
|
+
hour: 3_600_000,
|
|
30
|
+
minute: 60_000,
|
|
31
|
+
second: 1_000,
|
|
32
|
+
millisecond: 1,
|
|
33
|
+
};
|
|
34
|
+
const result = ms / (divisors[unit] ?? 1);
|
|
35
|
+
return float ? result : Math.trunc(result);
|
|
36
|
+
}
|
|
37
|
+
function diffHelper(from, to, unit, float) {
|
|
38
|
+
switch (unit) {
|
|
39
|
+
case 'year': {
|
|
40
|
+
const months = monthDiff(from, to, float);
|
|
41
|
+
return float ? months / 12 : Math.trunc(months / 12);
|
|
42
|
+
}
|
|
43
|
+
case 'quarter': {
|
|
44
|
+
const months = monthDiff(from, to, float);
|
|
45
|
+
return float ? months / 3 : Math.trunc(months / 3);
|
|
46
|
+
}
|
|
47
|
+
case 'month':
|
|
48
|
+
return monthDiff(from, to, float);
|
|
49
|
+
case 'week': {
|
|
50
|
+
const days = dayDiff(from, to, true);
|
|
51
|
+
const result = days / 7;
|
|
52
|
+
return float ? result : Math.trunc(result);
|
|
53
|
+
}
|
|
54
|
+
case 'day':
|
|
55
|
+
case 'date':
|
|
56
|
+
return dayDiff(from, to, float);
|
|
57
|
+
default:
|
|
58
|
+
return msDiff(from, to, unit, float);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.formatDate = formatDate;
|
|
4
|
+
const constants_js_1 = require("./constants.js");
|
|
5
|
+
function pad(n, len = 2) {
|
|
6
|
+
return String(n).padStart(len, '0');
|
|
7
|
+
}
|
|
8
|
+
function offsetStr(ns, sep) {
|
|
9
|
+
const totalMin = Math.round(ns / 60_000_000_000);
|
|
10
|
+
const sign = totalMin >= 0 ? '+' : '-';
|
|
11
|
+
const absMin = Math.abs(totalMin);
|
|
12
|
+
const h = pad(Math.floor(absMin / 60));
|
|
13
|
+
const m = pad(absMin % 60);
|
|
14
|
+
return sep ? `${sign}${h}:${m}` : `${sign}${h}${m}`;
|
|
15
|
+
}
|
|
16
|
+
// Temporal: 1=Mon...7=Sun → dayjs: 0=Sun...6=Sat
|
|
17
|
+
function toDayjsDay(dow) {
|
|
18
|
+
return dow % 7;
|
|
19
|
+
}
|
|
20
|
+
function matchToken(zdt, locale, token) {
|
|
21
|
+
const { year, month, day, hour, minute, second, millisecond, dayOfWeek, offsetNanoseconds, } = zdt;
|
|
22
|
+
const djDay = toDayjsDay(dayOfWeek);
|
|
23
|
+
switch (token) {
|
|
24
|
+
case 'YYYY':
|
|
25
|
+
return pad(year, 4);
|
|
26
|
+
case 'YY':
|
|
27
|
+
return String(year).slice(-2);
|
|
28
|
+
case 'M':
|
|
29
|
+
return String(month);
|
|
30
|
+
case 'MM':
|
|
31
|
+
return pad(month);
|
|
32
|
+
case 'MMM':
|
|
33
|
+
return locale.monthsShort[month - 1];
|
|
34
|
+
case 'MMMM':
|
|
35
|
+
return locale.months[month - 1];
|
|
36
|
+
case 'D':
|
|
37
|
+
return String(day);
|
|
38
|
+
case 'DD':
|
|
39
|
+
return pad(day);
|
|
40
|
+
case 'd':
|
|
41
|
+
return String(djDay);
|
|
42
|
+
case 'dd':
|
|
43
|
+
return locale.weekdaysMin[djDay];
|
|
44
|
+
case 'ddd':
|
|
45
|
+
return locale.weekdaysShort[djDay];
|
|
46
|
+
case 'dddd':
|
|
47
|
+
return locale.weekdays[djDay];
|
|
48
|
+
case 'H':
|
|
49
|
+
return String(hour);
|
|
50
|
+
case 'HH':
|
|
51
|
+
return pad(hour);
|
|
52
|
+
case 'h':
|
|
53
|
+
return String(hour % 12 || 12);
|
|
54
|
+
case 'hh':
|
|
55
|
+
return pad(hour % 12 || 12);
|
|
56
|
+
case 'a':
|
|
57
|
+
return hour < 12 ? 'am' : 'pm';
|
|
58
|
+
case 'A':
|
|
59
|
+
return hour < 12 ? 'AM' : 'PM';
|
|
60
|
+
case 'm':
|
|
61
|
+
return String(minute);
|
|
62
|
+
case 'mm':
|
|
63
|
+
return pad(minute);
|
|
64
|
+
case 's':
|
|
65
|
+
return String(second);
|
|
66
|
+
case 'ss':
|
|
67
|
+
return pad(second);
|
|
68
|
+
case 'SSS':
|
|
69
|
+
return pad(millisecond, 3);
|
|
70
|
+
case 'Z':
|
|
71
|
+
return offsetStr(offsetNanoseconds, ':');
|
|
72
|
+
case 'ZZ':
|
|
73
|
+
return offsetStr(offsetNanoseconds, '');
|
|
74
|
+
default:
|
|
75
|
+
return null;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
function formatDate(zdt, template, locale) {
|
|
79
|
+
return template.replace(constants_js_1.REGEX_FORMAT, (match, escaped) => {
|
|
80
|
+
if (escaped != null)
|
|
81
|
+
return escaped;
|
|
82
|
+
return matchToken(zdt, locale, match) ?? match;
|
|
83
|
+
});
|
|
84
|
+
}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.TDayjs = void 0;
|
|
4
|
+
require("temporal-polyfill/global");
|
|
5
|
+
const dayjs_js_1 = require("./dayjs.js");
|
|
6
|
+
const locale_js_1 = require("./locale.js");
|
|
7
|
+
var dayjs_js_2 = require("./dayjs.js");
|
|
8
|
+
Object.defineProperty(exports, "TDayjs", { enumerable: true, get: function () { return dayjs_js_2.TDayjs; } });
|
|
9
|
+
const installed = new Set();
|
|
10
|
+
function tdayjs(date) {
|
|
11
|
+
if (date instanceof dayjs_js_1.TDayjs)
|
|
12
|
+
return date.clone();
|
|
13
|
+
return new dayjs_js_1.TDayjs({ date });
|
|
14
|
+
}
|
|
15
|
+
tdayjs.extend = (plugin, option) => {
|
|
16
|
+
if (!installed.has(plugin)) {
|
|
17
|
+
plugin(option, dayjs_js_1.TDayjs, tdayjs);
|
|
18
|
+
installed.add(plugin);
|
|
19
|
+
}
|
|
20
|
+
return tdayjs;
|
|
21
|
+
};
|
|
22
|
+
tdayjs.isDayjs = (d) => {
|
|
23
|
+
return d instanceof dayjs_js_1.TDayjs;
|
|
24
|
+
};
|
|
25
|
+
tdayjs.unix = (t) => {
|
|
26
|
+
return new dayjs_js_1.TDayjs({
|
|
27
|
+
date: t * 1_000,
|
|
28
|
+
tz: dayjs_js_1.DEFAULT_TZ,
|
|
29
|
+
});
|
|
30
|
+
};
|
|
31
|
+
tdayjs.locale = (preset, object) => {
|
|
32
|
+
if (preset === undefined)
|
|
33
|
+
return (0, locale_js_1.getGlobalLocale)();
|
|
34
|
+
let name;
|
|
35
|
+
if (typeof preset === 'string') {
|
|
36
|
+
name = preset.toLowerCase();
|
|
37
|
+
if (object) {
|
|
38
|
+
(0, locale_js_1.registerLocale)({ ...object, name });
|
|
39
|
+
}
|
|
40
|
+
(0, locale_js_1.setGlobalLocale)(name);
|
|
41
|
+
}
|
|
42
|
+
else {
|
|
43
|
+
(0, locale_js_1.registerLocale)(preset);
|
|
44
|
+
name = preset.name;
|
|
45
|
+
(0, locale_js_1.setGlobalLocale)(name);
|
|
46
|
+
}
|
|
47
|
+
return name;
|
|
48
|
+
};
|
|
49
|
+
exports.default = tdayjs;
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Ls = void 0;
|
|
4
|
+
exports.getLocaleObj = getLocaleObj;
|
|
5
|
+
exports.getGlobalLocale = getGlobalLocale;
|
|
6
|
+
exports.setGlobalLocale = setGlobalLocale;
|
|
7
|
+
exports.registerLocale = registerLocale;
|
|
8
|
+
const en = {
|
|
9
|
+
name: 'en',
|
|
10
|
+
months: [
|
|
11
|
+
'January',
|
|
12
|
+
'February',
|
|
13
|
+
'March',
|
|
14
|
+
'April',
|
|
15
|
+
'May',
|
|
16
|
+
'June',
|
|
17
|
+
'July',
|
|
18
|
+
'August',
|
|
19
|
+
'September',
|
|
20
|
+
'October',
|
|
21
|
+
'November',
|
|
22
|
+
'December',
|
|
23
|
+
],
|
|
24
|
+
monthsShort: [
|
|
25
|
+
'Jan',
|
|
26
|
+
'Feb',
|
|
27
|
+
'Mar',
|
|
28
|
+
'Apr',
|
|
29
|
+
'May',
|
|
30
|
+
'Jun',
|
|
31
|
+
'Jul',
|
|
32
|
+
'Aug',
|
|
33
|
+
'Sep',
|
|
34
|
+
'Oct',
|
|
35
|
+
'Nov',
|
|
36
|
+
'Dec',
|
|
37
|
+
],
|
|
38
|
+
weekdays: [
|
|
39
|
+
'Sunday',
|
|
40
|
+
'Monday',
|
|
41
|
+
'Tuesday',
|
|
42
|
+
'Wednesday',
|
|
43
|
+
'Thursday',
|
|
44
|
+
'Friday',
|
|
45
|
+
'Saturday',
|
|
46
|
+
],
|
|
47
|
+
weekdaysShort: [
|
|
48
|
+
'Sun',
|
|
49
|
+
'Mon',
|
|
50
|
+
'Tue',
|
|
51
|
+
'Wed',
|
|
52
|
+
'Thu',
|
|
53
|
+
'Fri',
|
|
54
|
+
'Sat',
|
|
55
|
+
],
|
|
56
|
+
weekdaysMin: ['Su', 'Mo', 'Tu', 'We', 'Th', 'Fr', 'Sa'],
|
|
57
|
+
weekStart: 0,
|
|
58
|
+
};
|
|
59
|
+
const Ls = { en };
|
|
60
|
+
exports.Ls = Ls;
|
|
61
|
+
let globalLocale = 'en';
|
|
62
|
+
function getLocaleObj(name) {
|
|
63
|
+
return Ls[name] ?? en;
|
|
64
|
+
}
|
|
65
|
+
function getGlobalLocale() {
|
|
66
|
+
return globalLocale;
|
|
67
|
+
}
|
|
68
|
+
function setGlobalLocale(name) {
|
|
69
|
+
globalLocale = name;
|
|
70
|
+
}
|
|
71
|
+
function registerLocale(locale) {
|
|
72
|
+
Ls[locale.name] = locale;
|
|
73
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"type":"commonjs"}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.parseInput = parseInput;
|
|
4
|
+
const DATE_ONLY = /^\d{4}-\d{2}-\d{2}$/;
|
|
5
|
+
const HAS_OFFSET = /Z|[+-]\d{2}:\d{2}/;
|
|
6
|
+
const HAS_ANNOTATION = /\[/;
|
|
7
|
+
function parseString(s, tz) {
|
|
8
|
+
try {
|
|
9
|
+
if (HAS_ANNOTATION.test(s)) {
|
|
10
|
+
return Temporal.ZonedDateTime.from(s);
|
|
11
|
+
}
|
|
12
|
+
if (HAS_OFFSET.test(s)) {
|
|
13
|
+
return Temporal.Instant.from(s).toZonedDateTimeISO(tz);
|
|
14
|
+
}
|
|
15
|
+
if (DATE_ONLY.test(s)) {
|
|
16
|
+
return Temporal.PlainDate.from(s).toZonedDateTime(tz);
|
|
17
|
+
}
|
|
18
|
+
return Temporal.PlainDateTime.from(s).toZonedDateTime(tz);
|
|
19
|
+
}
|
|
20
|
+
catch {
|
|
21
|
+
return null;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
function isTDayjsLike(d) {
|
|
25
|
+
return d !== null && typeof d === 'object' && '$zdt' in d;
|
|
26
|
+
}
|
|
27
|
+
function parseInput(date, tz) {
|
|
28
|
+
if (date === null)
|
|
29
|
+
return null;
|
|
30
|
+
if (date === undefined) {
|
|
31
|
+
return Temporal.Now.zonedDateTimeISO(tz);
|
|
32
|
+
}
|
|
33
|
+
if (isTDayjsLike(date)) {
|
|
34
|
+
return date.$zdt;
|
|
35
|
+
}
|
|
36
|
+
if (typeof date === 'number') {
|
|
37
|
+
return Temporal.Instant.fromEpochMilliseconds(date).toZonedDateTimeISO(tz);
|
|
38
|
+
}
|
|
39
|
+
if (date instanceof Date) {
|
|
40
|
+
return Temporal.Instant.fromEpochMilliseconds(date.getTime()).toZonedDateTimeISO(tz);
|
|
41
|
+
}
|
|
42
|
+
if (typeof date === 'string') {
|
|
43
|
+
return parseString(date, tz);
|
|
44
|
+
}
|
|
45
|
+
return null;
|
|
46
|
+
}
|