@naturalcycles/js-lib 14.232.0 → 14.233.1
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 +46 -51
- package/dist/datetime/localTime.js +181 -197
- 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 +177 -192
- package/dist-esm/datetime/timeInterval.js +5 -5
- package/dist-esm/env/buildInfo.js +2 -2
- package/package.json +2 -2
- package/src/datetime/dateInterval.ts +6 -7
- package/src/datetime/localDate.ts +307 -237
- package/src/datetime/localTime.ts +203 -212
- package/src/datetime/timeInterval.ts +6 -7
- package/src/env/buildInfo.ts +2 -2
|
@@ -23,40 +23,16 @@ 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;
|
|
37
|
-
/**
|
|
38
|
-
* Create LocalTime from unixTimestamp in milliseconds (not in seconds).
|
|
39
|
-
*/
|
|
40
|
-
static ofMillis(millis: UnixTimestampMillisNumber): LocalTime;
|
|
41
|
-
/**
|
|
42
|
-
* Returns null if invalid
|
|
43
|
-
*/
|
|
44
|
-
static parseOrNull(d: LocalTimeInput | undefined | null): LocalTime | null;
|
|
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;
|
|
27
|
+
$date: Date;
|
|
28
|
+
constructor($date: Date);
|
|
53
29
|
/**
|
|
54
|
-
* Returns LocalTime that is based on the same unixtimestamp, but in UTC timezone.
|
|
30
|
+
* Returns [cloned] LocalTime that is based on the same unixtimestamp, but in UTC timezone.
|
|
55
31
|
* Opposite of `.local()` method.
|
|
56
32
|
*/
|
|
57
33
|
utc(): LocalTime;
|
|
58
34
|
/**
|
|
59
|
-
* Returns LocalTime that is based on the same unixtimestamp, but in local timezone.
|
|
35
|
+
* Returns [cloned] LocalTime that is based on the same unixtimestamp, but in local timezone.
|
|
60
36
|
* Opposite of `.utc()` method.
|
|
61
37
|
*/
|
|
62
38
|
local(): LocalTime;
|
|
@@ -93,11 +69,6 @@ export declare class LocalTime {
|
|
|
93
69
|
* E.g 31 for January.
|
|
94
70
|
*/
|
|
95
71
|
daysInMonth(): number;
|
|
96
|
-
static sort(items: LocalTime[], mutate?: boolean, dir?: SortDirection): LocalTime[];
|
|
97
|
-
static earliestOrUndefined(items: LocalTimeInput[]): LocalTime | undefined;
|
|
98
|
-
static earliest(items: LocalTimeInput[]): LocalTime;
|
|
99
|
-
static latestOrUndefined(items: LocalTimeInput[]): LocalTime | undefined;
|
|
100
|
-
static latest(items: LocalTimeInput[]): LocalTime;
|
|
101
72
|
isSame(d: LocalTimeInput): boolean;
|
|
102
73
|
isBefore(d: LocalTimeInput, inclusive?: boolean): boolean;
|
|
103
74
|
isSameOrBefore(d: LocalTimeInput): boolean;
|
|
@@ -175,24 +146,48 @@ export declare class LocalTime {
|
|
|
175
146
|
toMonthId(): MonthId;
|
|
176
147
|
format(fmt: Intl.DateTimeFormat | LocalTimeFormatter): string;
|
|
177
148
|
}
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
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 | undefined): LocalTime | undefined;
|
|
173
|
+
/**
|
|
174
|
+
* Creates a LocalTime from the input, unless it's falsy - then returns LocalTime.now
|
|
175
|
+
*/
|
|
176
|
+
orNow(d: LocalTimeInput | null | undefined): 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;
|
|
196
191
|
/**
|
|
197
192
|
Convenience function to return current Unix timestamp in seconds.
|
|
198
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,104 +20,19 @@ 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
|
-
* Input can already be a LocalDate - it is returned as-is in that case.
|
|
33
|
-
*/
|
|
34
|
-
static of(d) {
|
|
35
|
-
const t = this.parseOrNull(d);
|
|
36
|
-
(0, assert_1._assert)(t !== null, `Cannot parse "${d}" into LocalTime`, {
|
|
37
|
-
input: d,
|
|
38
|
-
});
|
|
39
|
-
return t;
|
|
40
|
-
}
|
|
41
|
-
/**
|
|
42
|
-
* Create LocalTime from unixTimestamp in milliseconds (not in seconds).
|
|
43
|
-
*/
|
|
44
|
-
static ofMillis(millis) {
|
|
45
|
-
return LocalTime.of(new Date(millis));
|
|
46
|
-
}
|
|
47
|
-
/**
|
|
48
|
-
* Returns null if invalid
|
|
49
|
-
*/
|
|
50
|
-
static parseOrNull(d) {
|
|
51
|
-
if (!d)
|
|
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
|
-
return new LocalTime(date);
|
|
80
|
-
}
|
|
81
|
-
static parseToDate(d) {
|
|
82
|
-
if (d instanceof LocalTime)
|
|
83
|
-
return d.$date;
|
|
84
|
-
if (d instanceof Date)
|
|
85
|
-
return d;
|
|
86
|
-
const date = typeof d === 'number' ? new Date(d * 1000) : new Date(d);
|
|
87
|
-
(0, assert_1._assert)(!isNaN(date.getDate()), `Cannot parse "${d}" to Date`, {
|
|
88
|
-
input: d,
|
|
89
|
-
});
|
|
90
|
-
return date;
|
|
91
|
-
}
|
|
92
|
-
static parseToUnixTimestamp(d) {
|
|
93
|
-
if (typeof d === 'number')
|
|
94
|
-
return d;
|
|
95
|
-
if (d instanceof LocalTime)
|
|
96
|
-
return d.unix();
|
|
97
|
-
const date = d instanceof Date ? d : new Date(d);
|
|
98
|
-
(0, assert_1._assert)(!isNaN(date.getDate()), `Cannot parse "${d}" to UnixTimestamp`, {
|
|
99
|
-
input: d,
|
|
100
|
-
});
|
|
101
|
-
return date.valueOf() / 1000;
|
|
102
|
-
}
|
|
103
|
-
static isValid(d) {
|
|
104
|
-
return this.parseOrNull(d) !== null;
|
|
105
|
-
}
|
|
106
|
-
static now() {
|
|
107
|
-
return new LocalTime(new Date());
|
|
108
|
-
}
|
|
109
|
-
static fromComponents(c) {
|
|
110
|
-
return new LocalTime(new Date(c.year, c.month - 1, c.day || 1, c.hour || 0, c.minute || 0, c.second || 0));
|
|
111
|
-
}
|
|
112
|
-
/**
|
|
113
|
-
* Returns LocalTime that is based on the same unixtimestamp, but in UTC timezone.
|
|
28
|
+
* Returns [cloned] LocalTime that is based on the same unixtimestamp, but in UTC timezone.
|
|
114
29
|
* Opposite of `.local()` method.
|
|
115
30
|
*/
|
|
116
31
|
utc() {
|
|
117
32
|
return new LocalTime(new Date(this.$date.toISOString()));
|
|
118
33
|
}
|
|
119
34
|
/**
|
|
120
|
-
* Returns LocalTime that is based on the same unixtimestamp, but in local timezone.
|
|
35
|
+
* Returns [cloned] LocalTime that is based on the same unixtimestamp, but in local timezone.
|
|
121
36
|
* Opposite of `.utc()` method.
|
|
122
37
|
*/
|
|
123
38
|
local() {
|
|
@@ -223,7 +138,7 @@ class LocalTime {
|
|
|
223
138
|
}
|
|
224
139
|
if (unit === 'year' || unit === 'month') {
|
|
225
140
|
const d = addMonths(this.$date, unit === 'month' ? num : num * 12, mutate);
|
|
226
|
-
return mutate ? this :
|
|
141
|
+
return mutate ? this : exports.localTime.of(d);
|
|
227
142
|
}
|
|
228
143
|
return this.set(unit, this.get(unit) + num, mutate);
|
|
229
144
|
}
|
|
@@ -234,16 +149,16 @@ class LocalTime {
|
|
|
234
149
|
return Math.abs(this.diff(other, unit));
|
|
235
150
|
}
|
|
236
151
|
diff(other, unit) {
|
|
237
|
-
const date2 =
|
|
152
|
+
const date2 = exports.localTime.parseToDate(other);
|
|
238
153
|
const secDiff = (this.$date.valueOf() - date2.valueOf()) / 1000;
|
|
239
154
|
if (!secDiff)
|
|
240
155
|
return 0;
|
|
241
156
|
let r;
|
|
242
157
|
if (unit === 'year') {
|
|
243
|
-
r = differenceInMonths(this
|
|
158
|
+
r = differenceInMonths(this.$date, date2) / 12;
|
|
244
159
|
}
|
|
245
160
|
else if (unit === 'month') {
|
|
246
|
-
r = differenceInMonths(this
|
|
161
|
+
r = differenceInMonths(this.$date, date2);
|
|
247
162
|
}
|
|
248
163
|
else if (unit === 'day') {
|
|
249
164
|
r = secDiff / SECONDS_IN_DAY;
|
|
@@ -310,7 +225,7 @@ class LocalTime {
|
|
|
310
225
|
}
|
|
311
226
|
else {
|
|
312
227
|
// year or month
|
|
313
|
-
const lastDay = localDate_1.
|
|
228
|
+
const lastDay = localDate_1.localDate.getMonthLength(d.getFullYear(), d.getMonth() + 1);
|
|
314
229
|
d.setDate(lastDay);
|
|
315
230
|
}
|
|
316
231
|
}
|
|
@@ -323,35 +238,7 @@ class LocalTime {
|
|
|
323
238
|
* E.g 31 for January.
|
|
324
239
|
*/
|
|
325
240
|
daysInMonth() {
|
|
326
|
-
return localDate_1.
|
|
327
|
-
}
|
|
328
|
-
static sort(items, mutate = false, dir = 'asc') {
|
|
329
|
-
const mod = dir === 'desc' ? -1 : 1;
|
|
330
|
-
return (mutate ? items : [...items]).sort((a, b) => {
|
|
331
|
-
const v1 = a.$date.valueOf();
|
|
332
|
-
const v2 = b.$date.valueOf();
|
|
333
|
-
if (v1 === v2)
|
|
334
|
-
return 0;
|
|
335
|
-
return (v1 < v2 ? -1 : 1) * mod;
|
|
336
|
-
});
|
|
337
|
-
}
|
|
338
|
-
static earliestOrUndefined(items) {
|
|
339
|
-
return items.length ? LocalTime.earliest(items) : undefined;
|
|
340
|
-
}
|
|
341
|
-
static earliest(items) {
|
|
342
|
-
(0, assert_1._assert)(items.length, 'LocalTime.earliest called on empty array');
|
|
343
|
-
return items
|
|
344
|
-
.map(i => LocalTime.of(i))
|
|
345
|
-
.reduce((min, item) => (min.isSameOrBefore(item) ? min : item));
|
|
346
|
-
}
|
|
347
|
-
static latestOrUndefined(items) {
|
|
348
|
-
return items.length ? LocalTime.latest(items) : undefined;
|
|
349
|
-
}
|
|
350
|
-
static latest(items) {
|
|
351
|
-
(0, assert_1._assert)(items.length, 'LocalTime.latest called on empty array');
|
|
352
|
-
return items
|
|
353
|
-
.map(i => LocalTime.of(i))
|
|
354
|
-
.reduce((max, item) => (max.isSameOrAfter(item) ? max : item));
|
|
241
|
+
return localDate_1.localDate.getMonthLength(this.$date.getFullYear(), this.$date.getMonth() + 1);
|
|
355
242
|
}
|
|
356
243
|
isSame(d) {
|
|
357
244
|
return this.cmp(d) === 0;
|
|
@@ -390,13 +277,13 @@ class LocalTime {
|
|
|
390
277
|
* Third argument allows to override "now".
|
|
391
278
|
*/
|
|
392
279
|
isOlderThan(n, unit, now) {
|
|
393
|
-
return this.isBefore(
|
|
280
|
+
return this.isBefore(exports.localTime.of(now ?? new Date()).plus(-n, unit));
|
|
394
281
|
}
|
|
395
282
|
/**
|
|
396
283
|
* Checks if this localTime is same or older (<=) than "now" by X units.
|
|
397
284
|
*/
|
|
398
285
|
isSameOrOlderThan(n, unit, now) {
|
|
399
|
-
return this.isSameOrBefore(
|
|
286
|
+
return this.isSameOrBefore(exports.localTime.of(now ?? new Date()).plus(-n, unit));
|
|
400
287
|
}
|
|
401
288
|
/**
|
|
402
289
|
* Checks if this localTime is younger (>) than "now" by X units.
|
|
@@ -408,13 +295,13 @@ class LocalTime {
|
|
|
408
295
|
* Third argument allows to override "now".
|
|
409
296
|
*/
|
|
410
297
|
isYoungerThan(n, unit, now) {
|
|
411
|
-
return this.isAfter(
|
|
298
|
+
return this.isAfter(exports.localTime.of(now ?? new Date()).plus(-n, unit));
|
|
412
299
|
}
|
|
413
300
|
/**
|
|
414
301
|
* Checks if this localTime is same or younger (>=) than "now" by X units.
|
|
415
302
|
*/
|
|
416
303
|
isSameOrYoungerThan(n, unit, now) {
|
|
417
|
-
return this.isSameOrAfter(
|
|
304
|
+
return this.isSameOrAfter(exports.localTime.of(now ?? new Date()).plus(-n, unit));
|
|
418
305
|
}
|
|
419
306
|
/**
|
|
420
307
|
* Returns 1 if this > d
|
|
@@ -423,19 +310,15 @@ class LocalTime {
|
|
|
423
310
|
*/
|
|
424
311
|
cmp(d) {
|
|
425
312
|
const t1 = this.$date.valueOf();
|
|
426
|
-
const t2 =
|
|
313
|
+
const t2 = exports.localTime.parseToDate(d).valueOf();
|
|
427
314
|
if (t1 === t2)
|
|
428
315
|
return 0;
|
|
429
316
|
return t1 < t2 ? -1 : 1;
|
|
430
317
|
}
|
|
431
318
|
components() {
|
|
432
319
|
return {
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
day: this.$date.getDate(),
|
|
436
|
-
hour: this.$date.getHours(),
|
|
437
|
-
minute: this.$date.getMinutes(),
|
|
438
|
-
second: this.$date.getSeconds(),
|
|
320
|
+
...this.dateComponents(),
|
|
321
|
+
...this.timeComponents(),
|
|
439
322
|
};
|
|
440
323
|
}
|
|
441
324
|
dateComponents() {
|
|
@@ -453,7 +336,7 @@ class LocalTime {
|
|
|
453
336
|
};
|
|
454
337
|
}
|
|
455
338
|
fromNow(now = new Date()) {
|
|
456
|
-
const msDiff =
|
|
339
|
+
const msDiff = exports.localTime.parseToDate(now).valueOf() - this.$date.valueOf();
|
|
457
340
|
if (msDiff === 0)
|
|
458
341
|
return 'now';
|
|
459
342
|
if (msDiff >= 0) {
|
|
@@ -477,7 +360,7 @@ class LocalTime {
|
|
|
477
360
|
return Math.floor(this.$date.valueOf() / 1000);
|
|
478
361
|
}
|
|
479
362
|
toLocalDate() {
|
|
480
|
-
return localDate_1.
|
|
363
|
+
return localDate_1.localDate.fromDate(this.$date);
|
|
481
364
|
}
|
|
482
365
|
/**
|
|
483
366
|
* Returns e.g: `1984-06-21 17:56:21`
|
|
@@ -558,68 +441,132 @@ class LocalTime {
|
|
|
558
441
|
}
|
|
559
442
|
}
|
|
560
443
|
exports.LocalTime = LocalTime;
|
|
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
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
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
|
+
}
|
|
621
569
|
}
|
|
622
|
-
exports.getUTCOffsetHours = getUTCOffsetHours;
|
|
623
570
|
// based on: https://github.com/date-fns/date-fns/blob/master/src/getISOWeek/index.ts
|
|
624
571
|
function getWeek(date) {
|
|
625
572
|
const diff = startOfWeek(date).getTime() - startOfWeekYear(date).getTime();
|
|
@@ -693,7 +640,7 @@ function addMonths(d, num, mutate = false) {
|
|
|
693
640
|
year--;
|
|
694
641
|
month += 12;
|
|
695
642
|
}
|
|
696
|
-
const monthLen = localDate_1.
|
|
643
|
+
const monthLen = localDate_1.localDate.getMonthLength(year, month);
|
|
697
644
|
if (day > monthLen)
|
|
698
645
|
day = monthLen;
|
|
699
646
|
d.setFullYear(year, month - 1, day);
|
|
@@ -708,3 +655,40 @@ function differenceInMonths(a, b) {
|
|
|
708
655
|
const anchor2 = addMonths(a, wholeMonthDiff + sign).getTime();
|
|
709
656
|
return -(wholeMonthDiff + ((b.getTime() - anchor) / (anchor2 - anchor)) * sign);
|
|
710
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';
|