@naturalcycles/js-lib 14.91.2 → 14.94.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 +38 -0
- package/dist/datetime/dateInterval.js +80 -0
- package/dist/datetime/localDate.d.ts +4 -0
- package/dist/datetime/localDate.js +15 -0
- package/dist/datetime/localTime.d.ts +10 -2
- package/dist/datetime/localTime.js +146 -80
- package/dist/index.d.ts +4 -2
- package/dist/index.js +1 -0
- package/dist-esm/datetime/dateInterval.js +76 -0
- package/dist-esm/datetime/localDate.js +15 -0
- package/dist-esm/datetime/localTime.js +146 -80
- package/dist-esm/index.js +1 -0
- package/package.json +1 -1
- package/src/datetime/dateInterval.ts +91 -0
- package/src/datetime/localDate.ts +17 -0
- package/src/datetime/localTime.ts +165 -75
- package/src/index.ts +5 -1
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { LocalDate, LocalDateConfig } from './localDate';
|
|
2
|
+
export declare type DateIntervalConfig = DateInterval | string;
|
|
3
|
+
/**
|
|
4
|
+
* Class that supports ISO8601 "Time interval" standard that looks like `2022-02-24/2022-03-30`.
|
|
5
|
+
*
|
|
6
|
+
* @experimental
|
|
7
|
+
*/
|
|
8
|
+
export declare class DateInterval {
|
|
9
|
+
start: LocalDate;
|
|
10
|
+
end: LocalDate;
|
|
11
|
+
private constructor();
|
|
12
|
+
static of(start: LocalDateConfig, end: LocalDateConfig): DateInterval;
|
|
13
|
+
/**
|
|
14
|
+
* Parses string like `2022-02-24/2023-03-30` into a DateInterval.
|
|
15
|
+
*/
|
|
16
|
+
static parse(d: DateIntervalConfig): DateInterval;
|
|
17
|
+
isSame(d: DateIntervalConfig): boolean;
|
|
18
|
+
isBefore(d: DateIntervalConfig): boolean;
|
|
19
|
+
isSameOrBefore(d: DateIntervalConfig): boolean;
|
|
20
|
+
isAfter(d: DateIntervalConfig): boolean;
|
|
21
|
+
isSameOrAfter(d: DateIntervalConfig): boolean;
|
|
22
|
+
/**
|
|
23
|
+
* Ranges of DateInterval (start, end) are INCLUSIVE.
|
|
24
|
+
*/
|
|
25
|
+
includes(d: LocalDateConfig): boolean;
|
|
26
|
+
/**
|
|
27
|
+
* DateIntervals compare by start date.
|
|
28
|
+
* If it's the same - then by end date.
|
|
29
|
+
*/
|
|
30
|
+
cmp(d: DateIntervalConfig): -1 | 0 | 1;
|
|
31
|
+
/**
|
|
32
|
+
* Returns an array of LocalDates that are included in the interval.
|
|
33
|
+
* Ranges are INCLUSIVE.
|
|
34
|
+
*/
|
|
35
|
+
getDays(): LocalDate[];
|
|
36
|
+
toString(): string;
|
|
37
|
+
toJSON(): string;
|
|
38
|
+
}
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.DateInterval = void 0;
|
|
4
|
+
const localDate_1 = require("./localDate");
|
|
5
|
+
/**
|
|
6
|
+
* Class that supports ISO8601 "Time interval" standard that looks like `2022-02-24/2022-03-30`.
|
|
7
|
+
*
|
|
8
|
+
* @experimental
|
|
9
|
+
*/
|
|
10
|
+
class DateInterval {
|
|
11
|
+
constructor(start, end) {
|
|
12
|
+
this.start = start;
|
|
13
|
+
this.end = end;
|
|
14
|
+
}
|
|
15
|
+
static of(start, end) {
|
|
16
|
+
return new DateInterval(localDate_1.LocalDate.of(start), localDate_1.LocalDate.of(end));
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Parses string like `2022-02-24/2023-03-30` into a DateInterval.
|
|
20
|
+
*/
|
|
21
|
+
static parse(d) {
|
|
22
|
+
if (d instanceof DateInterval)
|
|
23
|
+
return d;
|
|
24
|
+
const [start, end] = d.split('/');
|
|
25
|
+
if (!end || !start) {
|
|
26
|
+
throw new Error(`Cannot parse "${d}" into DateInterval`);
|
|
27
|
+
}
|
|
28
|
+
return new DateInterval(localDate_1.LocalDate.of(start), localDate_1.LocalDate.of(end));
|
|
29
|
+
}
|
|
30
|
+
isSame(d) {
|
|
31
|
+
return this.cmp(d) === 0;
|
|
32
|
+
}
|
|
33
|
+
isBefore(d) {
|
|
34
|
+
return this.cmp(d) === -1;
|
|
35
|
+
}
|
|
36
|
+
isSameOrBefore(d) {
|
|
37
|
+
return this.cmp(d) <= 0;
|
|
38
|
+
}
|
|
39
|
+
isAfter(d) {
|
|
40
|
+
return this.cmp(d) === 1;
|
|
41
|
+
}
|
|
42
|
+
isSameOrAfter(d) {
|
|
43
|
+
return this.cmp(d) >= 0;
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Ranges of DateInterval (start, end) are INCLUSIVE.
|
|
47
|
+
*/
|
|
48
|
+
includes(d) {
|
|
49
|
+
d = localDate_1.LocalDate.of(d);
|
|
50
|
+
return d.isSameOrAfter(this.start) && d.isSameOrBefore(this.end);
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* DateIntervals compare by start date.
|
|
54
|
+
* If it's the same - then by end date.
|
|
55
|
+
*/
|
|
56
|
+
cmp(d) {
|
|
57
|
+
d = DateInterval.parse(d);
|
|
58
|
+
return this.start.cmp(d.start) || this.end.cmp(d.end);
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Returns an array of LocalDates that are included in the interval.
|
|
62
|
+
* Ranges are INCLUSIVE.
|
|
63
|
+
*/
|
|
64
|
+
getDays() {
|
|
65
|
+
const days = [];
|
|
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;
|
|
72
|
+
}
|
|
73
|
+
toString() {
|
|
74
|
+
return [this.start, this.end].join('/');
|
|
75
|
+
}
|
|
76
|
+
toJSON() {
|
|
77
|
+
return this.toString();
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
exports.DateInterval = DateInterval;
|
|
@@ -2,6 +2,7 @@ import { Sequence } from '../seq/seq';
|
|
|
2
2
|
import { IsoDate, UnixTimestamp } from '../types';
|
|
3
3
|
import { LocalTime } from './localTime';
|
|
4
4
|
export declare type LocalDateUnit = 'year' | 'month' | 'day';
|
|
5
|
+
export declare type Inclusiveness = '()' | '[]' | '[)' | '(]';
|
|
5
6
|
export declare type LocalDateConfig = LocalDate | string;
|
|
6
7
|
/**
|
|
7
8
|
* @experimental
|
|
@@ -19,12 +20,14 @@ export declare class LocalDate {
|
|
|
19
20
|
static of(d: LocalDateConfig): LocalDate;
|
|
20
21
|
static parseCompact(d: string): LocalDate;
|
|
21
22
|
static fromDate(d: Date): LocalDate;
|
|
23
|
+
static fromDateUTC(d: Date): LocalDate;
|
|
22
24
|
/**
|
|
23
25
|
* Returns null if invalid.
|
|
24
26
|
*/
|
|
25
27
|
static parseOrNull(d: LocalDateConfig): LocalDate | null;
|
|
26
28
|
static isValid(iso: string): boolean;
|
|
27
29
|
static today(): LocalDate;
|
|
30
|
+
static todayUTC(): LocalDate;
|
|
28
31
|
static sort(items: LocalDate[], mutate?: boolean, descending?: boolean): LocalDate[];
|
|
29
32
|
static earliestOrUndefined(items: LocalDate[]): LocalDate | undefined;
|
|
30
33
|
static earliest(items: LocalDate[]): LocalDate;
|
|
@@ -40,6 +43,7 @@ export declare class LocalDate {
|
|
|
40
43
|
isSameOrBefore(d: LocalDateConfig): boolean;
|
|
41
44
|
isAfter(d: LocalDateConfig): boolean;
|
|
42
45
|
isSameOrAfter(d: LocalDateConfig): boolean;
|
|
46
|
+
isBetween(min: LocalDateConfig, max: LocalDateConfig, incl?: Inclusiveness): boolean;
|
|
43
47
|
/**
|
|
44
48
|
* Returns 1 if this > d
|
|
45
49
|
* returns 0 if they are equal
|
|
@@ -39,6 +39,9 @@ class LocalDate {
|
|
|
39
39
|
static fromDate(d) {
|
|
40
40
|
return new LocalDate(d.getFullYear(), d.getMonth() + 1, d.getDate());
|
|
41
41
|
}
|
|
42
|
+
static fromDateUTC(d) {
|
|
43
|
+
return new LocalDate(d.getUTCFullYear(), d.getUTCMonth() + 1, d.getUTCDate());
|
|
44
|
+
}
|
|
42
45
|
/**
|
|
43
46
|
* Returns null if invalid.
|
|
44
47
|
*/
|
|
@@ -64,6 +67,9 @@ class LocalDate {
|
|
|
64
67
|
static today() {
|
|
65
68
|
return this.fromDate(new Date());
|
|
66
69
|
}
|
|
70
|
+
static todayUTC() {
|
|
71
|
+
return this.fromDateUTC(new Date());
|
|
72
|
+
}
|
|
67
73
|
static sort(items, mutate = false, descending = false) {
|
|
68
74
|
const mod = descending ? -1 : 1;
|
|
69
75
|
return (mutate ? items : [...items]).sort((a, b) => a.cmp(b) * mod);
|
|
@@ -125,6 +131,15 @@ class LocalDate {
|
|
|
125
131
|
isSameOrAfter(d) {
|
|
126
132
|
return this.cmp(d) >= 0;
|
|
127
133
|
}
|
|
134
|
+
isBetween(min, max, incl = '[)') {
|
|
135
|
+
let r = this.cmp(min);
|
|
136
|
+
if (r < 0 || (r === 0 && incl[0] === '('))
|
|
137
|
+
return false;
|
|
138
|
+
r = this.cmp(max);
|
|
139
|
+
if (r > 0 || (r === 0 && incl[1] === ')'))
|
|
140
|
+
return false;
|
|
141
|
+
return true;
|
|
142
|
+
}
|
|
128
143
|
/**
|
|
129
144
|
* Returns 1 if this > d
|
|
130
145
|
* returns 0 if they are equal
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { IsoDate, IsoDateTime, UnixTimestamp } from '../types';
|
|
2
|
-
import { LocalDate } from './localDate';
|
|
2
|
+
import { Inclusiveness, LocalDate } from './localDate';
|
|
3
3
|
export declare type LocalTimeUnit = 'year' | 'month' | 'day' | 'hour' | 'minute' | 'second';
|
|
4
4
|
export declare type LocalTimeConfig = LocalTime | Date | IsoDateTime | UnixTimestamp;
|
|
5
5
|
export interface LocalTimeComponents {
|
|
@@ -15,18 +15,20 @@ export interface LocalTimeComponents {
|
|
|
15
15
|
*/
|
|
16
16
|
export declare class LocalTime {
|
|
17
17
|
private $date;
|
|
18
|
+
utcMode: boolean;
|
|
18
19
|
private constructor();
|
|
19
20
|
/**
|
|
20
21
|
* Parses input String into LocalDate.
|
|
21
22
|
* Input can already be a LocalDate - it is returned as-is in that case.
|
|
22
23
|
*/
|
|
23
24
|
static of(d: LocalTimeConfig): LocalTime;
|
|
25
|
+
utc(): this;
|
|
26
|
+
local(): this;
|
|
24
27
|
/**
|
|
25
28
|
* Returns null if invalid
|
|
26
29
|
*/
|
|
27
30
|
static parseOrNull(d: LocalTimeConfig): LocalTime | null;
|
|
28
31
|
static isValid(d: LocalTimeConfig): boolean;
|
|
29
|
-
static unix(ts: UnixTimestamp): LocalTime;
|
|
30
32
|
static now(): LocalTime;
|
|
31
33
|
static fromComponents(c: {
|
|
32
34
|
year: number;
|
|
@@ -62,6 +64,7 @@ export declare class LocalTime {
|
|
|
62
64
|
isSameOrBefore(d: LocalTimeConfig): boolean;
|
|
63
65
|
isAfter(d: LocalTimeConfig): boolean;
|
|
64
66
|
isSameOrAfter(d: LocalTimeConfig): boolean;
|
|
67
|
+
isBetween(min: LocalTimeConfig, max: LocalTimeConfig, incl?: Inclusiveness): boolean;
|
|
65
68
|
/**
|
|
66
69
|
* Returns 1 if this > d
|
|
67
70
|
* returns 0 if they are equal
|
|
@@ -69,6 +72,7 @@ export declare class LocalTime {
|
|
|
69
72
|
*/
|
|
70
73
|
cmp(d: LocalTimeConfig): -1 | 0 | 1;
|
|
71
74
|
components(): LocalTimeComponents;
|
|
75
|
+
fromNow(now?: LocalTimeConfig): string;
|
|
72
76
|
getDate(): Date;
|
|
73
77
|
clone(): LocalTime;
|
|
74
78
|
unix(): UnixTimestamp;
|
|
@@ -84,6 +88,10 @@ export declare class LocalTime {
|
|
|
84
88
|
* Returns e.g: `1984-06-21`, only the date part of DateTime
|
|
85
89
|
*/
|
|
86
90
|
toISODate(): IsoDate;
|
|
91
|
+
/**
|
|
92
|
+
* Returns e.g: `17:03:15` (or `17:03` with seconds=false)
|
|
93
|
+
*/
|
|
94
|
+
toISOTime(seconds?: boolean): string;
|
|
87
95
|
/**
|
|
88
96
|
* Returns e.g: `19840621_1705`
|
|
89
97
|
*/
|
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.localTime = exports.LocalTime = void 0;
|
|
4
4
|
const assert_1 = require("../error/assert");
|
|
5
|
+
const time_util_1 = require("../time/time.util");
|
|
5
6
|
const localDate_1 = require("./localDate");
|
|
6
7
|
/* eslint-disable no-dupe-class-members */
|
|
7
8
|
// Design choices:
|
|
@@ -13,12 +14,16 @@ const localDate_1 = require("./localDate");
|
|
|
13
14
|
// .valueOf returns unix timestamp (no millis)
|
|
14
15
|
// Prevents dayjs(undefined) being dayjs.now()
|
|
15
16
|
// Validates on parse, throws if invalid. Doesn't allow invalid objects
|
|
17
|
+
// No arbitrary .format('') (which is slow to parse) vs well-defined (opinionated) formats
|
|
18
|
+
// Separate LocalTime, powered by native js Date, and LocalDate - a smaller functionality class when
|
|
19
|
+
// you only need "whole dates", no time, no timezone worry
|
|
16
20
|
/**
|
|
17
21
|
* @experimental
|
|
18
22
|
*/
|
|
19
23
|
class LocalTime {
|
|
20
|
-
constructor($date) {
|
|
24
|
+
constructor($date, utcMode) {
|
|
21
25
|
this.$date = $date;
|
|
26
|
+
this.utcMode = utcMode;
|
|
22
27
|
}
|
|
23
28
|
/**
|
|
24
29
|
* Parses input String into LocalDate.
|
|
@@ -31,6 +36,14 @@ class LocalTime {
|
|
|
31
36
|
}
|
|
32
37
|
return t;
|
|
33
38
|
}
|
|
39
|
+
utc() {
|
|
40
|
+
this.utcMode = true;
|
|
41
|
+
return this;
|
|
42
|
+
}
|
|
43
|
+
local() {
|
|
44
|
+
this.utcMode = false;
|
|
45
|
+
return this;
|
|
46
|
+
}
|
|
34
47
|
/**
|
|
35
48
|
* Returns null if invalid
|
|
36
49
|
*/
|
|
@@ -52,106 +65,110 @@ class LocalTime {
|
|
|
52
65
|
// throw new TypeError(`Cannot parse "${d}" into LocalTime`)
|
|
53
66
|
return null;
|
|
54
67
|
}
|
|
55
|
-
|
|
68
|
+
// if (utc) {
|
|
69
|
+
// date.setMinutes(date.getMinutes() + date.getTimezoneOffset())
|
|
70
|
+
// }
|
|
71
|
+
return new LocalTime(date, false);
|
|
56
72
|
}
|
|
57
73
|
static isValid(d) {
|
|
58
74
|
return this.parseOrNull(d) !== null;
|
|
59
75
|
}
|
|
60
|
-
static unix(ts) {
|
|
61
|
-
return new LocalTime(new Date(ts * 1000));
|
|
62
|
-
}
|
|
63
76
|
static now() {
|
|
64
|
-
return
|
|
77
|
+
return new LocalTime(new Date(), false);
|
|
65
78
|
}
|
|
66
79
|
static fromComponents(c) {
|
|
67
|
-
return new LocalTime(new Date(c.year, c.month - 1, c.day, c.hour, c.minute, c.second));
|
|
80
|
+
return new LocalTime(new Date(c.year, c.month - 1, c.day, c.hour, c.minute, c.second), false);
|
|
68
81
|
}
|
|
69
82
|
get(unit) {
|
|
70
83
|
if (unit === 'year') {
|
|
71
|
-
return this.$date.getFullYear();
|
|
84
|
+
return this.utcMode ? this.$date.getUTCFullYear() : this.$date.getFullYear();
|
|
72
85
|
}
|
|
73
86
|
if (unit === 'month') {
|
|
74
|
-
return this.$date.getMonth() + 1;
|
|
87
|
+
return (this.utcMode ? this.$date.getUTCMonth() : this.$date.getMonth()) + 1;
|
|
75
88
|
}
|
|
76
89
|
if (unit === 'day') {
|
|
77
|
-
return this.$date.getDate();
|
|
90
|
+
return this.utcMode ? this.$date.getUTCDate() : this.$date.getDate();
|
|
78
91
|
}
|
|
79
92
|
if (unit === 'hour') {
|
|
80
|
-
return this.$date.getHours();
|
|
93
|
+
return this.utcMode ? this.$date.getUTCHours() : this.$date.getHours();
|
|
81
94
|
}
|
|
82
95
|
if (unit === 'minute') {
|
|
83
|
-
return this.$date.getMinutes();
|
|
96
|
+
return this.utcMode ? this.$date.getUTCMinutes() : this.$date.getMinutes();
|
|
84
97
|
}
|
|
85
98
|
// second
|
|
86
|
-
return this.$date.getSeconds();
|
|
99
|
+
return this.utcMode ? this.$date.getUTCSeconds() : this.$date.getSeconds();
|
|
87
100
|
}
|
|
88
101
|
set(unit, v, mutate = false) {
|
|
89
102
|
const t = mutate ? this : this.clone();
|
|
103
|
+
/* eslint-disable @typescript-eslint/no-unused-expressions */
|
|
90
104
|
if (unit === 'year') {
|
|
91
|
-
t.$date.setFullYear(v);
|
|
105
|
+
this.utcMode ? t.$date.setUTCFullYear(v) : t.$date.setFullYear(v);
|
|
92
106
|
}
|
|
93
107
|
else if (unit === 'month') {
|
|
94
|
-
t.$date.setMonth(v - 1);
|
|
108
|
+
this.utcMode ? t.$date.setUTCMonth(v - 1) : t.$date.setMonth(v - 1);
|
|
95
109
|
}
|
|
96
110
|
else if (unit === 'day') {
|
|
97
|
-
t.$date.setDate(v);
|
|
111
|
+
this.utcMode ? t.$date.setUTCDate(v) : t.$date.setDate(v);
|
|
98
112
|
}
|
|
99
113
|
else if (unit === 'hour') {
|
|
100
|
-
t.$date.setHours(v);
|
|
114
|
+
this.utcMode ? t.$date.setUTCHours(v) : t.$date.setHours(v);
|
|
101
115
|
}
|
|
102
116
|
else if (unit === 'minute') {
|
|
103
|
-
t.$date.setMinutes(v);
|
|
117
|
+
this.utcMode ? t.$date.setUTCMinutes(v) : t.$date.setMinutes(v);
|
|
104
118
|
}
|
|
105
119
|
else if (unit === 'second') {
|
|
106
|
-
t.$date.setSeconds(v);
|
|
120
|
+
this.utcMode ? t.$date.setUTCSeconds(v) : t.$date.setSeconds(v);
|
|
107
121
|
}
|
|
122
|
+
/* eslint-enable @typescript-eslint/no-unused-expressions */
|
|
108
123
|
return t;
|
|
109
124
|
}
|
|
110
125
|
year(v) {
|
|
111
|
-
return v === undefined ? this
|
|
126
|
+
return v === undefined ? this.get('year') : this.set('year', v);
|
|
112
127
|
}
|
|
113
128
|
month(v) {
|
|
114
|
-
return v === undefined ? this
|
|
129
|
+
return v === undefined ? this.get('month') : this.set('month', v);
|
|
115
130
|
}
|
|
116
131
|
date(v) {
|
|
117
|
-
return v === undefined ? this
|
|
132
|
+
return v === undefined ? this.get('day') : this.set('day', v);
|
|
118
133
|
}
|
|
119
134
|
hour(v) {
|
|
120
|
-
return v === undefined ? this
|
|
135
|
+
return v === undefined ? this.get('hour') : this.set('hour', v);
|
|
121
136
|
}
|
|
122
137
|
minute(v) {
|
|
123
|
-
return v === undefined ? this
|
|
138
|
+
return v === undefined ? this.get('minute') : this.set('minute', v);
|
|
124
139
|
}
|
|
125
140
|
second(v) {
|
|
126
|
-
return v === undefined ? this
|
|
141
|
+
return v === undefined ? this.get('second') : this.set('second', v);
|
|
127
142
|
}
|
|
128
143
|
setComponents(c, mutate = false) {
|
|
129
144
|
const d = mutate ? this.$date : new Date(this.$date);
|
|
145
|
+
/* eslint-disable @typescript-eslint/no-unused-expressions */
|
|
130
146
|
if (c.year) {
|
|
131
|
-
d.setFullYear(c.year);
|
|
147
|
+
this.utcMode ? d.setUTCFullYear(c.year) : d.setFullYear(c.year);
|
|
132
148
|
}
|
|
133
149
|
if (c.month) {
|
|
134
|
-
d.setMonth(c.month - 1);
|
|
150
|
+
this.utcMode ? d.setUTCMonth(c.month - 1) : d.setMonth(c.month - 1);
|
|
135
151
|
}
|
|
136
152
|
if (c.day) {
|
|
137
|
-
d.setDate(c.day);
|
|
153
|
+
this.utcMode ? d.setUTCDate(c.day) : d.setDate(c.day);
|
|
138
154
|
}
|
|
139
155
|
if (c.hour !== undefined) {
|
|
140
|
-
d.setHours(c.hour);
|
|
156
|
+
this.utcMode ? d.setUTCHours(c.hour) : d.setHours(c.hour);
|
|
141
157
|
}
|
|
142
158
|
if (c.minute !== undefined) {
|
|
143
|
-
d.setMinutes(c.minute);
|
|
159
|
+
this.utcMode ? d.setUTCMinutes(c.minute) : d.setMinutes(c.minute);
|
|
144
160
|
}
|
|
145
161
|
if (c.second !== undefined) {
|
|
146
|
-
d.setSeconds(c.second);
|
|
162
|
+
this.utcMode ? d.setUTCSeconds(c.second) : d.setSeconds(c.second);
|
|
147
163
|
}
|
|
148
|
-
|
|
164
|
+
/* eslint-enable @typescript-eslint/no-unused-expressions */
|
|
165
|
+
return mutate ? this : new LocalTime(d, this.utcMode);
|
|
149
166
|
}
|
|
150
167
|
add(num, unit, mutate = false) {
|
|
151
168
|
return this.set(unit, this.get(unit) + num, mutate);
|
|
152
169
|
}
|
|
153
170
|
subtract(num, unit, mutate = false) {
|
|
154
|
-
return this.add(-
|
|
171
|
+
return this.add(num * -1, unit, mutate);
|
|
155
172
|
}
|
|
156
173
|
absDiff(other, unit) {
|
|
157
174
|
return Math.abs(this.diff(other, unit));
|
|
@@ -189,40 +206,23 @@ class LocalTime {
|
|
|
189
206
|
startOf(unit, mutate = false) {
|
|
190
207
|
if (unit === 'second')
|
|
191
208
|
return this;
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
const c = this.components();
|
|
210
|
-
c.second = 0;
|
|
211
|
-
if (unit === 'year') {
|
|
212
|
-
c.month = c.day = 1;
|
|
213
|
-
c.hour = c.minute = 0;
|
|
214
|
-
}
|
|
215
|
-
else if (unit === 'month') {
|
|
216
|
-
c.day = 1;
|
|
217
|
-
c.hour = c.minute = 0;
|
|
218
|
-
}
|
|
219
|
-
else if (unit === 'day') {
|
|
220
|
-
c.hour = c.minute = 0;
|
|
221
|
-
}
|
|
222
|
-
else if (unit === 'hour') {
|
|
223
|
-
c.minute = 0;
|
|
224
|
-
}
|
|
225
|
-
return LocalTime.fromComponents(c);
|
|
209
|
+
const d = mutate ? this.$date : new Date(this.$date);
|
|
210
|
+
/* eslint-disable @typescript-eslint/no-unused-expressions */
|
|
211
|
+
this.utcMode ? d.setUTCSeconds(0) : d.setSeconds(0);
|
|
212
|
+
if (unit !== 'minute') {
|
|
213
|
+
this.utcMode ? d.setUTCMinutes(0) : d.setMinutes(0);
|
|
214
|
+
if (unit !== 'hour') {
|
|
215
|
+
this.utcMode ? d.setUTCHours(0) : d.setHours(0);
|
|
216
|
+
if (unit !== 'day') {
|
|
217
|
+
this.utcMode ? d.setUTCDate(0) : d.setDate(0);
|
|
218
|
+
if (unit !== 'month') {
|
|
219
|
+
this.utcMode ? d.setUTCMonth(0) : d.setMonth(0);
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
/* eslint-enable @typescript-eslint/no-unused-expressions */
|
|
225
|
+
return mutate ? this : new LocalTime(d, this.utcMode);
|
|
226
226
|
}
|
|
227
227
|
static sort(items, mutate = false, descending = false) {
|
|
228
228
|
const mod = descending ? -1 : 1;
|
|
@@ -263,6 +263,15 @@ class LocalTime {
|
|
|
263
263
|
isSameOrAfter(d) {
|
|
264
264
|
return this.cmp(d) >= 0;
|
|
265
265
|
}
|
|
266
|
+
isBetween(min, max, incl = '[)') {
|
|
267
|
+
let r = this.cmp(min);
|
|
268
|
+
if (r < 0 || (r === 0 && incl[0] === '('))
|
|
269
|
+
return false;
|
|
270
|
+
r = this.cmp(max);
|
|
271
|
+
if (r > 0 || (r === 0 && incl[1] === ')'))
|
|
272
|
+
return false;
|
|
273
|
+
return true;
|
|
274
|
+
}
|
|
266
275
|
/**
|
|
267
276
|
* Returns 1 if this > d
|
|
268
277
|
* returns 0 if they are equal
|
|
@@ -277,6 +286,16 @@ class LocalTime {
|
|
|
277
286
|
}
|
|
278
287
|
// todo: endOf
|
|
279
288
|
components() {
|
|
289
|
+
if (this.utcMode) {
|
|
290
|
+
return {
|
|
291
|
+
year: this.$date.getUTCFullYear(),
|
|
292
|
+
month: this.$date.getUTCMonth() + 1,
|
|
293
|
+
day: this.$date.getUTCDate(),
|
|
294
|
+
hour: this.$date.getUTCHours(),
|
|
295
|
+
minute: this.$date.getUTCMinutes(),
|
|
296
|
+
second: this.$date.getSeconds(),
|
|
297
|
+
};
|
|
298
|
+
}
|
|
280
299
|
return {
|
|
281
300
|
year: this.$date.getFullYear(),
|
|
282
301
|
month: this.$date.getMonth() + 1,
|
|
@@ -286,11 +305,20 @@ class LocalTime {
|
|
|
286
305
|
second: this.$date.getSeconds(),
|
|
287
306
|
};
|
|
288
307
|
}
|
|
308
|
+
fromNow(now = LocalTime.now()) {
|
|
309
|
+
const msDiff = LocalTime.of(now).unixMillis() - this.unixMillis();
|
|
310
|
+
if (msDiff === 0)
|
|
311
|
+
return 'now';
|
|
312
|
+
if (msDiff >= 0) {
|
|
313
|
+
return `${(0, time_util_1._ms)(msDiff)} ago`;
|
|
314
|
+
}
|
|
315
|
+
return `in ${(0, time_util_1._ms)(msDiff * -1)}`;
|
|
316
|
+
}
|
|
289
317
|
getDate() {
|
|
290
318
|
return this.$date;
|
|
291
319
|
}
|
|
292
320
|
clone() {
|
|
293
|
-
return new LocalTime(new Date(this.$date));
|
|
321
|
+
return new LocalTime(new Date(this.$date), this.utcMode);
|
|
294
322
|
}
|
|
295
323
|
unix() {
|
|
296
324
|
return Math.floor(this.$date.valueOf() / 1000);
|
|
@@ -302,14 +330,31 @@ class LocalTime {
|
|
|
302
330
|
return Math.floor(this.$date.valueOf() / 1000);
|
|
303
331
|
}
|
|
304
332
|
toLocalDate() {
|
|
333
|
+
if (this.utcMode) {
|
|
334
|
+
return localDate_1.LocalDate.create(this.$date.getUTCFullYear(), this.$date.getUTCMonth() + 1, this.$date.getUTCDate());
|
|
335
|
+
}
|
|
305
336
|
return localDate_1.LocalDate.create(this.$date.getFullYear(), this.$date.getMonth() + 1, this.$date.getDate());
|
|
306
337
|
}
|
|
307
338
|
toPretty(seconds = true) {
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
.
|
|
311
|
-
.
|
|
312
|
-
.
|
|
339
|
+
const { year, month, day, hour, minute, second } = this.components();
|
|
340
|
+
return ([
|
|
341
|
+
String(year).padStart(4, '0'),
|
|
342
|
+
String(month).padStart(2, '0'),
|
|
343
|
+
String(day).padStart(2, '0'),
|
|
344
|
+
].join('-') +
|
|
345
|
+
' ' +
|
|
346
|
+
[
|
|
347
|
+
String(hour).padStart(2, '0'),
|
|
348
|
+
String(minute).padStart(2, '0'),
|
|
349
|
+
seconds && String(second).padStart(2, '0'),
|
|
350
|
+
]
|
|
351
|
+
.filter(Boolean)
|
|
352
|
+
.join(':'));
|
|
353
|
+
// return this.$date
|
|
354
|
+
// .toISOString()
|
|
355
|
+
// .slice(0, seconds ? 19 : 16)
|
|
356
|
+
// .split('T')
|
|
357
|
+
// .join(' ')
|
|
313
358
|
}
|
|
314
359
|
/**
|
|
315
360
|
* Returns e.g: `1984-06-21T17:56:21`, only the date part of DateTime
|
|
@@ -321,20 +366,41 @@ class LocalTime {
|
|
|
321
366
|
* Returns e.g: `1984-06-21`, only the date part of DateTime
|
|
322
367
|
*/
|
|
323
368
|
toISODate() {
|
|
324
|
-
|
|
369
|
+
const { year, month, day } = this.components();
|
|
370
|
+
return [
|
|
371
|
+
String(year).padStart(4, '0'),
|
|
372
|
+
String(month).padStart(2, '0'),
|
|
373
|
+
String(day).padStart(2, '0'),
|
|
374
|
+
].join('-');
|
|
375
|
+
// return this.$date.toISOString().slice(0, 10)
|
|
376
|
+
}
|
|
377
|
+
/**
|
|
378
|
+
* Returns e.g: `17:03:15` (or `17:03` with seconds=false)
|
|
379
|
+
*/
|
|
380
|
+
toISOTime(seconds = true) {
|
|
381
|
+
// return this.$date.toISOString().slice(11, seconds ? 19 : 16)
|
|
382
|
+
const { hour, minute, second } = this.components();
|
|
383
|
+
return [
|
|
384
|
+
String(hour).padStart(2, '0'),
|
|
385
|
+
String(minute).padStart(2, '0'),
|
|
386
|
+
seconds && String(second).padStart(2, '0'),
|
|
387
|
+
]
|
|
388
|
+
.filter(Boolean)
|
|
389
|
+
.join(':');
|
|
325
390
|
}
|
|
326
391
|
/**
|
|
327
392
|
* Returns e.g: `19840621_1705`
|
|
328
393
|
*/
|
|
329
394
|
toStringCompact(seconds = false) {
|
|
395
|
+
const { year, month, day, hour, minute, second } = this.components();
|
|
330
396
|
return [
|
|
331
|
-
String(
|
|
332
|
-
String(
|
|
333
|
-
String(
|
|
397
|
+
String(year).padStart(4, '0'),
|
|
398
|
+
String(month).padStart(2, '0'),
|
|
399
|
+
String(day).padStart(2, '0'),
|
|
334
400
|
'_',
|
|
335
|
-
String(
|
|
336
|
-
String(
|
|
337
|
-
seconds ? String(
|
|
401
|
+
String(hour).padStart(2, '0'),
|
|
402
|
+
String(minute).padStart(2, '0'),
|
|
403
|
+
seconds ? String(second).padStart(2, '0') : '',
|
|
338
404
|
].join('');
|
|
339
405
|
}
|
|
340
406
|
toString() {
|
package/dist/index.d.ts
CHANGED
|
@@ -63,7 +63,9 @@ export * from './math/stack.util';
|
|
|
63
63
|
export * from './string/leven';
|
|
64
64
|
export * from './datetime/localDate';
|
|
65
65
|
export * from './datetime/localTime';
|
|
66
|
-
|
|
66
|
+
export * from './datetime/dateInterval';
|
|
67
|
+
import { LocalDateConfig, LocalDateUnit, Inclusiveness } from './datetime/localDate';
|
|
67
68
|
import { LocalTimeConfig, LocalTimeUnit, LocalTimeComponents } from './datetime/localTime';
|
|
68
|
-
|
|
69
|
+
import { DateIntervalConfig } from './datetime/dateInterval';
|
|
70
|
+
export type { DateIntervalConfig, 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, IsoDateTime, Reviver, PMapOptions, Mapper, AsyncMapper, Predicate, AsyncPredicate, BatchResult, DeferredPromise, PRetryOptions, PTimeoutOptions, TryCatchOptions, StringifyAnyOptions, JsonStringifyFunction, Merge, ReadonlyDeep, Promisable, Simplify, ConditionalPick, ConditionalExcept, Class, 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, };
|
|
69
71
|
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/index.js
CHANGED
|
@@ -95,3 +95,4 @@ tslib_1.__exportStar(require("./math/stack.util"), exports);
|
|
|
95
95
|
tslib_1.__exportStar(require("./string/leven"), exports);
|
|
96
96
|
tslib_1.__exportStar(require("./datetime/localDate"), exports);
|
|
97
97
|
tslib_1.__exportStar(require("./datetime/localTime"), exports);
|
|
98
|
+
tslib_1.__exportStar(require("./datetime/dateInterval"), exports);
|