@naturalcycles/js-lib 14.96.1 → 14.98.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/datetime/dateInterval.d.ts +8 -7
- package/dist/datetime/dateInterval.js +18 -15
- package/dist/datetime/localDate.d.ts +20 -17
- package/dist/datetime/localDate.js +130 -102
- package/dist/datetime/localTime.d.ts +14 -12
- package/dist/datetime/localTime.js +34 -11
- package/dist/datetime/timeInterval.d.ts +38 -0
- package/dist/datetime/timeInterval.js +91 -0
- package/dist/index.d.ts +4 -2
- package/dist/index.js +1 -0
- package/dist/json-schema/jsonSchemaBuilder.d.ts +2 -2
- package/dist/math/math.util.d.ts +4 -0
- package/dist/math/math.util.js +8 -1
- package/dist/math/sma.d.ts +4 -0
- package/dist/math/sma.js +4 -0
- package/dist/types.d.ts +19 -11
- package/dist-esm/datetime/dateInterval.js +18 -15
- package/dist-esm/datetime/localDate.js +130 -102
- package/dist-esm/datetime/localTime.js +34 -11
- package/dist-esm/datetime/timeInterval.js +87 -0
- package/dist-esm/index.js +1 -0
- package/dist-esm/math/math.util.js +6 -0
- package/dist-esm/math/sma.js +4 -0
- package/package.json +1 -1
- package/src/datetime/dateInterval.ts +22 -18
- package/src/datetime/localDate.ts +149 -132
- package/src/datetime/localTime.ts +48 -22
- package/src/datetime/timeInterval.ts +104 -0
- package/src/index.ts +10 -2
- package/src/json-schema/jsonSchemaBuilder.ts +6 -2
- package/src/math/math.util.ts +7 -0
- package/src/math/sma.ts +4 -0
- package/src/types.ts +23 -11
|
@@ -1,20 +1,22 @@
|
|
|
1
1
|
import { _assert } from '../error/assert'
|
|
2
|
-
import {
|
|
3
|
-
import { END, IsoDate, UnixTimestamp } from '../types'
|
|
2
|
+
import { IsoDateString, UnixTimestampNumber } from '../types'
|
|
4
3
|
import { LocalTime } from './localTime'
|
|
5
4
|
|
|
6
5
|
export type LocalDateUnit = 'year' | 'month' | 'day'
|
|
7
6
|
export type Inclusiveness = '()' | '[]' | '[)' | '(]'
|
|
8
7
|
|
|
9
|
-
const
|
|
8
|
+
const MDAYS = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
|
|
9
|
+
const DATE_REGEX = /^(\d\d\d\d)-(\d\d)-(\d\d)$/
|
|
10
10
|
|
|
11
|
-
export type LocalDateConfig = LocalDate |
|
|
11
|
+
export type LocalDateConfig = LocalDate | IsoDateString
|
|
12
|
+
|
|
13
|
+
/* eslint-disable no-dupe-class-members */
|
|
12
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)
|
|
@@ -59,8 +61,13 @@ export class LocalDate {
|
|
|
59
61
|
if (!d) return null
|
|
60
62
|
if (d instanceof LocalDate) return d
|
|
61
63
|
|
|
62
|
-
//
|
|
63
|
-
const
|
|
64
|
+
// const [year, month, day] = d.slice(0, 10).split('-').map(Number)
|
|
65
|
+
const matches = DATE_REGEX.exec(d.slice(0, 10))
|
|
66
|
+
if (!matches) return null
|
|
67
|
+
|
|
68
|
+
const year = Number(matches[1])
|
|
69
|
+
const month = Number(matches[2])
|
|
70
|
+
const day = Number(matches[3])
|
|
64
71
|
|
|
65
72
|
if (
|
|
66
73
|
!year ||
|
|
@@ -77,6 +84,11 @@ export class LocalDate {
|
|
|
77
84
|
return new LocalDate(year, month, day)
|
|
78
85
|
}
|
|
79
86
|
|
|
87
|
+
// Can use just .toString()
|
|
88
|
+
// static parseToString(d: LocalDateConfig): IsoDateString {
|
|
89
|
+
// return typeof d === 'string' ? d : d.toString()
|
|
90
|
+
// }
|
|
91
|
+
|
|
80
92
|
static isValid(iso: string | undefined | null): boolean {
|
|
81
93
|
return this.parseOrNull(iso) !== null
|
|
82
94
|
}
|
|
@@ -115,81 +127,83 @@ export class LocalDate {
|
|
|
115
127
|
}
|
|
116
128
|
|
|
117
129
|
static range(
|
|
118
|
-
|
|
119
|
-
|
|
130
|
+
min: LocalDateConfig,
|
|
131
|
+
max: LocalDateConfig,
|
|
132
|
+
incl: Inclusiveness = '[)',
|
|
120
133
|
step = 1,
|
|
121
134
|
stepUnit: LocalDateUnit = 'day',
|
|
122
135
|
): LocalDate[] {
|
|
123
|
-
const
|
|
124
|
-
|
|
125
|
-
const max = LocalDate.of(
|
|
136
|
+
const dates: LocalDate[] = []
|
|
137
|
+
const $min = LocalDate.of(min)
|
|
138
|
+
const $max = LocalDate.of(max).startOf(stepUnit)
|
|
139
|
+
|
|
140
|
+
let current = $min.startOf(stepUnit)
|
|
141
|
+
if (current.isAfter($min, incl[0] === '[')) {
|
|
142
|
+
// ok
|
|
143
|
+
} else {
|
|
144
|
+
current.add(1, stepUnit, true)
|
|
145
|
+
}
|
|
126
146
|
|
|
127
|
-
|
|
128
|
-
|
|
147
|
+
const incl2 = incl[1] === ']'
|
|
148
|
+
while (current.isBefore($max, incl2)) {
|
|
149
|
+
dates.push(current)
|
|
129
150
|
current = current.add(step, stepUnit)
|
|
130
|
-
}
|
|
151
|
+
}
|
|
131
152
|
|
|
132
|
-
return
|
|
153
|
+
return dates
|
|
133
154
|
}
|
|
134
155
|
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
maxExcl: LocalDateConfig,
|
|
138
|
-
step = 1,
|
|
139
|
-
stepUnit: LocalDateUnit = 'day',
|
|
140
|
-
): Sequence<LocalDate> {
|
|
141
|
-
const min = LocalDate.of(minIncl).startOf(stepUnit)
|
|
142
|
-
const max = LocalDate.of(maxExcl).startOf(stepUnit)
|
|
143
|
-
return Sequence.create(min, d => {
|
|
144
|
-
const next = d.add(step, stepUnit)
|
|
145
|
-
return next.isAfter(max) ? END : next
|
|
146
|
-
})
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
static rangeString(
|
|
150
|
-
minIncl: LocalDateConfig,
|
|
151
|
-
maxExcl: LocalDateConfig,
|
|
152
|
-
step = 1,
|
|
153
|
-
stepUnit: LocalDateUnit = 'day',
|
|
154
|
-
): IsoDate[] {
|
|
155
|
-
return LocalDate.range(minIncl, maxExcl, step, stepUnit).map(ld => ld.toString())
|
|
156
|
+
get(unit: LocalDateUnit): number {
|
|
157
|
+
return unit === 'year' ? this.$year : unit === 'month' ? this.$month : this.$day
|
|
156
158
|
}
|
|
157
159
|
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
160
|
+
set(unit: LocalDateUnit, v: number, mutate = false): LocalDate {
|
|
161
|
+
const t = mutate ? this : this.clone()
|
|
162
|
+
|
|
163
|
+
if (unit === 'year') {
|
|
164
|
+
t.$year = v
|
|
165
|
+
} else if (unit === 'month') {
|
|
166
|
+
t.$month = v
|
|
167
|
+
} else {
|
|
168
|
+
t.$day = v
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
return t
|
|
165
172
|
}
|
|
166
173
|
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
):
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
)
|
|
174
|
+
year(): number
|
|
175
|
+
year(v: number): LocalDate
|
|
176
|
+
year(v?: number): number | LocalDate {
|
|
177
|
+
return v === undefined ? this.$year : this.set('year', v)
|
|
178
|
+
}
|
|
179
|
+
month(): number
|
|
180
|
+
month(v: number): LocalDate
|
|
181
|
+
month(v?: number): number | LocalDate {
|
|
182
|
+
return v === undefined ? this.$month : this.set('month', v)
|
|
183
|
+
}
|
|
184
|
+
day(): number
|
|
185
|
+
day(v: number): LocalDate
|
|
186
|
+
day(v?: number): number | LocalDate {
|
|
187
|
+
return v === undefined ? this.$day : this.set('day', v)
|
|
176
188
|
}
|
|
177
189
|
|
|
178
190
|
isSame(d: LocalDateConfig): boolean {
|
|
179
191
|
d = LocalDate.of(d)
|
|
180
|
-
return this
|
|
192
|
+
return this.$day === d.$day && this.$month === d.$month && this.$year === d.$year
|
|
181
193
|
}
|
|
182
194
|
|
|
183
|
-
isBefore(d: LocalDateConfig): boolean {
|
|
184
|
-
|
|
195
|
+
isBefore(d: LocalDateConfig, inclusive = false): boolean {
|
|
196
|
+
const r = this.cmp(d)
|
|
197
|
+
return r === -1 || (r === 0 && inclusive)
|
|
185
198
|
}
|
|
186
199
|
|
|
187
200
|
isSameOrBefore(d: LocalDateConfig): boolean {
|
|
188
201
|
return this.cmp(d) <= 0
|
|
189
202
|
}
|
|
190
203
|
|
|
191
|
-
isAfter(d: LocalDateConfig): boolean {
|
|
192
|
-
|
|
204
|
+
isAfter(d: LocalDateConfig, inclusive = false): boolean {
|
|
205
|
+
const r = this.cmp(d)
|
|
206
|
+
return r === 1 || (r === 0 && inclusive)
|
|
193
207
|
}
|
|
194
208
|
|
|
195
209
|
isSameOrAfter(d: LocalDateConfig): boolean {
|
|
@@ -211,12 +225,12 @@ export class LocalDate {
|
|
|
211
225
|
*/
|
|
212
226
|
cmp(d: LocalDateConfig): -1 | 0 | 1 {
|
|
213
227
|
d = LocalDate.of(d)
|
|
214
|
-
if (this
|
|
215
|
-
if (this
|
|
216
|
-
if (this
|
|
217
|
-
if (this
|
|
218
|
-
if (this
|
|
219
|
-
if (this
|
|
228
|
+
if (this.$year < d.$year) return -1
|
|
229
|
+
if (this.$year > d.$year) return 1
|
|
230
|
+
if (this.$month < d.$month) return -1
|
|
231
|
+
if (this.$month > d.$month) return 1
|
|
232
|
+
if (this.$day < d.$day) return -1
|
|
233
|
+
if (this.$day > d.$day) return 1
|
|
220
234
|
return 0
|
|
221
235
|
}
|
|
222
236
|
|
|
@@ -236,33 +250,33 @@ export class LocalDate {
|
|
|
236
250
|
d = LocalDate.of(d)
|
|
237
251
|
|
|
238
252
|
if (unit === 'year') {
|
|
239
|
-
return this
|
|
253
|
+
return this.$year - d.$year
|
|
240
254
|
}
|
|
241
255
|
|
|
242
256
|
if (unit === 'month') {
|
|
243
|
-
return (this
|
|
257
|
+
return (this.$year - d.$year) * 12 + (this.$month - d.$month)
|
|
244
258
|
}
|
|
245
259
|
|
|
246
260
|
// unit is 'day'
|
|
247
|
-
let days = this
|
|
261
|
+
let days = this.$day - d.$day
|
|
248
262
|
|
|
249
|
-
if (d
|
|
250
|
-
for (let year = d
|
|
263
|
+
if (d.$year < this.$year) {
|
|
264
|
+
for (let year = d.$year; year < this.$year; year++) {
|
|
251
265
|
days += LocalDate.getYearLength(year)
|
|
252
266
|
}
|
|
253
|
-
} else if (this
|
|
254
|
-
for (let year = this
|
|
267
|
+
} else if (this.$year < d.$year) {
|
|
268
|
+
for (let year = this.$year; year < d.$year; year++) {
|
|
255
269
|
days -= LocalDate.getYearLength(year)
|
|
256
270
|
}
|
|
257
271
|
}
|
|
258
272
|
|
|
259
|
-
if (d
|
|
260
|
-
for (let month = d
|
|
261
|
-
days += LocalDate.getMonthLength(this
|
|
273
|
+
if (d.$month < this.$month) {
|
|
274
|
+
for (let month = d.$month; month < this.$month; month++) {
|
|
275
|
+
days += LocalDate.getMonthLength(this.$year, month)
|
|
262
276
|
}
|
|
263
|
-
} else if (this
|
|
264
|
-
for (let month = this
|
|
265
|
-
days -= LocalDate.getMonthLength(d
|
|
277
|
+
} else if (this.$month < d.$month) {
|
|
278
|
+
for (let month = this.$month; month < d.$month; month++) {
|
|
279
|
+
days -= LocalDate.getMonthLength(d.$year, month)
|
|
266
280
|
}
|
|
267
281
|
}
|
|
268
282
|
|
|
@@ -270,57 +284,62 @@ export class LocalDate {
|
|
|
270
284
|
}
|
|
271
285
|
|
|
272
286
|
add(num: number, unit: LocalDateUnit, mutate = false): LocalDate {
|
|
273
|
-
let { day, month, year } = this
|
|
287
|
+
let { $day, $month, $year } = this
|
|
274
288
|
|
|
275
289
|
if (unit === 'day') {
|
|
276
|
-
day += num
|
|
290
|
+
$day += num
|
|
277
291
|
} else if (unit === 'month') {
|
|
278
|
-
month += num
|
|
292
|
+
$month += num
|
|
279
293
|
} else if (unit === 'year') {
|
|
280
|
-
year += num
|
|
294
|
+
$year += num
|
|
281
295
|
}
|
|
282
296
|
|
|
283
297
|
// check day overflow
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
298
|
+
if (unit === 'day') {
|
|
299
|
+
if ($day < 1) {
|
|
300
|
+
while ($day < 1) {
|
|
301
|
+
$month -= 1
|
|
302
|
+
if ($month < 1) {
|
|
303
|
+
$year -= 1
|
|
304
|
+
$month += 12
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
$day += LocalDate.getMonthLength($year, $month)
|
|
308
|
+
}
|
|
309
|
+
} else {
|
|
310
|
+
let monLen = LocalDate.getMonthLength($year, $month)
|
|
311
|
+
|
|
312
|
+
while ($day > monLen) {
|
|
313
|
+
$day -= monLen
|
|
314
|
+
$month += 1
|
|
315
|
+
if ($month > 12) {
|
|
316
|
+
$year += 1
|
|
317
|
+
$month -= 12
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
monLen = LocalDate.getMonthLength($year, $month)
|
|
321
|
+
}
|
|
301
322
|
}
|
|
302
|
-
|
|
303
|
-
monLen = LocalDate.getMonthLength(year, month)
|
|
304
323
|
}
|
|
305
324
|
|
|
306
325
|
// check month overflow
|
|
307
|
-
while (month > 12) {
|
|
308
|
-
year += 1
|
|
309
|
-
month -= 12
|
|
326
|
+
while ($month > 12) {
|
|
327
|
+
$year += 1
|
|
328
|
+
$month -= 12
|
|
310
329
|
}
|
|
311
|
-
while (month < 1) {
|
|
312
|
-
year -= 1
|
|
313
|
-
month += 12
|
|
330
|
+
while ($month < 1) {
|
|
331
|
+
$year -= 1
|
|
332
|
+
$month += 12
|
|
314
333
|
}
|
|
315
334
|
|
|
316
335
|
if (mutate) {
|
|
317
|
-
this
|
|
318
|
-
this
|
|
319
|
-
this
|
|
336
|
+
this.$year = $year
|
|
337
|
+
this.$month = $month
|
|
338
|
+
this.$day = $day
|
|
320
339
|
return this
|
|
321
340
|
}
|
|
322
341
|
|
|
323
|
-
return new LocalDate(year, month, day)
|
|
342
|
+
return new LocalDate($year, $month, $day)
|
|
324
343
|
}
|
|
325
344
|
|
|
326
345
|
subtract(num: number, unit: LocalDateUnit, mutate = false): LocalDate {
|
|
@@ -329,21 +348,21 @@ export class LocalDate {
|
|
|
329
348
|
|
|
330
349
|
startOf(unit: LocalDateUnit): LocalDate {
|
|
331
350
|
if (unit === 'day') return this
|
|
332
|
-
if (unit === 'month') return LocalDate.create(this
|
|
351
|
+
if (unit === 'month') return LocalDate.create(this.$year, this.$month, 1)
|
|
333
352
|
// year
|
|
334
|
-
return LocalDate.create(this
|
|
353
|
+
return LocalDate.create(this.$year, 1, 1)
|
|
335
354
|
}
|
|
336
355
|
|
|
337
356
|
endOf(unit: LocalDateUnit): LocalDate {
|
|
338
357
|
if (unit === 'day') return this
|
|
339
358
|
if (unit === 'month')
|
|
340
359
|
return LocalDate.create(
|
|
341
|
-
this
|
|
342
|
-
this
|
|
343
|
-
LocalDate.getMonthLength(this
|
|
360
|
+
this.$year,
|
|
361
|
+
this.$month,
|
|
362
|
+
LocalDate.getMonthLength(this.$year, this.$month),
|
|
344
363
|
)
|
|
345
364
|
// year
|
|
346
|
-
return LocalDate.create(this
|
|
365
|
+
return LocalDate.create(this.$year, 12, 31)
|
|
347
366
|
}
|
|
348
367
|
|
|
349
368
|
static getYearLength(year: number): number {
|
|
@@ -352,17 +371,15 @@ export class LocalDate {
|
|
|
352
371
|
|
|
353
372
|
static getMonthLength(year: number, month: number): number {
|
|
354
373
|
if (month === 2) return this.isLeapYear(year) ? 29 : 28
|
|
355
|
-
return
|
|
374
|
+
return MDAYS[month]!
|
|
356
375
|
}
|
|
357
376
|
|
|
358
377
|
static isLeapYear(year: number): boolean {
|
|
359
|
-
|
|
360
|
-
if (year % 100 !== 0) return true
|
|
361
|
-
return year % 400 === 0
|
|
378
|
+
return year % 4 === 0 && (year % 100 !== 0 || year % 400 === 0)
|
|
362
379
|
}
|
|
363
380
|
|
|
364
381
|
clone(): LocalDate {
|
|
365
|
-
return new LocalDate(this
|
|
382
|
+
return new LocalDate(this.$year, this.$month, this.$day)
|
|
366
383
|
}
|
|
367
384
|
|
|
368
385
|
/**
|
|
@@ -372,35 +389,35 @@ export class LocalDate {
|
|
|
372
389
|
* Timezone will match local timezone.
|
|
373
390
|
*/
|
|
374
391
|
toDate(): Date {
|
|
375
|
-
return new Date(this
|
|
392
|
+
return new Date(this.$year, this.$month - 1, this.$day)
|
|
376
393
|
}
|
|
377
394
|
|
|
378
395
|
toLocalTime(): LocalTime {
|
|
379
396
|
return LocalTime.of(this.toDate())
|
|
380
397
|
}
|
|
381
398
|
|
|
382
|
-
toISODate():
|
|
399
|
+
toISODate(): IsoDateString {
|
|
383
400
|
return this.toString()
|
|
384
401
|
}
|
|
385
402
|
|
|
386
|
-
toString():
|
|
403
|
+
toString(): IsoDateString {
|
|
387
404
|
return [
|
|
388
|
-
String(this
|
|
389
|
-
String(this
|
|
390
|
-
String(this
|
|
405
|
+
String(this.$year).padStart(4, '0'),
|
|
406
|
+
String(this.$month).padStart(2, '0'),
|
|
407
|
+
String(this.$day).padStart(2, '0'),
|
|
391
408
|
].join('-')
|
|
392
409
|
}
|
|
393
410
|
|
|
394
411
|
toStringCompact(): string {
|
|
395
412
|
return [
|
|
396
|
-
String(this
|
|
397
|
-
String(this
|
|
398
|
-
String(this
|
|
413
|
+
String(this.$year).padStart(4, '0'),
|
|
414
|
+
String(this.$month).padStart(2, '0'),
|
|
415
|
+
String(this.$day).padStart(2, '0'),
|
|
399
416
|
].join('')
|
|
400
417
|
}
|
|
401
418
|
|
|
402
419
|
// May be not optimal, as LocalTime better suits it
|
|
403
|
-
unix():
|
|
420
|
+
unix(): UnixTimestampNumber {
|
|
404
421
|
return Math.floor(this.toDate().valueOf() / 1000)
|
|
405
422
|
}
|
|
406
423
|
|
|
@@ -408,7 +425,7 @@ export class LocalDate {
|
|
|
408
425
|
return this.toDate().valueOf()
|
|
409
426
|
}
|
|
410
427
|
|
|
411
|
-
toJSON():
|
|
428
|
+
toJSON(): IsoDateString {
|
|
412
429
|
return this.toString()
|
|
413
430
|
}
|
|
414
431
|
}
|
|
@@ -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
|
|
@@ -62,7 +62,7 @@ export class LocalTime {
|
|
|
62
62
|
} else if (typeof d === 'number') {
|
|
63
63
|
date = new Date(d * 1000)
|
|
64
64
|
} else {
|
|
65
|
-
date = new Date(d)
|
|
65
|
+
date = new Date(d.slice(0, 19))
|
|
66
66
|
}
|
|
67
67
|
|
|
68
68
|
// validation
|
|
@@ -78,6 +78,32 @@ export class LocalTime {
|
|
|
78
78
|
return new LocalTime(date, false)
|
|
79
79
|
}
|
|
80
80
|
|
|
81
|
+
static parseToDate(d: LocalTimeConfig): Date {
|
|
82
|
+
if (d instanceof LocalTime) return d.$date
|
|
83
|
+
if (d instanceof Date) return d
|
|
84
|
+
|
|
85
|
+
const date = typeof d === 'number' ? new Date(d * 1000) : new Date(d)
|
|
86
|
+
|
|
87
|
+
if (isNaN(date.getDate())) {
|
|
88
|
+
throw new TypeError(`Cannot parse "${d}" to Date`)
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
return date
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
static parseToUnixTimestamp(d: LocalTimeConfig): UnixTimestampNumber {
|
|
95
|
+
if (typeof d === 'number') return d
|
|
96
|
+
if (d instanceof LocalTime) return d.unix()
|
|
97
|
+
|
|
98
|
+
const date = d instanceof Date ? d : new Date(d)
|
|
99
|
+
|
|
100
|
+
if (isNaN(date.getDate())) {
|
|
101
|
+
throw new TypeError(`Cannot parse "${d}" to UnixTimestamp`)
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
return date.valueOf() / 1000
|
|
105
|
+
}
|
|
106
|
+
|
|
81
107
|
static isValid(d: LocalTimeConfig | undefined | null): boolean {
|
|
82
108
|
return this.parseOrNull(d) !== null
|
|
83
109
|
}
|
|
@@ -144,9 +170,9 @@ export class LocalTime {
|
|
|
144
170
|
month(v?: number): number | LocalTime {
|
|
145
171
|
return v === undefined ? this.get('month') : this.set('month', v)
|
|
146
172
|
}
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
173
|
+
day(): number
|
|
174
|
+
day(v: number): LocalTime
|
|
175
|
+
day(v?: number): number | LocalTime {
|
|
150
176
|
return v === undefined ? this.get('day') : this.set('day', v)
|
|
151
177
|
}
|
|
152
178
|
hour(): number
|
|
@@ -205,7 +231,7 @@ export class LocalTime {
|
|
|
205
231
|
}
|
|
206
232
|
|
|
207
233
|
diff(other: LocalTimeConfig, unit: LocalTimeUnit): number {
|
|
208
|
-
const date2 = LocalTime.
|
|
234
|
+
const date2 = LocalTime.parseToDate(other)
|
|
209
235
|
|
|
210
236
|
if (unit === 'year') {
|
|
211
237
|
return this.$date.getFullYear() - date2.getFullYear()
|
|
@@ -295,16 +321,18 @@ export class LocalTime {
|
|
|
295
321
|
return this.cmp(d) === 0
|
|
296
322
|
}
|
|
297
323
|
|
|
298
|
-
isBefore(d: LocalTimeConfig): boolean {
|
|
299
|
-
|
|
324
|
+
isBefore(d: LocalTimeConfig, inclusive = false): boolean {
|
|
325
|
+
const r = this.cmp(d)
|
|
326
|
+
return r === -1 || (r === 0 && inclusive)
|
|
300
327
|
}
|
|
301
328
|
|
|
302
329
|
isSameOrBefore(d: LocalTimeConfig): boolean {
|
|
303
330
|
return this.cmp(d) <= 0
|
|
304
331
|
}
|
|
305
332
|
|
|
306
|
-
isAfter(d: LocalTimeConfig): boolean {
|
|
307
|
-
|
|
333
|
+
isAfter(d: LocalTimeConfig, inclusive = false): boolean {
|
|
334
|
+
const r = this.cmp(d)
|
|
335
|
+
return r === 1 || (r === 0 && inclusive)
|
|
308
336
|
}
|
|
309
337
|
|
|
310
338
|
isSameOrAfter(d: LocalTimeConfig): boolean {
|
|
@@ -326,7 +354,7 @@ export class LocalTime {
|
|
|
326
354
|
*/
|
|
327
355
|
cmp(d: LocalTimeConfig): -1 | 0 | 1 {
|
|
328
356
|
const t1 = this.$date.valueOf()
|
|
329
|
-
const t2 = LocalTime.
|
|
357
|
+
const t2 = LocalTime.parseToDate(d).valueOf()
|
|
330
358
|
if (t1 === t2) return 0
|
|
331
359
|
return t1 < t2 ? -1 : 1
|
|
332
360
|
}
|
|
@@ -355,8 +383,8 @@ export class LocalTime {
|
|
|
355
383
|
}
|
|
356
384
|
}
|
|
357
385
|
|
|
358
|
-
fromNow(now: LocalTimeConfig =
|
|
359
|
-
const msDiff = LocalTime.
|
|
386
|
+
fromNow(now: LocalTimeConfig = new Date()): string {
|
|
387
|
+
const msDiff = LocalTime.parseToDate(now).valueOf() - this.$date.valueOf()
|
|
360
388
|
|
|
361
389
|
if (msDiff === 0) return 'now'
|
|
362
390
|
|
|
@@ -375,7 +403,7 @@ export class LocalTime {
|
|
|
375
403
|
return new LocalTime(new Date(this.$date), this.utcMode)
|
|
376
404
|
}
|
|
377
405
|
|
|
378
|
-
unix():
|
|
406
|
+
unix(): UnixTimestampNumber {
|
|
379
407
|
return Math.floor(this.$date.valueOf() / 1000)
|
|
380
408
|
}
|
|
381
409
|
|
|
@@ -383,7 +411,7 @@ export class LocalTime {
|
|
|
383
411
|
return this.$date.valueOf()
|
|
384
412
|
}
|
|
385
413
|
|
|
386
|
-
valueOf():
|
|
414
|
+
valueOf(): UnixTimestampNumber {
|
|
387
415
|
return Math.floor(this.$date.valueOf() / 1000)
|
|
388
416
|
}
|
|
389
417
|
|
|
@@ -403,7 +431,7 @@ export class LocalTime {
|
|
|
403
431
|
)
|
|
404
432
|
}
|
|
405
433
|
|
|
406
|
-
toPretty(seconds = true):
|
|
434
|
+
toPretty(seconds = true): IsoDateTimeString {
|
|
407
435
|
const { year, month, day, hour, minute, second } = this.components()
|
|
408
436
|
|
|
409
437
|
return (
|
|
@@ -432,14 +460,14 @@ export class LocalTime {
|
|
|
432
460
|
/**
|
|
433
461
|
* Returns e.g: `1984-06-21T17:56:21`, only the date part of DateTime
|
|
434
462
|
*/
|
|
435
|
-
toISODateTime():
|
|
463
|
+
toISODateTime(): IsoDateTimeString {
|
|
436
464
|
return this.$date.toISOString().slice(0, 19)
|
|
437
465
|
}
|
|
438
466
|
|
|
439
467
|
/**
|
|
440
468
|
* Returns e.g: `1984-06-21`, only the date part of DateTime
|
|
441
469
|
*/
|
|
442
|
-
toISODate():
|
|
470
|
+
toISODate(): IsoDateString {
|
|
443
471
|
const { year, month, day } = this.components()
|
|
444
472
|
|
|
445
473
|
return [
|
|
@@ -488,7 +516,7 @@ export class LocalTime {
|
|
|
488
516
|
return String(this.unix())
|
|
489
517
|
}
|
|
490
518
|
|
|
491
|
-
toJSON():
|
|
519
|
+
toJSON(): UnixTimestampNumber {
|
|
492
520
|
return this.unix()
|
|
493
521
|
}
|
|
494
522
|
}
|
|
@@ -499,5 +527,3 @@ export class LocalTime {
|
|
|
499
527
|
export function localTime(d?: LocalTimeConfig): LocalTime {
|
|
500
528
|
return d ? LocalTime.of(d) : LocalTime.now()
|
|
501
529
|
}
|
|
502
|
-
|
|
503
|
-
// todo: range
|