@naturalcycles/js-lib 14.99.0 → 14.99.3
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 +1 -1
- package/dist/datetime/localDate.js +28 -21
- package/dist/datetime/localTime.d.ts +0 -3
- package/dist/datetime/localTime.js +67 -79
- package/dist-esm/datetime/localDate.js +28 -21
- package/dist-esm/datetime/localTime.js +67 -79
- package/package.json +1 -1
- package/src/datetime/localDate.ts +30 -20
- package/src/datetime/localTime.ts +71 -87
|
@@ -60,7 +60,7 @@ export declare class LocalDate {
|
|
|
60
60
|
*/
|
|
61
61
|
absDiff(d: LocalDateConfig, unit: LocalDateUnit): number;
|
|
62
62
|
/**
|
|
63
|
-
* Returns the number of **full** units difference (aka `Math.
|
|
63
|
+
* Returns the number of **full** units difference (aka `Math.floor`).
|
|
64
64
|
*
|
|
65
65
|
* a.diff(b) means "a minus b"
|
|
66
66
|
*/
|
|
@@ -205,44 +205,51 @@ class LocalDate {
|
|
|
205
205
|
return Math.abs(this.diff(d, unit));
|
|
206
206
|
}
|
|
207
207
|
/**
|
|
208
|
-
* Returns the number of **full** units difference (aka `Math.
|
|
208
|
+
* Returns the number of **full** units difference (aka `Math.floor`).
|
|
209
209
|
*
|
|
210
210
|
* a.diff(b) means "a minus b"
|
|
211
211
|
*/
|
|
212
212
|
diff(d, unit) {
|
|
213
213
|
d = LocalDate.of(d);
|
|
214
|
+
const sign = this.cmp(d);
|
|
215
|
+
if (!sign)
|
|
216
|
+
return 0;
|
|
217
|
+
// Put items in descending order: "big minus small"
|
|
218
|
+
const [big, small] = sign === 1 ? [this, d] : [d, this];
|
|
214
219
|
if (unit === 'year') {
|
|
215
|
-
|
|
220
|
+
let years = big.$year - small.$year;
|
|
221
|
+
if (big.$month < small.$month || (big.$month === small.$month && big.$day < small.$day)) {
|
|
222
|
+
years--;
|
|
223
|
+
}
|
|
224
|
+
return years * sign || 0;
|
|
216
225
|
}
|
|
217
226
|
if (unit === 'month') {
|
|
218
|
-
|
|
227
|
+
let months = (big.$year - small.$year) * 12 + (big.$month - small.$month);
|
|
228
|
+
if (big.$day < small.$day)
|
|
229
|
+
months--;
|
|
230
|
+
return months * sign || 0;
|
|
219
231
|
}
|
|
220
232
|
// unit is 'day' or 'week'
|
|
221
|
-
let days =
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
}
|
|
227
|
-
else if (this.$year < d.$year) {
|
|
228
|
-
for (let year = this.$year; year < d.$year; year++) {
|
|
229
|
-
days -= LocalDate.getYearLength(year);
|
|
230
|
-
}
|
|
233
|
+
let days = big.$day - small.$day;
|
|
234
|
+
// If small date is after 1st of March - next year's "leapness" should be used
|
|
235
|
+
const offsetYear = small.$month >= 3 ? 1 : 0;
|
|
236
|
+
for (let year = small.$year; year < big.$year; year++) {
|
|
237
|
+
days += LocalDate.getYearLength(year + offsetYear);
|
|
231
238
|
}
|
|
232
|
-
if (
|
|
233
|
-
for (let month =
|
|
234
|
-
days += LocalDate.getMonthLength(
|
|
239
|
+
if (small.$month < big.$month) {
|
|
240
|
+
for (let month = small.$month; month < big.$month; month++) {
|
|
241
|
+
days += LocalDate.getMonthLength(big.$year, month);
|
|
235
242
|
}
|
|
236
243
|
}
|
|
237
|
-
else if (
|
|
238
|
-
for (let month =
|
|
239
|
-
days -= LocalDate.getMonthLength(
|
|
244
|
+
else if (big.$month < small.$month) {
|
|
245
|
+
for (let month = big.$month; month < small.$month; month++) {
|
|
246
|
+
days -= LocalDate.getMonthLength(big.$year, month);
|
|
240
247
|
}
|
|
241
248
|
}
|
|
242
249
|
if (unit === 'week') {
|
|
243
|
-
return Math.
|
|
250
|
+
return Math.trunc(days / 7) * sign || 0;
|
|
244
251
|
}
|
|
245
|
-
return days;
|
|
252
|
+
return days * sign || 0;
|
|
246
253
|
}
|
|
247
254
|
add(num, unit, mutate = false) {
|
|
248
255
|
let { $day, $month, $year } = this;
|
|
@@ -25,15 +25,12 @@ export interface LocalTimeComponents {
|
|
|
25
25
|
*/
|
|
26
26
|
export declare class LocalTime {
|
|
27
27
|
private $date;
|
|
28
|
-
utcMode: boolean;
|
|
29
28
|
private constructor();
|
|
30
29
|
/**
|
|
31
30
|
* Parses input String into LocalDate.
|
|
32
31
|
* Input can already be a LocalDate - it is returned as-is in that case.
|
|
33
32
|
*/
|
|
34
33
|
static of(d: LocalTimeConfig): LocalTime;
|
|
35
|
-
utc(): this;
|
|
36
|
-
local(): this;
|
|
37
34
|
/**
|
|
38
35
|
* Returns null if invalid
|
|
39
36
|
*/
|
|
@@ -16,6 +16,7 @@ var ISODayOfWeek;
|
|
|
16
16
|
})(ISODayOfWeek = exports.ISODayOfWeek || (exports.ISODayOfWeek = {}));
|
|
17
17
|
const weekStartsOn = 1; // mon, as per ISO
|
|
18
18
|
const MILLISECONDS_IN_WEEK = 604800000;
|
|
19
|
+
const SECONDS_IN_DAY = 86400;
|
|
19
20
|
// const MILLISECONDS_IN_DAY = 86400000
|
|
20
21
|
// const MILLISECONDS_IN_MINUTE = 60000
|
|
21
22
|
const VALID_DAYS_OF_WEEK = new Set([1, 2, 3, 4, 5, 6, 7]);
|
|
@@ -24,9 +25,8 @@ const VALID_DAYS_OF_WEEK = new Set([1, 2, 3, 4, 5, 6, 7]);
|
|
|
24
25
|
* @experimental
|
|
25
26
|
*/
|
|
26
27
|
class LocalTime {
|
|
27
|
-
constructor($date
|
|
28
|
+
constructor($date) {
|
|
28
29
|
this.$date = $date;
|
|
29
|
-
this.utcMode = utcMode;
|
|
30
30
|
}
|
|
31
31
|
/**
|
|
32
32
|
* Parses input String into LocalDate.
|
|
@@ -39,14 +39,6 @@ class LocalTime {
|
|
|
39
39
|
}
|
|
40
40
|
return t;
|
|
41
41
|
}
|
|
42
|
-
utc() {
|
|
43
|
-
this.utcMode = true;
|
|
44
|
-
return this;
|
|
45
|
-
}
|
|
46
|
-
local() {
|
|
47
|
-
this.utcMode = false;
|
|
48
|
-
return this;
|
|
49
|
-
}
|
|
50
42
|
/**
|
|
51
43
|
* Returns null if invalid
|
|
52
44
|
*/
|
|
@@ -73,7 +65,7 @@ class LocalTime {
|
|
|
73
65
|
// if (utc) {
|
|
74
66
|
// date.setMinutes(date.getMinutes() + date.getTimezoneOffset())
|
|
75
67
|
// }
|
|
76
|
-
return new LocalTime(date
|
|
68
|
+
return new LocalTime(date);
|
|
77
69
|
}
|
|
78
70
|
static parseToDate(d) {
|
|
79
71
|
if (d instanceof LocalTime)
|
|
@@ -101,58 +93,56 @@ class LocalTime {
|
|
|
101
93
|
return this.parseOrNull(d) !== null;
|
|
102
94
|
}
|
|
103
95
|
static now() {
|
|
104
|
-
return new LocalTime(new Date()
|
|
96
|
+
return new LocalTime(new Date());
|
|
105
97
|
}
|
|
106
98
|
static fromComponents(c) {
|
|
107
|
-
return new LocalTime(new Date(c.year, c.month - 1, c.day, c.hour, c.minute, c.second
|
|
99
|
+
return new LocalTime(new Date(c.year, c.month - 1, c.day || 1, c.hour || 0, c.minute || 0, c.second || 0));
|
|
108
100
|
}
|
|
109
101
|
get(unit) {
|
|
110
102
|
if (unit === 'year') {
|
|
111
|
-
return this
|
|
103
|
+
return this.$date.getFullYear();
|
|
112
104
|
}
|
|
113
105
|
if (unit === 'month') {
|
|
114
|
-
return
|
|
106
|
+
return this.$date.getMonth() + 1;
|
|
115
107
|
}
|
|
116
108
|
if (unit === 'day') {
|
|
117
|
-
return this
|
|
109
|
+
return this.$date.getDate();
|
|
118
110
|
}
|
|
119
111
|
if (unit === 'hour') {
|
|
120
|
-
return this
|
|
112
|
+
return this.$date.getHours();
|
|
121
113
|
}
|
|
122
114
|
if (unit === 'minute') {
|
|
123
|
-
return this
|
|
115
|
+
return this.$date.getMinutes();
|
|
124
116
|
}
|
|
125
117
|
if (unit === 'week') {
|
|
126
118
|
return getWeek(this.$date);
|
|
127
119
|
}
|
|
128
120
|
// second
|
|
129
|
-
return this
|
|
121
|
+
return this.$date.getSeconds();
|
|
130
122
|
}
|
|
131
123
|
set(unit, v, mutate = false) {
|
|
132
124
|
const t = mutate ? this : this.clone();
|
|
133
|
-
/* eslint-disable @typescript-eslint/no-unused-expressions */
|
|
134
125
|
if (unit === 'year') {
|
|
135
|
-
|
|
126
|
+
t.$date.setFullYear(v);
|
|
136
127
|
}
|
|
137
128
|
else if (unit === 'month') {
|
|
138
|
-
|
|
129
|
+
t.$date.setMonth(v - 1);
|
|
139
130
|
}
|
|
140
131
|
else if (unit === 'day') {
|
|
141
|
-
|
|
132
|
+
t.$date.setDate(v);
|
|
142
133
|
}
|
|
143
134
|
else if (unit === 'hour') {
|
|
144
|
-
|
|
135
|
+
t.$date.setHours(v);
|
|
145
136
|
}
|
|
146
137
|
else if (unit === 'minute') {
|
|
147
|
-
|
|
138
|
+
t.$date.setMinutes(v);
|
|
148
139
|
}
|
|
149
140
|
else if (unit === 'second') {
|
|
150
|
-
|
|
141
|
+
t.$date.setSeconds(v);
|
|
151
142
|
}
|
|
152
143
|
else if (unit === 'week') {
|
|
153
144
|
setWeek(t.$date, v, true);
|
|
154
145
|
}
|
|
155
|
-
/* eslint-enable @typescript-eslint/no-unused-expressions */
|
|
156
146
|
return t;
|
|
157
147
|
}
|
|
158
148
|
year(v) {
|
|
@@ -187,27 +177,25 @@ class LocalTime {
|
|
|
187
177
|
}
|
|
188
178
|
setComponents(c, mutate = false) {
|
|
189
179
|
const d = mutate ? this.$date : new Date(this.$date);
|
|
190
|
-
/* eslint-disable @typescript-eslint/no-unused-expressions */
|
|
191
180
|
if (c.year) {
|
|
192
|
-
|
|
181
|
+
d.setFullYear(c.year);
|
|
193
182
|
}
|
|
194
183
|
if (c.month) {
|
|
195
|
-
|
|
184
|
+
d.setMonth(c.month - 1);
|
|
196
185
|
}
|
|
197
186
|
if (c.day) {
|
|
198
|
-
|
|
187
|
+
d.setDate(c.day);
|
|
199
188
|
}
|
|
200
189
|
if (c.hour !== undefined) {
|
|
201
|
-
|
|
190
|
+
d.setHours(c.hour);
|
|
202
191
|
}
|
|
203
192
|
if (c.minute !== undefined) {
|
|
204
|
-
|
|
193
|
+
d.setMinutes(c.minute);
|
|
205
194
|
}
|
|
206
195
|
if (c.second !== undefined) {
|
|
207
|
-
|
|
196
|
+
d.setSeconds(c.second);
|
|
208
197
|
}
|
|
209
|
-
|
|
210
|
-
return mutate ? this : new LocalTime(d, this.utcMode);
|
|
198
|
+
return mutate ? this : new LocalTime(d);
|
|
211
199
|
}
|
|
212
200
|
add(num, unit, mutate = false) {
|
|
213
201
|
if (unit === 'week') {
|
|
@@ -224,24 +212,43 @@ class LocalTime {
|
|
|
224
212
|
}
|
|
225
213
|
diff(other, unit) {
|
|
226
214
|
const date2 = LocalTime.parseToDate(other);
|
|
227
|
-
if (unit === 'year') {
|
|
228
|
-
return this.$date.getFullYear() - date2.getFullYear();
|
|
229
|
-
}
|
|
230
|
-
if (unit === 'month') {
|
|
231
|
-
return ((this.$date.getFullYear() - date2.getFullYear()) * 12 +
|
|
232
|
-
this.$date.getMonth() -
|
|
233
|
-
date2.getMonth());
|
|
234
|
-
}
|
|
235
215
|
const secDiff = (this.$date.valueOf() - date2.valueOf()) / 1000;
|
|
216
|
+
if (!secDiff)
|
|
217
|
+
return 0;
|
|
218
|
+
if (unit === 'year' || unit === 'month') {
|
|
219
|
+
const sign = secDiff > 0 ? 1 : -1;
|
|
220
|
+
// Put items in descending order: "big minus small"
|
|
221
|
+
const [big, small] = sign === 1 ? [this.$date, date2] : [date2, this.$date];
|
|
222
|
+
if (unit === 'year') {
|
|
223
|
+
let years = big.getFullYear() - small.getFullYear();
|
|
224
|
+
const big2 = new Date(big);
|
|
225
|
+
const small2 = new Date(small);
|
|
226
|
+
big2.setFullYear(1584);
|
|
227
|
+
small2.setFullYear(1584);
|
|
228
|
+
if (big2 < small2)
|
|
229
|
+
years--;
|
|
230
|
+
return years * sign || 0;
|
|
231
|
+
}
|
|
232
|
+
if (unit === 'month') {
|
|
233
|
+
let months = (big.getFullYear() - small.getFullYear()) * 12 + big.getMonth() - small.getMonth();
|
|
234
|
+
const big2 = new Date(big);
|
|
235
|
+
const small2 = new Date(small);
|
|
236
|
+
big2.setFullYear(1584, 0);
|
|
237
|
+
small2.setFullYear(1584, 0);
|
|
238
|
+
if (big2 < small2)
|
|
239
|
+
months--;
|
|
240
|
+
return months * sign || 0;
|
|
241
|
+
}
|
|
242
|
+
}
|
|
236
243
|
let r;
|
|
237
244
|
if (unit === 'day') {
|
|
238
|
-
r = secDiff /
|
|
245
|
+
r = secDiff / SECONDS_IN_DAY;
|
|
239
246
|
}
|
|
240
247
|
else if (unit === 'week') {
|
|
241
248
|
r = secDiff / (7 * 24 * 60 * 60);
|
|
242
249
|
}
|
|
243
250
|
else if (unit === 'hour') {
|
|
244
|
-
r = secDiff /
|
|
251
|
+
r = secDiff / 3600;
|
|
245
252
|
}
|
|
246
253
|
else if (unit === 'minute') {
|
|
247
254
|
r = secDiff / 60;
|
|
@@ -250,29 +257,26 @@ class LocalTime {
|
|
|
250
257
|
// unit === 'second'
|
|
251
258
|
r = secDiff;
|
|
252
259
|
}
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
return 0;
|
|
256
|
-
return r;
|
|
260
|
+
// `|| 0` is to avoid returning -0
|
|
261
|
+
return Math.trunc(r) || 0;
|
|
257
262
|
}
|
|
258
263
|
startOf(unit, mutate = false) {
|
|
259
264
|
if (unit === 'second')
|
|
260
265
|
return this;
|
|
261
266
|
const d = mutate ? this.$date : new Date(this.$date);
|
|
262
267
|
d.setSeconds(0, 0);
|
|
263
|
-
/* eslint-disable @typescript-eslint/no-unused-expressions */
|
|
264
268
|
if (unit !== 'minute') {
|
|
265
|
-
|
|
269
|
+
d.setMinutes(0);
|
|
266
270
|
if (unit !== 'hour') {
|
|
267
|
-
|
|
271
|
+
d.setHours(0);
|
|
268
272
|
if (unit !== 'day') {
|
|
269
273
|
// year, month or week
|
|
270
274
|
if (unit === 'year') {
|
|
271
|
-
|
|
272
|
-
|
|
275
|
+
d.setMonth(0);
|
|
276
|
+
d.setDate(1);
|
|
273
277
|
}
|
|
274
278
|
else if (unit === 'month') {
|
|
275
|
-
|
|
279
|
+
d.setDate(1);
|
|
276
280
|
}
|
|
277
281
|
else {
|
|
278
282
|
// week
|
|
@@ -281,23 +285,21 @@ class LocalTime {
|
|
|
281
285
|
}
|
|
282
286
|
}
|
|
283
287
|
}
|
|
284
|
-
|
|
285
|
-
return mutate ? this : new LocalTime(d, this.utcMode);
|
|
288
|
+
return mutate ? this : new LocalTime(d);
|
|
286
289
|
}
|
|
287
290
|
endOf(unit, mutate = false) {
|
|
288
291
|
if (unit === 'second')
|
|
289
292
|
return this;
|
|
290
293
|
const d = mutate ? this.$date : new Date(this.$date);
|
|
291
294
|
d.setSeconds(59, 0);
|
|
292
|
-
/* eslint-disable @typescript-eslint/no-unused-expressions */
|
|
293
295
|
if (unit !== 'minute') {
|
|
294
|
-
|
|
296
|
+
d.setMinutes(59);
|
|
295
297
|
if (unit !== 'hour') {
|
|
296
|
-
|
|
298
|
+
d.setHours(23);
|
|
297
299
|
if (unit !== 'day') {
|
|
298
300
|
// year, month or week
|
|
299
301
|
if (unit === 'year') {
|
|
300
|
-
|
|
302
|
+
d.setMonth(11);
|
|
301
303
|
}
|
|
302
304
|
if (unit === 'week') {
|
|
303
305
|
endOfWeek(d, true);
|
|
@@ -305,13 +307,12 @@ class LocalTime {
|
|
|
305
307
|
else {
|
|
306
308
|
// year or month
|
|
307
309
|
const lastDay = localDate_1.LocalDate.getMonthLength(d.getFullYear(), d.getMonth() + 1);
|
|
308
|
-
|
|
310
|
+
d.setDate(lastDay);
|
|
309
311
|
}
|
|
310
312
|
}
|
|
311
313
|
}
|
|
312
314
|
}
|
|
313
|
-
|
|
314
|
-
return mutate ? this : new LocalTime(d, this.utcMode);
|
|
315
|
+
return mutate ? this : new LocalTime(d);
|
|
315
316
|
}
|
|
316
317
|
static sort(items, mutate = false, descending = false) {
|
|
317
318
|
const mod = descending ? -1 : 1;
|
|
@@ -380,16 +381,6 @@ class LocalTime {
|
|
|
380
381
|
return t1 < t2 ? -1 : 1;
|
|
381
382
|
}
|
|
382
383
|
components() {
|
|
383
|
-
if (this.utcMode) {
|
|
384
|
-
return {
|
|
385
|
-
year: this.$date.getUTCFullYear(),
|
|
386
|
-
month: this.$date.getUTCMonth() + 1,
|
|
387
|
-
day: this.$date.getUTCDate(),
|
|
388
|
-
hour: this.$date.getUTCHours(),
|
|
389
|
-
minute: this.$date.getUTCMinutes(),
|
|
390
|
-
second: this.$date.getSeconds(),
|
|
391
|
-
};
|
|
392
|
-
}
|
|
393
384
|
return {
|
|
394
385
|
year: this.$date.getFullYear(),
|
|
395
386
|
month: this.$date.getMonth() + 1,
|
|
@@ -412,7 +403,7 @@ class LocalTime {
|
|
|
412
403
|
return this.$date;
|
|
413
404
|
}
|
|
414
405
|
clone() {
|
|
415
|
-
return new LocalTime(new Date(this.$date)
|
|
406
|
+
return new LocalTime(new Date(this.$date));
|
|
416
407
|
}
|
|
417
408
|
unix() {
|
|
418
409
|
return Math.floor(this.$date.valueOf() / 1000);
|
|
@@ -424,9 +415,6 @@ class LocalTime {
|
|
|
424
415
|
return Math.floor(this.$date.valueOf() / 1000);
|
|
425
416
|
}
|
|
426
417
|
toLocalDate() {
|
|
427
|
-
if (this.utcMode) {
|
|
428
|
-
return localDate_1.LocalDate.create(this.$date.getUTCFullYear(), this.$date.getUTCMonth() + 1, this.$date.getUTCDate());
|
|
429
|
-
}
|
|
430
418
|
return localDate_1.LocalDate.create(this.$date.getFullYear(), this.$date.getMonth() + 1, this.$date.getDate());
|
|
431
419
|
}
|
|
432
420
|
toPretty(seconds = true) {
|
|
@@ -202,44 +202,51 @@ export class LocalDate {
|
|
|
202
202
|
return Math.abs(this.diff(d, unit));
|
|
203
203
|
}
|
|
204
204
|
/**
|
|
205
|
-
* Returns the number of **full** units difference (aka `Math.
|
|
205
|
+
* Returns the number of **full** units difference (aka `Math.floor`).
|
|
206
206
|
*
|
|
207
207
|
* a.diff(b) means "a minus b"
|
|
208
208
|
*/
|
|
209
209
|
diff(d, unit) {
|
|
210
210
|
d = LocalDate.of(d);
|
|
211
|
+
const sign = this.cmp(d);
|
|
212
|
+
if (!sign)
|
|
213
|
+
return 0;
|
|
214
|
+
// Put items in descending order: "big minus small"
|
|
215
|
+
const [big, small] = sign === 1 ? [this, d] : [d, this];
|
|
211
216
|
if (unit === 'year') {
|
|
212
|
-
|
|
217
|
+
let years = big.$year - small.$year;
|
|
218
|
+
if (big.$month < small.$month || (big.$month === small.$month && big.$day < small.$day)) {
|
|
219
|
+
years--;
|
|
220
|
+
}
|
|
221
|
+
return years * sign || 0;
|
|
213
222
|
}
|
|
214
223
|
if (unit === 'month') {
|
|
215
|
-
|
|
224
|
+
let months = (big.$year - small.$year) * 12 + (big.$month - small.$month);
|
|
225
|
+
if (big.$day < small.$day)
|
|
226
|
+
months--;
|
|
227
|
+
return months * sign || 0;
|
|
216
228
|
}
|
|
217
229
|
// unit is 'day' or 'week'
|
|
218
|
-
let days =
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
}
|
|
224
|
-
else if (this.$year < d.$year) {
|
|
225
|
-
for (let year = this.$year; year < d.$year; year++) {
|
|
226
|
-
days -= LocalDate.getYearLength(year);
|
|
227
|
-
}
|
|
230
|
+
let days = big.$day - small.$day;
|
|
231
|
+
// If small date is after 1st of March - next year's "leapness" should be used
|
|
232
|
+
const offsetYear = small.$month >= 3 ? 1 : 0;
|
|
233
|
+
for (let year = small.$year; year < big.$year; year++) {
|
|
234
|
+
days += LocalDate.getYearLength(year + offsetYear);
|
|
228
235
|
}
|
|
229
|
-
if (
|
|
230
|
-
for (let month =
|
|
231
|
-
days += LocalDate.getMonthLength(
|
|
236
|
+
if (small.$month < big.$month) {
|
|
237
|
+
for (let month = small.$month; month < big.$month; month++) {
|
|
238
|
+
days += LocalDate.getMonthLength(big.$year, month);
|
|
232
239
|
}
|
|
233
240
|
}
|
|
234
|
-
else if (
|
|
235
|
-
for (let month =
|
|
236
|
-
days -= LocalDate.getMonthLength(
|
|
241
|
+
else if (big.$month < small.$month) {
|
|
242
|
+
for (let month = big.$month; month < small.$month; month++) {
|
|
243
|
+
days -= LocalDate.getMonthLength(big.$year, month);
|
|
237
244
|
}
|
|
238
245
|
}
|
|
239
246
|
if (unit === 'week') {
|
|
240
|
-
return Math.
|
|
247
|
+
return Math.trunc(days / 7) * sign || 0;
|
|
241
248
|
}
|
|
242
|
-
return days;
|
|
249
|
+
return days * sign || 0;
|
|
243
250
|
}
|
|
244
251
|
add(num, unit, mutate = false) {
|
|
245
252
|
let { $day, $month, $year } = this;
|
|
@@ -13,6 +13,7 @@ export var ISODayOfWeek;
|
|
|
13
13
|
})(ISODayOfWeek || (ISODayOfWeek = {}));
|
|
14
14
|
const weekStartsOn = 1; // mon, as per ISO
|
|
15
15
|
const MILLISECONDS_IN_WEEK = 604800000;
|
|
16
|
+
const SECONDS_IN_DAY = 86400;
|
|
16
17
|
// const MILLISECONDS_IN_DAY = 86400000
|
|
17
18
|
// const MILLISECONDS_IN_MINUTE = 60000
|
|
18
19
|
const VALID_DAYS_OF_WEEK = new Set([1, 2, 3, 4, 5, 6, 7]);
|
|
@@ -21,9 +22,8 @@ const VALID_DAYS_OF_WEEK = new Set([1, 2, 3, 4, 5, 6, 7]);
|
|
|
21
22
|
* @experimental
|
|
22
23
|
*/
|
|
23
24
|
export class LocalTime {
|
|
24
|
-
constructor($date
|
|
25
|
+
constructor($date) {
|
|
25
26
|
this.$date = $date;
|
|
26
|
-
this.utcMode = utcMode;
|
|
27
27
|
}
|
|
28
28
|
/**
|
|
29
29
|
* Parses input String into LocalDate.
|
|
@@ -36,14 +36,6 @@ export class LocalTime {
|
|
|
36
36
|
}
|
|
37
37
|
return t;
|
|
38
38
|
}
|
|
39
|
-
utc() {
|
|
40
|
-
this.utcMode = true;
|
|
41
|
-
return this;
|
|
42
|
-
}
|
|
43
|
-
local() {
|
|
44
|
-
this.utcMode = false;
|
|
45
|
-
return this;
|
|
46
|
-
}
|
|
47
39
|
/**
|
|
48
40
|
* Returns null if invalid
|
|
49
41
|
*/
|
|
@@ -70,7 +62,7 @@ export class LocalTime {
|
|
|
70
62
|
// if (utc) {
|
|
71
63
|
// date.setMinutes(date.getMinutes() + date.getTimezoneOffset())
|
|
72
64
|
// }
|
|
73
|
-
return new LocalTime(date
|
|
65
|
+
return new LocalTime(date);
|
|
74
66
|
}
|
|
75
67
|
static parseToDate(d) {
|
|
76
68
|
if (d instanceof LocalTime)
|
|
@@ -98,58 +90,56 @@ export class LocalTime {
|
|
|
98
90
|
return this.parseOrNull(d) !== null;
|
|
99
91
|
}
|
|
100
92
|
static now() {
|
|
101
|
-
return new LocalTime(new Date()
|
|
93
|
+
return new LocalTime(new Date());
|
|
102
94
|
}
|
|
103
95
|
static fromComponents(c) {
|
|
104
|
-
return new LocalTime(new Date(c.year, c.month - 1, c.day, c.hour, c.minute, c.second
|
|
96
|
+
return new LocalTime(new Date(c.year, c.month - 1, c.day || 1, c.hour || 0, c.minute || 0, c.second || 0));
|
|
105
97
|
}
|
|
106
98
|
get(unit) {
|
|
107
99
|
if (unit === 'year') {
|
|
108
|
-
return this
|
|
100
|
+
return this.$date.getFullYear();
|
|
109
101
|
}
|
|
110
102
|
if (unit === 'month') {
|
|
111
|
-
return
|
|
103
|
+
return this.$date.getMonth() + 1;
|
|
112
104
|
}
|
|
113
105
|
if (unit === 'day') {
|
|
114
|
-
return this
|
|
106
|
+
return this.$date.getDate();
|
|
115
107
|
}
|
|
116
108
|
if (unit === 'hour') {
|
|
117
|
-
return this
|
|
109
|
+
return this.$date.getHours();
|
|
118
110
|
}
|
|
119
111
|
if (unit === 'minute') {
|
|
120
|
-
return this
|
|
112
|
+
return this.$date.getMinutes();
|
|
121
113
|
}
|
|
122
114
|
if (unit === 'week') {
|
|
123
115
|
return getWeek(this.$date);
|
|
124
116
|
}
|
|
125
117
|
// second
|
|
126
|
-
return this
|
|
118
|
+
return this.$date.getSeconds();
|
|
127
119
|
}
|
|
128
120
|
set(unit, v, mutate = false) {
|
|
129
121
|
const t = mutate ? this : this.clone();
|
|
130
|
-
/* eslint-disable @typescript-eslint/no-unused-expressions */
|
|
131
122
|
if (unit === 'year') {
|
|
132
|
-
|
|
123
|
+
t.$date.setFullYear(v);
|
|
133
124
|
}
|
|
134
125
|
else if (unit === 'month') {
|
|
135
|
-
|
|
126
|
+
t.$date.setMonth(v - 1);
|
|
136
127
|
}
|
|
137
128
|
else if (unit === 'day') {
|
|
138
|
-
|
|
129
|
+
t.$date.setDate(v);
|
|
139
130
|
}
|
|
140
131
|
else if (unit === 'hour') {
|
|
141
|
-
|
|
132
|
+
t.$date.setHours(v);
|
|
142
133
|
}
|
|
143
134
|
else if (unit === 'minute') {
|
|
144
|
-
|
|
135
|
+
t.$date.setMinutes(v);
|
|
145
136
|
}
|
|
146
137
|
else if (unit === 'second') {
|
|
147
|
-
|
|
138
|
+
t.$date.setSeconds(v);
|
|
148
139
|
}
|
|
149
140
|
else if (unit === 'week') {
|
|
150
141
|
setWeek(t.$date, v, true);
|
|
151
142
|
}
|
|
152
|
-
/* eslint-enable @typescript-eslint/no-unused-expressions */
|
|
153
143
|
return t;
|
|
154
144
|
}
|
|
155
145
|
year(v) {
|
|
@@ -184,27 +174,25 @@ export class LocalTime {
|
|
|
184
174
|
}
|
|
185
175
|
setComponents(c, mutate = false) {
|
|
186
176
|
const d = mutate ? this.$date : new Date(this.$date);
|
|
187
|
-
/* eslint-disable @typescript-eslint/no-unused-expressions */
|
|
188
177
|
if (c.year) {
|
|
189
|
-
|
|
178
|
+
d.setFullYear(c.year);
|
|
190
179
|
}
|
|
191
180
|
if (c.month) {
|
|
192
|
-
|
|
181
|
+
d.setMonth(c.month - 1);
|
|
193
182
|
}
|
|
194
183
|
if (c.day) {
|
|
195
|
-
|
|
184
|
+
d.setDate(c.day);
|
|
196
185
|
}
|
|
197
186
|
if (c.hour !== undefined) {
|
|
198
|
-
|
|
187
|
+
d.setHours(c.hour);
|
|
199
188
|
}
|
|
200
189
|
if (c.minute !== undefined) {
|
|
201
|
-
|
|
190
|
+
d.setMinutes(c.minute);
|
|
202
191
|
}
|
|
203
192
|
if (c.second !== undefined) {
|
|
204
|
-
|
|
193
|
+
d.setSeconds(c.second);
|
|
205
194
|
}
|
|
206
|
-
|
|
207
|
-
return mutate ? this : new LocalTime(d, this.utcMode);
|
|
195
|
+
return mutate ? this : new LocalTime(d);
|
|
208
196
|
}
|
|
209
197
|
add(num, unit, mutate = false) {
|
|
210
198
|
if (unit === 'week') {
|
|
@@ -221,24 +209,43 @@ export class LocalTime {
|
|
|
221
209
|
}
|
|
222
210
|
diff(other, unit) {
|
|
223
211
|
const date2 = LocalTime.parseToDate(other);
|
|
224
|
-
if (unit === 'year') {
|
|
225
|
-
return this.$date.getFullYear() - date2.getFullYear();
|
|
226
|
-
}
|
|
227
|
-
if (unit === 'month') {
|
|
228
|
-
return ((this.$date.getFullYear() - date2.getFullYear()) * 12 +
|
|
229
|
-
this.$date.getMonth() -
|
|
230
|
-
date2.getMonth());
|
|
231
|
-
}
|
|
232
212
|
const secDiff = (this.$date.valueOf() - date2.valueOf()) / 1000;
|
|
213
|
+
if (!secDiff)
|
|
214
|
+
return 0;
|
|
215
|
+
if (unit === 'year' || unit === 'month') {
|
|
216
|
+
const sign = secDiff > 0 ? 1 : -1;
|
|
217
|
+
// Put items in descending order: "big minus small"
|
|
218
|
+
const [big, small] = sign === 1 ? [this.$date, date2] : [date2, this.$date];
|
|
219
|
+
if (unit === 'year') {
|
|
220
|
+
let years = big.getFullYear() - small.getFullYear();
|
|
221
|
+
const big2 = new Date(big);
|
|
222
|
+
const small2 = new Date(small);
|
|
223
|
+
big2.setFullYear(1584);
|
|
224
|
+
small2.setFullYear(1584);
|
|
225
|
+
if (big2 < small2)
|
|
226
|
+
years--;
|
|
227
|
+
return years * sign || 0;
|
|
228
|
+
}
|
|
229
|
+
if (unit === 'month') {
|
|
230
|
+
let months = (big.getFullYear() - small.getFullYear()) * 12 + big.getMonth() - small.getMonth();
|
|
231
|
+
const big2 = new Date(big);
|
|
232
|
+
const small2 = new Date(small);
|
|
233
|
+
big2.setFullYear(1584, 0);
|
|
234
|
+
small2.setFullYear(1584, 0);
|
|
235
|
+
if (big2 < small2)
|
|
236
|
+
months--;
|
|
237
|
+
return months * sign || 0;
|
|
238
|
+
}
|
|
239
|
+
}
|
|
233
240
|
let r;
|
|
234
241
|
if (unit === 'day') {
|
|
235
|
-
r = secDiff /
|
|
242
|
+
r = secDiff / SECONDS_IN_DAY;
|
|
236
243
|
}
|
|
237
244
|
else if (unit === 'week') {
|
|
238
245
|
r = secDiff / (7 * 24 * 60 * 60);
|
|
239
246
|
}
|
|
240
247
|
else if (unit === 'hour') {
|
|
241
|
-
r = secDiff /
|
|
248
|
+
r = secDiff / 3600;
|
|
242
249
|
}
|
|
243
250
|
else if (unit === 'minute') {
|
|
244
251
|
r = secDiff / 60;
|
|
@@ -247,29 +254,26 @@ export class LocalTime {
|
|
|
247
254
|
// unit === 'second'
|
|
248
255
|
r = secDiff;
|
|
249
256
|
}
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
return 0;
|
|
253
|
-
return r;
|
|
257
|
+
// `|| 0` is to avoid returning -0
|
|
258
|
+
return Math.trunc(r) || 0;
|
|
254
259
|
}
|
|
255
260
|
startOf(unit, mutate = false) {
|
|
256
261
|
if (unit === 'second')
|
|
257
262
|
return this;
|
|
258
263
|
const d = mutate ? this.$date : new Date(this.$date);
|
|
259
264
|
d.setSeconds(0, 0);
|
|
260
|
-
/* eslint-disable @typescript-eslint/no-unused-expressions */
|
|
261
265
|
if (unit !== 'minute') {
|
|
262
|
-
|
|
266
|
+
d.setMinutes(0);
|
|
263
267
|
if (unit !== 'hour') {
|
|
264
|
-
|
|
268
|
+
d.setHours(0);
|
|
265
269
|
if (unit !== 'day') {
|
|
266
270
|
// year, month or week
|
|
267
271
|
if (unit === 'year') {
|
|
268
|
-
|
|
269
|
-
|
|
272
|
+
d.setMonth(0);
|
|
273
|
+
d.setDate(1);
|
|
270
274
|
}
|
|
271
275
|
else if (unit === 'month') {
|
|
272
|
-
|
|
276
|
+
d.setDate(1);
|
|
273
277
|
}
|
|
274
278
|
else {
|
|
275
279
|
// week
|
|
@@ -278,23 +282,21 @@ export class LocalTime {
|
|
|
278
282
|
}
|
|
279
283
|
}
|
|
280
284
|
}
|
|
281
|
-
|
|
282
|
-
return mutate ? this : new LocalTime(d, this.utcMode);
|
|
285
|
+
return mutate ? this : new LocalTime(d);
|
|
283
286
|
}
|
|
284
287
|
endOf(unit, mutate = false) {
|
|
285
288
|
if (unit === 'second')
|
|
286
289
|
return this;
|
|
287
290
|
const d = mutate ? this.$date : new Date(this.$date);
|
|
288
291
|
d.setSeconds(59, 0);
|
|
289
|
-
/* eslint-disable @typescript-eslint/no-unused-expressions */
|
|
290
292
|
if (unit !== 'minute') {
|
|
291
|
-
|
|
293
|
+
d.setMinutes(59);
|
|
292
294
|
if (unit !== 'hour') {
|
|
293
|
-
|
|
295
|
+
d.setHours(23);
|
|
294
296
|
if (unit !== 'day') {
|
|
295
297
|
// year, month or week
|
|
296
298
|
if (unit === 'year') {
|
|
297
|
-
|
|
299
|
+
d.setMonth(11);
|
|
298
300
|
}
|
|
299
301
|
if (unit === 'week') {
|
|
300
302
|
endOfWeek(d, true);
|
|
@@ -302,13 +304,12 @@ export class LocalTime {
|
|
|
302
304
|
else {
|
|
303
305
|
// year or month
|
|
304
306
|
const lastDay = LocalDate.getMonthLength(d.getFullYear(), d.getMonth() + 1);
|
|
305
|
-
|
|
307
|
+
d.setDate(lastDay);
|
|
306
308
|
}
|
|
307
309
|
}
|
|
308
310
|
}
|
|
309
311
|
}
|
|
310
|
-
|
|
311
|
-
return mutate ? this : new LocalTime(d, this.utcMode);
|
|
312
|
+
return mutate ? this : new LocalTime(d);
|
|
312
313
|
}
|
|
313
314
|
static sort(items, mutate = false, descending = false) {
|
|
314
315
|
const mod = descending ? -1 : 1;
|
|
@@ -377,16 +378,6 @@ export class LocalTime {
|
|
|
377
378
|
return t1 < t2 ? -1 : 1;
|
|
378
379
|
}
|
|
379
380
|
components() {
|
|
380
|
-
if (this.utcMode) {
|
|
381
|
-
return {
|
|
382
|
-
year: this.$date.getUTCFullYear(),
|
|
383
|
-
month: this.$date.getUTCMonth() + 1,
|
|
384
|
-
day: this.$date.getUTCDate(),
|
|
385
|
-
hour: this.$date.getUTCHours(),
|
|
386
|
-
minute: this.$date.getUTCMinutes(),
|
|
387
|
-
second: this.$date.getSeconds(),
|
|
388
|
-
};
|
|
389
|
-
}
|
|
390
381
|
return {
|
|
391
382
|
year: this.$date.getFullYear(),
|
|
392
383
|
month: this.$date.getMonth() + 1,
|
|
@@ -409,7 +400,7 @@ export class LocalTime {
|
|
|
409
400
|
return this.$date;
|
|
410
401
|
}
|
|
411
402
|
clone() {
|
|
412
|
-
return new LocalTime(new Date(this.$date)
|
|
403
|
+
return new LocalTime(new Date(this.$date));
|
|
413
404
|
}
|
|
414
405
|
unix() {
|
|
415
406
|
return Math.floor(this.$date.valueOf() / 1000);
|
|
@@ -421,9 +412,6 @@ export class LocalTime {
|
|
|
421
412
|
return Math.floor(this.$date.valueOf() / 1000);
|
|
422
413
|
}
|
|
423
414
|
toLocalDate() {
|
|
424
|
-
if (this.utcMode) {
|
|
425
|
-
return LocalDate.create(this.$date.getUTCFullYear(), this.$date.getUTCMonth() + 1, this.$date.getUTCDate());
|
|
426
|
-
}
|
|
427
415
|
return LocalDate.create(this.$date.getFullYear(), this.$date.getMonth() + 1, this.$date.getDate());
|
|
428
416
|
}
|
|
429
417
|
toPretty(seconds = true) {
|
package/package.json
CHANGED
|
@@ -253,49 +253,59 @@ export class LocalDate {
|
|
|
253
253
|
}
|
|
254
254
|
|
|
255
255
|
/**
|
|
256
|
-
* Returns the number of **full** units difference (aka `Math.
|
|
256
|
+
* Returns the number of **full** units difference (aka `Math.floor`).
|
|
257
257
|
*
|
|
258
258
|
* a.diff(b) means "a minus b"
|
|
259
259
|
*/
|
|
260
260
|
diff(d: LocalDateConfig, unit: LocalDateUnit): number {
|
|
261
261
|
d = LocalDate.of(d)
|
|
262
262
|
|
|
263
|
+
const sign = this.cmp(d)
|
|
264
|
+
if (!sign) return 0
|
|
265
|
+
|
|
266
|
+
// Put items in descending order: "big minus small"
|
|
267
|
+
const [big, small] = sign === 1 ? [this, d] : [d, this]
|
|
268
|
+
|
|
263
269
|
if (unit === 'year') {
|
|
264
|
-
|
|
270
|
+
let years = big.$year - small.$year
|
|
271
|
+
|
|
272
|
+
if (big.$month < small.$month || (big.$month === small.$month && big.$day < small.$day)) {
|
|
273
|
+
years--
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
return years * sign || 0
|
|
265
277
|
}
|
|
266
278
|
|
|
267
279
|
if (unit === 'month') {
|
|
268
|
-
|
|
280
|
+
let months = (big.$year - small.$year) * 12 + (big.$month - small.$month)
|
|
281
|
+
if (big.$day < small.$day) months--
|
|
282
|
+
return months * sign || 0
|
|
269
283
|
}
|
|
270
284
|
|
|
271
285
|
// unit is 'day' or 'week'
|
|
272
|
-
let days =
|
|
286
|
+
let days = big.$day - small.$day
|
|
273
287
|
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
} else if (this.$year < d.$year) {
|
|
279
|
-
for (let year = this.$year; year < d.$year; year++) {
|
|
280
|
-
days -= LocalDate.getYearLength(year)
|
|
281
|
-
}
|
|
288
|
+
// If small date is after 1st of March - next year's "leapness" should be used
|
|
289
|
+
const offsetYear = small.$month >= 3 ? 1 : 0
|
|
290
|
+
for (let year = small.$year; year < big.$year; year++) {
|
|
291
|
+
days += LocalDate.getYearLength(year + offsetYear)
|
|
282
292
|
}
|
|
283
293
|
|
|
284
|
-
if (
|
|
285
|
-
for (let month =
|
|
286
|
-
days += LocalDate.getMonthLength(
|
|
294
|
+
if (small.$month < big.$month) {
|
|
295
|
+
for (let month = small.$month; month < big.$month; month++) {
|
|
296
|
+
days += LocalDate.getMonthLength(big.$year, month)
|
|
287
297
|
}
|
|
288
|
-
} else if (
|
|
289
|
-
for (let month =
|
|
290
|
-
days -= LocalDate.getMonthLength(
|
|
298
|
+
} else if (big.$month < small.$month) {
|
|
299
|
+
for (let month = big.$month; month < small.$month; month++) {
|
|
300
|
+
days -= LocalDate.getMonthLength(big.$year, month)
|
|
291
301
|
}
|
|
292
302
|
}
|
|
293
303
|
|
|
294
304
|
if (unit === 'week') {
|
|
295
|
-
return Math.
|
|
305
|
+
return Math.trunc(days / 7) * sign || 0
|
|
296
306
|
}
|
|
297
307
|
|
|
298
|
-
return days
|
|
308
|
+
return days * sign || 0
|
|
299
309
|
}
|
|
300
310
|
|
|
301
311
|
add(num: number, unit: LocalDateUnit, mutate = false): LocalDate {
|
|
@@ -29,6 +29,7 @@ export interface LocalTimeComponents {
|
|
|
29
29
|
|
|
30
30
|
const weekStartsOn = 1 // mon, as per ISO
|
|
31
31
|
const MILLISECONDS_IN_WEEK = 604800000
|
|
32
|
+
const SECONDS_IN_DAY = 86400
|
|
32
33
|
// const MILLISECONDS_IN_DAY = 86400000
|
|
33
34
|
// const MILLISECONDS_IN_MINUTE = 60000
|
|
34
35
|
const VALID_DAYS_OF_WEEK = new Set([1, 2, 3, 4, 5, 6, 7])
|
|
@@ -39,7 +40,7 @@ const VALID_DAYS_OF_WEEK = new Set([1, 2, 3, 4, 5, 6, 7])
|
|
|
39
40
|
* @experimental
|
|
40
41
|
*/
|
|
41
42
|
export class LocalTime {
|
|
42
|
-
private constructor(private $date: Date
|
|
43
|
+
private constructor(private $date: Date) {}
|
|
43
44
|
|
|
44
45
|
/**
|
|
45
46
|
* Parses input String into LocalDate.
|
|
@@ -55,16 +56,6 @@ export class LocalTime {
|
|
|
55
56
|
return t
|
|
56
57
|
}
|
|
57
58
|
|
|
58
|
-
utc(): this {
|
|
59
|
-
this.utcMode = true
|
|
60
|
-
return this
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
local(): this {
|
|
64
|
-
this.utcMode = false
|
|
65
|
-
return this
|
|
66
|
-
}
|
|
67
|
-
|
|
68
59
|
/**
|
|
69
60
|
* Returns null if invalid
|
|
70
61
|
*/
|
|
@@ -92,7 +83,7 @@ export class LocalTime {
|
|
|
92
83
|
// date.setMinutes(date.getMinutes() + date.getTimezoneOffset())
|
|
93
84
|
// }
|
|
94
85
|
|
|
95
|
-
return new LocalTime(date
|
|
86
|
+
return new LocalTime(date)
|
|
96
87
|
}
|
|
97
88
|
|
|
98
89
|
static parseToDate(d: LocalTimeConfig): Date {
|
|
@@ -126,58 +117,58 @@ export class LocalTime {
|
|
|
126
117
|
}
|
|
127
118
|
|
|
128
119
|
static now(): LocalTime {
|
|
129
|
-
return new LocalTime(new Date()
|
|
120
|
+
return new LocalTime(new Date())
|
|
130
121
|
}
|
|
131
122
|
|
|
132
123
|
static fromComponents(
|
|
133
124
|
c: { year: number; month: number } & Partial<LocalTimeComponents>,
|
|
134
125
|
): LocalTime {
|
|
135
|
-
return new LocalTime(
|
|
126
|
+
return new LocalTime(
|
|
127
|
+
new Date(c.year, c.month - 1, c.day || 1, c.hour || 0, c.minute || 0, c.second || 0),
|
|
128
|
+
)
|
|
136
129
|
}
|
|
137
130
|
|
|
138
131
|
get(unit: LocalTimeUnit): number {
|
|
139
132
|
if (unit === 'year') {
|
|
140
|
-
return this
|
|
133
|
+
return this.$date.getFullYear()
|
|
141
134
|
}
|
|
142
135
|
if (unit === 'month') {
|
|
143
|
-
return
|
|
136
|
+
return this.$date.getMonth() + 1
|
|
144
137
|
}
|
|
145
138
|
if (unit === 'day') {
|
|
146
|
-
return this
|
|
139
|
+
return this.$date.getDate()
|
|
147
140
|
}
|
|
148
141
|
if (unit === 'hour') {
|
|
149
|
-
return this
|
|
142
|
+
return this.$date.getHours()
|
|
150
143
|
}
|
|
151
144
|
if (unit === 'minute') {
|
|
152
|
-
return this
|
|
145
|
+
return this.$date.getMinutes()
|
|
153
146
|
}
|
|
154
147
|
if (unit === 'week') {
|
|
155
148
|
return getWeek(this.$date)
|
|
156
149
|
}
|
|
157
150
|
// second
|
|
158
|
-
return this
|
|
151
|
+
return this.$date.getSeconds()
|
|
159
152
|
}
|
|
160
153
|
|
|
161
154
|
set(unit: LocalTimeUnit, v: number, mutate = false): LocalTime {
|
|
162
155
|
const t = mutate ? this : this.clone()
|
|
163
156
|
|
|
164
|
-
/* eslint-disable @typescript-eslint/no-unused-expressions */
|
|
165
157
|
if (unit === 'year') {
|
|
166
|
-
|
|
158
|
+
t.$date.setFullYear(v)
|
|
167
159
|
} else if (unit === 'month') {
|
|
168
|
-
|
|
160
|
+
t.$date.setMonth(v - 1)
|
|
169
161
|
} else if (unit === 'day') {
|
|
170
|
-
|
|
162
|
+
t.$date.setDate(v)
|
|
171
163
|
} else if (unit === 'hour') {
|
|
172
|
-
|
|
164
|
+
t.$date.setHours(v)
|
|
173
165
|
} else if (unit === 'minute') {
|
|
174
|
-
|
|
166
|
+
t.$date.setMinutes(v)
|
|
175
167
|
} else if (unit === 'second') {
|
|
176
|
-
|
|
168
|
+
t.$date.setSeconds(v)
|
|
177
169
|
} else if (unit === 'week') {
|
|
178
170
|
setWeek(t.$date, v, true)
|
|
179
171
|
}
|
|
180
|
-
/* eslint-enable @typescript-eslint/no-unused-expressions */
|
|
181
172
|
|
|
182
173
|
return t
|
|
183
174
|
}
|
|
@@ -238,28 +229,26 @@ export class LocalTime {
|
|
|
238
229
|
setComponents(c: Partial<LocalTimeComponents>, mutate = false): LocalTime {
|
|
239
230
|
const d = mutate ? this.$date : new Date(this.$date)
|
|
240
231
|
|
|
241
|
-
/* eslint-disable @typescript-eslint/no-unused-expressions */
|
|
242
232
|
if (c.year) {
|
|
243
|
-
|
|
233
|
+
d.setFullYear(c.year)
|
|
244
234
|
}
|
|
245
235
|
if (c.month) {
|
|
246
|
-
|
|
236
|
+
d.setMonth(c.month - 1)
|
|
247
237
|
}
|
|
248
238
|
if (c.day) {
|
|
249
|
-
|
|
239
|
+
d.setDate(c.day)
|
|
250
240
|
}
|
|
251
241
|
if (c.hour !== undefined) {
|
|
252
|
-
|
|
242
|
+
d.setHours(c.hour)
|
|
253
243
|
}
|
|
254
244
|
if (c.minute !== undefined) {
|
|
255
|
-
|
|
245
|
+
d.setMinutes(c.minute)
|
|
256
246
|
}
|
|
257
247
|
if (c.second !== undefined) {
|
|
258
|
-
|
|
248
|
+
d.setSeconds(c.second)
|
|
259
249
|
}
|
|
260
|
-
/* eslint-enable @typescript-eslint/no-unused-expressions */
|
|
261
250
|
|
|
262
|
-
return mutate ? this : new LocalTime(d
|
|
251
|
+
return mutate ? this : new LocalTime(d)
|
|
263
252
|
}
|
|
264
253
|
|
|
265
254
|
add(num: number, unit: LocalTimeUnit, mutate = false): LocalTime {
|
|
@@ -281,26 +270,45 @@ export class LocalTime {
|
|
|
281
270
|
diff(other: LocalTimeConfig, unit: LocalTimeUnit): number {
|
|
282
271
|
const date2 = LocalTime.parseToDate(other)
|
|
283
272
|
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
if (unit === 'month') {
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
273
|
+
const secDiff = (this.$date.valueOf() - date2.valueOf()) / 1000
|
|
274
|
+
if (!secDiff) return 0
|
|
275
|
+
|
|
276
|
+
if (unit === 'year' || unit === 'month') {
|
|
277
|
+
const sign = secDiff > 0 ? 1 : -1
|
|
278
|
+
|
|
279
|
+
// Put items in descending order: "big minus small"
|
|
280
|
+
const [big, small] = sign === 1 ? [this.$date, date2] : [date2, this.$date]
|
|
281
|
+
|
|
282
|
+
if (unit === 'year') {
|
|
283
|
+
let years = big.getFullYear() - small.getFullYear()
|
|
284
|
+
const big2 = new Date(big)
|
|
285
|
+
const small2 = new Date(small)
|
|
286
|
+
big2.setFullYear(1584)
|
|
287
|
+
small2.setFullYear(1584)
|
|
288
|
+
if (big2 < small2) years--
|
|
289
|
+
return years * sign || 0
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
if (unit === 'month') {
|
|
293
|
+
let months =
|
|
294
|
+
(big.getFullYear() - small.getFullYear()) * 12 + big.getMonth() - small.getMonth()
|
|
295
|
+
const big2 = new Date(big)
|
|
296
|
+
const small2 = new Date(small)
|
|
297
|
+
big2.setFullYear(1584, 0)
|
|
298
|
+
small2.setFullYear(1584, 0)
|
|
299
|
+
if (big2 < small2) months--
|
|
300
|
+
return months * sign || 0
|
|
301
|
+
}
|
|
293
302
|
}
|
|
294
303
|
|
|
295
|
-
const secDiff = (this.$date.valueOf() - date2.valueOf()) / 1000
|
|
296
304
|
let r
|
|
297
305
|
|
|
298
306
|
if (unit === 'day') {
|
|
299
|
-
r = secDiff /
|
|
307
|
+
r = secDiff / SECONDS_IN_DAY
|
|
300
308
|
} else if (unit === 'week') {
|
|
301
309
|
r = secDiff / (7 * 24 * 60 * 60)
|
|
302
310
|
} else if (unit === 'hour') {
|
|
303
|
-
r = secDiff /
|
|
311
|
+
r = secDiff / 3600
|
|
304
312
|
} else if (unit === 'minute') {
|
|
305
313
|
r = secDiff / 60
|
|
306
314
|
} else {
|
|
@@ -308,9 +316,8 @@ export class LocalTime {
|
|
|
308
316
|
r = secDiff
|
|
309
317
|
}
|
|
310
318
|
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
return r
|
|
319
|
+
// `|| 0` is to avoid returning -0
|
|
320
|
+
return Math.trunc(r) || 0
|
|
314
321
|
}
|
|
315
322
|
|
|
316
323
|
startOf(unit: LocalTimeUnit, mutate = false): LocalTime {
|
|
@@ -318,19 +325,18 @@ export class LocalTime {
|
|
|
318
325
|
const d = mutate ? this.$date : new Date(this.$date)
|
|
319
326
|
d.setSeconds(0, 0)
|
|
320
327
|
|
|
321
|
-
/* eslint-disable @typescript-eslint/no-unused-expressions */
|
|
322
328
|
if (unit !== 'minute') {
|
|
323
|
-
|
|
329
|
+
d.setMinutes(0)
|
|
324
330
|
if (unit !== 'hour') {
|
|
325
|
-
|
|
331
|
+
d.setHours(0)
|
|
326
332
|
if (unit !== 'day') {
|
|
327
333
|
// year, month or week
|
|
328
334
|
|
|
329
335
|
if (unit === 'year') {
|
|
330
|
-
|
|
331
|
-
|
|
336
|
+
d.setMonth(0)
|
|
337
|
+
d.setDate(1)
|
|
332
338
|
} else if (unit === 'month') {
|
|
333
|
-
|
|
339
|
+
d.setDate(1)
|
|
334
340
|
} else {
|
|
335
341
|
// week
|
|
336
342
|
startOfWeek(d, true)
|
|
@@ -338,9 +344,8 @@ export class LocalTime {
|
|
|
338
344
|
}
|
|
339
345
|
}
|
|
340
346
|
}
|
|
341
|
-
/* eslint-enable @typescript-eslint/no-unused-expressions */
|
|
342
347
|
|
|
343
|
-
return mutate ? this : new LocalTime(d
|
|
348
|
+
return mutate ? this : new LocalTime(d)
|
|
344
349
|
}
|
|
345
350
|
|
|
346
351
|
endOf(unit: LocalTimeUnit, mutate = false): LocalTime {
|
|
@@ -348,16 +353,15 @@ export class LocalTime {
|
|
|
348
353
|
const d = mutate ? this.$date : new Date(this.$date)
|
|
349
354
|
d.setSeconds(59, 0)
|
|
350
355
|
|
|
351
|
-
/* eslint-disable @typescript-eslint/no-unused-expressions */
|
|
352
356
|
if (unit !== 'minute') {
|
|
353
|
-
|
|
357
|
+
d.setMinutes(59)
|
|
354
358
|
if (unit !== 'hour') {
|
|
355
|
-
|
|
359
|
+
d.setHours(23)
|
|
356
360
|
if (unit !== 'day') {
|
|
357
361
|
// year, month or week
|
|
358
362
|
|
|
359
363
|
if (unit === 'year') {
|
|
360
|
-
|
|
364
|
+
d.setMonth(11)
|
|
361
365
|
}
|
|
362
366
|
|
|
363
367
|
if (unit === 'week') {
|
|
@@ -365,14 +369,13 @@ export class LocalTime {
|
|
|
365
369
|
} else {
|
|
366
370
|
// year or month
|
|
367
371
|
const lastDay = LocalDate.getMonthLength(d.getFullYear(), d.getMonth() + 1)
|
|
368
|
-
|
|
372
|
+
d.setDate(lastDay)
|
|
369
373
|
}
|
|
370
374
|
}
|
|
371
375
|
}
|
|
372
376
|
}
|
|
373
|
-
/* eslint-enable @typescript-eslint/no-unused-expressions */
|
|
374
377
|
|
|
375
|
-
return mutate ? this : new LocalTime(d
|
|
378
|
+
return mutate ? this : new LocalTime(d)
|
|
376
379
|
}
|
|
377
380
|
|
|
378
381
|
static sort(items: LocalTime[], mutate = false, descending = false): LocalTime[] {
|
|
@@ -452,17 +455,6 @@ export class LocalTime {
|
|
|
452
455
|
}
|
|
453
456
|
|
|
454
457
|
components(): LocalTimeComponents {
|
|
455
|
-
if (this.utcMode) {
|
|
456
|
-
return {
|
|
457
|
-
year: this.$date.getUTCFullYear(),
|
|
458
|
-
month: this.$date.getUTCMonth() + 1,
|
|
459
|
-
day: this.$date.getUTCDate(),
|
|
460
|
-
hour: this.$date.getUTCHours(),
|
|
461
|
-
minute: this.$date.getUTCMinutes(),
|
|
462
|
-
second: this.$date.getSeconds(),
|
|
463
|
-
}
|
|
464
|
-
}
|
|
465
|
-
|
|
466
458
|
return {
|
|
467
459
|
year: this.$date.getFullYear(),
|
|
468
460
|
month: this.$date.getMonth() + 1,
|
|
@@ -490,7 +482,7 @@ export class LocalTime {
|
|
|
490
482
|
}
|
|
491
483
|
|
|
492
484
|
clone(): LocalTime {
|
|
493
|
-
return new LocalTime(new Date(this.$date)
|
|
485
|
+
return new LocalTime(new Date(this.$date))
|
|
494
486
|
}
|
|
495
487
|
|
|
496
488
|
unix(): UnixTimestampNumber {
|
|
@@ -506,14 +498,6 @@ export class LocalTime {
|
|
|
506
498
|
}
|
|
507
499
|
|
|
508
500
|
toLocalDate(): LocalDate {
|
|
509
|
-
if (this.utcMode) {
|
|
510
|
-
return LocalDate.create(
|
|
511
|
-
this.$date.getUTCFullYear(),
|
|
512
|
-
this.$date.getUTCMonth() + 1,
|
|
513
|
-
this.$date.getUTCDate(),
|
|
514
|
-
)
|
|
515
|
-
}
|
|
516
|
-
|
|
517
501
|
return LocalDate.create(
|
|
518
502
|
this.$date.getFullYear(),
|
|
519
503
|
this.$date.getMonth() + 1,
|