@naturalcycles/js-lib 14.96.1 → 14.97.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/localDate.d.ts +18 -10
- package/dist/datetime/localDate.js +91 -65
- package/dist/datetime/localTime.d.ts +10 -10
- package/dist/datetime/localTime.js +1 -1
- package/dist/index.d.ts +2 -2
- package/dist/types.d.ts +11 -3
- package/dist-esm/datetime/localDate.js +91 -65
- package/dist-esm/datetime/localTime.js +1 -1
- package/package.json +1 -1
- package/src/datetime/localDate.ts +107 -71
- package/src/datetime/localTime.ts +11 -11
- package/src/index.ts +6 -2
- package/src/types.ts +13 -3
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Sequence } from '../seq/seq';
|
|
2
|
-
import {
|
|
2
|
+
import { IsoDateString, UnixTimestampNumber } from '../types';
|
|
3
3
|
import { LocalTime } from './localTime';
|
|
4
4
|
export declare type LocalDateUnit = 'year' | 'month' | 'day';
|
|
5
5
|
export declare type Inclusiveness = '()' | '[]' | '[)' | '(]';
|
|
@@ -8,9 +8,9 @@ export declare type LocalDateConfig = LocalDate | string;
|
|
|
8
8
|
* @experimental
|
|
9
9
|
*/
|
|
10
10
|
export declare class LocalDate {
|
|
11
|
-
year
|
|
12
|
-
month
|
|
13
|
-
day
|
|
11
|
+
private $year;
|
|
12
|
+
private $month;
|
|
13
|
+
private $day;
|
|
14
14
|
private constructor();
|
|
15
15
|
static create(year: number, month: number, day: number): LocalDate;
|
|
16
16
|
/**
|
|
@@ -35,9 +35,17 @@ export declare class LocalDate {
|
|
|
35
35
|
static latest(items: LocalDate[]): LocalDate;
|
|
36
36
|
static range(minIncl: LocalDateConfig, maxExcl: LocalDateConfig, step?: number, stepUnit?: LocalDateUnit): LocalDate[];
|
|
37
37
|
static rangeSeq(minIncl: LocalDateConfig, maxExcl: LocalDateConfig, step?: number, stepUnit?: LocalDateUnit): Sequence<LocalDate>;
|
|
38
|
-
static rangeString(minIncl: LocalDateConfig, maxExcl: LocalDateConfig, step?: number, stepUnit?: LocalDateUnit):
|
|
38
|
+
static rangeString(minIncl: LocalDateConfig, maxExcl: LocalDateConfig, step?: number, stepUnit?: LocalDateUnit): IsoDateString[];
|
|
39
39
|
static rangeIncl(minIncl: LocalDateConfig, maxIncl: LocalDateConfig, step?: number, stepUnit?: LocalDateUnit): LocalDate[];
|
|
40
|
-
static rangeInclString(minIncl: LocalDateConfig, maxIncl: LocalDateConfig, step?: number, stepUnit?: LocalDateUnit):
|
|
40
|
+
static rangeInclString(minIncl: LocalDateConfig, maxIncl: LocalDateConfig, step?: number, stepUnit?: LocalDateUnit): IsoDateString[];
|
|
41
|
+
get(unit: LocalDateUnit): number;
|
|
42
|
+
set(unit: LocalDateUnit, v: number, mutate?: boolean): LocalDate;
|
|
43
|
+
year(): number;
|
|
44
|
+
year(v: number): LocalDate;
|
|
45
|
+
month(): number;
|
|
46
|
+
month(v: number): LocalDate;
|
|
47
|
+
day(): number;
|
|
48
|
+
day(v: number): LocalDate;
|
|
41
49
|
isSame(d: LocalDateConfig): boolean;
|
|
42
50
|
isBefore(d: LocalDateConfig): boolean;
|
|
43
51
|
isSameOrBefore(d: LocalDateConfig): boolean;
|
|
@@ -76,12 +84,12 @@ export declare class LocalDate {
|
|
|
76
84
|
*/
|
|
77
85
|
toDate(): Date;
|
|
78
86
|
toLocalTime(): LocalTime;
|
|
79
|
-
toISODate():
|
|
80
|
-
toString():
|
|
87
|
+
toISODate(): IsoDateString;
|
|
88
|
+
toString(): IsoDateString;
|
|
81
89
|
toStringCompact(): string;
|
|
82
|
-
unix():
|
|
90
|
+
unix(): UnixTimestampNumber;
|
|
83
91
|
unixMillis(): number;
|
|
84
|
-
toJSON():
|
|
92
|
+
toJSON(): IsoDateString;
|
|
85
93
|
}
|
|
86
94
|
/**
|
|
87
95
|
* Shortcut wrapper around `LocalDate.parse` / `LocalDate.today`
|
|
@@ -6,14 +6,15 @@ const seq_1 = require("../seq/seq");
|
|
|
6
6
|
const types_1 = require("../types");
|
|
7
7
|
const localTime_1 = require("./localTime");
|
|
8
8
|
const m31 = new Set([1, 3, 5, 7, 8, 10, 12]);
|
|
9
|
+
/* eslint-disable no-dupe-class-members */
|
|
9
10
|
/**
|
|
10
11
|
* @experimental
|
|
11
12
|
*/
|
|
12
13
|
class LocalDate {
|
|
13
|
-
constructor(year, month, day) {
|
|
14
|
-
this
|
|
15
|
-
this
|
|
16
|
-
this
|
|
14
|
+
constructor($year, $month, $day) {
|
|
15
|
+
this.$year = $year;
|
|
16
|
+
this.$month = $month;
|
|
17
|
+
this.$day = $day;
|
|
17
18
|
}
|
|
18
19
|
static create(year, month, day) {
|
|
19
20
|
return new LocalDate(year, month, day);
|
|
@@ -117,9 +118,34 @@ class LocalDate {
|
|
|
117
118
|
static rangeInclString(minIncl, maxIncl, step = 1, stepUnit = 'day') {
|
|
118
119
|
return LocalDate.range(minIncl, LocalDate.of(maxIncl).add(1, stepUnit), step, stepUnit).map(ld => ld.toString());
|
|
119
120
|
}
|
|
121
|
+
get(unit) {
|
|
122
|
+
return unit === 'year' ? this.$year : unit === 'month' ? this.$month : this.$day;
|
|
123
|
+
}
|
|
124
|
+
set(unit, v, mutate = false) {
|
|
125
|
+
const t = mutate ? this : this.clone();
|
|
126
|
+
if (unit === 'year') {
|
|
127
|
+
t.$year = v;
|
|
128
|
+
}
|
|
129
|
+
else if (unit === 'month') {
|
|
130
|
+
t.$month = v;
|
|
131
|
+
}
|
|
132
|
+
else {
|
|
133
|
+
t.$day = v;
|
|
134
|
+
}
|
|
135
|
+
return t;
|
|
136
|
+
}
|
|
137
|
+
year(v) {
|
|
138
|
+
return v === undefined ? this.$year : this.set('year', v);
|
|
139
|
+
}
|
|
140
|
+
month(v) {
|
|
141
|
+
return v === undefined ? this.$month : this.set('month', v);
|
|
142
|
+
}
|
|
143
|
+
day(v) {
|
|
144
|
+
return v === undefined ? this.$day : this.set('day', v);
|
|
145
|
+
}
|
|
120
146
|
isSame(d) {
|
|
121
147
|
d = LocalDate.of(d);
|
|
122
|
-
return this
|
|
148
|
+
return this.$day === d.$day && this.$month === d.$month && this.$year === d.$year;
|
|
123
149
|
}
|
|
124
150
|
isBefore(d) {
|
|
125
151
|
return this.cmp(d) === -1;
|
|
@@ -149,17 +175,17 @@ class LocalDate {
|
|
|
149
175
|
*/
|
|
150
176
|
cmp(d) {
|
|
151
177
|
d = LocalDate.of(d);
|
|
152
|
-
if (this
|
|
178
|
+
if (this.$year < d.$year)
|
|
153
179
|
return -1;
|
|
154
|
-
if (this
|
|
180
|
+
if (this.$year > d.$year)
|
|
155
181
|
return 1;
|
|
156
|
-
if (this
|
|
182
|
+
if (this.$month < d.$month)
|
|
157
183
|
return -1;
|
|
158
|
-
if (this
|
|
184
|
+
if (this.$month > d.$month)
|
|
159
185
|
return 1;
|
|
160
|
-
if (this
|
|
186
|
+
if (this.$day < d.$day)
|
|
161
187
|
return -1;
|
|
162
|
-
if (this
|
|
188
|
+
if (this.$day > d.$day)
|
|
163
189
|
return 1;
|
|
164
190
|
return 0;
|
|
165
191
|
}
|
|
@@ -177,82 +203,82 @@ class LocalDate {
|
|
|
177
203
|
diff(d, unit) {
|
|
178
204
|
d = LocalDate.of(d);
|
|
179
205
|
if (unit === 'year') {
|
|
180
|
-
return this
|
|
206
|
+
return this.$year - d.$year;
|
|
181
207
|
}
|
|
182
208
|
if (unit === 'month') {
|
|
183
|
-
return (this
|
|
209
|
+
return (this.$year - d.$year) * 12 + (this.$month - d.$month);
|
|
184
210
|
}
|
|
185
211
|
// unit is 'day'
|
|
186
|
-
let days = this
|
|
187
|
-
if (d
|
|
188
|
-
for (let year = d
|
|
212
|
+
let days = this.$day - d.$day;
|
|
213
|
+
if (d.$year < this.$year) {
|
|
214
|
+
for (let year = d.$year; year < this.$year; year++) {
|
|
189
215
|
days += LocalDate.getYearLength(year);
|
|
190
216
|
}
|
|
191
217
|
}
|
|
192
|
-
else if (this
|
|
193
|
-
for (let year = this
|
|
218
|
+
else if (this.$year < d.$year) {
|
|
219
|
+
for (let year = this.$year; year < d.$year; year++) {
|
|
194
220
|
days -= LocalDate.getYearLength(year);
|
|
195
221
|
}
|
|
196
222
|
}
|
|
197
|
-
if (d
|
|
198
|
-
for (let month = d
|
|
199
|
-
days += LocalDate.getMonthLength(this
|
|
223
|
+
if (d.$month < this.$month) {
|
|
224
|
+
for (let month = d.$month; month < this.$month; month++) {
|
|
225
|
+
days += LocalDate.getMonthLength(this.$year, month);
|
|
200
226
|
}
|
|
201
227
|
}
|
|
202
|
-
else if (this
|
|
203
|
-
for (let month = this
|
|
204
|
-
days -= LocalDate.getMonthLength(d
|
|
228
|
+
else if (this.$month < d.$month) {
|
|
229
|
+
for (let month = this.$month; month < d.$month; month++) {
|
|
230
|
+
days -= LocalDate.getMonthLength(d.$year, month);
|
|
205
231
|
}
|
|
206
232
|
}
|
|
207
233
|
return days;
|
|
208
234
|
}
|
|
209
235
|
add(num, unit, mutate = false) {
|
|
210
|
-
let { day, month, year } = this;
|
|
236
|
+
let { $day, $month, $year } = this;
|
|
211
237
|
if (unit === 'day') {
|
|
212
|
-
day += num;
|
|
238
|
+
$day += num;
|
|
213
239
|
}
|
|
214
240
|
else if (unit === 'month') {
|
|
215
|
-
month += num;
|
|
241
|
+
$month += num;
|
|
216
242
|
}
|
|
217
243
|
else if (unit === 'year') {
|
|
218
|
-
year += num;
|
|
244
|
+
$year += num;
|
|
219
245
|
}
|
|
220
246
|
// check day overflow
|
|
221
|
-
let monLen = LocalDate.getMonthLength(year, month);
|
|
222
|
-
while (day > monLen) {
|
|
223
|
-
day -= monLen;
|
|
224
|
-
month += 1;
|
|
225
|
-
if (month > 12) {
|
|
226
|
-
year += 1;
|
|
227
|
-
month -= 12;
|
|
247
|
+
let monLen = LocalDate.getMonthLength($year, $month);
|
|
248
|
+
while ($day > monLen) {
|
|
249
|
+
$day -= monLen;
|
|
250
|
+
$month += 1;
|
|
251
|
+
if ($month > 12) {
|
|
252
|
+
$year += 1;
|
|
253
|
+
$month -= 12;
|
|
228
254
|
}
|
|
229
|
-
monLen = LocalDate.getMonthLength(year, month);
|
|
255
|
+
monLen = LocalDate.getMonthLength($year, $month);
|
|
230
256
|
}
|
|
231
|
-
while (day < 1) {
|
|
232
|
-
day += monLen;
|
|
233
|
-
month -= 1;
|
|
234
|
-
if (month < 1) {
|
|
235
|
-
year -= 1;
|
|
236
|
-
month += 12;
|
|
257
|
+
while ($day < 1) {
|
|
258
|
+
$day += monLen;
|
|
259
|
+
$month -= 1;
|
|
260
|
+
if ($month < 1) {
|
|
261
|
+
$year -= 1;
|
|
262
|
+
$month += 12;
|
|
237
263
|
}
|
|
238
|
-
monLen = LocalDate.getMonthLength(year, month);
|
|
264
|
+
monLen = LocalDate.getMonthLength($year, $month);
|
|
239
265
|
}
|
|
240
266
|
// check month overflow
|
|
241
|
-
while (month > 12) {
|
|
242
|
-
year += 1;
|
|
243
|
-
month -= 12;
|
|
267
|
+
while ($month > 12) {
|
|
268
|
+
$year += 1;
|
|
269
|
+
$month -= 12;
|
|
244
270
|
}
|
|
245
|
-
while (month < 1) {
|
|
246
|
-
year -= 1;
|
|
247
|
-
month += 12;
|
|
271
|
+
while ($month < 1) {
|
|
272
|
+
$year -= 1;
|
|
273
|
+
$month += 12;
|
|
248
274
|
}
|
|
249
275
|
if (mutate) {
|
|
250
|
-
this
|
|
251
|
-
this
|
|
252
|
-
this
|
|
276
|
+
this.$year = $year;
|
|
277
|
+
this.$month = $month;
|
|
278
|
+
this.$day = $day;
|
|
253
279
|
return this;
|
|
254
280
|
}
|
|
255
|
-
return new LocalDate(year, month, day);
|
|
281
|
+
return new LocalDate($year, $month, $day);
|
|
256
282
|
}
|
|
257
283
|
subtract(num, unit, mutate = false) {
|
|
258
284
|
return this.add(-num, unit, mutate);
|
|
@@ -261,17 +287,17 @@ class LocalDate {
|
|
|
261
287
|
if (unit === 'day')
|
|
262
288
|
return this;
|
|
263
289
|
if (unit === 'month')
|
|
264
|
-
return LocalDate.create(this
|
|
290
|
+
return LocalDate.create(this.$year, this.$month, 1);
|
|
265
291
|
// year
|
|
266
|
-
return LocalDate.create(this
|
|
292
|
+
return LocalDate.create(this.$year, 1, 1);
|
|
267
293
|
}
|
|
268
294
|
endOf(unit) {
|
|
269
295
|
if (unit === 'day')
|
|
270
296
|
return this;
|
|
271
297
|
if (unit === 'month')
|
|
272
|
-
return LocalDate.create(this
|
|
298
|
+
return LocalDate.create(this.$year, this.$month, LocalDate.getMonthLength(this.$year, this.$month));
|
|
273
299
|
// year
|
|
274
|
-
return LocalDate.create(this
|
|
300
|
+
return LocalDate.create(this.$year, 12, 31);
|
|
275
301
|
}
|
|
276
302
|
static getYearLength(year) {
|
|
277
303
|
return this.isLeapYear(year) ? 366 : 365;
|
|
@@ -289,7 +315,7 @@ class LocalDate {
|
|
|
289
315
|
return year % 400 === 0;
|
|
290
316
|
}
|
|
291
317
|
clone() {
|
|
292
|
-
return new LocalDate(this
|
|
318
|
+
return new LocalDate(this.$year, this.$month, this.$day);
|
|
293
319
|
}
|
|
294
320
|
/**
|
|
295
321
|
* Converts LocalDate into instance of Date.
|
|
@@ -298,7 +324,7 @@ class LocalDate {
|
|
|
298
324
|
* Timezone will match local timezone.
|
|
299
325
|
*/
|
|
300
326
|
toDate() {
|
|
301
|
-
return new Date(this
|
|
327
|
+
return new Date(this.$year, this.$month - 1, this.$day);
|
|
302
328
|
}
|
|
303
329
|
toLocalTime() {
|
|
304
330
|
return localTime_1.LocalTime.of(this.toDate());
|
|
@@ -308,16 +334,16 @@ class LocalDate {
|
|
|
308
334
|
}
|
|
309
335
|
toString() {
|
|
310
336
|
return [
|
|
311
|
-
String(this
|
|
312
|
-
String(this
|
|
313
|
-
String(this
|
|
337
|
+
String(this.$year).padStart(4, '0'),
|
|
338
|
+
String(this.$month).padStart(2, '0'),
|
|
339
|
+
String(this.$day).padStart(2, '0'),
|
|
314
340
|
].join('-');
|
|
315
341
|
}
|
|
316
342
|
toStringCompact() {
|
|
317
343
|
return [
|
|
318
|
-
String(this
|
|
319
|
-
String(this
|
|
320
|
-
String(this
|
|
344
|
+
String(this.$year).padStart(4, '0'),
|
|
345
|
+
String(this.$month).padStart(2, '0'),
|
|
346
|
+
String(this.$day).padStart(2, '0'),
|
|
321
347
|
].join('');
|
|
322
348
|
}
|
|
323
349
|
// May be not optimal, as LocalTime better suits it
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { IsoDateString, IsoDateTimeString, UnixTimestampNumber } from '../types';
|
|
2
2
|
import { Inclusiveness, LocalDate } from './localDate';
|
|
3
3
|
export declare type LocalTimeUnit = 'year' | 'month' | 'day' | 'hour' | 'minute' | 'second';
|
|
4
|
-
export declare type LocalTimeConfig = LocalTime | Date |
|
|
4
|
+
export declare type LocalTimeConfig = LocalTime | Date | IsoDateTimeString | UnixTimestampNumber;
|
|
5
5
|
export interface LocalTimeComponents {
|
|
6
6
|
year: number;
|
|
7
7
|
month: number;
|
|
@@ -40,8 +40,8 @@ export declare class LocalTime {
|
|
|
40
40
|
year(v: number): LocalTime;
|
|
41
41
|
month(): number;
|
|
42
42
|
month(v: number): LocalTime;
|
|
43
|
-
|
|
44
|
-
|
|
43
|
+
day(): number;
|
|
44
|
+
day(v: number): LocalTime;
|
|
45
45
|
hour(): number;
|
|
46
46
|
hour(v: number): LocalTime;
|
|
47
47
|
minute(): number;
|
|
@@ -75,19 +75,19 @@ export declare class LocalTime {
|
|
|
75
75
|
fromNow(now?: LocalTimeConfig): string;
|
|
76
76
|
getDate(): Date;
|
|
77
77
|
clone(): LocalTime;
|
|
78
|
-
unix():
|
|
78
|
+
unix(): UnixTimestampNumber;
|
|
79
79
|
unixMillis(): number;
|
|
80
|
-
valueOf():
|
|
80
|
+
valueOf(): UnixTimestampNumber;
|
|
81
81
|
toLocalDate(): LocalDate;
|
|
82
|
-
toPretty(seconds?: boolean):
|
|
82
|
+
toPretty(seconds?: boolean): IsoDateTimeString;
|
|
83
83
|
/**
|
|
84
84
|
* Returns e.g: `1984-06-21T17:56:21`, only the date part of DateTime
|
|
85
85
|
*/
|
|
86
|
-
toISODateTime():
|
|
86
|
+
toISODateTime(): IsoDateTimeString;
|
|
87
87
|
/**
|
|
88
88
|
* Returns e.g: `1984-06-21`, only the date part of DateTime
|
|
89
89
|
*/
|
|
90
|
-
toISODate():
|
|
90
|
+
toISODate(): IsoDateString;
|
|
91
91
|
/**
|
|
92
92
|
* Returns e.g: `17:03:15` (or `17:03` with seconds=false)
|
|
93
93
|
*/
|
|
@@ -97,7 +97,7 @@ export declare class LocalTime {
|
|
|
97
97
|
*/
|
|
98
98
|
toStringCompact(seconds?: boolean): string;
|
|
99
99
|
toString(): string;
|
|
100
|
-
toJSON():
|
|
100
|
+
toJSON(): UnixTimestampNumber;
|
|
101
101
|
}
|
|
102
102
|
/**
|
|
103
103
|
* Shortcut wrapper around `LocalDate.parse` / `LocalDate.today`
|
package/dist/index.d.ts
CHANGED
|
@@ -51,7 +51,7 @@ export * from './string/string.util';
|
|
|
51
51
|
import { JsonStringifyFunction, StringifyAnyOptions, _stringifyAny } from './string/stringifyAny';
|
|
52
52
|
export * from './time/time.util';
|
|
53
53
|
import { Class, ConditionalExcept, ConditionalPick, Merge, Promisable, ReadonlyDeep, Simplify } from './typeFest';
|
|
54
|
-
import { AsyncMapper, AsyncPredicate, BaseDBEntity, CreatedUpdated, CreatedUpdatedId, ObjectWithId, AnyObjectWithId, Saved, Unsaved, BatchResult, InstanceId, IsoDate,
|
|
54
|
+
import { AsyncMapper, AsyncPredicate, BaseDBEntity, CreatedUpdated, CreatedUpdatedId, ObjectWithId, AnyObjectWithId, Saved, Unsaved, BatchResult, InstanceId, IsoDate, IsoDateString, IsoDateTimeString, KeyValueTuple, Mapper, ObjectMapper, ObjectPredicate, Predicate, PromiseMap, AnyObject, AnyFunction, Reviver, SavedDBEntity, StringMap, UnixTimestampNumber, UnixTimestamp, Integer, ValueOf, ValuesOf, AbortableMapper, AbortableAsyncPredicate, AbortableAsyncMapper, AbortablePredicate, END, SKIP, _noop, _objectKeys, _passNothingPredicate, _passthroughMapper, _passthroughPredicate, _passUndefinedMapper, _stringMapEntries, _stringMapValues } from './types';
|
|
55
55
|
export * from './unit/size.util';
|
|
56
56
|
import { is } from './vendor/is';
|
|
57
57
|
import { CommonLogLevel, CommonLogFunction, CommonLogger, commonLoggerMinLevel, commonLoggerNoop, commonLogLevelNumber, commonLoggerPipe, commonLoggerPrefix, CommonLogWithLevelFunction, commonLoggerCreate } from './log/commonLogger';
|
|
@@ -66,5 +66,5 @@ export * from './datetime/dateInterval';
|
|
|
66
66
|
import { LocalDateConfig, LocalDateUnit, Inclusiveness } from './datetime/localDate';
|
|
67
67
|
import { LocalTimeConfig, LocalTimeUnit, LocalTimeComponents } from './datetime/localTime';
|
|
68
68
|
import { DateIntervalConfig, DateIntervalString } from './datetime/dateInterval';
|
|
69
|
-
export type { DateIntervalConfig, DateIntervalString, LocalDateConfig, LocalDateUnit, Inclusiveness, LocalTimeConfig, LocalTimeUnit, LocalTimeComponents, AbortableMapper, AbortablePredicate, AbortableAsyncPredicate, AbortableAsyncMapper, PQueueCfg, MemoCache, AsyncMemoCache, PromiseDecoratorCfg, PromiseDecoratorResp, ErrorData, ErrorObject, HttpErrorData, HttpErrorResponse, Admin401ErrorData, Admin403ErrorData, StringMap, PromiseMap, AnyObject, AnyFunction, ValuesOf, ValueOf, KeyValueTuple, ObjectMapper, ObjectPredicate, InstanceId, IsoDate,
|
|
69
|
+
export type { DateIntervalConfig, DateIntervalString, LocalDateConfig, LocalDateUnit, Inclusiveness, LocalTimeConfig, LocalTimeUnit, LocalTimeComponents, AbortableMapper, AbortablePredicate, AbortableAsyncPredicate, AbortableAsyncMapper, PQueueCfg, MemoCache, AsyncMemoCache, PromiseDecoratorCfg, PromiseDecoratorResp, ErrorData, ErrorObject, HttpErrorData, HttpErrorResponse, Admin401ErrorData, Admin403ErrorData, StringMap, PromiseMap, AnyObject, AnyFunction, ValuesOf, ValueOf, KeyValueTuple, ObjectMapper, ObjectPredicate, InstanceId, IsoDate, IsoDateString, IsoDateTimeString, Reviver, PMapOptions, Mapper, AsyncMapper, Predicate, AsyncPredicate, BatchResult, DeferredPromise, PRetryOptions, PTimeoutOptions, TryCatchOptions, StringifyAnyOptions, JsonStringifyFunction, Merge, ReadonlyDeep, Promisable, Simplify, ConditionalPick, ConditionalExcept, Class, UnixTimestampNumber, UnixTimestamp, Integer, BaseDBEntity, SavedDBEntity, Saved, Unsaved, CreatedUpdated, CreatedUpdatedId, ObjectWithId, AnyObjectWithId, JsonSchema, JsonSchemaAny, JsonSchemaOneOf, JsonSchemaAllOf, JsonSchemaAnyOf, JsonSchemaNot, JsonSchemaRef, JsonSchemaConst, JsonSchemaEnum, JsonSchemaString, JsonSchemaNumber, JsonSchemaBoolean, JsonSchemaNull, JsonSchemaRootObject, JsonSchemaObject, JsonSchemaArray, JsonSchemaTuple, JsonSchemaBuilder, CommonLogLevel, CommonLogWithLevelFunction, CommonLogFunction, CommonLogger, };
|
|
70
70
|
export { is, _createPromiseDecorator, _stringMapValues, _stringMapEntries, _objectKeys, pMap, _passthroughMapper, _passUndefinedMapper, _passthroughPredicate, _passNothingPredicate, _noop, ErrorMode, pDefer, AggregatedError, pRetry, pRetryFn, pTimeout, pTimeoutFn, _tryCatch, _TryCatch, _stringifyAny, jsonSchema, JsonSchemaAnyBuilder, commonLoggerMinLevel, commonLoggerNoop, commonLogLevelNumber, commonLoggerPipe, commonLoggerPrefix, commonLoggerCreate, PQueue, END, SKIP, };
|
package/dist/types.d.ts
CHANGED
|
@@ -111,18 +111,26 @@ export interface InstanceId {
|
|
|
111
111
|
*
|
|
112
112
|
* @example '2019-06-21'
|
|
113
113
|
*/
|
|
114
|
+
export declare type IsoDateString = string;
|
|
115
|
+
/**
|
|
116
|
+
* @deprecated use IsoDateString
|
|
117
|
+
*/
|
|
114
118
|
export declare type IsoDate = string;
|
|
115
119
|
/**
|
|
116
120
|
* Interface explicitly states that the value is an ISO DateTime string (with time).
|
|
117
121
|
*
|
|
118
122
|
* @example '2019-06-21T05:21:73Z'
|
|
119
123
|
*/
|
|
120
|
-
export declare type
|
|
124
|
+
export declare type IsoDateTimeString = string;
|
|
121
125
|
/**
|
|
122
126
|
* Interface explicitly states that the value is a Unix timestamp (in seconds).
|
|
123
127
|
*
|
|
124
128
|
* @example 1628945450
|
|
125
129
|
*/
|
|
130
|
+
export declare type UnixTimestampNumber = number;
|
|
131
|
+
/**
|
|
132
|
+
* @deprecated use UnixTimestampNumber
|
|
133
|
+
*/
|
|
126
134
|
export declare type UnixTimestamp = number;
|
|
127
135
|
/**
|
|
128
136
|
* Same as `number`, but with semantic meaning that it's an Integer.
|
|
@@ -136,11 +144,11 @@ export interface SavedDBEntity<ID = string> {
|
|
|
136
144
|
/**
|
|
137
145
|
* unixTimestamp of when the entity was first created (in the DB).
|
|
138
146
|
*/
|
|
139
|
-
created:
|
|
147
|
+
created: UnixTimestampNumber;
|
|
140
148
|
/**
|
|
141
149
|
* unixTimestamp of when the entity was last updated (in the DB).
|
|
142
150
|
*/
|
|
143
|
-
updated:
|
|
151
|
+
updated: UnixTimestampNumber;
|
|
144
152
|
}
|
|
145
153
|
/**
|
|
146
154
|
* Base interface for any Entity that can be saved to DB.
|
|
@@ -3,14 +3,15 @@ import { Sequence } from '../seq/seq';
|
|
|
3
3
|
import { END } from '../types';
|
|
4
4
|
import { LocalTime } from './localTime';
|
|
5
5
|
const m31 = new Set([1, 3, 5, 7, 8, 10, 12]);
|
|
6
|
+
/* eslint-disable no-dupe-class-members */
|
|
6
7
|
/**
|
|
7
8
|
* @experimental
|
|
8
9
|
*/
|
|
9
10
|
export class LocalDate {
|
|
10
|
-
constructor(year, month, day) {
|
|
11
|
-
this
|
|
12
|
-
this
|
|
13
|
-
this
|
|
11
|
+
constructor($year, $month, $day) {
|
|
12
|
+
this.$year = $year;
|
|
13
|
+
this.$month = $month;
|
|
14
|
+
this.$day = $day;
|
|
14
15
|
}
|
|
15
16
|
static create(year, month, day) {
|
|
16
17
|
return new LocalDate(year, month, day);
|
|
@@ -114,9 +115,34 @@ export class LocalDate {
|
|
|
114
115
|
static rangeInclString(minIncl, maxIncl, step = 1, stepUnit = 'day') {
|
|
115
116
|
return LocalDate.range(minIncl, LocalDate.of(maxIncl).add(1, stepUnit), step, stepUnit).map(ld => ld.toString());
|
|
116
117
|
}
|
|
118
|
+
get(unit) {
|
|
119
|
+
return unit === 'year' ? this.$year : unit === 'month' ? this.$month : this.$day;
|
|
120
|
+
}
|
|
121
|
+
set(unit, v, mutate = false) {
|
|
122
|
+
const t = mutate ? this : this.clone();
|
|
123
|
+
if (unit === 'year') {
|
|
124
|
+
t.$year = v;
|
|
125
|
+
}
|
|
126
|
+
else if (unit === 'month') {
|
|
127
|
+
t.$month = v;
|
|
128
|
+
}
|
|
129
|
+
else {
|
|
130
|
+
t.$day = v;
|
|
131
|
+
}
|
|
132
|
+
return t;
|
|
133
|
+
}
|
|
134
|
+
year(v) {
|
|
135
|
+
return v === undefined ? this.$year : this.set('year', v);
|
|
136
|
+
}
|
|
137
|
+
month(v) {
|
|
138
|
+
return v === undefined ? this.$month : this.set('month', v);
|
|
139
|
+
}
|
|
140
|
+
day(v) {
|
|
141
|
+
return v === undefined ? this.$day : this.set('day', v);
|
|
142
|
+
}
|
|
117
143
|
isSame(d) {
|
|
118
144
|
d = LocalDate.of(d);
|
|
119
|
-
return this
|
|
145
|
+
return this.$day === d.$day && this.$month === d.$month && this.$year === d.$year;
|
|
120
146
|
}
|
|
121
147
|
isBefore(d) {
|
|
122
148
|
return this.cmp(d) === -1;
|
|
@@ -146,17 +172,17 @@ export class LocalDate {
|
|
|
146
172
|
*/
|
|
147
173
|
cmp(d) {
|
|
148
174
|
d = LocalDate.of(d);
|
|
149
|
-
if (this
|
|
175
|
+
if (this.$year < d.$year)
|
|
150
176
|
return -1;
|
|
151
|
-
if (this
|
|
177
|
+
if (this.$year > d.$year)
|
|
152
178
|
return 1;
|
|
153
|
-
if (this
|
|
179
|
+
if (this.$month < d.$month)
|
|
154
180
|
return -1;
|
|
155
|
-
if (this
|
|
181
|
+
if (this.$month > d.$month)
|
|
156
182
|
return 1;
|
|
157
|
-
if (this
|
|
183
|
+
if (this.$day < d.$day)
|
|
158
184
|
return -1;
|
|
159
|
-
if (this
|
|
185
|
+
if (this.$day > d.$day)
|
|
160
186
|
return 1;
|
|
161
187
|
return 0;
|
|
162
188
|
}
|
|
@@ -174,82 +200,82 @@ export class LocalDate {
|
|
|
174
200
|
diff(d, unit) {
|
|
175
201
|
d = LocalDate.of(d);
|
|
176
202
|
if (unit === 'year') {
|
|
177
|
-
return this
|
|
203
|
+
return this.$year - d.$year;
|
|
178
204
|
}
|
|
179
205
|
if (unit === 'month') {
|
|
180
|
-
return (this
|
|
206
|
+
return (this.$year - d.$year) * 12 + (this.$month - d.$month);
|
|
181
207
|
}
|
|
182
208
|
// unit is 'day'
|
|
183
|
-
let days = this
|
|
184
|
-
if (d
|
|
185
|
-
for (let year = d
|
|
209
|
+
let days = this.$day - d.$day;
|
|
210
|
+
if (d.$year < this.$year) {
|
|
211
|
+
for (let year = d.$year; year < this.$year; year++) {
|
|
186
212
|
days += LocalDate.getYearLength(year);
|
|
187
213
|
}
|
|
188
214
|
}
|
|
189
|
-
else if (this
|
|
190
|
-
for (let year = this
|
|
215
|
+
else if (this.$year < d.$year) {
|
|
216
|
+
for (let year = this.$year; year < d.$year; year++) {
|
|
191
217
|
days -= LocalDate.getYearLength(year);
|
|
192
218
|
}
|
|
193
219
|
}
|
|
194
|
-
if (d
|
|
195
|
-
for (let month = d
|
|
196
|
-
days += LocalDate.getMonthLength(this
|
|
220
|
+
if (d.$month < this.$month) {
|
|
221
|
+
for (let month = d.$month; month < this.$month; month++) {
|
|
222
|
+
days += LocalDate.getMonthLength(this.$year, month);
|
|
197
223
|
}
|
|
198
224
|
}
|
|
199
|
-
else if (this
|
|
200
|
-
for (let month = this
|
|
201
|
-
days -= LocalDate.getMonthLength(d
|
|
225
|
+
else if (this.$month < d.$month) {
|
|
226
|
+
for (let month = this.$month; month < d.$month; month++) {
|
|
227
|
+
days -= LocalDate.getMonthLength(d.$year, month);
|
|
202
228
|
}
|
|
203
229
|
}
|
|
204
230
|
return days;
|
|
205
231
|
}
|
|
206
232
|
add(num, unit, mutate = false) {
|
|
207
|
-
let { day, month, year } = this;
|
|
233
|
+
let { $day, $month, $year } = this;
|
|
208
234
|
if (unit === 'day') {
|
|
209
|
-
day += num;
|
|
235
|
+
$day += num;
|
|
210
236
|
}
|
|
211
237
|
else if (unit === 'month') {
|
|
212
|
-
month += num;
|
|
238
|
+
$month += num;
|
|
213
239
|
}
|
|
214
240
|
else if (unit === 'year') {
|
|
215
|
-
year += num;
|
|
241
|
+
$year += num;
|
|
216
242
|
}
|
|
217
243
|
// check day overflow
|
|
218
|
-
let monLen = LocalDate.getMonthLength(year, month);
|
|
219
|
-
while (day > monLen) {
|
|
220
|
-
day -= monLen;
|
|
221
|
-
month += 1;
|
|
222
|
-
if (month > 12) {
|
|
223
|
-
year += 1;
|
|
224
|
-
month -= 12;
|
|
244
|
+
let monLen = LocalDate.getMonthLength($year, $month);
|
|
245
|
+
while ($day > monLen) {
|
|
246
|
+
$day -= monLen;
|
|
247
|
+
$month += 1;
|
|
248
|
+
if ($month > 12) {
|
|
249
|
+
$year += 1;
|
|
250
|
+
$month -= 12;
|
|
225
251
|
}
|
|
226
|
-
monLen = LocalDate.getMonthLength(year, month);
|
|
252
|
+
monLen = LocalDate.getMonthLength($year, $month);
|
|
227
253
|
}
|
|
228
|
-
while (day < 1) {
|
|
229
|
-
day += monLen;
|
|
230
|
-
month -= 1;
|
|
231
|
-
if (month < 1) {
|
|
232
|
-
year -= 1;
|
|
233
|
-
month += 12;
|
|
254
|
+
while ($day < 1) {
|
|
255
|
+
$day += monLen;
|
|
256
|
+
$month -= 1;
|
|
257
|
+
if ($month < 1) {
|
|
258
|
+
$year -= 1;
|
|
259
|
+
$month += 12;
|
|
234
260
|
}
|
|
235
|
-
monLen = LocalDate.getMonthLength(year, month);
|
|
261
|
+
monLen = LocalDate.getMonthLength($year, $month);
|
|
236
262
|
}
|
|
237
263
|
// check month overflow
|
|
238
|
-
while (month > 12) {
|
|
239
|
-
year += 1;
|
|
240
|
-
month -= 12;
|
|
264
|
+
while ($month > 12) {
|
|
265
|
+
$year += 1;
|
|
266
|
+
$month -= 12;
|
|
241
267
|
}
|
|
242
|
-
while (month < 1) {
|
|
243
|
-
year -= 1;
|
|
244
|
-
month += 12;
|
|
268
|
+
while ($month < 1) {
|
|
269
|
+
$year -= 1;
|
|
270
|
+
$month += 12;
|
|
245
271
|
}
|
|
246
272
|
if (mutate) {
|
|
247
|
-
this
|
|
248
|
-
this
|
|
249
|
-
this
|
|
273
|
+
this.$year = $year;
|
|
274
|
+
this.$month = $month;
|
|
275
|
+
this.$day = $day;
|
|
250
276
|
return this;
|
|
251
277
|
}
|
|
252
|
-
return new LocalDate(year, month, day);
|
|
278
|
+
return new LocalDate($year, $month, $day);
|
|
253
279
|
}
|
|
254
280
|
subtract(num, unit, mutate = false) {
|
|
255
281
|
return this.add(-num, unit, mutate);
|
|
@@ -258,17 +284,17 @@ export class LocalDate {
|
|
|
258
284
|
if (unit === 'day')
|
|
259
285
|
return this;
|
|
260
286
|
if (unit === 'month')
|
|
261
|
-
return LocalDate.create(this
|
|
287
|
+
return LocalDate.create(this.$year, this.$month, 1);
|
|
262
288
|
// year
|
|
263
|
-
return LocalDate.create(this
|
|
289
|
+
return LocalDate.create(this.$year, 1, 1);
|
|
264
290
|
}
|
|
265
291
|
endOf(unit) {
|
|
266
292
|
if (unit === 'day')
|
|
267
293
|
return this;
|
|
268
294
|
if (unit === 'month')
|
|
269
|
-
return LocalDate.create(this
|
|
295
|
+
return LocalDate.create(this.$year, this.$month, LocalDate.getMonthLength(this.$year, this.$month));
|
|
270
296
|
// year
|
|
271
|
-
return LocalDate.create(this
|
|
297
|
+
return LocalDate.create(this.$year, 12, 31);
|
|
272
298
|
}
|
|
273
299
|
static getYearLength(year) {
|
|
274
300
|
return this.isLeapYear(year) ? 366 : 365;
|
|
@@ -286,7 +312,7 @@ export class LocalDate {
|
|
|
286
312
|
return year % 400 === 0;
|
|
287
313
|
}
|
|
288
314
|
clone() {
|
|
289
|
-
return new LocalDate(this
|
|
315
|
+
return new LocalDate(this.$year, this.$month, this.$day);
|
|
290
316
|
}
|
|
291
317
|
/**
|
|
292
318
|
* Converts LocalDate into instance of Date.
|
|
@@ -295,7 +321,7 @@ export class LocalDate {
|
|
|
295
321
|
* Timezone will match local timezone.
|
|
296
322
|
*/
|
|
297
323
|
toDate() {
|
|
298
|
-
return new Date(this
|
|
324
|
+
return new Date(this.$year, this.$month - 1, this.$day);
|
|
299
325
|
}
|
|
300
326
|
toLocalTime() {
|
|
301
327
|
return LocalTime.of(this.toDate());
|
|
@@ -305,16 +331,16 @@ export class LocalDate {
|
|
|
305
331
|
}
|
|
306
332
|
toString() {
|
|
307
333
|
return [
|
|
308
|
-
String(this
|
|
309
|
-
String(this
|
|
310
|
-
String(this
|
|
334
|
+
String(this.$year).padStart(4, '0'),
|
|
335
|
+
String(this.$month).padStart(2, '0'),
|
|
336
|
+
String(this.$day).padStart(2, '0'),
|
|
311
337
|
].join('-');
|
|
312
338
|
}
|
|
313
339
|
toStringCompact() {
|
|
314
340
|
return [
|
|
315
|
-
String(this
|
|
316
|
-
String(this
|
|
317
|
-
String(this
|
|
341
|
+
String(this.$year).padStart(4, '0'),
|
|
342
|
+
String(this.$month).padStart(2, '0'),
|
|
343
|
+
String(this.$day).padStart(2, '0'),
|
|
318
344
|
].join('');
|
|
319
345
|
}
|
|
320
346
|
// May be not optimal, as LocalTime better suits it
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { _assert } from '../error/assert'
|
|
2
2
|
import { Sequence } from '../seq/seq'
|
|
3
|
-
import { END,
|
|
3
|
+
import { END, IsoDateString, UnixTimestampNumber } from '../types'
|
|
4
4
|
import { LocalTime } from './localTime'
|
|
5
5
|
|
|
6
6
|
export type LocalDateUnit = 'year' | 'month' | 'day'
|
|
@@ -10,11 +10,13 @@ const m31 = new Set<number>([1, 3, 5, 7, 8, 10, 12])
|
|
|
10
10
|
|
|
11
11
|
export type LocalDateConfig = LocalDate | string
|
|
12
12
|
|
|
13
|
+
/* eslint-disable no-dupe-class-members */
|
|
14
|
+
|
|
13
15
|
/**
|
|
14
16
|
* @experimental
|
|
15
17
|
*/
|
|
16
18
|
export class LocalDate {
|
|
17
|
-
private constructor(
|
|
19
|
+
private constructor(private $year: number, private $month: number, private $day: number) {}
|
|
18
20
|
|
|
19
21
|
static create(year: number, month: number, day: number): LocalDate {
|
|
20
22
|
return new LocalDate(year, month, day)
|
|
@@ -151,7 +153,7 @@ export class LocalDate {
|
|
|
151
153
|
maxExcl: LocalDateConfig,
|
|
152
154
|
step = 1,
|
|
153
155
|
stepUnit: LocalDateUnit = 'day',
|
|
154
|
-
):
|
|
156
|
+
): IsoDateString[] {
|
|
155
157
|
return LocalDate.range(minIncl, maxExcl, step, stepUnit).map(ld => ld.toString())
|
|
156
158
|
}
|
|
157
159
|
|
|
@@ -169,15 +171,49 @@ export class LocalDate {
|
|
|
169
171
|
maxIncl: LocalDateConfig,
|
|
170
172
|
step = 1,
|
|
171
173
|
stepUnit: LocalDateUnit = 'day',
|
|
172
|
-
):
|
|
174
|
+
): IsoDateString[] {
|
|
173
175
|
return LocalDate.range(minIncl, LocalDate.of(maxIncl).add(1, stepUnit), step, stepUnit).map(
|
|
174
176
|
ld => ld.toString(),
|
|
175
177
|
)
|
|
176
178
|
}
|
|
177
179
|
|
|
180
|
+
get(unit: LocalDateUnit): number {
|
|
181
|
+
return unit === 'year' ? this.$year : unit === 'month' ? this.$month : this.$day
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
set(unit: LocalDateUnit, v: number, mutate = false): LocalDate {
|
|
185
|
+
const t = mutate ? this : this.clone()
|
|
186
|
+
|
|
187
|
+
if (unit === 'year') {
|
|
188
|
+
t.$year = v
|
|
189
|
+
} else if (unit === 'month') {
|
|
190
|
+
t.$month = v
|
|
191
|
+
} else {
|
|
192
|
+
t.$day = v
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
return t
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
year(): number
|
|
199
|
+
year(v: number): LocalDate
|
|
200
|
+
year(v?: number): number | LocalDate {
|
|
201
|
+
return v === undefined ? this.$year : this.set('year', v)
|
|
202
|
+
}
|
|
203
|
+
month(): number
|
|
204
|
+
month(v: number): LocalDate
|
|
205
|
+
month(v?: number): number | LocalDate {
|
|
206
|
+
return v === undefined ? this.$month : this.set('month', v)
|
|
207
|
+
}
|
|
208
|
+
day(): number
|
|
209
|
+
day(v: number): LocalDate
|
|
210
|
+
day(v?: number): number | LocalDate {
|
|
211
|
+
return v === undefined ? this.$day : this.set('day', v)
|
|
212
|
+
}
|
|
213
|
+
|
|
178
214
|
isSame(d: LocalDateConfig): boolean {
|
|
179
215
|
d = LocalDate.of(d)
|
|
180
|
-
return this
|
|
216
|
+
return this.$day === d.$day && this.$month === d.$month && this.$year === d.$year
|
|
181
217
|
}
|
|
182
218
|
|
|
183
219
|
isBefore(d: LocalDateConfig): boolean {
|
|
@@ -211,12 +247,12 @@ export class LocalDate {
|
|
|
211
247
|
*/
|
|
212
248
|
cmp(d: LocalDateConfig): -1 | 0 | 1 {
|
|
213
249
|
d = LocalDate.of(d)
|
|
214
|
-
if (this
|
|
215
|
-
if (this
|
|
216
|
-
if (this
|
|
217
|
-
if (this
|
|
218
|
-
if (this
|
|
219
|
-
if (this
|
|
250
|
+
if (this.$year < d.$year) return -1
|
|
251
|
+
if (this.$year > d.$year) return 1
|
|
252
|
+
if (this.$month < d.$month) return -1
|
|
253
|
+
if (this.$month > d.$month) return 1
|
|
254
|
+
if (this.$day < d.$day) return -1
|
|
255
|
+
if (this.$day > d.$day) return 1
|
|
220
256
|
return 0
|
|
221
257
|
}
|
|
222
258
|
|
|
@@ -236,33 +272,33 @@ export class LocalDate {
|
|
|
236
272
|
d = LocalDate.of(d)
|
|
237
273
|
|
|
238
274
|
if (unit === 'year') {
|
|
239
|
-
return this
|
|
275
|
+
return this.$year - d.$year
|
|
240
276
|
}
|
|
241
277
|
|
|
242
278
|
if (unit === 'month') {
|
|
243
|
-
return (this
|
|
279
|
+
return (this.$year - d.$year) * 12 + (this.$month - d.$month)
|
|
244
280
|
}
|
|
245
281
|
|
|
246
282
|
// unit is 'day'
|
|
247
|
-
let days = this
|
|
283
|
+
let days = this.$day - d.$day
|
|
248
284
|
|
|
249
|
-
if (d
|
|
250
|
-
for (let year = d
|
|
285
|
+
if (d.$year < this.$year) {
|
|
286
|
+
for (let year = d.$year; year < this.$year; year++) {
|
|
251
287
|
days += LocalDate.getYearLength(year)
|
|
252
288
|
}
|
|
253
|
-
} else if (this
|
|
254
|
-
for (let year = this
|
|
289
|
+
} else if (this.$year < d.$year) {
|
|
290
|
+
for (let year = this.$year; year < d.$year; year++) {
|
|
255
291
|
days -= LocalDate.getYearLength(year)
|
|
256
292
|
}
|
|
257
293
|
}
|
|
258
294
|
|
|
259
|
-
if (d
|
|
260
|
-
for (let month = d
|
|
261
|
-
days += LocalDate.getMonthLength(this
|
|
295
|
+
if (d.$month < this.$month) {
|
|
296
|
+
for (let month = d.$month; month < this.$month; month++) {
|
|
297
|
+
days += LocalDate.getMonthLength(this.$year, month)
|
|
262
298
|
}
|
|
263
|
-
} else if (this
|
|
264
|
-
for (let month = this
|
|
265
|
-
days -= LocalDate.getMonthLength(d
|
|
299
|
+
} else if (this.$month < d.$month) {
|
|
300
|
+
for (let month = this.$month; month < d.$month; month++) {
|
|
301
|
+
days -= LocalDate.getMonthLength(d.$year, month)
|
|
266
302
|
}
|
|
267
303
|
}
|
|
268
304
|
|
|
@@ -270,57 +306,57 @@ export class LocalDate {
|
|
|
270
306
|
}
|
|
271
307
|
|
|
272
308
|
add(num: number, unit: LocalDateUnit, mutate = false): LocalDate {
|
|
273
|
-
let { day, month, year } = this
|
|
309
|
+
let { $day, $month, $year } = this
|
|
274
310
|
|
|
275
311
|
if (unit === 'day') {
|
|
276
|
-
day += num
|
|
312
|
+
$day += num
|
|
277
313
|
} else if (unit === 'month') {
|
|
278
|
-
month += num
|
|
314
|
+
$month += num
|
|
279
315
|
} else if (unit === 'year') {
|
|
280
|
-
year += num
|
|
316
|
+
$year += num
|
|
281
317
|
}
|
|
282
318
|
|
|
283
319
|
// check day overflow
|
|
284
|
-
let monLen = LocalDate.getMonthLength(year, month)
|
|
285
|
-
while (day > monLen) {
|
|
286
|
-
day -= monLen
|
|
287
|
-
month += 1
|
|
288
|
-
if (month > 12) {
|
|
289
|
-
year += 1
|
|
290
|
-
month -= 12
|
|
320
|
+
let monLen = LocalDate.getMonthLength($year, $month)
|
|
321
|
+
while ($day > monLen) {
|
|
322
|
+
$day -= monLen
|
|
323
|
+
$month += 1
|
|
324
|
+
if ($month > 12) {
|
|
325
|
+
$year += 1
|
|
326
|
+
$month -= 12
|
|
291
327
|
}
|
|
292
328
|
|
|
293
|
-
monLen = LocalDate.getMonthLength(year, month)
|
|
329
|
+
monLen = LocalDate.getMonthLength($year, $month)
|
|
294
330
|
}
|
|
295
|
-
while (day < 1) {
|
|
296
|
-
day += monLen
|
|
297
|
-
month -= 1
|
|
298
|
-
if (month < 1) {
|
|
299
|
-
year -= 1
|
|
300
|
-
month += 12
|
|
331
|
+
while ($day < 1) {
|
|
332
|
+
$day += monLen
|
|
333
|
+
$month -= 1
|
|
334
|
+
if ($month < 1) {
|
|
335
|
+
$year -= 1
|
|
336
|
+
$month += 12
|
|
301
337
|
}
|
|
302
338
|
|
|
303
|
-
monLen = LocalDate.getMonthLength(year, month)
|
|
339
|
+
monLen = LocalDate.getMonthLength($year, $month)
|
|
304
340
|
}
|
|
305
341
|
|
|
306
342
|
// check month overflow
|
|
307
|
-
while (month > 12) {
|
|
308
|
-
year += 1
|
|
309
|
-
month -= 12
|
|
343
|
+
while ($month > 12) {
|
|
344
|
+
$year += 1
|
|
345
|
+
$month -= 12
|
|
310
346
|
}
|
|
311
|
-
while (month < 1) {
|
|
312
|
-
year -= 1
|
|
313
|
-
month += 12
|
|
347
|
+
while ($month < 1) {
|
|
348
|
+
$year -= 1
|
|
349
|
+
$month += 12
|
|
314
350
|
}
|
|
315
351
|
|
|
316
352
|
if (mutate) {
|
|
317
|
-
this
|
|
318
|
-
this
|
|
319
|
-
this
|
|
353
|
+
this.$year = $year
|
|
354
|
+
this.$month = $month
|
|
355
|
+
this.$day = $day
|
|
320
356
|
return this
|
|
321
357
|
}
|
|
322
358
|
|
|
323
|
-
return new LocalDate(year, month, day)
|
|
359
|
+
return new LocalDate($year, $month, $day)
|
|
324
360
|
}
|
|
325
361
|
|
|
326
362
|
subtract(num: number, unit: LocalDateUnit, mutate = false): LocalDate {
|
|
@@ -329,21 +365,21 @@ export class LocalDate {
|
|
|
329
365
|
|
|
330
366
|
startOf(unit: LocalDateUnit): LocalDate {
|
|
331
367
|
if (unit === 'day') return this
|
|
332
|
-
if (unit === 'month') return LocalDate.create(this
|
|
368
|
+
if (unit === 'month') return LocalDate.create(this.$year, this.$month, 1)
|
|
333
369
|
// year
|
|
334
|
-
return LocalDate.create(this
|
|
370
|
+
return LocalDate.create(this.$year, 1, 1)
|
|
335
371
|
}
|
|
336
372
|
|
|
337
373
|
endOf(unit: LocalDateUnit): LocalDate {
|
|
338
374
|
if (unit === 'day') return this
|
|
339
375
|
if (unit === 'month')
|
|
340
376
|
return LocalDate.create(
|
|
341
|
-
this
|
|
342
|
-
this
|
|
343
|
-
LocalDate.getMonthLength(this
|
|
377
|
+
this.$year,
|
|
378
|
+
this.$month,
|
|
379
|
+
LocalDate.getMonthLength(this.$year, this.$month),
|
|
344
380
|
)
|
|
345
381
|
// year
|
|
346
|
-
return LocalDate.create(this
|
|
382
|
+
return LocalDate.create(this.$year, 12, 31)
|
|
347
383
|
}
|
|
348
384
|
|
|
349
385
|
static getYearLength(year: number): number {
|
|
@@ -362,7 +398,7 @@ export class LocalDate {
|
|
|
362
398
|
}
|
|
363
399
|
|
|
364
400
|
clone(): LocalDate {
|
|
365
|
-
return new LocalDate(this
|
|
401
|
+
return new LocalDate(this.$year, this.$month, this.$day)
|
|
366
402
|
}
|
|
367
403
|
|
|
368
404
|
/**
|
|
@@ -372,35 +408,35 @@ export class LocalDate {
|
|
|
372
408
|
* Timezone will match local timezone.
|
|
373
409
|
*/
|
|
374
410
|
toDate(): Date {
|
|
375
|
-
return new Date(this
|
|
411
|
+
return new Date(this.$year, this.$month - 1, this.$day)
|
|
376
412
|
}
|
|
377
413
|
|
|
378
414
|
toLocalTime(): LocalTime {
|
|
379
415
|
return LocalTime.of(this.toDate())
|
|
380
416
|
}
|
|
381
417
|
|
|
382
|
-
toISODate():
|
|
418
|
+
toISODate(): IsoDateString {
|
|
383
419
|
return this.toString()
|
|
384
420
|
}
|
|
385
421
|
|
|
386
|
-
toString():
|
|
422
|
+
toString(): IsoDateString {
|
|
387
423
|
return [
|
|
388
|
-
String(this
|
|
389
|
-
String(this
|
|
390
|
-
String(this
|
|
424
|
+
String(this.$year).padStart(4, '0'),
|
|
425
|
+
String(this.$month).padStart(2, '0'),
|
|
426
|
+
String(this.$day).padStart(2, '0'),
|
|
391
427
|
].join('-')
|
|
392
428
|
}
|
|
393
429
|
|
|
394
430
|
toStringCompact(): string {
|
|
395
431
|
return [
|
|
396
|
-
String(this
|
|
397
|
-
String(this
|
|
398
|
-
String(this
|
|
432
|
+
String(this.$year).padStart(4, '0'),
|
|
433
|
+
String(this.$month).padStart(2, '0'),
|
|
434
|
+
String(this.$day).padStart(2, '0'),
|
|
399
435
|
].join('')
|
|
400
436
|
}
|
|
401
437
|
|
|
402
438
|
// May be not optimal, as LocalTime better suits it
|
|
403
|
-
unix():
|
|
439
|
+
unix(): UnixTimestampNumber {
|
|
404
440
|
return Math.floor(this.toDate().valueOf() / 1000)
|
|
405
441
|
}
|
|
406
442
|
|
|
@@ -408,7 +444,7 @@ export class LocalDate {
|
|
|
408
444
|
return this.toDate().valueOf()
|
|
409
445
|
}
|
|
410
446
|
|
|
411
|
-
toJSON():
|
|
447
|
+
toJSON(): IsoDateString {
|
|
412
448
|
return this.toString()
|
|
413
449
|
}
|
|
414
450
|
}
|
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
import { _assert } from '../error/assert'
|
|
2
2
|
import { _ms } from '../time/time.util'
|
|
3
|
-
import {
|
|
3
|
+
import { IsoDateString, IsoDateTimeString, UnixTimestampNumber } from '../types'
|
|
4
4
|
import { Inclusiveness, LocalDate } from './localDate'
|
|
5
5
|
|
|
6
6
|
export type LocalTimeUnit = 'year' | 'month' | 'day' | 'hour' | 'minute' | 'second'
|
|
7
7
|
|
|
8
|
-
export type LocalTimeConfig = LocalTime | Date |
|
|
8
|
+
export type LocalTimeConfig = LocalTime | Date | IsoDateTimeString | UnixTimestampNumber
|
|
9
9
|
|
|
10
10
|
export interface LocalTimeComponents {
|
|
11
11
|
year: number
|
|
@@ -144,9 +144,9 @@ export class LocalTime {
|
|
|
144
144
|
month(v?: number): number | LocalTime {
|
|
145
145
|
return v === undefined ? this.get('month') : this.set('month', v)
|
|
146
146
|
}
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
147
|
+
day(): number
|
|
148
|
+
day(v: number): LocalTime
|
|
149
|
+
day(v?: number): number | LocalTime {
|
|
150
150
|
return v === undefined ? this.get('day') : this.set('day', v)
|
|
151
151
|
}
|
|
152
152
|
hour(): number
|
|
@@ -375,7 +375,7 @@ export class LocalTime {
|
|
|
375
375
|
return new LocalTime(new Date(this.$date), this.utcMode)
|
|
376
376
|
}
|
|
377
377
|
|
|
378
|
-
unix():
|
|
378
|
+
unix(): UnixTimestampNumber {
|
|
379
379
|
return Math.floor(this.$date.valueOf() / 1000)
|
|
380
380
|
}
|
|
381
381
|
|
|
@@ -383,7 +383,7 @@ export class LocalTime {
|
|
|
383
383
|
return this.$date.valueOf()
|
|
384
384
|
}
|
|
385
385
|
|
|
386
|
-
valueOf():
|
|
386
|
+
valueOf(): UnixTimestampNumber {
|
|
387
387
|
return Math.floor(this.$date.valueOf() / 1000)
|
|
388
388
|
}
|
|
389
389
|
|
|
@@ -403,7 +403,7 @@ export class LocalTime {
|
|
|
403
403
|
)
|
|
404
404
|
}
|
|
405
405
|
|
|
406
|
-
toPretty(seconds = true):
|
|
406
|
+
toPretty(seconds = true): IsoDateTimeString {
|
|
407
407
|
const { year, month, day, hour, minute, second } = this.components()
|
|
408
408
|
|
|
409
409
|
return (
|
|
@@ -432,14 +432,14 @@ export class LocalTime {
|
|
|
432
432
|
/**
|
|
433
433
|
* Returns e.g: `1984-06-21T17:56:21`, only the date part of DateTime
|
|
434
434
|
*/
|
|
435
|
-
toISODateTime():
|
|
435
|
+
toISODateTime(): IsoDateTimeString {
|
|
436
436
|
return this.$date.toISOString().slice(0, 19)
|
|
437
437
|
}
|
|
438
438
|
|
|
439
439
|
/**
|
|
440
440
|
* Returns e.g: `1984-06-21`, only the date part of DateTime
|
|
441
441
|
*/
|
|
442
|
-
toISODate():
|
|
442
|
+
toISODate(): IsoDateString {
|
|
443
443
|
const { year, month, day } = this.components()
|
|
444
444
|
|
|
445
445
|
return [
|
|
@@ -488,7 +488,7 @@ export class LocalTime {
|
|
|
488
488
|
return String(this.unix())
|
|
489
489
|
}
|
|
490
490
|
|
|
491
|
-
toJSON():
|
|
491
|
+
toJSON(): UnixTimestampNumber {
|
|
492
492
|
return this.unix()
|
|
493
493
|
}
|
|
494
494
|
}
|
package/src/index.ts
CHANGED
|
@@ -105,7 +105,8 @@ import {
|
|
|
105
105
|
BatchResult,
|
|
106
106
|
InstanceId,
|
|
107
107
|
IsoDate,
|
|
108
|
-
|
|
108
|
+
IsoDateString,
|
|
109
|
+
IsoDateTimeString,
|
|
109
110
|
KeyValueTuple,
|
|
110
111
|
Mapper,
|
|
111
112
|
ObjectMapper,
|
|
@@ -117,6 +118,7 @@ import {
|
|
|
117
118
|
Reviver,
|
|
118
119
|
SavedDBEntity,
|
|
119
120
|
StringMap,
|
|
121
|
+
UnixTimestampNumber,
|
|
120
122
|
UnixTimestamp,
|
|
121
123
|
Integer,
|
|
122
124
|
ValueOf,
|
|
@@ -197,7 +199,8 @@ export type {
|
|
|
197
199
|
ObjectPredicate,
|
|
198
200
|
InstanceId,
|
|
199
201
|
IsoDate,
|
|
200
|
-
|
|
202
|
+
IsoDateString,
|
|
203
|
+
IsoDateTimeString,
|
|
201
204
|
Reviver,
|
|
202
205
|
PMapOptions,
|
|
203
206
|
Mapper,
|
|
@@ -218,6 +221,7 @@ export type {
|
|
|
218
221
|
ConditionalPick,
|
|
219
222
|
ConditionalExcept,
|
|
220
223
|
Class,
|
|
224
|
+
UnixTimestampNumber,
|
|
221
225
|
UnixTimestamp,
|
|
222
226
|
Integer,
|
|
223
227
|
BaseDBEntity,
|
package/src/types.ts
CHANGED
|
@@ -150,6 +150,11 @@ export interface InstanceId {
|
|
|
150
150
|
*
|
|
151
151
|
* @example '2019-06-21'
|
|
152
152
|
*/
|
|
153
|
+
export type IsoDateString = string
|
|
154
|
+
|
|
155
|
+
/**
|
|
156
|
+
* @deprecated use IsoDateString
|
|
157
|
+
*/
|
|
153
158
|
export type IsoDate = string
|
|
154
159
|
|
|
155
160
|
/**
|
|
@@ -157,13 +162,18 @@ export type IsoDate = string
|
|
|
157
162
|
*
|
|
158
163
|
* @example '2019-06-21T05:21:73Z'
|
|
159
164
|
*/
|
|
160
|
-
export type
|
|
165
|
+
export type IsoDateTimeString = string
|
|
161
166
|
|
|
162
167
|
/**
|
|
163
168
|
* Interface explicitly states that the value is a Unix timestamp (in seconds).
|
|
164
169
|
*
|
|
165
170
|
* @example 1628945450
|
|
166
171
|
*/
|
|
172
|
+
export type UnixTimestampNumber = number
|
|
173
|
+
|
|
174
|
+
/**
|
|
175
|
+
* @deprecated use UnixTimestampNumber
|
|
176
|
+
*/
|
|
167
177
|
export type UnixTimestamp = number
|
|
168
178
|
|
|
169
179
|
/**
|
|
@@ -180,12 +190,12 @@ export interface SavedDBEntity<ID = string> {
|
|
|
180
190
|
/**
|
|
181
191
|
* unixTimestamp of when the entity was first created (in the DB).
|
|
182
192
|
*/
|
|
183
|
-
created:
|
|
193
|
+
created: UnixTimestampNumber
|
|
184
194
|
|
|
185
195
|
/**
|
|
186
196
|
* unixTimestamp of when the entity was last updated (in the DB).
|
|
187
197
|
*/
|
|
188
|
-
updated:
|
|
198
|
+
updated: UnixTimestampNumber
|
|
189
199
|
}
|
|
190
200
|
|
|
191
201
|
/**
|