@naturalcycles/js-lib 14.231.0 → 14.233.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/datetime/dateInterval.d.ts +1 -2
- package/dist/datetime/dateInterval.js +5 -5
- package/dist/datetime/localDate.d.ts +125 -52
- package/dist/datetime/localDate.js +259 -201
- package/dist/datetime/localTime.d.ts +50 -46
- package/dist/datetime/localTime.js +187 -195
- package/dist/datetime/timeInterval.d.ts +1 -2
- package/dist/datetime/timeInterval.js +4 -4
- package/dist/env/buildInfo.js +1 -1
- package/dist-esm/datetime/dateInterval.js +6 -6
- package/dist-esm/datetime/localDate.js +259 -195
- package/dist-esm/datetime/localTime.js +183 -190
- package/dist-esm/datetime/timeInterval.js +5 -5
- package/dist-esm/env/buildInfo.js +2 -2
- package/package.json +4 -4
- package/src/datetime/dateInterval.ts +6 -7
- package/src/datetime/localDate.ts +307 -237
- package/src/datetime/localTime.ts +209 -210
- package/src/datetime/timeInterval.ts +6 -7
- package/src/env/buildInfo.ts +2 -2
|
@@ -23,34 +23,19 @@ interface TimeComponents {
|
|
|
23
23
|
minute: number;
|
|
24
24
|
second: number;
|
|
25
25
|
}
|
|
26
|
-
/**
|
|
27
|
-
* @experimental
|
|
28
|
-
*/
|
|
29
26
|
export declare class LocalTime {
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
/**
|
|
33
|
-
* Parses input String into LocalDate.
|
|
34
|
-
* Input can already be a LocalDate - it is returned as-is in that case.
|
|
35
|
-
*/
|
|
36
|
-
static of(d: LocalTimeInput): LocalTime;
|
|
27
|
+
$date: Date;
|
|
28
|
+
constructor($date: Date);
|
|
37
29
|
/**
|
|
38
|
-
*
|
|
30
|
+
* Returns [cloned] LocalTime that is based on the same unixtimestamp, but in UTC timezone.
|
|
31
|
+
* Opposite of `.local()` method.
|
|
39
32
|
*/
|
|
40
|
-
|
|
33
|
+
utc(): LocalTime;
|
|
41
34
|
/**
|
|
42
|
-
* Returns
|
|
35
|
+
* Returns [cloned] LocalTime that is based on the same unixtimestamp, but in local timezone.
|
|
36
|
+
* Opposite of `.utc()` method.
|
|
43
37
|
*/
|
|
44
|
-
|
|
45
|
-
static parseToDate(d: LocalTimeInput): Date;
|
|
46
|
-
static parseToUnixTimestamp(d: LocalTimeInput): UnixTimestampNumber;
|
|
47
|
-
static isValid(d: LocalTimeInput | undefined | null): boolean;
|
|
48
|
-
static now(): LocalTime;
|
|
49
|
-
static fromComponents(c: {
|
|
50
|
-
year: number;
|
|
51
|
-
month: number;
|
|
52
|
-
} & Partial<LocalTimeComponents>): LocalTime;
|
|
53
|
-
static fromDateUTC(d: Date): LocalTime;
|
|
38
|
+
local(): LocalTime;
|
|
54
39
|
get(unit: LocalTimeUnit): number;
|
|
55
40
|
set(unit: LocalTimeUnit, v: number, mutate?: boolean): LocalTime;
|
|
56
41
|
year(): number;
|
|
@@ -84,11 +69,6 @@ export declare class LocalTime {
|
|
|
84
69
|
* E.g 31 for January.
|
|
85
70
|
*/
|
|
86
71
|
daysInMonth(): number;
|
|
87
|
-
static sort(items: LocalTime[], mutate?: boolean, dir?: SortDirection): LocalTime[];
|
|
88
|
-
static earliestOrUndefined(items: LocalTimeInput[]): LocalTime | undefined;
|
|
89
|
-
static earliest(items: LocalTimeInput[]): LocalTime;
|
|
90
|
-
static latestOrUndefined(items: LocalTimeInput[]): LocalTime | undefined;
|
|
91
|
-
static latest(items: LocalTimeInput[]): LocalTime;
|
|
92
72
|
isSame(d: LocalTimeInput): boolean;
|
|
93
73
|
isBefore(d: LocalTimeInput, inclusive?: boolean): boolean;
|
|
94
74
|
isSameOrBefore(d: LocalTimeInput): boolean;
|
|
@@ -166,24 +146,48 @@ export declare class LocalTime {
|
|
|
166
146
|
toMonthId(): MonthId;
|
|
167
147
|
format(fmt: Intl.DateTimeFormat | LocalTimeFormatter): string;
|
|
168
148
|
}
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
149
|
+
declare class LocalTimeFactory {
|
|
150
|
+
/**
|
|
151
|
+
* Parses input String into LocalDate.
|
|
152
|
+
* Input can already be a LocalDate - it is returned as-is in that case.
|
|
153
|
+
*/
|
|
154
|
+
of(d: LocalTimeInput): LocalTime;
|
|
155
|
+
/**
|
|
156
|
+
* Create LocalTime from unixTimestamp in milliseconds (not in seconds).
|
|
157
|
+
*/
|
|
158
|
+
ofMillis(millis: UnixTimestampMillisNumber): LocalTime;
|
|
159
|
+
/**
|
|
160
|
+
* Returns null if invalid
|
|
161
|
+
*/
|
|
162
|
+
parseOrNull(d: LocalTimeInput | undefined | null): LocalTime | null;
|
|
163
|
+
parseToDate(d: LocalTimeInput): Date;
|
|
164
|
+
parseToUnixTimestamp(d: LocalTimeInput): UnixTimestampNumber;
|
|
165
|
+
isValid(d: LocalTimeInput | undefined | null): boolean;
|
|
166
|
+
now(): LocalTime;
|
|
167
|
+
/**
|
|
168
|
+
* Creates a LocalTime from the input, unless it's falsy - then returns undefined.
|
|
169
|
+
*
|
|
170
|
+
* `localTime` function will instead return LocalTime of `now` for falsy input.
|
|
171
|
+
*/
|
|
172
|
+
orUndefined(d?: LocalTimeInput | null): LocalTime | undefined;
|
|
173
|
+
/**
|
|
174
|
+
* Creates a LocalTime from the input, unless it's falsy - then returns LocalTime.now
|
|
175
|
+
*/
|
|
176
|
+
orNow(d?: LocalTimeInput | null): LocalTime;
|
|
177
|
+
fromComponents(c: {
|
|
178
|
+
year: number;
|
|
179
|
+
month: number;
|
|
180
|
+
} & Partial<LocalTimeComponents>): LocalTime;
|
|
181
|
+
sort(items: LocalTime[], dir?: SortDirection, mutate?: boolean): LocalTime[];
|
|
182
|
+
minOrUndefined(items: LocalTimeInput[]): LocalTime | undefined;
|
|
183
|
+
min(items: LocalTimeInput[]): LocalTime;
|
|
184
|
+
maxOrUndefined(items: LocalTimeInput[]): LocalTime | undefined;
|
|
185
|
+
max(items: LocalTimeInput[]): LocalTime;
|
|
186
|
+
}
|
|
187
|
+
interface LocalTimeFn extends LocalTimeFactory {
|
|
188
|
+
(d: LocalTimeInput): LocalTime;
|
|
189
|
+
}
|
|
190
|
+
export declare const localTime: LocalTimeFn;
|
|
187
191
|
/**
|
|
188
192
|
Convenience function to return current Unix timestamp in seconds.
|
|
189
193
|
Like Date.now(), but in seconds.
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.getUTCOffsetHours = exports.getUTCOffsetMinutes = exports.nowUnix = exports.
|
|
3
|
+
exports.getUTCOffsetHours = exports.getUTCOffsetMinutes = exports.nowUnix = exports.localTime = exports.LocalTime = exports.ISODayOfWeek = void 0;
|
|
4
4
|
const assert_1 = require("../error/assert");
|
|
5
5
|
const time_util_1 = require("../time/time.util");
|
|
6
6
|
const localDate_1 = require("./localDate");
|
|
@@ -20,100 +20,23 @@ const SECONDS_IN_DAY = 86400;
|
|
|
20
20
|
// const MILLISECONDS_IN_DAY = 86400000
|
|
21
21
|
// const MILLISECONDS_IN_MINUTE = 60000
|
|
22
22
|
const VALID_DAYS_OF_WEEK = new Set([1, 2, 3, 4, 5, 6, 7]);
|
|
23
|
-
/**
|
|
24
|
-
* @experimental
|
|
25
|
-
*/
|
|
26
23
|
class LocalTime {
|
|
27
24
|
constructor($date) {
|
|
28
25
|
this.$date = $date;
|
|
29
26
|
}
|
|
30
27
|
/**
|
|
31
|
-
*
|
|
32
|
-
*
|
|
28
|
+
* Returns [cloned] LocalTime that is based on the same unixtimestamp, but in UTC timezone.
|
|
29
|
+
* Opposite of `.local()` method.
|
|
33
30
|
*/
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
(0, assert_1._assert)(t !== null, `Cannot parse "${d}" into LocalTime`, {
|
|
37
|
-
input: d,
|
|
38
|
-
});
|
|
39
|
-
return t;
|
|
31
|
+
utc() {
|
|
32
|
+
return new LocalTime(new Date(this.$date.toISOString()));
|
|
40
33
|
}
|
|
41
34
|
/**
|
|
42
|
-
*
|
|
43
|
-
|
|
44
|
-
static ofMillis(millis) {
|
|
45
|
-
return LocalTime.of(new Date(millis));
|
|
46
|
-
}
|
|
47
|
-
/**
|
|
48
|
-
* Returns null if invalid
|
|
35
|
+
* Returns [cloned] LocalTime that is based on the same unixtimestamp, but in local timezone.
|
|
36
|
+
* Opposite of `.utc()` method.
|
|
49
37
|
*/
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
return null;
|
|
53
|
-
if (d instanceof LocalTime)
|
|
54
|
-
return d;
|
|
55
|
-
let date;
|
|
56
|
-
if (d instanceof Date) {
|
|
57
|
-
date = d;
|
|
58
|
-
}
|
|
59
|
-
else if (typeof d === 'number') {
|
|
60
|
-
date = new Date(d * 1000);
|
|
61
|
-
}
|
|
62
|
-
else if (typeof d !== 'string') {
|
|
63
|
-
// unexpected type, e.g Function or something
|
|
64
|
-
return null;
|
|
65
|
-
}
|
|
66
|
-
else {
|
|
67
|
-
// Slicing removes the "timezone component", and makes the date "local"
|
|
68
|
-
// e.g 2022-04-06T23:15:00+09:00
|
|
69
|
-
// becomes 2022-04-06T23:15:00
|
|
70
|
-
date = new Date(d.slice(0, 19));
|
|
71
|
-
// We used to slice to remove the timezone information, now we don't
|
|
72
|
-
// date = new Date(d)
|
|
73
|
-
}
|
|
74
|
-
// validation
|
|
75
|
-
if (isNaN(date.getDate())) {
|
|
76
|
-
// throw new TypeError(`Cannot parse "${d}" into LocalTime`)
|
|
77
|
-
return null;
|
|
78
|
-
}
|
|
79
|
-
// if (utc) {
|
|
80
|
-
// date.setMinutes(date.getMinutes() + date.getTimezoneOffset())
|
|
81
|
-
// }
|
|
82
|
-
return new LocalTime(date);
|
|
83
|
-
}
|
|
84
|
-
static parseToDate(d) {
|
|
85
|
-
if (d instanceof LocalTime)
|
|
86
|
-
return d.$date;
|
|
87
|
-
if (d instanceof Date)
|
|
88
|
-
return d;
|
|
89
|
-
const date = typeof d === 'number' ? new Date(d * 1000) : new Date(d);
|
|
90
|
-
(0, assert_1._assert)(!isNaN(date.getDate()), `Cannot parse "${d}" to Date`, {
|
|
91
|
-
input: d,
|
|
92
|
-
});
|
|
93
|
-
return date;
|
|
94
|
-
}
|
|
95
|
-
static parseToUnixTimestamp(d) {
|
|
96
|
-
if (typeof d === 'number')
|
|
97
|
-
return d;
|
|
98
|
-
if (d instanceof LocalTime)
|
|
99
|
-
return d.unix();
|
|
100
|
-
const date = d instanceof Date ? d : new Date(d);
|
|
101
|
-
(0, assert_1._assert)(!isNaN(date.getDate()), `Cannot parse "${d}" to UnixTimestamp`, {
|
|
102
|
-
input: d,
|
|
103
|
-
});
|
|
104
|
-
return date.valueOf() / 1000;
|
|
105
|
-
}
|
|
106
|
-
static isValid(d) {
|
|
107
|
-
return this.parseOrNull(d) !== null;
|
|
108
|
-
}
|
|
109
|
-
static now() {
|
|
110
|
-
return new LocalTime(new Date());
|
|
111
|
-
}
|
|
112
|
-
static fromComponents(c) {
|
|
113
|
-
return new LocalTime(new Date(c.year, c.month - 1, c.day || 1, c.hour || 0, c.minute || 0, c.second || 0));
|
|
114
|
-
}
|
|
115
|
-
static fromDateUTC(d) {
|
|
116
|
-
return new LocalTime(new Date(d.toISOString()));
|
|
38
|
+
local() {
|
|
39
|
+
return new LocalTime(new Date(this.$date.getTime()));
|
|
117
40
|
}
|
|
118
41
|
get(unit) {
|
|
119
42
|
if (unit === 'year') {
|
|
@@ -215,7 +138,7 @@ class LocalTime {
|
|
|
215
138
|
}
|
|
216
139
|
if (unit === 'year' || unit === 'month') {
|
|
217
140
|
const d = addMonths(this.$date, unit === 'month' ? num : num * 12, mutate);
|
|
218
|
-
return mutate ? this :
|
|
141
|
+
return mutate ? this : exports.localTime.of(d);
|
|
219
142
|
}
|
|
220
143
|
return this.set(unit, this.get(unit) + num, mutate);
|
|
221
144
|
}
|
|
@@ -226,16 +149,16 @@ class LocalTime {
|
|
|
226
149
|
return Math.abs(this.diff(other, unit));
|
|
227
150
|
}
|
|
228
151
|
diff(other, unit) {
|
|
229
|
-
const date2 =
|
|
152
|
+
const date2 = exports.localTime.parseToDate(other);
|
|
230
153
|
const secDiff = (this.$date.valueOf() - date2.valueOf()) / 1000;
|
|
231
154
|
if (!secDiff)
|
|
232
155
|
return 0;
|
|
233
156
|
let r;
|
|
234
157
|
if (unit === 'year') {
|
|
235
|
-
r = differenceInMonths(this
|
|
158
|
+
r = differenceInMonths(this.$date, date2) / 12;
|
|
236
159
|
}
|
|
237
160
|
else if (unit === 'month') {
|
|
238
|
-
r = differenceInMonths(this
|
|
161
|
+
r = differenceInMonths(this.$date, date2);
|
|
239
162
|
}
|
|
240
163
|
else if (unit === 'day') {
|
|
241
164
|
r = secDiff / SECONDS_IN_DAY;
|
|
@@ -302,7 +225,7 @@ class LocalTime {
|
|
|
302
225
|
}
|
|
303
226
|
else {
|
|
304
227
|
// year or month
|
|
305
|
-
const lastDay = localDate_1.
|
|
228
|
+
const lastDay = localDate_1.localDate.getMonthLength(d.getFullYear(), d.getMonth() + 1);
|
|
306
229
|
d.setDate(lastDay);
|
|
307
230
|
}
|
|
308
231
|
}
|
|
@@ -315,35 +238,7 @@ class LocalTime {
|
|
|
315
238
|
* E.g 31 for January.
|
|
316
239
|
*/
|
|
317
240
|
daysInMonth() {
|
|
318
|
-
return localDate_1.
|
|
319
|
-
}
|
|
320
|
-
static sort(items, mutate = false, dir = 'asc') {
|
|
321
|
-
const mod = dir === 'desc' ? -1 : 1;
|
|
322
|
-
return (mutate ? items : [...items]).sort((a, b) => {
|
|
323
|
-
const v1 = a.$date.valueOf();
|
|
324
|
-
const v2 = b.$date.valueOf();
|
|
325
|
-
if (v1 === v2)
|
|
326
|
-
return 0;
|
|
327
|
-
return (v1 < v2 ? -1 : 1) * mod;
|
|
328
|
-
});
|
|
329
|
-
}
|
|
330
|
-
static earliestOrUndefined(items) {
|
|
331
|
-
return items.length ? LocalTime.earliest(items) : undefined;
|
|
332
|
-
}
|
|
333
|
-
static earliest(items) {
|
|
334
|
-
(0, assert_1._assert)(items.length, 'LocalTime.earliest called on empty array');
|
|
335
|
-
return items
|
|
336
|
-
.map(i => LocalTime.of(i))
|
|
337
|
-
.reduce((min, item) => (min.isSameOrBefore(item) ? min : item));
|
|
338
|
-
}
|
|
339
|
-
static latestOrUndefined(items) {
|
|
340
|
-
return items.length ? LocalTime.latest(items) : undefined;
|
|
341
|
-
}
|
|
342
|
-
static latest(items) {
|
|
343
|
-
(0, assert_1._assert)(items.length, 'LocalTime.latest called on empty array');
|
|
344
|
-
return items
|
|
345
|
-
.map(i => LocalTime.of(i))
|
|
346
|
-
.reduce((max, item) => (max.isSameOrAfter(item) ? max : item));
|
|
241
|
+
return localDate_1.localDate.getMonthLength(this.$date.getFullYear(), this.$date.getMonth() + 1);
|
|
347
242
|
}
|
|
348
243
|
isSame(d) {
|
|
349
244
|
return this.cmp(d) === 0;
|
|
@@ -382,13 +277,13 @@ class LocalTime {
|
|
|
382
277
|
* Third argument allows to override "now".
|
|
383
278
|
*/
|
|
384
279
|
isOlderThan(n, unit, now) {
|
|
385
|
-
return this.isBefore(
|
|
280
|
+
return this.isBefore(exports.localTime.of(now ?? new Date()).plus(-n, unit));
|
|
386
281
|
}
|
|
387
282
|
/**
|
|
388
283
|
* Checks if this localTime is same or older (<=) than "now" by X units.
|
|
389
284
|
*/
|
|
390
285
|
isSameOrOlderThan(n, unit, now) {
|
|
391
|
-
return this.isSameOrBefore(
|
|
286
|
+
return this.isSameOrBefore(exports.localTime.of(now ?? new Date()).plus(-n, unit));
|
|
392
287
|
}
|
|
393
288
|
/**
|
|
394
289
|
* Checks if this localTime is younger (>) than "now" by X units.
|
|
@@ -400,13 +295,13 @@ class LocalTime {
|
|
|
400
295
|
* Third argument allows to override "now".
|
|
401
296
|
*/
|
|
402
297
|
isYoungerThan(n, unit, now) {
|
|
403
|
-
return this.isAfter(
|
|
298
|
+
return this.isAfter(exports.localTime.of(now ?? new Date()).plus(-n, unit));
|
|
404
299
|
}
|
|
405
300
|
/**
|
|
406
301
|
* Checks if this localTime is same or younger (>=) than "now" by X units.
|
|
407
302
|
*/
|
|
408
303
|
isSameOrYoungerThan(n, unit, now) {
|
|
409
|
-
return this.isSameOrAfter(
|
|
304
|
+
return this.isSameOrAfter(exports.localTime.of(now ?? new Date()).plus(-n, unit));
|
|
410
305
|
}
|
|
411
306
|
/**
|
|
412
307
|
* Returns 1 if this > d
|
|
@@ -415,19 +310,15 @@ class LocalTime {
|
|
|
415
310
|
*/
|
|
416
311
|
cmp(d) {
|
|
417
312
|
const t1 = this.$date.valueOf();
|
|
418
|
-
const t2 =
|
|
313
|
+
const t2 = exports.localTime.parseToDate(d).valueOf();
|
|
419
314
|
if (t1 === t2)
|
|
420
315
|
return 0;
|
|
421
316
|
return t1 < t2 ? -1 : 1;
|
|
422
317
|
}
|
|
423
318
|
components() {
|
|
424
319
|
return {
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
day: this.$date.getDate(),
|
|
428
|
-
hour: this.$date.getHours(),
|
|
429
|
-
minute: this.$date.getMinutes(),
|
|
430
|
-
second: this.$date.getSeconds(),
|
|
320
|
+
...this.dateComponents(),
|
|
321
|
+
...this.timeComponents(),
|
|
431
322
|
};
|
|
432
323
|
}
|
|
433
324
|
dateComponents() {
|
|
@@ -445,7 +336,7 @@ class LocalTime {
|
|
|
445
336
|
};
|
|
446
337
|
}
|
|
447
338
|
fromNow(now = new Date()) {
|
|
448
|
-
const msDiff =
|
|
339
|
+
const msDiff = exports.localTime.parseToDate(now).valueOf() - this.$date.valueOf();
|
|
449
340
|
if (msDiff === 0)
|
|
450
341
|
return 'now';
|
|
451
342
|
if (msDiff >= 0) {
|
|
@@ -469,7 +360,7 @@ class LocalTime {
|
|
|
469
360
|
return Math.floor(this.$date.valueOf() / 1000);
|
|
470
361
|
}
|
|
471
362
|
toLocalDate() {
|
|
472
|
-
return localDate_1.
|
|
363
|
+
return localDate_1.localDate.fromDate(this.$date);
|
|
473
364
|
}
|
|
474
365
|
/**
|
|
475
366
|
* Returns e.g: `1984-06-21 17:56:21`
|
|
@@ -550,68 +441,132 @@ class LocalTime {
|
|
|
550
441
|
}
|
|
551
442
|
}
|
|
552
443
|
exports.LocalTime = LocalTime;
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
}
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
}
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
444
|
+
class LocalTimeFactory {
|
|
445
|
+
/**
|
|
446
|
+
* Parses input String into LocalDate.
|
|
447
|
+
* Input can already be a LocalDate - it is returned as-is in that case.
|
|
448
|
+
*/
|
|
449
|
+
of(d) {
|
|
450
|
+
const t = this.parseOrNull(d);
|
|
451
|
+
(0, assert_1._assert)(t !== null, `Cannot parse "${d}" into LocalTime`, {
|
|
452
|
+
input: d,
|
|
453
|
+
});
|
|
454
|
+
return t;
|
|
455
|
+
}
|
|
456
|
+
/**
|
|
457
|
+
* Create LocalTime from unixTimestamp in milliseconds (not in seconds).
|
|
458
|
+
*/
|
|
459
|
+
ofMillis(millis) {
|
|
460
|
+
return this.of(new Date(millis));
|
|
461
|
+
}
|
|
462
|
+
/**
|
|
463
|
+
* Returns null if invalid
|
|
464
|
+
*/
|
|
465
|
+
parseOrNull(d) {
|
|
466
|
+
if (!d)
|
|
467
|
+
return null;
|
|
468
|
+
if (d instanceof LocalTime)
|
|
469
|
+
return d;
|
|
470
|
+
let date;
|
|
471
|
+
if (d instanceof Date) {
|
|
472
|
+
date = d;
|
|
473
|
+
}
|
|
474
|
+
else if (typeof d === 'number') {
|
|
475
|
+
date = new Date(d * 1000);
|
|
476
|
+
}
|
|
477
|
+
else if (typeof d !== 'string') {
|
|
478
|
+
// unexpected type, e.g Function or something
|
|
479
|
+
return null;
|
|
480
|
+
}
|
|
481
|
+
else {
|
|
482
|
+
// Slicing removes the "timezone component", and makes the date "local"
|
|
483
|
+
// e.g 2022-04-06T23:15:00+09:00
|
|
484
|
+
// becomes 2022-04-06T23:15:00
|
|
485
|
+
date = new Date(d.slice(0, 19));
|
|
486
|
+
// We used to slice to remove the timezone information, now we don't
|
|
487
|
+
// date = new Date(d)
|
|
488
|
+
}
|
|
489
|
+
// validation
|
|
490
|
+
if (isNaN(date.getDate())) {
|
|
491
|
+
// throw new TypeError(`Cannot parse "${d}" into LocalTime`)
|
|
492
|
+
return null;
|
|
493
|
+
}
|
|
494
|
+
return new LocalTime(date);
|
|
495
|
+
}
|
|
496
|
+
parseToDate(d) {
|
|
497
|
+
if (d instanceof LocalTime)
|
|
498
|
+
return d.$date;
|
|
499
|
+
if (d instanceof Date)
|
|
500
|
+
return d;
|
|
501
|
+
const date = typeof d === 'number' ? new Date(d * 1000) : new Date(d);
|
|
502
|
+
(0, assert_1._assert)(!isNaN(date.getDate()), `Cannot parse "${d}" to Date`, {
|
|
503
|
+
input: d,
|
|
504
|
+
});
|
|
505
|
+
return date;
|
|
506
|
+
}
|
|
507
|
+
parseToUnixTimestamp(d) {
|
|
508
|
+
if (typeof d === 'number')
|
|
509
|
+
return d;
|
|
510
|
+
if (d instanceof LocalTime)
|
|
511
|
+
return d.unix();
|
|
512
|
+
const date = d instanceof Date ? d : new Date(d);
|
|
513
|
+
(0, assert_1._assert)(!isNaN(date.getDate()), `Cannot parse "${d}" to UnixTimestamp`, {
|
|
514
|
+
input: d,
|
|
515
|
+
});
|
|
516
|
+
return date.valueOf() / 1000;
|
|
517
|
+
}
|
|
518
|
+
isValid(d) {
|
|
519
|
+
return this.parseOrNull(d) !== null;
|
|
520
|
+
}
|
|
521
|
+
now() {
|
|
522
|
+
return new LocalTime(new Date());
|
|
523
|
+
}
|
|
524
|
+
/**
|
|
525
|
+
* Creates a LocalTime from the input, unless it's falsy - then returns undefined.
|
|
526
|
+
*
|
|
527
|
+
* `localTime` function will instead return LocalTime of `now` for falsy input.
|
|
528
|
+
*/
|
|
529
|
+
orUndefined(d) {
|
|
530
|
+
return d ? this.of(d) : undefined;
|
|
531
|
+
}
|
|
532
|
+
/**
|
|
533
|
+
* Creates a LocalTime from the input, unless it's falsy - then returns LocalTime.now
|
|
534
|
+
*/
|
|
535
|
+
orNow(d) {
|
|
536
|
+
return d ? this.of(d) : this.now();
|
|
537
|
+
}
|
|
538
|
+
fromComponents(c) {
|
|
539
|
+
return new LocalTime(new Date(c.year, c.month - 1, c.day || 1, c.hour || 0, c.minute || 0, c.second || 0));
|
|
540
|
+
}
|
|
541
|
+
sort(items, dir = 'asc', mutate = false) {
|
|
542
|
+
const mod = dir === 'desc' ? -1 : 1;
|
|
543
|
+
return (mutate ? items : [...items]).sort((a, b) => {
|
|
544
|
+
const v1 = a.$date.valueOf();
|
|
545
|
+
const v2 = b.$date.valueOf();
|
|
546
|
+
if (v1 === v2)
|
|
547
|
+
return 0;
|
|
548
|
+
return (v1 < v2 ? -1 : 1) * mod;
|
|
549
|
+
});
|
|
550
|
+
}
|
|
551
|
+
minOrUndefined(items) {
|
|
552
|
+
return items.length ? this.min(items) : undefined;
|
|
553
|
+
}
|
|
554
|
+
min(items) {
|
|
555
|
+
(0, assert_1._assert)(items.length, 'localTime.min called on empty array');
|
|
556
|
+
return items
|
|
557
|
+
.map(i => this.of(i))
|
|
558
|
+
.reduce((min, item) => (min.$date.valueOf() <= item.$date.valueOf() ? min : item));
|
|
559
|
+
}
|
|
560
|
+
maxOrUndefined(items) {
|
|
561
|
+
return items.length ? this.max(items) : undefined;
|
|
562
|
+
}
|
|
563
|
+
max(items) {
|
|
564
|
+
(0, assert_1._assert)(items.length, 'localTime.max called on empty array');
|
|
565
|
+
return items
|
|
566
|
+
.map(i => this.of(i))
|
|
567
|
+
.reduce((max, item) => (max.$date.valueOf() >= item.$date.valueOf() ? max : item));
|
|
568
|
+
}
|
|
613
569
|
}
|
|
614
|
-
exports.getUTCOffsetHours = getUTCOffsetHours;
|
|
615
570
|
// based on: https://github.com/date-fns/date-fns/blob/master/src/getISOWeek/index.ts
|
|
616
571
|
function getWeek(date) {
|
|
617
572
|
const diff = startOfWeek(date).getTime() - startOfWeekYear(date).getTime();
|
|
@@ -685,7 +640,7 @@ function addMonths(d, num, mutate = false) {
|
|
|
685
640
|
year--;
|
|
686
641
|
month += 12;
|
|
687
642
|
}
|
|
688
|
-
const monthLen = localDate_1.
|
|
643
|
+
const monthLen = localDate_1.localDate.getMonthLength(year, month);
|
|
689
644
|
if (day > monthLen)
|
|
690
645
|
day = monthLen;
|
|
691
646
|
d.setFullYear(year, month - 1, day);
|
|
@@ -700,3 +655,40 @@ function differenceInMonths(a, b) {
|
|
|
700
655
|
const anchor2 = addMonths(a, wholeMonthDiff + sign).getTime();
|
|
701
656
|
return -(wholeMonthDiff + ((b.getTime() - anchor) / (anchor2 - anchor)) * sign);
|
|
702
657
|
}
|
|
658
|
+
const localTimeFactory = new LocalTimeFactory();
|
|
659
|
+
exports.localTime = localTimeFactory.of.bind(localTimeFactory);
|
|
660
|
+
// The line below is the blackest of black magic I have ever written in 2024.
|
|
661
|
+
// And probably 2023 as well.
|
|
662
|
+
Object.setPrototypeOf(exports.localTime, localTimeFactory);
|
|
663
|
+
/**
|
|
664
|
+
Convenience function to return current Unix timestamp in seconds.
|
|
665
|
+
Like Date.now(), but in seconds.
|
|
666
|
+
*/
|
|
667
|
+
function nowUnix() {
|
|
668
|
+
return Math.floor(Date.now() / 1000);
|
|
669
|
+
}
|
|
670
|
+
exports.nowUnix = nowUnix;
|
|
671
|
+
/**
|
|
672
|
+
* UTC offset is the opposite of "timezone offset" - it's the number of minutes to add
|
|
673
|
+
* to the local time to get UTC time.
|
|
674
|
+
*
|
|
675
|
+
* E.g utcOffset for CEST is -120,
|
|
676
|
+
* which means that you need to add -120 minutes to the local time to get UTC time.
|
|
677
|
+
*
|
|
678
|
+
* Instead of -0 it returns 0, for the peace of mind and less weird test/snapshot differences.
|
|
679
|
+
*/
|
|
680
|
+
function getUTCOffsetMinutes() {
|
|
681
|
+
return -new Date().getTimezoneOffset() || 0;
|
|
682
|
+
}
|
|
683
|
+
exports.getUTCOffsetMinutes = getUTCOffsetMinutes;
|
|
684
|
+
/**
|
|
685
|
+
* Same as getUTCOffsetMinutes, but rounded to hours.
|
|
686
|
+
*
|
|
687
|
+
* E.g for CEST it is -2.
|
|
688
|
+
*
|
|
689
|
+
* Instead of -0 it returns 0, for the peace of mind and less weird test/snapshot differences.
|
|
690
|
+
*/
|
|
691
|
+
function getUTCOffsetHours() {
|
|
692
|
+
return Math.round(getUTCOffsetMinutes() / 60);
|
|
693
|
+
}
|
|
694
|
+
exports.getUTCOffsetHours = getUTCOffsetHours;
|
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import type { UnixTimestampNumber, Inclusiveness } from '../types';
|
|
2
|
-
import
|
|
3
|
-
import { LocalTime } from './localTime';
|
|
2
|
+
import { LocalTimeInput, LocalTime } from './localTime';
|
|
4
3
|
export type TimeIntervalConfig = TimeInterval | TimeIntervalString;
|
|
5
4
|
export type TimeIntervalString = string;
|
|
6
5
|
/**
|
|
@@ -14,7 +14,7 @@ class TimeInterval {
|
|
|
14
14
|
this.$end = $end;
|
|
15
15
|
}
|
|
16
16
|
static of(start, end) {
|
|
17
|
-
return new TimeInterval(localTime_1.
|
|
17
|
+
return new TimeInterval(localTime_1.localTime.parseToUnixTimestamp(start), localTime_1.localTime.parseToUnixTimestamp(end));
|
|
18
18
|
}
|
|
19
19
|
get start() {
|
|
20
20
|
return this.$start;
|
|
@@ -23,10 +23,10 @@ class TimeInterval {
|
|
|
23
23
|
return this.$end;
|
|
24
24
|
}
|
|
25
25
|
get startTime() {
|
|
26
|
-
return localTime_1.
|
|
26
|
+
return (0, localTime_1.localTime)(this.$start);
|
|
27
27
|
}
|
|
28
28
|
get endTime() {
|
|
29
|
-
return localTime_1.
|
|
29
|
+
return (0, localTime_1.localTime)(this.$end);
|
|
30
30
|
}
|
|
31
31
|
/**
|
|
32
32
|
* Parses string like `1649267185/1649267187` into a TimeInterval.
|
|
@@ -58,7 +58,7 @@ class TimeInterval {
|
|
|
58
58
|
return this.cmp(d) >= 0;
|
|
59
59
|
}
|
|
60
60
|
includes(d, incl = '[)') {
|
|
61
|
-
d = localTime_1.
|
|
61
|
+
d = localTime_1.localTime.parseToUnixTimestamp(d);
|
|
62
62
|
// eslint-disable-next-line @typescript-eslint/prefer-string-starts-ends-with
|
|
63
63
|
if (d < this.$start || (d === this.$start && incl[0] === '('))
|
|
64
64
|
return false;
|
package/dist/env/buildInfo.js
CHANGED
|
@@ -3,7 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.generateBuildInfoDev = void 0;
|
|
4
4
|
const localTime_1 = require("../datetime/localTime");
|
|
5
5
|
function generateBuildInfoDev() {
|
|
6
|
-
const now =
|
|
6
|
+
const now = localTime_1.localTime.now();
|
|
7
7
|
const ts = now.unix();
|
|
8
8
|
const rev = 'devRev';
|
|
9
9
|
const branchName = 'devBranch';
|