@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,5 +1,5 @@
|
|
|
1
|
-
import { LocalDate, LocalDateConfig } from './localDate';
|
|
2
|
-
export declare type DateIntervalConfig = DateInterval |
|
|
1
|
+
import { Inclusiveness, LocalDate, LocalDateConfig, LocalDateUnit } from './localDate';
|
|
2
|
+
export declare type DateIntervalConfig = DateInterval | DateIntervalString;
|
|
3
3
|
export declare type DateIntervalString = string;
|
|
4
4
|
/**
|
|
5
5
|
* Class that supports ISO8601 "Time interval" standard that looks like `2022-02-24/2022-03-30`.
|
|
@@ -16,24 +16,25 @@ export declare class DateInterval {
|
|
|
16
16
|
*/
|
|
17
17
|
static parse(d: DateIntervalConfig): DateInterval;
|
|
18
18
|
isSame(d: DateIntervalConfig): boolean;
|
|
19
|
-
isBefore(d: DateIntervalConfig): boolean;
|
|
19
|
+
isBefore(d: DateIntervalConfig, inclusive?: boolean): boolean;
|
|
20
20
|
isSameOrBefore(d: DateIntervalConfig): boolean;
|
|
21
|
-
isAfter(d: DateIntervalConfig): boolean;
|
|
21
|
+
isAfter(d: DateIntervalConfig, inclusive?: boolean): boolean;
|
|
22
22
|
isSameOrAfter(d: DateIntervalConfig): boolean;
|
|
23
23
|
/**
|
|
24
24
|
* Ranges of DateInterval (start, end) are INCLUSIVE.
|
|
25
25
|
*/
|
|
26
|
-
includes(d: LocalDateConfig): boolean;
|
|
26
|
+
includes(d: LocalDateConfig, incl?: Inclusiveness): boolean;
|
|
27
|
+
intersects(int: DateIntervalConfig, inclusive?: boolean): boolean;
|
|
27
28
|
/**
|
|
28
29
|
* DateIntervals compare by start date.
|
|
29
30
|
* If it's the same - then by end date.
|
|
30
31
|
*/
|
|
31
32
|
cmp(d: DateIntervalConfig): -1 | 0 | 1;
|
|
33
|
+
getDays(incl?: Inclusiveness): LocalDate[];
|
|
32
34
|
/**
|
|
33
35
|
* Returns an array of LocalDates that are included in the interval.
|
|
34
|
-
* Ranges are INCLUSIVE.
|
|
35
36
|
*/
|
|
36
|
-
|
|
37
|
+
range(incl?: Inclusiveness, step?: number, stepUnit?: LocalDateUnit): LocalDate[];
|
|
37
38
|
toString(): DateIntervalString;
|
|
38
39
|
toJSON(): DateIntervalString;
|
|
39
40
|
}
|
|
@@ -30,14 +30,16 @@ class DateInterval {
|
|
|
30
30
|
isSame(d) {
|
|
31
31
|
return this.cmp(d) === 0;
|
|
32
32
|
}
|
|
33
|
-
isBefore(d) {
|
|
34
|
-
|
|
33
|
+
isBefore(d, inclusive = false) {
|
|
34
|
+
const r = this.cmp(d);
|
|
35
|
+
return r === -1 || (r === 0 && inclusive);
|
|
35
36
|
}
|
|
36
37
|
isSameOrBefore(d) {
|
|
37
38
|
return this.cmp(d) <= 0;
|
|
38
39
|
}
|
|
39
|
-
isAfter(d) {
|
|
40
|
-
|
|
40
|
+
isAfter(d, inclusive = false) {
|
|
41
|
+
const r = this.cmp(d);
|
|
42
|
+
return r === 1 || (r === 0 && inclusive);
|
|
41
43
|
}
|
|
42
44
|
isSameOrAfter(d) {
|
|
43
45
|
return this.cmp(d) >= 0;
|
|
@@ -45,9 +47,14 @@ class DateInterval {
|
|
|
45
47
|
/**
|
|
46
48
|
* Ranges of DateInterval (start, end) are INCLUSIVE.
|
|
47
49
|
*/
|
|
48
|
-
includes(d) {
|
|
50
|
+
includes(d, incl = '[]') {
|
|
49
51
|
d = localDate_1.LocalDate.of(d);
|
|
50
|
-
return d.
|
|
52
|
+
return d.isAfter(this.start, incl[0] === '[') && d.isBefore(this.end, incl[1] === ']');
|
|
53
|
+
}
|
|
54
|
+
intersects(int, inclusive = true) {
|
|
55
|
+
const $int = DateInterval.parse(int);
|
|
56
|
+
const incl = inclusive ? '[]' : '()';
|
|
57
|
+
return this.includes($int.start, incl) || this.includes($int.end, incl);
|
|
51
58
|
}
|
|
52
59
|
/**
|
|
53
60
|
* DateIntervals compare by start date.
|
|
@@ -57,18 +64,14 @@ class DateInterval {
|
|
|
57
64
|
d = DateInterval.parse(d);
|
|
58
65
|
return this.start.cmp(d.start) || this.end.cmp(d.end);
|
|
59
66
|
}
|
|
67
|
+
getDays(incl = '[]') {
|
|
68
|
+
return localDate_1.LocalDate.range(this.start, this.end, incl, 1, 'day');
|
|
69
|
+
}
|
|
60
70
|
/**
|
|
61
71
|
* Returns an array of LocalDates that are included in the interval.
|
|
62
|
-
* Ranges are INCLUSIVE.
|
|
63
72
|
*/
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
let current = this.start;
|
|
67
|
-
do {
|
|
68
|
-
days.push(current);
|
|
69
|
-
current = current.add(1, 'day');
|
|
70
|
-
} while (current.isSameOrBefore(this.end));
|
|
71
|
-
return days;
|
|
73
|
+
range(incl = '[]', step = 1, stepUnit = 'day') {
|
|
74
|
+
return localDate_1.LocalDate.range(this.start, this.end, incl, step, stepUnit);
|
|
72
75
|
}
|
|
73
76
|
toString() {
|
|
74
77
|
return [this.start, this.end].join('/');
|
|
@@ -1,16 +1,15 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { IsoDate, UnixTimestamp } from '../types';
|
|
1
|
+
import { IsoDateString, UnixTimestampNumber } from '../types';
|
|
3
2
|
import { LocalTime } from './localTime';
|
|
4
3
|
export declare type LocalDateUnit = 'year' | 'month' | 'day';
|
|
5
4
|
export declare type Inclusiveness = '()' | '[]' | '[)' | '(]';
|
|
6
|
-
export declare type LocalDateConfig = LocalDate |
|
|
5
|
+
export declare type LocalDateConfig = LocalDate | IsoDateString;
|
|
7
6
|
/**
|
|
8
7
|
* @experimental
|
|
9
8
|
*/
|
|
10
9
|
export declare class LocalDate {
|
|
11
|
-
year
|
|
12
|
-
month
|
|
13
|
-
day
|
|
10
|
+
private $year;
|
|
11
|
+
private $month;
|
|
12
|
+
private $day;
|
|
14
13
|
private constructor();
|
|
15
14
|
static create(year: number, month: number, day: number): LocalDate;
|
|
16
15
|
/**
|
|
@@ -33,15 +32,19 @@ export declare class LocalDate {
|
|
|
33
32
|
static earliest(items: LocalDate[]): LocalDate;
|
|
34
33
|
static latestOrUndefined(items: LocalDate[]): LocalDate | undefined;
|
|
35
34
|
static latest(items: LocalDate[]): LocalDate;
|
|
36
|
-
static range(
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
35
|
+
static range(min: LocalDateConfig, max: LocalDateConfig, incl?: Inclusiveness, step?: number, stepUnit?: LocalDateUnit): LocalDate[];
|
|
36
|
+
get(unit: LocalDateUnit): number;
|
|
37
|
+
set(unit: LocalDateUnit, v: number, mutate?: boolean): LocalDate;
|
|
38
|
+
year(): number;
|
|
39
|
+
year(v: number): LocalDate;
|
|
40
|
+
month(): number;
|
|
41
|
+
month(v: number): LocalDate;
|
|
42
|
+
day(): number;
|
|
43
|
+
day(v: number): LocalDate;
|
|
41
44
|
isSame(d: LocalDateConfig): boolean;
|
|
42
|
-
isBefore(d: LocalDateConfig): boolean;
|
|
45
|
+
isBefore(d: LocalDateConfig, inclusive?: boolean): boolean;
|
|
43
46
|
isSameOrBefore(d: LocalDateConfig): boolean;
|
|
44
|
-
isAfter(d: LocalDateConfig): boolean;
|
|
47
|
+
isAfter(d: LocalDateConfig, inclusive?: boolean): boolean;
|
|
45
48
|
isSameOrAfter(d: LocalDateConfig): boolean;
|
|
46
49
|
isBetween(min: LocalDateConfig, max: LocalDateConfig, incl?: Inclusiveness): boolean;
|
|
47
50
|
/**
|
|
@@ -76,12 +79,12 @@ export declare class LocalDate {
|
|
|
76
79
|
*/
|
|
77
80
|
toDate(): Date;
|
|
78
81
|
toLocalTime(): LocalTime;
|
|
79
|
-
toISODate():
|
|
80
|
-
toString():
|
|
82
|
+
toISODate(): IsoDateString;
|
|
83
|
+
toString(): IsoDateString;
|
|
81
84
|
toStringCompact(): string;
|
|
82
|
-
unix():
|
|
85
|
+
unix(): UnixTimestampNumber;
|
|
83
86
|
unixMillis(): number;
|
|
84
|
-
toJSON():
|
|
87
|
+
toJSON(): IsoDateString;
|
|
85
88
|
}
|
|
86
89
|
/**
|
|
87
90
|
* Shortcut wrapper around `LocalDate.parse` / `LocalDate.today`
|
|
@@ -2,18 +2,18 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.localDate = exports.LocalDate = void 0;
|
|
4
4
|
const assert_1 = require("../error/assert");
|
|
5
|
-
const seq_1 = require("../seq/seq");
|
|
6
|
-
const types_1 = require("../types");
|
|
7
5
|
const localTime_1 = require("./localTime");
|
|
8
|
-
const
|
|
6
|
+
const MDAYS = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
|
|
7
|
+
const DATE_REGEX = /^(\d\d\d\d)-(\d\d)-(\d\d)$/;
|
|
8
|
+
/* eslint-disable no-dupe-class-members */
|
|
9
9
|
/**
|
|
10
10
|
* @experimental
|
|
11
11
|
*/
|
|
12
12
|
class LocalDate {
|
|
13
|
-
constructor(year, month, day) {
|
|
14
|
-
this
|
|
15
|
-
this
|
|
16
|
-
this
|
|
13
|
+
constructor($year, $month, $day) {
|
|
14
|
+
this.$year = $year;
|
|
15
|
+
this.$month = $month;
|
|
16
|
+
this.$day = $day;
|
|
17
17
|
}
|
|
18
18
|
static create(year, month, day) {
|
|
19
19
|
return new LocalDate(year, month, day);
|
|
@@ -50,8 +50,13 @@ class LocalDate {
|
|
|
50
50
|
return null;
|
|
51
51
|
if (d instanceof LocalDate)
|
|
52
52
|
return d;
|
|
53
|
-
//
|
|
54
|
-
const
|
|
53
|
+
// const [year, month, day] = d.slice(0, 10).split('-').map(Number)
|
|
54
|
+
const matches = DATE_REGEX.exec(d.slice(0, 10));
|
|
55
|
+
if (!matches)
|
|
56
|
+
return null;
|
|
57
|
+
const year = Number(matches[1]);
|
|
58
|
+
const month = Number(matches[2]);
|
|
59
|
+
const day = Number(matches[3]);
|
|
55
60
|
if (!year ||
|
|
56
61
|
!month ||
|
|
57
62
|
month < 1 ||
|
|
@@ -63,6 +68,10 @@ class LocalDate {
|
|
|
63
68
|
}
|
|
64
69
|
return new LocalDate(year, month, day);
|
|
65
70
|
}
|
|
71
|
+
// Can use just .toString()
|
|
72
|
+
// static parseToString(d: LocalDateConfig): IsoDateString {
|
|
73
|
+
// return typeof d === 'string' ? d : d.toString()
|
|
74
|
+
// }
|
|
66
75
|
static isValid(iso) {
|
|
67
76
|
return this.parseOrNull(iso) !== null;
|
|
68
77
|
}
|
|
@@ -90,45 +99,63 @@ class LocalDate {
|
|
|
90
99
|
(0, assert_1._assert)(items.length, 'LocalDate.latest called on empty array');
|
|
91
100
|
return items.reduce((max, item) => (max.isSameOrAfter(item) ? max : item));
|
|
92
101
|
}
|
|
93
|
-
static range(
|
|
94
|
-
const
|
|
95
|
-
|
|
96
|
-
const max = LocalDate.of(
|
|
97
|
-
|
|
98
|
-
|
|
102
|
+
static range(min, max, incl = '[)', step = 1, stepUnit = 'day') {
|
|
103
|
+
const dates = [];
|
|
104
|
+
const $min = LocalDate.of(min);
|
|
105
|
+
const $max = LocalDate.of(max).startOf(stepUnit);
|
|
106
|
+
let current = $min.startOf(stepUnit);
|
|
107
|
+
if (current.isAfter($min, incl[0] === '[')) {
|
|
108
|
+
// ok
|
|
109
|
+
}
|
|
110
|
+
else {
|
|
111
|
+
current.add(1, stepUnit, true);
|
|
112
|
+
}
|
|
113
|
+
const incl2 = incl[1] === ']';
|
|
114
|
+
while (current.isBefore($max, incl2)) {
|
|
115
|
+
dates.push(current);
|
|
99
116
|
current = current.add(step, stepUnit);
|
|
100
|
-
}
|
|
101
|
-
return
|
|
117
|
+
}
|
|
118
|
+
return dates;
|
|
119
|
+
}
|
|
120
|
+
get(unit) {
|
|
121
|
+
return unit === 'year' ? this.$year : unit === 'month' ? this.$month : this.$day;
|
|
102
122
|
}
|
|
103
|
-
|
|
104
|
-
const
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
123
|
+
set(unit, v, mutate = false) {
|
|
124
|
+
const t = mutate ? this : this.clone();
|
|
125
|
+
if (unit === 'year') {
|
|
126
|
+
t.$year = v;
|
|
127
|
+
}
|
|
128
|
+
else if (unit === 'month') {
|
|
129
|
+
t.$month = v;
|
|
130
|
+
}
|
|
131
|
+
else {
|
|
132
|
+
t.$day = v;
|
|
133
|
+
}
|
|
134
|
+
return t;
|
|
110
135
|
}
|
|
111
|
-
|
|
112
|
-
return
|
|
136
|
+
year(v) {
|
|
137
|
+
return v === undefined ? this.$year : this.set('year', v);
|
|
113
138
|
}
|
|
114
|
-
|
|
115
|
-
return
|
|
139
|
+
month(v) {
|
|
140
|
+
return v === undefined ? this.$month : this.set('month', v);
|
|
116
141
|
}
|
|
117
|
-
|
|
118
|
-
return
|
|
142
|
+
day(v) {
|
|
143
|
+
return v === undefined ? this.$day : this.set('day', v);
|
|
119
144
|
}
|
|
120
145
|
isSame(d) {
|
|
121
146
|
d = LocalDate.of(d);
|
|
122
|
-
return this
|
|
147
|
+
return this.$day === d.$day && this.$month === d.$month && this.$year === d.$year;
|
|
123
148
|
}
|
|
124
|
-
isBefore(d) {
|
|
125
|
-
|
|
149
|
+
isBefore(d, inclusive = false) {
|
|
150
|
+
const r = this.cmp(d);
|
|
151
|
+
return r === -1 || (r === 0 && inclusive);
|
|
126
152
|
}
|
|
127
153
|
isSameOrBefore(d) {
|
|
128
154
|
return this.cmp(d) <= 0;
|
|
129
155
|
}
|
|
130
|
-
isAfter(d) {
|
|
131
|
-
|
|
156
|
+
isAfter(d, inclusive = false) {
|
|
157
|
+
const r = this.cmp(d);
|
|
158
|
+
return r === 1 || (r === 0 && inclusive);
|
|
132
159
|
}
|
|
133
160
|
isSameOrAfter(d) {
|
|
134
161
|
return this.cmp(d) >= 0;
|
|
@@ -149,17 +176,17 @@ class LocalDate {
|
|
|
149
176
|
*/
|
|
150
177
|
cmp(d) {
|
|
151
178
|
d = LocalDate.of(d);
|
|
152
|
-
if (this
|
|
179
|
+
if (this.$year < d.$year)
|
|
153
180
|
return -1;
|
|
154
|
-
if (this
|
|
181
|
+
if (this.$year > d.$year)
|
|
155
182
|
return 1;
|
|
156
|
-
if (this
|
|
183
|
+
if (this.$month < d.$month)
|
|
157
184
|
return -1;
|
|
158
|
-
if (this
|
|
185
|
+
if (this.$month > d.$month)
|
|
159
186
|
return 1;
|
|
160
|
-
if (this
|
|
187
|
+
if (this.$day < d.$day)
|
|
161
188
|
return -1;
|
|
162
|
-
if (this
|
|
189
|
+
if (this.$day > d.$day)
|
|
163
190
|
return 1;
|
|
164
191
|
return 0;
|
|
165
192
|
}
|
|
@@ -177,82 +204,87 @@ class LocalDate {
|
|
|
177
204
|
diff(d, unit) {
|
|
178
205
|
d = LocalDate.of(d);
|
|
179
206
|
if (unit === 'year') {
|
|
180
|
-
return this
|
|
207
|
+
return this.$year - d.$year;
|
|
181
208
|
}
|
|
182
209
|
if (unit === 'month') {
|
|
183
|
-
return (this
|
|
210
|
+
return (this.$year - d.$year) * 12 + (this.$month - d.$month);
|
|
184
211
|
}
|
|
185
212
|
// unit is 'day'
|
|
186
|
-
let days = this
|
|
187
|
-
if (d
|
|
188
|
-
for (let year = d
|
|
213
|
+
let days = this.$day - d.$day;
|
|
214
|
+
if (d.$year < this.$year) {
|
|
215
|
+
for (let year = d.$year; year < this.$year; year++) {
|
|
189
216
|
days += LocalDate.getYearLength(year);
|
|
190
217
|
}
|
|
191
218
|
}
|
|
192
|
-
else if (this
|
|
193
|
-
for (let year = this
|
|
219
|
+
else if (this.$year < d.$year) {
|
|
220
|
+
for (let year = this.$year; year < d.$year; year++) {
|
|
194
221
|
days -= LocalDate.getYearLength(year);
|
|
195
222
|
}
|
|
196
223
|
}
|
|
197
|
-
if (d
|
|
198
|
-
for (let month = d
|
|
199
|
-
days += LocalDate.getMonthLength(this
|
|
224
|
+
if (d.$month < this.$month) {
|
|
225
|
+
for (let month = d.$month; month < this.$month; month++) {
|
|
226
|
+
days += LocalDate.getMonthLength(this.$year, month);
|
|
200
227
|
}
|
|
201
228
|
}
|
|
202
|
-
else if (this
|
|
203
|
-
for (let month = this
|
|
204
|
-
days -= LocalDate.getMonthLength(d
|
|
229
|
+
else if (this.$month < d.$month) {
|
|
230
|
+
for (let month = this.$month; month < d.$month; month++) {
|
|
231
|
+
days -= LocalDate.getMonthLength(d.$year, month);
|
|
205
232
|
}
|
|
206
233
|
}
|
|
207
234
|
return days;
|
|
208
235
|
}
|
|
209
236
|
add(num, unit, mutate = false) {
|
|
210
|
-
let { day, month, year } = this;
|
|
237
|
+
let { $day, $month, $year } = this;
|
|
211
238
|
if (unit === 'day') {
|
|
212
|
-
day += num;
|
|
239
|
+
$day += num;
|
|
213
240
|
}
|
|
214
241
|
else if (unit === 'month') {
|
|
215
|
-
month += num;
|
|
242
|
+
$month += num;
|
|
216
243
|
}
|
|
217
244
|
else if (unit === 'year') {
|
|
218
|
-
year += num;
|
|
245
|
+
$year += num;
|
|
219
246
|
}
|
|
220
247
|
// check day overflow
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
248
|
+
if (unit === 'day') {
|
|
249
|
+
if ($day < 1) {
|
|
250
|
+
while ($day < 1) {
|
|
251
|
+
$month -= 1;
|
|
252
|
+
if ($month < 1) {
|
|
253
|
+
$year -= 1;
|
|
254
|
+
$month += 12;
|
|
255
|
+
}
|
|
256
|
+
$day += LocalDate.getMonthLength($year, $month);
|
|
257
|
+
}
|
|
228
258
|
}
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
259
|
+
else {
|
|
260
|
+
let monLen = LocalDate.getMonthLength($year, $month);
|
|
261
|
+
while ($day > monLen) {
|
|
262
|
+
$day -= monLen;
|
|
263
|
+
$month += 1;
|
|
264
|
+
if ($month > 12) {
|
|
265
|
+
$year += 1;
|
|
266
|
+
$month -= 12;
|
|
267
|
+
}
|
|
268
|
+
monLen = LocalDate.getMonthLength($year, $month);
|
|
269
|
+
}
|
|
237
270
|
}
|
|
238
|
-
monLen = LocalDate.getMonthLength(year, month);
|
|
239
271
|
}
|
|
240
272
|
// check month overflow
|
|
241
|
-
while (month > 12) {
|
|
242
|
-
year += 1;
|
|
243
|
-
month -= 12;
|
|
273
|
+
while ($month > 12) {
|
|
274
|
+
$year += 1;
|
|
275
|
+
$month -= 12;
|
|
244
276
|
}
|
|
245
|
-
while (month < 1) {
|
|
246
|
-
year -= 1;
|
|
247
|
-
month += 12;
|
|
277
|
+
while ($month < 1) {
|
|
278
|
+
$year -= 1;
|
|
279
|
+
$month += 12;
|
|
248
280
|
}
|
|
249
281
|
if (mutate) {
|
|
250
|
-
this
|
|
251
|
-
this
|
|
252
|
-
this
|
|
282
|
+
this.$year = $year;
|
|
283
|
+
this.$month = $month;
|
|
284
|
+
this.$day = $day;
|
|
253
285
|
return this;
|
|
254
286
|
}
|
|
255
|
-
return new LocalDate(year, month, day);
|
|
287
|
+
return new LocalDate($year, $month, $day);
|
|
256
288
|
}
|
|
257
289
|
subtract(num, unit, mutate = false) {
|
|
258
290
|
return this.add(-num, unit, mutate);
|
|
@@ -261,17 +293,17 @@ class LocalDate {
|
|
|
261
293
|
if (unit === 'day')
|
|
262
294
|
return this;
|
|
263
295
|
if (unit === 'month')
|
|
264
|
-
return LocalDate.create(this
|
|
296
|
+
return LocalDate.create(this.$year, this.$month, 1);
|
|
265
297
|
// year
|
|
266
|
-
return LocalDate.create(this
|
|
298
|
+
return LocalDate.create(this.$year, 1, 1);
|
|
267
299
|
}
|
|
268
300
|
endOf(unit) {
|
|
269
301
|
if (unit === 'day')
|
|
270
302
|
return this;
|
|
271
303
|
if (unit === 'month')
|
|
272
|
-
return LocalDate.create(this
|
|
304
|
+
return LocalDate.create(this.$year, this.$month, LocalDate.getMonthLength(this.$year, this.$month));
|
|
273
305
|
// year
|
|
274
|
-
return LocalDate.create(this
|
|
306
|
+
return LocalDate.create(this.$year, 12, 31);
|
|
275
307
|
}
|
|
276
308
|
static getYearLength(year) {
|
|
277
309
|
return this.isLeapYear(year) ? 366 : 365;
|
|
@@ -279,17 +311,13 @@ class LocalDate {
|
|
|
279
311
|
static getMonthLength(year, month) {
|
|
280
312
|
if (month === 2)
|
|
281
313
|
return this.isLeapYear(year) ? 29 : 28;
|
|
282
|
-
return
|
|
314
|
+
return MDAYS[month];
|
|
283
315
|
}
|
|
284
316
|
static isLeapYear(year) {
|
|
285
|
-
|
|
286
|
-
return false;
|
|
287
|
-
if (year % 100 !== 0)
|
|
288
|
-
return true;
|
|
289
|
-
return year % 400 === 0;
|
|
317
|
+
return year % 4 === 0 && (year % 100 !== 0 || year % 400 === 0);
|
|
290
318
|
}
|
|
291
319
|
clone() {
|
|
292
|
-
return new LocalDate(this
|
|
320
|
+
return new LocalDate(this.$year, this.$month, this.$day);
|
|
293
321
|
}
|
|
294
322
|
/**
|
|
295
323
|
* Converts LocalDate into instance of Date.
|
|
@@ -298,7 +326,7 @@ class LocalDate {
|
|
|
298
326
|
* Timezone will match local timezone.
|
|
299
327
|
*/
|
|
300
328
|
toDate() {
|
|
301
|
-
return new Date(this
|
|
329
|
+
return new Date(this.$year, this.$month - 1, this.$day);
|
|
302
330
|
}
|
|
303
331
|
toLocalTime() {
|
|
304
332
|
return localTime_1.LocalTime.of(this.toDate());
|
|
@@ -308,16 +336,16 @@ class LocalDate {
|
|
|
308
336
|
}
|
|
309
337
|
toString() {
|
|
310
338
|
return [
|
|
311
|
-
String(this
|
|
312
|
-
String(this
|
|
313
|
-
String(this
|
|
339
|
+
String(this.$year).padStart(4, '0'),
|
|
340
|
+
String(this.$month).padStart(2, '0'),
|
|
341
|
+
String(this.$day).padStart(2, '0'),
|
|
314
342
|
].join('-');
|
|
315
343
|
}
|
|
316
344
|
toStringCompact() {
|
|
317
345
|
return [
|
|
318
|
-
String(this
|
|
319
|
-
String(this
|
|
320
|
-
String(this
|
|
346
|
+
String(this.$year).padStart(4, '0'),
|
|
347
|
+
String(this.$month).padStart(2, '0'),
|
|
348
|
+
String(this.$day).padStart(2, '0'),
|
|
321
349
|
].join('');
|
|
322
350
|
}
|
|
323
351
|
// 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;
|
|
@@ -28,6 +28,8 @@ export declare class LocalTime {
|
|
|
28
28
|
* Returns null if invalid
|
|
29
29
|
*/
|
|
30
30
|
static parseOrNull(d: LocalTimeConfig | undefined | null): LocalTime | null;
|
|
31
|
+
static parseToDate(d: LocalTimeConfig): Date;
|
|
32
|
+
static parseToUnixTimestamp(d: LocalTimeConfig): UnixTimestampNumber;
|
|
31
33
|
static isValid(d: LocalTimeConfig | undefined | null): boolean;
|
|
32
34
|
static now(): LocalTime;
|
|
33
35
|
static fromComponents(c: {
|
|
@@ -40,8 +42,8 @@ export declare class LocalTime {
|
|
|
40
42
|
year(v: number): LocalTime;
|
|
41
43
|
month(): number;
|
|
42
44
|
month(v: number): LocalTime;
|
|
43
|
-
|
|
44
|
-
|
|
45
|
+
day(): number;
|
|
46
|
+
day(v: number): LocalTime;
|
|
45
47
|
hour(): number;
|
|
46
48
|
hour(v: number): LocalTime;
|
|
47
49
|
minute(): number;
|
|
@@ -60,9 +62,9 @@ export declare class LocalTime {
|
|
|
60
62
|
static latestOrUndefined(items: LocalTime[]): LocalTime | undefined;
|
|
61
63
|
static latest(items: LocalTime[]): LocalTime;
|
|
62
64
|
isSame(d: LocalTimeConfig): boolean;
|
|
63
|
-
isBefore(d: LocalTimeConfig): boolean;
|
|
65
|
+
isBefore(d: LocalTimeConfig, inclusive?: boolean): boolean;
|
|
64
66
|
isSameOrBefore(d: LocalTimeConfig): boolean;
|
|
65
|
-
isAfter(d: LocalTimeConfig): boolean;
|
|
67
|
+
isAfter(d: LocalTimeConfig, inclusive?: boolean): boolean;
|
|
66
68
|
isSameOrAfter(d: LocalTimeConfig): boolean;
|
|
67
69
|
isBetween(min: LocalTimeConfig, max: LocalTimeConfig, incl?: Inclusiveness): boolean;
|
|
68
70
|
/**
|
|
@@ -75,19 +77,19 @@ export declare class LocalTime {
|
|
|
75
77
|
fromNow(now?: LocalTimeConfig): string;
|
|
76
78
|
getDate(): Date;
|
|
77
79
|
clone(): LocalTime;
|
|
78
|
-
unix():
|
|
80
|
+
unix(): UnixTimestampNumber;
|
|
79
81
|
unixMillis(): number;
|
|
80
|
-
valueOf():
|
|
82
|
+
valueOf(): UnixTimestampNumber;
|
|
81
83
|
toLocalDate(): LocalDate;
|
|
82
|
-
toPretty(seconds?: boolean):
|
|
84
|
+
toPretty(seconds?: boolean): IsoDateTimeString;
|
|
83
85
|
/**
|
|
84
86
|
* Returns e.g: `1984-06-21T17:56:21`, only the date part of DateTime
|
|
85
87
|
*/
|
|
86
|
-
toISODateTime():
|
|
88
|
+
toISODateTime(): IsoDateTimeString;
|
|
87
89
|
/**
|
|
88
90
|
* Returns e.g: `1984-06-21`, only the date part of DateTime
|
|
89
91
|
*/
|
|
90
|
-
toISODate():
|
|
92
|
+
toISODate(): IsoDateString;
|
|
91
93
|
/**
|
|
92
94
|
* Returns e.g: `17:03:15` (or `17:03` with seconds=false)
|
|
93
95
|
*/
|
|
@@ -97,7 +99,7 @@ export declare class LocalTime {
|
|
|
97
99
|
*/
|
|
98
100
|
toStringCompact(seconds?: boolean): string;
|
|
99
101
|
toString(): string;
|
|
100
|
-
toJSON():
|
|
102
|
+
toJSON(): UnixTimestampNumber;
|
|
101
103
|
}
|
|
102
104
|
/**
|
|
103
105
|
* Shortcut wrapper around `LocalDate.parse` / `LocalDate.today`
|