@giveback007/util-lib 0.25.4 → 1.0.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/README.md +7 -7
- package/dist/@types.d.ts +40 -42
- package/dist/@types.js +2 -2
- package/dist/array.d.ts +66 -66
- package/dist/array.js +128 -128
- package/dist/array.js.map +1 -1
- package/dist/clone.d.ts +2 -2
- package/dist/clone.js +44 -45
- package/dist/clone.js.map +1 -1
- package/dist/equality.d.ts +1 -1
- package/dist/equality.js +81 -82
- package/dist/equality.js.map +1 -1
- package/dist/general.d.ts +44 -37
- package/dist/general.js +172 -139
- package/dist/general.js.map +1 -1
- package/dist/index.d.ts +11 -11
- package/dist/index.js +27 -23
- package/dist/index.js.map +1 -1
- package/dist/iterate.d.ts +44 -44
- package/dist/iterate.js +43 -44
- package/dist/iterate.js.map +1 -1
- package/dist/number.d.ts +33 -32
- package/dist/number.js +57 -55
- package/dist/number.js.map +1 -1
- package/dist/object.d.ts +45 -49
- package/dist/object.js +82 -92
- package/dist/object.js.map +1 -1
- package/dist/string.d.ts +3 -4
- package/dist/string.js +8 -18
- package/dist/string.js.map +1 -1
- package/dist/test.d.ts +40 -41
- package/dist/test.js +65 -67
- package/dist/test.js.map +1 -1
- package/dist/time.d.ts +87 -93
- package/dist/time.js +230 -174
- package/dist/time.js.map +1 -1
- package/package.json +33 -45
- package/src/@types/types.d.ts +34 -0
- package/src/@types.ts +67 -67
- package/src/array.ts +175 -175
- package/src/clone.ts +34 -35
- package/src/equality.ts +80 -80
- package/src/general.ts +192 -152
- package/src/index.ts +11 -11
- package/src/iterate.ts +86 -86
- package/src/number.ts +64 -62
- package/src/object.ts +109 -123
- package/src/string.ts +6 -20
- package/src/test.ts +71 -74
- package/src/time.ts +268 -219
- package/dist/node/file-systems.d.ts +0 -1
- package/dist/node/file-systems.js +0 -16
- package/dist/node/file-systems.js.map +0 -1
- package/dist/node/index.d.ts +0 -1
- package/dist/node/index.js +0 -14
- package/dist/node/index.js.map +0 -1
- package/src/node/file-systems.ts +0 -16
- package/src/node/index.ts +0 -1
package/dist/time.d.ts
CHANGED
|
@@ -1,93 +1,87 @@
|
|
|
1
|
-
import { MsTime } from '.';
|
|
2
|
-
export declare const msTime: MsTime;
|
|
3
|
-
export declare const weekTuple: readonly ["
|
|
4
|
-
/**
|
|
5
|
-
* Converts Date to time of day.
|
|
6
|
-
* Good for use in
|
|
7
|
-
*
|
|
8
|
-
* If no parameter `dt = new Date()`.
|
|
9
|
-
* @param dt instance of Date obj
|
|
10
|
-
* @returns `hh:mm:ss:ms`
|
|
11
|
-
* @example "15:07:56:150"
|
|
12
|
-
*/
|
|
13
|
-
export declare function dateTimeToString(dt?: Date): string;
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
*/
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
export declare
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
};
|
|
89
|
-
export declare function yearStartEnd(t: number | Date, type?: 'unix' | 'local'): {
|
|
90
|
-
start: Date;
|
|
91
|
-
end: Date;
|
|
92
|
-
};
|
|
93
|
-
export {};
|
|
1
|
+
import { MsTime } from '.';
|
|
2
|
+
export declare const msTime: MsTime;
|
|
3
|
+
export declare const weekTuple: readonly ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];
|
|
4
|
+
/**
|
|
5
|
+
* Converts Date to time of day.
|
|
6
|
+
* Good for use in logging.
|
|
7
|
+
*
|
|
8
|
+
* If no parameter `dt = new Date()`.
|
|
9
|
+
* @param dt instance of Date obj
|
|
10
|
+
* @returns `hh:mm:ss:ms`
|
|
11
|
+
* @example "15:07:56:150"
|
|
12
|
+
*/
|
|
13
|
+
export declare function dateTimeToString(dt?: Date): string;
|
|
14
|
+
/** Gives the 'start' and 'end' of a day.
|
|
15
|
+
*
|
|
16
|
+
* Start of day is the first ms of the day
|
|
17
|
+
*
|
|
18
|
+
* End of day is the last ms of the day
|
|
19
|
+
*/
|
|
20
|
+
export declare function getDayStartEnd(t: number | Date, type?: 'unix' | 'local'): {
|
|
21
|
+
start: Date;
|
|
22
|
+
end: Date;
|
|
23
|
+
};
|
|
24
|
+
export declare function weekStartEnd(t: number | Date, weekStart?: 'sun' | 'mon', type?: 'unix' | 'local'): {
|
|
25
|
+
start: Date;
|
|
26
|
+
end: Date;
|
|
27
|
+
};
|
|
28
|
+
export declare function monthStartEnd(t: number | Date, type?: 'unix' | 'local'): {
|
|
29
|
+
start: Date;
|
|
30
|
+
end: Date;
|
|
31
|
+
};
|
|
32
|
+
export declare function yearStartEnd(t: number | Date, type?: 'unix' | 'local'): {
|
|
33
|
+
start: Date;
|
|
34
|
+
end: Date;
|
|
35
|
+
};
|
|
36
|
+
export declare const time: {
|
|
37
|
+
/** Seconds(n) -> to ms */
|
|
38
|
+
readonly sec: (n: num) => number;
|
|
39
|
+
/** Minutes(n) -> to ms */
|
|
40
|
+
readonly min: (n: num) => number;
|
|
41
|
+
/** Hours(n) -> to ms */
|
|
42
|
+
readonly hrs: (n: num) => number;
|
|
43
|
+
/** Days(n) -> to ms */
|
|
44
|
+
readonly dys: (n: num) => number;
|
|
45
|
+
/** Weeks(n) -> to ms */
|
|
46
|
+
readonly wks: (n: num) => number;
|
|
47
|
+
/** Count time elapsed since: `from` until `to`, `to` def to `Date.now()` */
|
|
48
|
+
readonly since: (from: num, to?: num) => number;
|
|
49
|
+
/** Count down to: `target` time from `from`, `from` def to `Date.now()` */
|
|
50
|
+
readonly until: (target: num, from?: num) => number;
|
|
51
|
+
readonly future: {
|
|
52
|
+
/** fnc(n) -> add ms to now */
|
|
53
|
+
readonly ms: (n: num) => number;
|
|
54
|
+
/** fnc(n) -> add sec to now */
|
|
55
|
+
readonly sec: (n: num) => number;
|
|
56
|
+
/** fnc(n) -> add min to now */
|
|
57
|
+
readonly min: (n: num) => number;
|
|
58
|
+
};
|
|
59
|
+
readonly msTo: {
|
|
60
|
+
/** fnc(n) -> from ms to num of seconds */
|
|
61
|
+
readonly sec: (ms: num) => number;
|
|
62
|
+
/** fnc(n) -> from ms to num of minutes */
|
|
63
|
+
readonly min: (ms: num) => number;
|
|
64
|
+
/** fnc(n) -> from ms to num of hours */
|
|
65
|
+
readonly hrs: (ms: num) => number;
|
|
66
|
+
/** fnc(n) -> from ms to num of days */
|
|
67
|
+
readonly dys: (ms: num) => number;
|
|
68
|
+
/** fnc(n) -> from ms to num of weeks */
|
|
69
|
+
readonly wks: (ms: num) => number;
|
|
70
|
+
};
|
|
71
|
+
readonly now: number;
|
|
72
|
+
};
|
|
73
|
+
export declare const humanizedTime: (date: AnyDate) => string | null;
|
|
74
|
+
/** A Date substitute, to make working with time easier and more versatile */
|
|
75
|
+
export declare function getTime(date: TimeObj | PartialTimeObj | TimeArr, ianaTimeZone?: str): {
|
|
76
|
+
tzOffsetMin: number;
|
|
77
|
+
localISO: string;
|
|
78
|
+
isoStr: string;
|
|
79
|
+
Date: Date;
|
|
80
|
+
epochMs: number;
|
|
81
|
+
ianaTZ: string;
|
|
82
|
+
timeObj: TimeObj;
|
|
83
|
+
};
|
|
84
|
+
export declare function timeZoneOffsetMin(ianaTimeZone: string, date?: Date): number;
|
|
85
|
+
export declare const timeObj: (dt: Date) => TimeObj;
|
|
86
|
+
export declare function parseDate(d: AnyDate): Date | null;
|
|
87
|
+
export declare function toLocalISOString(date: Date, tzOffsetMin?: number): string;
|
package/dist/time.js
CHANGED
|
@@ -1,175 +1,231 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
4
|
-
|
|
5
|
-
exports.
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
exports.
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
const
|
|
33
|
-
|
|
34
|
-
const
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
const
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
return
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
const
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
s =
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
const
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
exports.
|
|
104
|
-
/**
|
|
105
|
-
|
|
106
|
-
/**
|
|
107
|
-
*
|
|
108
|
-
|
|
109
|
-
*
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
if (
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
exports.
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.timeObj = exports.humanizedTime = exports.time = exports.weekTuple = exports.msTime = void 0;
|
|
4
|
+
exports.dateTimeToString = dateTimeToString;
|
|
5
|
+
exports.getDayStartEnd = getDayStartEnd;
|
|
6
|
+
exports.weekStartEnd = weekStartEnd;
|
|
7
|
+
exports.monthStartEnd = monthStartEnd;
|
|
8
|
+
exports.yearStartEnd = yearStartEnd;
|
|
9
|
+
exports.getTime = getTime;
|
|
10
|
+
exports.timeZoneOffsetMin = timeZoneOffsetMin;
|
|
11
|
+
exports.parseDate = parseDate;
|
|
12
|
+
exports.toLocalISOString = toLocalISOString;
|
|
13
|
+
const _1 = require(".");
|
|
14
|
+
exports.msTime = {
|
|
15
|
+
s: 1000,
|
|
16
|
+
m: 60000,
|
|
17
|
+
h: 3600000,
|
|
18
|
+
d: 86400000,
|
|
19
|
+
w: 604800000,
|
|
20
|
+
};
|
|
21
|
+
exports.weekTuple = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'];
|
|
22
|
+
/**
|
|
23
|
+
* Converts Date to time of day.
|
|
24
|
+
* Good for use in logging.
|
|
25
|
+
*
|
|
26
|
+
* If no parameter `dt = new Date()`.
|
|
27
|
+
* @param dt instance of Date obj
|
|
28
|
+
* @returns `hh:mm:ss:ms`
|
|
29
|
+
* @example "15:07:56:150"
|
|
30
|
+
*/
|
|
31
|
+
function dateTimeToString(dt = new Date()) {
|
|
32
|
+
const h = ('0' + dt.getHours()).slice(-2);
|
|
33
|
+
const m = ('0' + dt.getMinutes()).slice(-2);
|
|
34
|
+
const s = ('0' + dt.getSeconds()).slice(-2);
|
|
35
|
+
const ms = ('00' + dt.getMilliseconds()).slice(-3);
|
|
36
|
+
return `${h}:${m}:${s}:${ms}`;
|
|
37
|
+
}
|
|
38
|
+
/** Gives the 'start' and 'end' of a day.
|
|
39
|
+
*
|
|
40
|
+
* Start of day is the first ms of the day
|
|
41
|
+
*
|
|
42
|
+
* End of day is the last ms of the day
|
|
43
|
+
*/
|
|
44
|
+
function getDayStartEnd(t, type = 'local') {
|
|
45
|
+
if ((0, _1.isType)(t, 'number'))
|
|
46
|
+
t = new Date(t);
|
|
47
|
+
const y = t.getFullYear();
|
|
48
|
+
const m = t.getMonth();
|
|
49
|
+
const d = t.getDate();
|
|
50
|
+
let start = new Date(y, m, d);
|
|
51
|
+
let end = new Date(y, m, d, 23, 59, 59, 999);
|
|
52
|
+
if (type === 'unix') {
|
|
53
|
+
const tzOffset = exports.time.min(start.getTimezoneOffset());
|
|
54
|
+
start = new Date(start.getTime() + tzOffset);
|
|
55
|
+
end = new Date(end.getTime() + tzOffset);
|
|
56
|
+
}
|
|
57
|
+
return { start, end };
|
|
58
|
+
}
|
|
59
|
+
function weekStartEnd(t, weekStart = 'sun', type = 'local') {
|
|
60
|
+
if ((0, _1.isType)(t, 'number'))
|
|
61
|
+
t = new Date(t);
|
|
62
|
+
const wd = t.getDay();
|
|
63
|
+
const startOffset = wd * -1 + (weekStart === 'mon' ? 1 : 0);
|
|
64
|
+
const endOffset = 6 + startOffset;
|
|
65
|
+
const s = new Date(t.getTime() + exports.time.dys(startOffset));
|
|
66
|
+
const e = new Date(t.getTime() + exports.time.dys(endOffset));
|
|
67
|
+
let start = new Date(s.getFullYear(), s.getMonth(), s.getDate());
|
|
68
|
+
let end = new Date(e.getFullYear(), e.getMonth(), e.getDate(), 23, 59, 59, 999);
|
|
69
|
+
if (type === 'unix') {
|
|
70
|
+
const tzOffset = exports.time.min(start.getTimezoneOffset());
|
|
71
|
+
start = new Date(start.getTime() + tzOffset);
|
|
72
|
+
end = new Date(end.getTime() + tzOffset);
|
|
73
|
+
}
|
|
74
|
+
return { start, end };
|
|
75
|
+
}
|
|
76
|
+
function monthStartEnd(t, type = 'local') {
|
|
77
|
+
if ((0, _1.isType)(t, 'number'))
|
|
78
|
+
t = new Date(t);
|
|
79
|
+
const y = t.getFullYear();
|
|
80
|
+
const m = t.getMonth();
|
|
81
|
+
let start = new Date(y, m, 1);
|
|
82
|
+
let end = new Date(y, m, 0, 23, 59, 59, 999);
|
|
83
|
+
if (type === 'unix') {
|
|
84
|
+
const tzOffset = exports.time.min(start.getTimezoneOffset());
|
|
85
|
+
start = new Date(start.getTime() + tzOffset);
|
|
86
|
+
end = new Date(end.getTime() + tzOffset);
|
|
87
|
+
}
|
|
88
|
+
return { start, end };
|
|
89
|
+
}
|
|
90
|
+
function yearStartEnd(t, type = 'local') {
|
|
91
|
+
if ((0, _1.isType)(t, 'number'))
|
|
92
|
+
t = new Date(t);
|
|
93
|
+
const y = t.getFullYear();
|
|
94
|
+
let start = new Date(y, 0, 1);
|
|
95
|
+
let end = new Date(y, 12, 0, 23, 59, 59, 999);
|
|
96
|
+
if (type === 'unix') {
|
|
97
|
+
const tzOffset = exports.time.min(start.getTimezoneOffset());
|
|
98
|
+
start = new Date(start.getTime() + tzOffset);
|
|
99
|
+
end = new Date(end.getTime() + tzOffset);
|
|
100
|
+
}
|
|
101
|
+
return { start, end };
|
|
102
|
+
}
|
|
103
|
+
exports.time = {
|
|
104
|
+
/** Seconds(n) -> to ms */
|
|
105
|
+
sec: (n) => 1_000 * n,
|
|
106
|
+
/** Minutes(n) -> to ms */
|
|
107
|
+
min: (n) => 60_000 * n,
|
|
108
|
+
/** Hours(n) -> to ms */
|
|
109
|
+
hrs: (n) => 3_600_000 * n,
|
|
110
|
+
/** Days(n) -> to ms */
|
|
111
|
+
dys: (n) => 86_400_000 * n,
|
|
112
|
+
/** Weeks(n) -> to ms */
|
|
113
|
+
wks: (n) => 604_800_000 * n,
|
|
114
|
+
/** Count time elapsed since: `from` until `to`, `to` def to `Date.now()` */
|
|
115
|
+
since: (from, to = Date.now()) => to - from,
|
|
116
|
+
/** Count down to: `target` time from `from`, `from` def to `Date.now()` */
|
|
117
|
+
until: (target, from = Date.now()) => target - from,
|
|
118
|
+
future: {
|
|
119
|
+
/** fnc(n) -> add ms to now */
|
|
120
|
+
ms: (n) => Date.now() + n,
|
|
121
|
+
/** fnc(n) -> add sec to now */
|
|
122
|
+
sec: (n) => Date.now() + n * exports.msTime.s,
|
|
123
|
+
/** fnc(n) -> add min to now */
|
|
124
|
+
min: (n) => Date.now() + n * exports.msTime.m,
|
|
125
|
+
},
|
|
126
|
+
msTo: {
|
|
127
|
+
/** fnc(n) -> from ms to num of seconds */
|
|
128
|
+
sec: (ms) => ms / exports.msTime.s,
|
|
129
|
+
/** fnc(n) -> from ms to num of minutes */
|
|
130
|
+
min: (ms) => ms / exports.msTime.m,
|
|
131
|
+
/** fnc(n) -> from ms to num of hours */
|
|
132
|
+
hrs: (ms) => ms / exports.msTime.h,
|
|
133
|
+
/** fnc(n) -> from ms to num of days */
|
|
134
|
+
dys: (ms) => ms / exports.msTime.d,
|
|
135
|
+
/** fnc(n) -> from ms to num of weeks */
|
|
136
|
+
wks: (ms) => ms / exports.msTime.w,
|
|
137
|
+
},
|
|
138
|
+
get now() { return Date.now(); }
|
|
139
|
+
};
|
|
140
|
+
const humanizedTime = (date) => {
|
|
141
|
+
const dt = parseDate(date);
|
|
142
|
+
if (!dt)
|
|
143
|
+
return null;
|
|
144
|
+
const dif = exports.time.since(dt.getTime());
|
|
145
|
+
/** p for plural */
|
|
146
|
+
const p = (n) => n > 1 ? 's' : '';
|
|
147
|
+
if (dif < 0) {
|
|
148
|
+
return dt.toLocaleString();
|
|
149
|
+
}
|
|
150
|
+
else if (dif < exports.time.min(1)) {
|
|
151
|
+
const s = Math.ceil(exports.time.msTo.sec(dif)) || 1;
|
|
152
|
+
return `${s} second${p(s)} ago`;
|
|
153
|
+
}
|
|
154
|
+
else if (dif < exports.time.hrs(1)) {
|
|
155
|
+
const m = Math.floor(exports.time.msTo.min(dif));
|
|
156
|
+
return `${m} minute${p(m)} ago`;
|
|
157
|
+
}
|
|
158
|
+
else if (dif < exports.time.hrs(24) && dt.getDate() === new Date().getDate()) {
|
|
159
|
+
const h = Math.floor(exports.time.msTo.hrs(dif));
|
|
160
|
+
const m = Math.floor(exports.time.msTo.min(dif - exports.time.hrs(h)));
|
|
161
|
+
return `${h} hour${p(h)} ${m > 1 ? `${m} minute${p(m)} ` : ''}ago`;
|
|
162
|
+
}
|
|
163
|
+
else {
|
|
164
|
+
return dt.toLocaleString();
|
|
165
|
+
}
|
|
166
|
+
};
|
|
167
|
+
exports.humanizedTime = humanizedTime;
|
|
168
|
+
// Intl.supportedValuesOf('timeZone');
|
|
169
|
+
/** A Date substitute, to make working with time easier and more versatile */
|
|
170
|
+
function getTime(date, ianaTimeZone) {
|
|
171
|
+
const dt = parseDate(date);
|
|
172
|
+
if (!dt)
|
|
173
|
+
throw new Error('Invalid Date');
|
|
174
|
+
ianaTimeZone = ianaTimeZone || Intl.DateTimeFormat().resolvedOptions().timeZone;
|
|
175
|
+
const tzOffsetMin = timeZoneOffsetMin(ianaTimeZone, dt);
|
|
176
|
+
const localISO = toLocalISOString(dt, tzOffsetMin);
|
|
177
|
+
const finalDateObj = new Date(localISO);
|
|
178
|
+
return {
|
|
179
|
+
tzOffsetMin, // this flips from the JS output, to align with the ISO output (- when js would be +)
|
|
180
|
+
localISO,
|
|
181
|
+
isoStr: finalDateObj.toISOString(),
|
|
182
|
+
Date: finalDateObj,
|
|
183
|
+
epochMs: finalDateObj.getTime(),
|
|
184
|
+
ianaTZ: ianaTimeZone,
|
|
185
|
+
timeObj: (0, exports.timeObj)(dt),
|
|
186
|
+
};
|
|
187
|
+
}
|
|
188
|
+
function timeZoneOffsetMin(ianaTimeZone, date = new Date()) {
|
|
189
|
+
const local = new Date(date);
|
|
190
|
+
local.setMilliseconds(0);
|
|
191
|
+
local.setSeconds(0);
|
|
192
|
+
const tzTime = local.toLocaleString('US-en', { timeZone: ianaTimeZone });
|
|
193
|
+
const tzDt = new Date(tzTime);
|
|
194
|
+
const offsetMs = -local.getTimezoneOffset() * 60_000;
|
|
195
|
+
const utcTime = tzDt.getTime() + offsetMs;
|
|
196
|
+
return (utcTime - local.getTime()) / 60_000;
|
|
197
|
+
}
|
|
198
|
+
const timeObj = (dt) => ({
|
|
199
|
+
y: dt.getFullYear(),
|
|
200
|
+
m: dt.getMonth() + 1,
|
|
201
|
+
d: dt.getDate(),
|
|
202
|
+
h: dt.getHours(),
|
|
203
|
+
min: dt.getMinutes(),
|
|
204
|
+
sec: dt.getSeconds(),
|
|
205
|
+
ms: dt.getMilliseconds(),
|
|
206
|
+
wDay: exports.weekTuple[dt.getDay()]
|
|
207
|
+
});
|
|
208
|
+
exports.timeObj = timeObj;
|
|
209
|
+
function parseDate(d) {
|
|
210
|
+
const dt = Array.isArray(d) ?
|
|
211
|
+
new Date(d[0], (d[1] || 1) - 1, d[2] || 1, d[3] || 0, d[4] || 0, d[5] || 0, d[6] || 0)
|
|
212
|
+
:
|
|
213
|
+
(0, _1.isType)(d, 'object') && !(d instanceof Date) ?
|
|
214
|
+
new Date(d.y, (d?.m || 1) - 1, d.d || 1, d.h || 0, d.min || 0, d.sec || 0, d.ms || 0)
|
|
215
|
+
:
|
|
216
|
+
new Date(d);
|
|
217
|
+
if (isNaN(dt)) {
|
|
218
|
+
console.error(new Error(`Invalid Date: ${(0, _1.isType)(d, 'string') ? `"${d}"` : JSON.stringify(d)}`));
|
|
219
|
+
return null;
|
|
220
|
+
}
|
|
221
|
+
return dt;
|
|
222
|
+
}
|
|
223
|
+
function toLocalISOString(date, tzOffsetMin = -date.getTimezoneOffset()) {
|
|
224
|
+
const sign = tzOffsetMin >= 0 ? '+' : '-';
|
|
225
|
+
const diffHours = String(Math.floor(Math.abs(tzOffsetMin) / 60)).padStart(2, '0');
|
|
226
|
+
const diffMinutes = String(Math.abs(tzOffsetMin) % 60).padStart(2, '0');
|
|
227
|
+
return date.toLocaleString('lt').replace(' ', 'T') // or use: "sv-SE" (same effect)
|
|
228
|
+
+ '.' + (date.getMilliseconds() + '').padStart(3, '0')
|
|
229
|
+
+ sign + diffHours + ':' + diffMinutes;
|
|
230
|
+
}
|
|
175
231
|
//# sourceMappingURL=time.js.map
|