tscommons-esm-core 0.0.2
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/classes/commons-cache.d.mts +25 -0
- package/dist/classes/commons-cache.mjs +115 -0
- package/dist/classes/commons-cache.mjs.map +1 -0
- package/dist/classes/commons-cursor.d.mts +29 -0
- package/dist/classes/commons-cursor.mjs +108 -0
- package/dist/classes/commons-cursor.mjs.map +1 -0
- package/dist/classes/commons-fixed-date-range.d.mts +70 -0
- package/dist/classes/commons-fixed-date-range.mjs +232 -0
- package/dist/classes/commons-fixed-date-range.mjs.map +1 -0
- package/dist/classes/commons-fixed-date.d.mts +119 -0
- package/dist/classes/commons-fixed-date.mjs +588 -0
- package/dist/classes/commons-fixed-date.mjs.map +1 -0
- package/dist/classes/commons-fixed-duration.d.mts +96 -0
- package/dist/classes/commons-fixed-duration.mjs +478 -0
- package/dist/classes/commons-fixed-duration.mjs.map +1 -0
- package/dist/classes/commons-join-table.d.mts +29 -0
- package/dist/classes/commons-join-table.mjs +71 -0
- package/dist/classes/commons-join-table.mjs.map +1 -0
- package/dist/classes/commons-tally.d.mts +32 -0
- package/dist/classes/commons-tally.mjs +122 -0
- package/dist/classes/commons-tally.mjs.map +1 -0
- package/dist/classes/commons-timestamp-join-table.d.mts +11 -0
- package/dist/classes/commons-timestamp-join-table.mjs +25 -0
- package/dist/classes/commons-timestamp-join-table.mjs.map +1 -0
- package/dist/classes/commons-window-carousell.d.mts +15 -0
- package/dist/classes/commons-window-carousell.mjs +114 -0
- package/dist/classes/commons-window-carousell.mjs.map +1 -0
- package/dist/consts/commons-chars.d.mts +4 -0
- package/dist/consts/commons-chars.mjs +6 -0
- package/dist/consts/commons-chars.mjs.map +1 -0
- package/dist/consts/commons-const.d.mts +3 -0
- package/dist/consts/commons-const.mjs +5 -0
- package/dist/consts/commons-const.mjs.map +1 -0
- package/dist/consts/commons-regex.d.mts +41 -0
- package/dist/consts/commons-regex.mjs +44 -0
- package/dist/consts/commons-regex.mjs.map +1 -0
- package/dist/consts/commons-stopwords.d.mts +2 -0
- package/dist/consts/commons-stopwords.mjs +111 -0
- package/dist/consts/commons-stopwords.mjs.map +1 -0
- package/dist/enums/emap-rank-direction.d.mts +4 -0
- package/dist/enums/emap-rank-direction.mjs +6 -0
- package/dist/enums/emap-rank-direction.mjs.map +1 -0
- package/dist/helpers/commons-array.d.mts +22 -0
- package/dist/helpers/commons-array.mjs +363 -0
- package/dist/helpers/commons-array.mjs.map +1 -0
- package/dist/helpers/commons-base.d.mts +83 -0
- package/dist/helpers/commons-base.mjs +363 -0
- package/dist/helpers/commons-base.mjs.map +1 -0
- package/dist/helpers/commons-date.d.mts +78 -0
- package/dist/helpers/commons-date.mjs +775 -0
- package/dist/helpers/commons-date.mjs.map +1 -0
- package/dist/helpers/commons-encoding.d.mts +6 -0
- package/dist/helpers/commons-encoding.mjs +25 -0
- package/dist/helpers/commons-encoding.mjs.map +1 -0
- package/dist/helpers/commons-fixed-date.d.mts +10 -0
- package/dist/helpers/commons-fixed-date.mjs +67 -0
- package/dist/helpers/commons-fixed-date.mjs.map +1 -0
- package/dist/helpers/commons-fixed-duration.d.mts +3 -0
- package/dist/helpers/commons-fixed-duration.mjs +14 -0
- package/dist/helpers/commons-fixed-duration.mjs.map +1 -0
- package/dist/helpers/commons-map.d.mts +10 -0
- package/dist/helpers/commons-map.mjs +38 -0
- package/dist/helpers/commons-map.mjs.map +1 -0
- package/dist/helpers/commons-matrix.d.mts +5 -0
- package/dist/helpers/commons-matrix.mjs +46 -0
- package/dist/helpers/commons-matrix.mjs.map +1 -0
- package/dist/helpers/commons-number.d.mts +27 -0
- package/dist/helpers/commons-number.mjs +425 -0
- package/dist/helpers/commons-number.mjs.map +1 -0
- package/dist/helpers/commons-object.d.mts +4 -0
- package/dist/helpers/commons-object.mjs +44 -0
- package/dist/helpers/commons-object.mjs.map +1 -0
- package/dist/helpers/commons-string.d.mts +55 -0
- package/dist/helpers/commons-string.mjs +680 -0
- package/dist/helpers/commons-string.mjs.map +1 -0
- package/dist/helpers/commons-tree.d.mts +13 -0
- package/dist/helpers/commons-tree.mjs +81 -0
- package/dist/helpers/commons-tree.mjs.map +1 -0
- package/dist/helpers/commons-type.d.mts +308 -0
- package/dist/helpers/commons-type.mjs +1632 -0
- package/dist/helpers/commons-type.mjs.map +1 -0
- package/dist/index.d.mts +40 -0
- package/dist/index.mjs +33 -0
- package/dist/index.mjs.map +1 -0
- package/dist/types/tarray-with-errors.d.mts +4 -0
- package/dist/types/tarray-with-errors.mjs +2 -0
- package/dist/types/tarray-with-errors.mjs.map +1 -0
- package/dist/types/tdate-range.d.mts +5 -0
- package/dist/types/tdate-range.mjs +9 -0
- package/dist/types/tdate-range.mjs.map +1 -0
- package/dist/types/tdefined.d.mts +4 -0
- package/dist/types/tdefined.mjs +2 -0
- package/dist/types/tdefined.mjs.map +1 -0
- package/dist/types/tempty-object.d.mts +3 -0
- package/dist/types/tempty-object.mjs +2 -0
- package/dist/types/tempty-object.mjs.map +1 -0
- package/dist/types/tencoded.d.mts +6 -0
- package/dist/types/tencoded.mjs +2 -0
- package/dist/types/tencoded.mjs.map +1 -0
- package/dist/types/tenum-object.d.mts +3 -0
- package/dist/types/tenum-object.mjs +2 -0
- package/dist/types/tenum-object.mjs.map +1 -0
- package/dist/types/tinterquartile-model.d.mts +16 -0
- package/dist/types/tinterquartile-model.mjs +29 -0
- package/dist/types/tinterquartile-model.mjs.map +1 -0
- package/dist/types/tmap-rank.d.mts +4 -0
- package/dist/types/tmap-rank.mjs +2 -0
- package/dist/types/tmap-rank.mjs.map +1 -0
- package/dist/types/tmodules.d.mts +3 -0
- package/dist/types/tmodules.mjs +2 -0
- package/dist/types/tmodules.mjs.map +1 -0
- package/dist/types/tprimative.d.mts +4 -0
- package/dist/types/tprimative.mjs +2 -0
- package/dist/types/tprimative.mjs.map +1 -0
- package/dist/types/tproperty-object.d.mts +3 -0
- package/dist/types/tproperty-object.mjs +2 -0
- package/dist/types/tproperty-object.mjs.map +1 -0
- package/dist/types/tt-or-tarray.d.mts +2 -0
- package/dist/types/tt-or-tarray.mjs +9 -0
- package/dist/types/tt-or-tarray.mjs.map +1 -0
- package/package.json +27 -0
|
@@ -0,0 +1,775 @@
|
|
|
1
|
+
import { COMMONS_REGEX_PATTERN_DATETIME_YMDHIS, COMMONS_REGEX_PATTERN_DATE_YMD, COMMONS_REGEX_PATTERN_TIME_HIS, COMMONS_REGEX_PATTERN_DATETIME_DMYHI, COMMONS_REGEX_PATTERN_DATE_DMY, COMMONS_REGEX_PATTERN_TIME_HI, COMMONS_REGEX_PATTERN_DATETIME_YMDHIS_COMPRESSED } from '../consts/commons-regex.mjs';
|
|
2
|
+
import { CommonsTally } from '../classes/commons-tally.mjs';
|
|
3
|
+
import { commonsNumberFixedDigitRange, commonsNumberOrdinalUnit } from './commons-number.mjs';
|
|
4
|
+
function parseIsoString(date) {
|
|
5
|
+
const match = date.toISOString().match(/^([0-9]{4})-([0-9]{2})-([0-9]{2})T([0-9]{2}):([0-9]{2}):([0-9]{2})(?:\.[0-9]+)?Z$/);
|
|
6
|
+
if (!match)
|
|
7
|
+
throw new Error('Unable to parse ISO string');
|
|
8
|
+
return {
|
|
9
|
+
y: match[1],
|
|
10
|
+
m: match[2],
|
|
11
|
+
d: match[3],
|
|
12
|
+
h: match[4],
|
|
13
|
+
i: match[5],
|
|
14
|
+
s: match[6]
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
function fromDate(date) {
|
|
18
|
+
return {
|
|
19
|
+
y: date.getFullYear().toString().padStart(4, '0'),
|
|
20
|
+
m: (date.getMonth() + 1).toString().padStart(2, '0'),
|
|
21
|
+
d: date.getDate().toString().padStart(2, '0'),
|
|
22
|
+
h: date.getHours().toString().padStart(2, '0'),
|
|
23
|
+
i: date.getMinutes().toString().padStart(2, '0'),
|
|
24
|
+
s: date.getSeconds().toString().padStart(2, '0')
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
export function commonsDateTruncateToSecond(date, iso = false) {
|
|
28
|
+
if (iso) {
|
|
29
|
+
date.setUTCMilliseconds(0);
|
|
30
|
+
}
|
|
31
|
+
else {
|
|
32
|
+
date.setMilliseconds(0);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
export function commonsDateTruncateToMinute(date, iso = false) {
|
|
36
|
+
commonsDateTruncateToSecond(date);
|
|
37
|
+
if (iso) {
|
|
38
|
+
date.setUTCSeconds(0);
|
|
39
|
+
}
|
|
40
|
+
else {
|
|
41
|
+
date.setSeconds(0);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
export function commonsDateTruncateToHour(date, iso = false) {
|
|
45
|
+
commonsDateTruncateToMinute(date);
|
|
46
|
+
if (iso) {
|
|
47
|
+
date.setUTCMinutes(0);
|
|
48
|
+
}
|
|
49
|
+
else {
|
|
50
|
+
date.setMinutes(0);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
export function commonsDateTruncateToDate(date, iso = false) {
|
|
54
|
+
commonsDateTruncateToHour(date);
|
|
55
|
+
if (iso) {
|
|
56
|
+
date.setUTCHours(0);
|
|
57
|
+
}
|
|
58
|
+
else {
|
|
59
|
+
date.setHours(0);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
export function commonsDateQuantise(date, millis, direction) {
|
|
63
|
+
switch (direction) {
|
|
64
|
+
case 'down':
|
|
65
|
+
date.setTime(Math.floor(date.getTime() / millis) * millis);
|
|
66
|
+
break;
|
|
67
|
+
case 'up':
|
|
68
|
+
date.setTime(Math.ceil(date.getTime() / millis) * millis);
|
|
69
|
+
break;
|
|
70
|
+
case 'closest':
|
|
71
|
+
date.setTime(Math.round(date.getTime() / millis) * millis);
|
|
72
|
+
break;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
export function commonsDateQuantiseRange(range, millis, direction) {
|
|
76
|
+
switch (direction) {
|
|
77
|
+
case 'down':
|
|
78
|
+
case 'up':
|
|
79
|
+
case 'closest':
|
|
80
|
+
commonsDateQuantise(range.from, millis, direction);
|
|
81
|
+
commonsDateQuantise(range.to, millis, direction);
|
|
82
|
+
break;
|
|
83
|
+
case 'inner':
|
|
84
|
+
commonsDateQuantise(range.from, millis, 'up');
|
|
85
|
+
commonsDateQuantise(range.to, millis, 'down');
|
|
86
|
+
break;
|
|
87
|
+
case 'outer':
|
|
88
|
+
commonsDateQuantise(range.from, millis, 'down');
|
|
89
|
+
commonsDateQuantise(range.to, millis, 'up');
|
|
90
|
+
break;
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
export function commonsDateTallyQuantisedRanges(ranges, millis) {
|
|
94
|
+
const clone = [...ranges]
|
|
95
|
+
.map((range) => ({
|
|
96
|
+
from: new Date(range.from.getTime()),
|
|
97
|
+
to: new Date(range.to.getTime())
|
|
98
|
+
}));
|
|
99
|
+
const tally = new CommonsTally();
|
|
100
|
+
for (const range of clone) {
|
|
101
|
+
commonsDateQuantiseRange(range, millis, 'outer');
|
|
102
|
+
if (range.from.getTime() > range.to.getTime())
|
|
103
|
+
continue;
|
|
104
|
+
for (let i = range.from.getTime(); i < range.to.getTime(); i += millis) {
|
|
105
|
+
tally.increment(i);
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
const array = tally.asArray();
|
|
109
|
+
if (array.length === 0)
|
|
110
|
+
return [];
|
|
111
|
+
array
|
|
112
|
+
.sort((a, b) => {
|
|
113
|
+
if (a.key < b.key)
|
|
114
|
+
return -1;
|
|
115
|
+
if (a.key > b.key)
|
|
116
|
+
return 1;
|
|
117
|
+
return 0;
|
|
118
|
+
});
|
|
119
|
+
const result = [];
|
|
120
|
+
for (let i = array[0].key; i <= array[array.length - 1].key; i += millis) {
|
|
121
|
+
result.push({
|
|
122
|
+
date: new Date(i),
|
|
123
|
+
tally: tally.get(i) || 0
|
|
124
|
+
});
|
|
125
|
+
}
|
|
126
|
+
return result;
|
|
127
|
+
}
|
|
128
|
+
export function commonsDatePivotNextDay(date, pivotUntilHour, iso = false) {
|
|
129
|
+
if (iso) {
|
|
130
|
+
if (date.getUTCHours() < pivotUntilHour)
|
|
131
|
+
date.setUTCDate(date.getUTCDate() + 1);
|
|
132
|
+
}
|
|
133
|
+
else {
|
|
134
|
+
if (date.getHours() < pivotUntilHour)
|
|
135
|
+
date.setDate(date.getDate() + 1);
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
export function commonsDateAutoNextDay(prev, date, iso = false) {
|
|
139
|
+
while (prev.getTime() > date.getTime()) {
|
|
140
|
+
if (iso) {
|
|
141
|
+
date.setUTCDate(date.getDate() + 1);
|
|
142
|
+
}
|
|
143
|
+
else {
|
|
144
|
+
date.setDate(date.getDate() + 1);
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
export function commonsDateAutoNextDayRange(range, iso = false) {
|
|
149
|
+
commonsDateAutoNextDay(range.from, range.to, iso);
|
|
150
|
+
}
|
|
151
|
+
export function commonsDateAutoPivotNextDayRange(range, pivotUntilHour, iso = false) {
|
|
152
|
+
commonsDatePivotNextDay(range.from, pivotUntilHour, iso);
|
|
153
|
+
commonsDatePivotNextDay(range.to, pivotUntilHour, iso);
|
|
154
|
+
commonsDateAutoNextDayRange(range, iso);
|
|
155
|
+
}
|
|
156
|
+
export function commonsDateDateToYmd(date, iso = false) {
|
|
157
|
+
const components = iso ? parseIsoString(date) : fromDate(date);
|
|
158
|
+
return `${components.y}-${components.m}-${components.d}`;
|
|
159
|
+
}
|
|
160
|
+
export function commonsDateDateTodmY(date, iso = false) {
|
|
161
|
+
const components = iso ? parseIsoString(date) : fromDate(date);
|
|
162
|
+
return `${components.d}/${components.m}/${components.y}`;
|
|
163
|
+
}
|
|
164
|
+
export function commonsDateDateToHi(date, iso = false) {
|
|
165
|
+
const components = iso ? parseIsoString(date) : fromDate(date);
|
|
166
|
+
return `${components.h}:${components.i}`;
|
|
167
|
+
}
|
|
168
|
+
export function commonsDateDateToHis(date, iso = false) {
|
|
169
|
+
const components = iso ? parseIsoString(date) : fromDate(date);
|
|
170
|
+
return `${commonsDateDateToHi(date, iso)}:${components.s}`;
|
|
171
|
+
}
|
|
172
|
+
export function commonsDateDateToYmdHis(date, iso = false) {
|
|
173
|
+
return `${commonsDateDateToYmd(date, iso)} ${commonsDateDateToHis(date, iso)}`;
|
|
174
|
+
}
|
|
175
|
+
export function commonsDateDateTodmYHi(date, iso = false) {
|
|
176
|
+
return `${commonsDateDateTodmY(date, iso)} ${commonsDateDateToHi(date, iso)}`;
|
|
177
|
+
}
|
|
178
|
+
export function commonsDateIsYmdHis(test) {
|
|
179
|
+
const regex = new RegExp(COMMONS_REGEX_PATTERN_DATETIME_YMDHIS);
|
|
180
|
+
return regex.test(test);
|
|
181
|
+
}
|
|
182
|
+
export function commonsDateYmdHisToDate(date, iso = false) {
|
|
183
|
+
const match = date.match(COMMONS_REGEX_PATTERN_DATETIME_YMDHIS);
|
|
184
|
+
if (match === null)
|
|
185
|
+
throw new Error('Unable to parse date');
|
|
186
|
+
if (iso) {
|
|
187
|
+
return new Date(Date.parse(`${match[1]}-${match[2]}-${match[3]}T${match[4]}:${match[5]}:${match[6]}.000Z`));
|
|
188
|
+
}
|
|
189
|
+
const timestamp = new Date();
|
|
190
|
+
// due to how JavaScript automatically adjusts times, dates and months depending on things like the number of days and leap years, we have to compensate by initially setting the date to a generic first of month one
|
|
191
|
+
// otherwise, if the current date is 2020-07-31, and you want to set to 02 (feb), calling setMonth(1) actually ends up as rolling into 2 (mar)
|
|
192
|
+
timestamp.setHours(12); // midday is never affected by daylight saving
|
|
193
|
+
timestamp.setDate(2); // second is never affected by leap years and the like
|
|
194
|
+
timestamp.setFullYear(parseInt(match[1], 10));
|
|
195
|
+
timestamp.setMonth(parseInt(match[2], 10) - 1);
|
|
196
|
+
timestamp.setDate(parseInt(match[3], 10));
|
|
197
|
+
timestamp.setHours(parseInt(match[4], 10));
|
|
198
|
+
timestamp.setMinutes(parseInt(match[5], 10));
|
|
199
|
+
timestamp.setSeconds(parseInt(match[6], 10));
|
|
200
|
+
commonsDateTruncateToSecond(timestamp);
|
|
201
|
+
return timestamp;
|
|
202
|
+
}
|
|
203
|
+
export function commonsDateIsdmYHi(test) {
|
|
204
|
+
const regex = new RegExp(COMMONS_REGEX_PATTERN_DATETIME_DMYHI);
|
|
205
|
+
return regex.test(test);
|
|
206
|
+
}
|
|
207
|
+
export function commonsDatedmYHiToDate(date, iso = false) {
|
|
208
|
+
const match = date.match(COMMONS_REGEX_PATTERN_DATETIME_DMYHI);
|
|
209
|
+
if (match === null)
|
|
210
|
+
throw new Error('Unable to parse date');
|
|
211
|
+
if (iso) {
|
|
212
|
+
return new Date(Date.parse(`${match[3]}-${match[2]}-${match[1]}T${match[4]}:${match[5]}:00.000Z`));
|
|
213
|
+
}
|
|
214
|
+
const timestamp = new Date();
|
|
215
|
+
// due to how JavaScript automatically adjusts times, dates and months depending on things like the number of days and leap years, we have to compensate by initially setting the date to a generic first of month one
|
|
216
|
+
// otherwise, if the current date is 2020-07-31, and you want to set to 02 (feb), calling setMonth(1) actually ends up as rolling into 2 (mar)
|
|
217
|
+
timestamp.setHours(12); // midday is never affected by daylight saving
|
|
218
|
+
timestamp.setDate(2); // second is never affected by leap years and the like
|
|
219
|
+
timestamp.setFullYear(parseInt(match[3], 10));
|
|
220
|
+
timestamp.setMonth(parseInt(match[2], 10) - 1);
|
|
221
|
+
timestamp.setDate(parseInt(match[1], 10));
|
|
222
|
+
timestamp.setHours(parseInt(match[4], 10));
|
|
223
|
+
timestamp.setMinutes(parseInt(match[5], 10));
|
|
224
|
+
commonsDateTruncateToMinute(timestamp);
|
|
225
|
+
return timestamp;
|
|
226
|
+
}
|
|
227
|
+
export function commonsDateIsYmd(test) {
|
|
228
|
+
const regex = new RegExp(COMMONS_REGEX_PATTERN_DATE_YMD);
|
|
229
|
+
return regex.test(test);
|
|
230
|
+
}
|
|
231
|
+
export function commonsDateIsdmY(test) {
|
|
232
|
+
const regex = new RegExp(COMMONS_REGEX_PATTERN_DATE_DMY);
|
|
233
|
+
return regex.test(test);
|
|
234
|
+
}
|
|
235
|
+
export function commonsDateYmdToDate(date, iso = false) {
|
|
236
|
+
const match = date.match(COMMONS_REGEX_PATTERN_DATE_YMD);
|
|
237
|
+
if (match === null)
|
|
238
|
+
throw new Error('Unable to parse date');
|
|
239
|
+
if (iso) {
|
|
240
|
+
return new Date(Date.parse(`${match[1]}-${match[2]}-${match[3]}T00:00:00.000Z`));
|
|
241
|
+
}
|
|
242
|
+
const timestamp = new Date();
|
|
243
|
+
// due to how JavaScript automatically adjusts times, dates and months depending on things like the number of days and leap years, we have to compensate by initially setting the date to a generic first of month one
|
|
244
|
+
// otherwise, if the current date is 2020-07-31, and you want to set to 02 (feb), calling setMonth(1) actually ends up as rolling into 2 (mar)
|
|
245
|
+
timestamp.setHours(12); // midday is never affected by daylight saving
|
|
246
|
+
timestamp.setDate(2); // second is never affected by leap years and the like
|
|
247
|
+
timestamp.setFullYear(parseInt(match[1], 10));
|
|
248
|
+
timestamp.setMonth(parseInt(match[2], 10) - 1);
|
|
249
|
+
timestamp.setDate(parseInt(match[3], 10));
|
|
250
|
+
commonsDateTruncateToDate(timestamp);
|
|
251
|
+
return timestamp;
|
|
252
|
+
}
|
|
253
|
+
export function commonsDatedmYToDate(date, iso = false) {
|
|
254
|
+
const match = date.match(COMMONS_REGEX_PATTERN_DATE_DMY);
|
|
255
|
+
if (match === null)
|
|
256
|
+
throw new Error('Unable to parse date');
|
|
257
|
+
if (iso) {
|
|
258
|
+
return new Date(Date.parse(`${match[3]}-${match[2]}-${match[1]}T00:00:00.000Z`));
|
|
259
|
+
}
|
|
260
|
+
const timestamp = new Date();
|
|
261
|
+
// due to how JavaScript automatically adjusts times, dates and months depending on things like the number of days and leap years, we have to compensate by initially setting the date to a generic first of month one
|
|
262
|
+
// otherwise, if the current date is 2020-07-31, and you want to set to 02 (feb), calling setMonth(1) actually ends up as rolling into 2 (mar)
|
|
263
|
+
timestamp.setHours(12); // midday is never affected by daylight saving
|
|
264
|
+
timestamp.setDate(2); // second is never affected by leap years and the like
|
|
265
|
+
timestamp.setFullYear(parseInt(match[3], 10));
|
|
266
|
+
timestamp.setMonth(parseInt(match[2], 10) - 1);
|
|
267
|
+
timestamp.setDate(parseInt(match[1], 10));
|
|
268
|
+
commonsDateTruncateToDate(timestamp);
|
|
269
|
+
return timestamp;
|
|
270
|
+
}
|
|
271
|
+
export function commonsDateIsHis(test) {
|
|
272
|
+
const regex = new RegExp(COMMONS_REGEX_PATTERN_TIME_HIS);
|
|
273
|
+
return regex.test(test);
|
|
274
|
+
}
|
|
275
|
+
export function commonsDateHisToDate(date, iso = false) {
|
|
276
|
+
const match = date.match(COMMONS_REGEX_PATTERN_TIME_HIS);
|
|
277
|
+
if (match === null)
|
|
278
|
+
throw new Error('Unable to parse date');
|
|
279
|
+
if (iso) {
|
|
280
|
+
return new Date(Date.parse(`1970-01-01T${match[1]}:${match[2]}:${match[3]}.000Z`));
|
|
281
|
+
}
|
|
282
|
+
const timestamp = new Date();
|
|
283
|
+
// due to how JavaScript automatically adjusts times, dates and months depending on things like the number of days and leap years, we have to compensate by initially setting the date to a generic first of month one
|
|
284
|
+
// otherwise, if the current date is 2020-07-31, and you want to set to 02 (feb), calling setMonth(1) actually ends up as rolling into 2 (mar)
|
|
285
|
+
timestamp.setHours(12); // midday is never affected by daylight saving
|
|
286
|
+
timestamp.setDate(2); // second is never affected by leap years and the like
|
|
287
|
+
timestamp.setFullYear(1970);
|
|
288
|
+
timestamp.setMonth(0);
|
|
289
|
+
timestamp.setDate(1);
|
|
290
|
+
timestamp.setHours(parseInt(match[1], 10));
|
|
291
|
+
timestamp.setMinutes(parseInt(match[2], 10));
|
|
292
|
+
timestamp.setSeconds(parseInt(match[3], 10));
|
|
293
|
+
commonsDateTruncateToSecond(timestamp);
|
|
294
|
+
return timestamp;
|
|
295
|
+
}
|
|
296
|
+
export function commonsDateIsHi(test) {
|
|
297
|
+
const regex = new RegExp(COMMONS_REGEX_PATTERN_TIME_HI);
|
|
298
|
+
return regex.test(test);
|
|
299
|
+
}
|
|
300
|
+
export function commonsDateHiToDate(date, iso = false) {
|
|
301
|
+
const match = date.match(COMMONS_REGEX_PATTERN_TIME_HI);
|
|
302
|
+
if (match === null)
|
|
303
|
+
throw new Error('Unable to parse date');
|
|
304
|
+
if (iso) {
|
|
305
|
+
return new Date(Date.parse(`1970-01-01T${match[1]}:${match[2]}:00.000Z`));
|
|
306
|
+
}
|
|
307
|
+
const timestamp = new Date();
|
|
308
|
+
// due to how JavaScript automatically adjusts times, dates and months depending on things like the number of days and leap years, we have to compensate by initially setting the date to a generic first of month one
|
|
309
|
+
// otherwise, if the current date is 2020-07-31, and you want to set to 02 (feb), calling setMonth(1) actually ends up as rolling into 2 (mar)
|
|
310
|
+
timestamp.setHours(12); // midday is never affected by daylight saving
|
|
311
|
+
timestamp.setDate(2); // second is never affected by leap years and the like
|
|
312
|
+
timestamp.setFullYear(1970);
|
|
313
|
+
timestamp.setMonth(0);
|
|
314
|
+
timestamp.setDate(1);
|
|
315
|
+
timestamp.setHours(parseInt(match[1], 10));
|
|
316
|
+
timestamp.setMinutes(parseInt(match[2], 10));
|
|
317
|
+
commonsDateTruncateToMinute(timestamp);
|
|
318
|
+
return timestamp;
|
|
319
|
+
}
|
|
320
|
+
export function commonsDateDateWithoutTime(date, iso = false) {
|
|
321
|
+
return commonsDateYmdToDate(commonsDateDateToYmd(date, iso), iso);
|
|
322
|
+
}
|
|
323
|
+
export function commonsDateDateWithTime(date, time, iso = false) {
|
|
324
|
+
const ymd = commonsDateDateToYmd(date, iso);
|
|
325
|
+
const his = commonsDateDateToHis(time, iso);
|
|
326
|
+
return commonsDateYmdHisToDate(`${ymd} ${his}`);
|
|
327
|
+
}
|
|
328
|
+
export function commonsDateTodayTime(time, iso = false) {
|
|
329
|
+
return commonsDateDateWithTime(new Date(), time, iso);
|
|
330
|
+
}
|
|
331
|
+
export function commonsDateIsSameDate(a, b, iso = false) {
|
|
332
|
+
return commonsDateDateWithoutTime(a, iso).getTime() === commonsDateDateWithoutTime(b, iso).getTime();
|
|
333
|
+
}
|
|
334
|
+
export function commonsDateIsToday(test, iso = false) {
|
|
335
|
+
return commonsDateIsSameDate(test, new Date(), iso);
|
|
336
|
+
}
|
|
337
|
+
export function commonsDateIsYesterday(test, iso = false) {
|
|
338
|
+
const now = new Date();
|
|
339
|
+
now.setDate(now.getDate() - 1);
|
|
340
|
+
return commonsDateIsSameDate(test, now, iso);
|
|
341
|
+
}
|
|
342
|
+
export function commonsDateIsTomorrow(test, iso = false) {
|
|
343
|
+
const now = new Date();
|
|
344
|
+
now.setDate(now.getDate() + 1);
|
|
345
|
+
return commonsDateIsSameDate(test, now, iso);
|
|
346
|
+
}
|
|
347
|
+
export function commonsDateIsLast24Hours(test) {
|
|
348
|
+
const now = new Date();
|
|
349
|
+
const delta = now.getTime() - test.getTime();
|
|
350
|
+
return delta < (24 * 60 * 60 * 1000);
|
|
351
|
+
}
|
|
352
|
+
export function commonsDateIsLeapYear(year) {
|
|
353
|
+
return new Date(year, 1, 29).getDate() === 29;
|
|
354
|
+
}
|
|
355
|
+
export function commonsDateListTextMonths() {
|
|
356
|
+
return 'January,February,March,April,May,June,July,August,September,October,November,December'.split(',');
|
|
357
|
+
}
|
|
358
|
+
export function commonsDateGetTextMonth(month) {
|
|
359
|
+
if (month < 0 || month > 11)
|
|
360
|
+
return '';
|
|
361
|
+
return commonsDateListTextMonths()[month];
|
|
362
|
+
}
|
|
363
|
+
export function commonsDateGetNumericMonth(month) {
|
|
364
|
+
return commonsDateListTextMonths()
|
|
365
|
+
.map((m) => m.toUpperCase())
|
|
366
|
+
.map((m) => month.length === 3 ? m.substring(0, 3) : m)
|
|
367
|
+
.indexOf(month.toUpperCase());
|
|
368
|
+
}
|
|
369
|
+
export function commonsDateListTextDays() {
|
|
370
|
+
return 'Sunday,Monday,Tuesday,Wednesday,Thursday,Friday,Saturday'.split(',');
|
|
371
|
+
}
|
|
372
|
+
export function commonsDateGetTextDay(day) {
|
|
373
|
+
if (day < 0 || day > 6)
|
|
374
|
+
return '';
|
|
375
|
+
return commonsDateListTextDays()[day];
|
|
376
|
+
}
|
|
377
|
+
export function commonsDateGetNumericDay(day) {
|
|
378
|
+
return commonsDateListTextDays()
|
|
379
|
+
.map((m) => m.toUpperCase())
|
|
380
|
+
.map((m) => day.length === 3 ? m.substring(0, 3) : m)
|
|
381
|
+
.indexOf(day.toUpperCase());
|
|
382
|
+
}
|
|
383
|
+
export function commonsDateGetWeekDate(date, startDay = 0) {
|
|
384
|
+
if (startDay < 0 || startDay > 6)
|
|
385
|
+
throw new Error('Trying to get week start for day outside 0-6');
|
|
386
|
+
const clone = new Date(date.getTime());
|
|
387
|
+
commonsDateTruncateToDate(clone);
|
|
388
|
+
while (clone.getDay() !== startDay) {
|
|
389
|
+
clone.setDate(clone.getDate() - 1);
|
|
390
|
+
}
|
|
391
|
+
return clone;
|
|
392
|
+
}
|
|
393
|
+
export function commonsDateGetNumberOfDaysInMonth(month, year) {
|
|
394
|
+
switch (commonsDateGetTextMonth(month)) {
|
|
395
|
+
case 'September':
|
|
396
|
+
case 'April':
|
|
397
|
+
case 'June':
|
|
398
|
+
case 'November':
|
|
399
|
+
return 30;
|
|
400
|
+
case 'February':
|
|
401
|
+
return commonsDateIsLeapYear(year) ? 29 : 28;
|
|
402
|
+
default:
|
|
403
|
+
return 31;
|
|
404
|
+
}
|
|
405
|
+
}
|
|
406
|
+
export function commonsDateGetDayOfYear(date) {
|
|
407
|
+
let days = 0;
|
|
408
|
+
for (let i = 0; i < date.getMonth(); i++) {
|
|
409
|
+
days += commonsDateGetNumberOfDaysInMonth(i, date.getFullYear());
|
|
410
|
+
}
|
|
411
|
+
days += date.getDate();
|
|
412
|
+
return days;
|
|
413
|
+
}
|
|
414
|
+
export function commonsDateListTwoDigitDays(days) {
|
|
415
|
+
return commonsNumberFixedDigitRange(1, days, 2);
|
|
416
|
+
}
|
|
417
|
+
export function commonsDateDateToBashTimestamp(date, iso = false) {
|
|
418
|
+
const parts = commonsDateDateTodmY(date, iso).split('/');
|
|
419
|
+
return `${parts[0]} ${commonsDateGetTextMonth(date.getMonth()).toUpperCase().substring(0, 3)} ${parts[2]} ${commonsDateDateToHis(date)}`;
|
|
420
|
+
}
|
|
421
|
+
export function commonsDateDateToCompressed(date, iso = false) {
|
|
422
|
+
return commonsDateDateToYmdHis(date, iso).replace(/[-: ]/g, '');
|
|
423
|
+
}
|
|
424
|
+
export function commonsDateCompressedToDate(date, iso = false) {
|
|
425
|
+
const match = date.match(COMMONS_REGEX_PATTERN_DATETIME_YMDHIS_COMPRESSED);
|
|
426
|
+
if (match === null)
|
|
427
|
+
throw new Error('Unable to parse compressed date');
|
|
428
|
+
return commonsDateYmdHisToDate(`${match[1]}-${match[2]}-${match[3]} ${match[4]}:${match[5]}:${match[6]}`, iso);
|
|
429
|
+
}
|
|
430
|
+
export function commonsDateBuildDateFromYmdAndOptionals(ymd, hour, minute, second, millisecond, iso = false) {
|
|
431
|
+
const base = commonsDateYmdToDate(ymd, iso);
|
|
432
|
+
if (hour !== undefined)
|
|
433
|
+
base.setHours(hour);
|
|
434
|
+
if (minute !== undefined)
|
|
435
|
+
base.setMinutes(minute);
|
|
436
|
+
if (second !== undefined)
|
|
437
|
+
base.setSeconds(second);
|
|
438
|
+
if (millisecond !== undefined)
|
|
439
|
+
base.setMilliseconds(millisecond);
|
|
440
|
+
return base;
|
|
441
|
+
}
|
|
442
|
+
export function commonsDateGetPastTime(date, time) {
|
|
443
|
+
const clone = new Date(date.getTime());
|
|
444
|
+
clone.setHours(time.getHours());
|
|
445
|
+
clone.setMinutes(time.getMinutes());
|
|
446
|
+
clone.setSeconds(time.getSeconds());
|
|
447
|
+
clone.setMilliseconds(time.getMilliseconds());
|
|
448
|
+
if (clone.getTime() > date.getTime()) {
|
|
449
|
+
// timestamp is 'yesterday'
|
|
450
|
+
clone.setDate(clone.getDate() - 1);
|
|
451
|
+
}
|
|
452
|
+
return clone;
|
|
453
|
+
}
|
|
454
|
+
export function commonsDateBuildClosestDateFromTime(original, hour, minute, second, millisecond, magic12Hours = false) {
|
|
455
|
+
if (magic12Hours && hour < 13) {
|
|
456
|
+
const magicHour = (hour + 12) % 24;
|
|
457
|
+
const normalResult = commonsDateBuildClosestDateFromTime(original, hour, minute, second, millisecond);
|
|
458
|
+
const magicResult = commonsDateBuildClosestDateFromTime(original, magicHour, minute, second, millisecond);
|
|
459
|
+
const normalDelta = Math.abs(original.getTime() - normalResult.getTime());
|
|
460
|
+
const magicDelta = Math.abs(original.getTime() - magicResult.getTime());
|
|
461
|
+
if (magicDelta < normalDelta)
|
|
462
|
+
return magicResult;
|
|
463
|
+
return normalResult;
|
|
464
|
+
}
|
|
465
|
+
const clone = new Date(original.getTime());
|
|
466
|
+
if (hour !== undefined)
|
|
467
|
+
clone.setHours(hour);
|
|
468
|
+
if (minute !== undefined)
|
|
469
|
+
clone.setMinutes(minute);
|
|
470
|
+
if (second !== undefined)
|
|
471
|
+
clone.setSeconds(second);
|
|
472
|
+
if (millisecond !== undefined)
|
|
473
|
+
clone.setMilliseconds(millisecond);
|
|
474
|
+
clone.setDate(clone.getDate() - 1);
|
|
475
|
+
let closest;
|
|
476
|
+
let minDelta;
|
|
477
|
+
for (let i = -1; i <= 1; i++) {
|
|
478
|
+
const delta = Math.abs(clone.getTime() - original.getTime());
|
|
479
|
+
if (minDelta === undefined || delta < minDelta) {
|
|
480
|
+
closest = new Date(clone.getTime());
|
|
481
|
+
minDelta = delta;
|
|
482
|
+
}
|
|
483
|
+
clone.setDate(clone.getDate() + 1);
|
|
484
|
+
}
|
|
485
|
+
if (!closest)
|
|
486
|
+
throw new Error('Unable to match closest. This should not be possible');
|
|
487
|
+
return closest;
|
|
488
|
+
}
|
|
489
|
+
// NB, this may not cope with daylight saving changes
|
|
490
|
+
export function commonsDateFillEmptyMinutes(timestamps, interval = 1) {
|
|
491
|
+
let min;
|
|
492
|
+
let max;
|
|
493
|
+
for (const t of timestamps) {
|
|
494
|
+
if (min === undefined || t.getTime() < min.getTime())
|
|
495
|
+
min = new Date(t.getTime());
|
|
496
|
+
if (max === undefined || t.getTime() > max.getTime())
|
|
497
|
+
max = new Date(t.getTime());
|
|
498
|
+
}
|
|
499
|
+
if (min === undefined || max === undefined)
|
|
500
|
+
return [];
|
|
501
|
+
commonsDateTruncateToMinute(min);
|
|
502
|
+
const range = [];
|
|
503
|
+
while (min.getTime() <= max.getTime()) {
|
|
504
|
+
range.push(new Date(min.getTime()));
|
|
505
|
+
min.setMinutes(min.getMinutes() + interval);
|
|
506
|
+
}
|
|
507
|
+
return range;
|
|
508
|
+
}
|
|
509
|
+
// NB, this may not cope with daylight saving changes
|
|
510
|
+
export function commonsDateFillEmptyHours(timestamps) {
|
|
511
|
+
let min;
|
|
512
|
+
let max;
|
|
513
|
+
for (const t of timestamps) {
|
|
514
|
+
if (min === undefined || t.getTime() < min.getTime())
|
|
515
|
+
min = new Date(t.getTime());
|
|
516
|
+
if (max === undefined || t.getTime() > max.getTime())
|
|
517
|
+
max = new Date(t.getTime());
|
|
518
|
+
}
|
|
519
|
+
if (min === undefined || max === undefined)
|
|
520
|
+
return [];
|
|
521
|
+
commonsDateTruncateToHour(min);
|
|
522
|
+
const range = [];
|
|
523
|
+
while (min.getTime() <= max.getTime()) {
|
|
524
|
+
range.push(new Date(min.getTime()));
|
|
525
|
+
min.setHours(min.getHours() + 1);
|
|
526
|
+
}
|
|
527
|
+
return range;
|
|
528
|
+
}
|
|
529
|
+
// NB, this may not cope with daylight saving changes
|
|
530
|
+
export function commonsDateFillEmptyDays(timestamps) {
|
|
531
|
+
let min;
|
|
532
|
+
let max;
|
|
533
|
+
for (const t of timestamps) {
|
|
534
|
+
if (min === undefined || t.getTime() < min.getTime())
|
|
535
|
+
min = new Date(t.getTime());
|
|
536
|
+
if (max === undefined || t.getTime() > max.getTime())
|
|
537
|
+
max = new Date(t.getTime());
|
|
538
|
+
}
|
|
539
|
+
if (min === undefined || max === undefined)
|
|
540
|
+
return [];
|
|
541
|
+
commonsDateTruncateToDate(min);
|
|
542
|
+
const range = [];
|
|
543
|
+
while (min.getTime() <= max.getTime()) {
|
|
544
|
+
range.push(new Date(min.getTime()));
|
|
545
|
+
min.setDate(min.getDate() + 1);
|
|
546
|
+
}
|
|
547
|
+
return range;
|
|
548
|
+
}
|
|
549
|
+
export function commonsDateGetDateRangeFromDateArray(dates) {
|
|
550
|
+
if (dates.length === 0)
|
|
551
|
+
throw new Error('No dates to derive range from');
|
|
552
|
+
const min = new Date(dates[0].getTime());
|
|
553
|
+
const max = new Date(dates[0].getTime());
|
|
554
|
+
for (const date of dates.slice(1)) {
|
|
555
|
+
if (date.getTime() < min.getTime())
|
|
556
|
+
min.setTime(date.getTime());
|
|
557
|
+
if (date.getTime() > max.getTime())
|
|
558
|
+
max.setTime(date.getTime());
|
|
559
|
+
}
|
|
560
|
+
return {
|
|
561
|
+
from: min,
|
|
562
|
+
to: max
|
|
563
|
+
};
|
|
564
|
+
}
|
|
565
|
+
export function commonsDateGetDateArrayFromDateRange(range) {
|
|
566
|
+
const dates = [range.from, range.to];
|
|
567
|
+
return commonsDateFillEmptyDays(dates);
|
|
568
|
+
}
|
|
569
|
+
export function commonsDateGetHourArrayFromDateRange(range) {
|
|
570
|
+
const dates = [range.from, range.to];
|
|
571
|
+
return commonsDateFillEmptyHours(dates);
|
|
572
|
+
}
|
|
573
|
+
export function commonsDatePhpDate(format, date, iso = false) {
|
|
574
|
+
if (!date)
|
|
575
|
+
date = new Date();
|
|
576
|
+
const components = iso ? parseIsoString(date) : fromDate(date);
|
|
577
|
+
const fixedDate = date;
|
|
578
|
+
return format
|
|
579
|
+
.split('')
|
|
580
|
+
.map((c) => {
|
|
581
|
+
switch (c) {
|
|
582
|
+
case 'd':
|
|
583
|
+
return components.d;
|
|
584
|
+
case 'D':
|
|
585
|
+
return commonsDateGetTextDay(fixedDate.getDay()).substr(0, 3);
|
|
586
|
+
case 'j':
|
|
587
|
+
return parseInt(components.d, 10).toString(10);
|
|
588
|
+
case 'l':
|
|
589
|
+
return commonsDateGetTextDay(fixedDate.getDay());
|
|
590
|
+
case 'S':
|
|
591
|
+
return commonsNumberOrdinalUnit(parseInt(components.d, 10));
|
|
592
|
+
case 'w':
|
|
593
|
+
return fixedDate.getDay().toString(10);
|
|
594
|
+
case 'F':
|
|
595
|
+
return commonsDateGetTextMonth(parseInt(components.m, 10) - 1);
|
|
596
|
+
case 'm':
|
|
597
|
+
return components.m;
|
|
598
|
+
case 'M':
|
|
599
|
+
return commonsDateGetTextMonth(parseInt(components.m, 10) - 1).substr(0, 3);
|
|
600
|
+
case 'n':
|
|
601
|
+
return parseInt(components.m, 10).toString(10);
|
|
602
|
+
case 'Y':
|
|
603
|
+
return components.y;
|
|
604
|
+
case 'y':
|
|
605
|
+
return components.y.substr(2);
|
|
606
|
+
case 'a': {
|
|
607
|
+
const h = parseInt(components.h, 10);
|
|
608
|
+
return h < 12 ? 'am' : 'pm';
|
|
609
|
+
}
|
|
610
|
+
case 'A': {
|
|
611
|
+
const h = parseInt(components.h, 10);
|
|
612
|
+
return h < 12 ? 'AM' : 'PM';
|
|
613
|
+
}
|
|
614
|
+
case 'g': {
|
|
615
|
+
const h = parseInt(components.h, 10) % 12;
|
|
616
|
+
if (h === 0)
|
|
617
|
+
return '12';
|
|
618
|
+
return h.toString(10);
|
|
619
|
+
}
|
|
620
|
+
case 'G':
|
|
621
|
+
return parseInt(components.h, 10).toString(10);
|
|
622
|
+
case 'h': {
|
|
623
|
+
const h = parseInt(components.h, 10) % 12;
|
|
624
|
+
if (h === 0)
|
|
625
|
+
return '12';
|
|
626
|
+
return h.toString(10).padStart(2, '0');
|
|
627
|
+
}
|
|
628
|
+
case 'H':
|
|
629
|
+
return components.h;
|
|
630
|
+
case 'i':
|
|
631
|
+
return components.i;
|
|
632
|
+
case 's':
|
|
633
|
+
return components.s;
|
|
634
|
+
default:
|
|
635
|
+
return c;
|
|
636
|
+
}
|
|
637
|
+
})
|
|
638
|
+
.join('');
|
|
639
|
+
}
|
|
640
|
+
export function commonsDateGetDeltaHours(range, inclusive = false) {
|
|
641
|
+
const from = new Date(range.from.getTime());
|
|
642
|
+
const to = new Date(range.to.getTime());
|
|
643
|
+
commonsDateTruncateToHour(from);
|
|
644
|
+
commonsDateTruncateToHour(to);
|
|
645
|
+
// we use this slightly ineffecient way to avoid BST
|
|
646
|
+
let count = 0;
|
|
647
|
+
while (from.getTime() < to.getTime()) {
|
|
648
|
+
count++;
|
|
649
|
+
from.setHours(from.getHours() + 1);
|
|
650
|
+
}
|
|
651
|
+
return count + (inclusive ? 1 : 0);
|
|
652
|
+
}
|
|
653
|
+
export function commonsDateGetDeltaDays(range, inclusive = false, iso = false) {
|
|
654
|
+
const from = commonsDateDateWithoutTime(range.from, iso);
|
|
655
|
+
const to = commonsDateDateWithoutTime(range.to, iso);
|
|
656
|
+
// we use this slightly ineffecient way to avoid BST
|
|
657
|
+
let count = 0;
|
|
658
|
+
while (from.getTime() < to.getTime()) {
|
|
659
|
+
count++;
|
|
660
|
+
from.setDate(from.getDate() + 1);
|
|
661
|
+
}
|
|
662
|
+
return count + (inclusive ? 1 : 0);
|
|
663
|
+
}
|
|
664
|
+
export function commonsDateGetDeltaMonths(range, inclusive = false, iso = false) {
|
|
665
|
+
const from = commonsDateDateWithoutTime(range.from, iso);
|
|
666
|
+
const to = commonsDateDateWithoutTime(range.to, iso);
|
|
667
|
+
from.setDate(1);
|
|
668
|
+
to.setDate(1);
|
|
669
|
+
// we use this slightly ineffecient way to avoid BST
|
|
670
|
+
let count = 0;
|
|
671
|
+
while (from.getTime() < to.getTime()) {
|
|
672
|
+
count++;
|
|
673
|
+
from.setMonth(from.getMonth() + 1);
|
|
674
|
+
}
|
|
675
|
+
return count + (inclusive ? 1 : 0);
|
|
676
|
+
}
|
|
677
|
+
export function commonsDateGetDeltaYears(range, inclusive = false, iso = false) {
|
|
678
|
+
const from = commonsDateDateWithoutTime(range.from, iso);
|
|
679
|
+
const to = commonsDateDateWithoutTime(range.to, iso);
|
|
680
|
+
from.setDate(1);
|
|
681
|
+
from.setMonth(0);
|
|
682
|
+
to.setDate(1);
|
|
683
|
+
to.setMonth(0);
|
|
684
|
+
// we use this slightly ineffecient way to avoid BST
|
|
685
|
+
let count = 0;
|
|
686
|
+
while (from.getTime() < to.getTime()) {
|
|
687
|
+
count++;
|
|
688
|
+
from.setFullYear(from.getFullYear() + 1);
|
|
689
|
+
}
|
|
690
|
+
return count + (inclusive ? 1 : 0);
|
|
691
|
+
}
|
|
692
|
+
export function commonsDatePrettyDate(date, absolute = false, now, includeYear = true, iso = false) {
|
|
693
|
+
if (!now)
|
|
694
|
+
now = new Date();
|
|
695
|
+
if (!absolute) { // default enabled
|
|
696
|
+
const clone = new Date(now.getTime());
|
|
697
|
+
if (commonsDateIsSameDate(date, clone))
|
|
698
|
+
return 'Today';
|
|
699
|
+
clone.setDate(now.getDate() - 1);
|
|
700
|
+
if (commonsDateIsSameDate(date, clone))
|
|
701
|
+
return 'Yesterday';
|
|
702
|
+
clone.setDate(now.getDate() + 1);
|
|
703
|
+
if (commonsDateIsSameDate(date, clone))
|
|
704
|
+
return 'Tomorrow';
|
|
705
|
+
}
|
|
706
|
+
return commonsDatePhpDate(`D d M${includeYear ? ' Y' : ''}`, date, iso);
|
|
707
|
+
}
|
|
708
|
+
function getDeltaSeconds(timestamp, future) {
|
|
709
|
+
const delta = (future.getTime() - timestamp.getTime()) / 1000;
|
|
710
|
+
if (delta < 0)
|
|
711
|
+
return undefined;
|
|
712
|
+
return delta;
|
|
713
|
+
}
|
|
714
|
+
export function commonsDatePrettyTime(time, absolute = false, now, iso = false) {
|
|
715
|
+
if (!now)
|
|
716
|
+
now = new Date();
|
|
717
|
+
if (!absolute) { // default enabled
|
|
718
|
+
const seconds = getDeltaSeconds(time, now);
|
|
719
|
+
if (seconds !== undefined) {
|
|
720
|
+
if (seconds < 60)
|
|
721
|
+
return 'Just now';
|
|
722
|
+
if (seconds < 120)
|
|
723
|
+
return '1 min ago';
|
|
724
|
+
if (seconds < (10 * 60)) {
|
|
725
|
+
const mins = Math.floor(seconds / 60);
|
|
726
|
+
return `${mins} mins ago`;
|
|
727
|
+
}
|
|
728
|
+
}
|
|
729
|
+
}
|
|
730
|
+
return commonsDatePhpDate('H:i', time, iso);
|
|
731
|
+
}
|
|
732
|
+
export function commonsDatePrettyDateTime(timestamp, absolute = false, now, includeYear = true, iso = false) {
|
|
733
|
+
if (!now)
|
|
734
|
+
now = new Date();
|
|
735
|
+
if (!absolute) { // default enabled
|
|
736
|
+
const seconds = getDeltaSeconds(timestamp, now);
|
|
737
|
+
if (seconds !== undefined && seconds < (10 * 60)) {
|
|
738
|
+
return commonsDatePrettyTime(timestamp, false, now, iso);
|
|
739
|
+
}
|
|
740
|
+
}
|
|
741
|
+
return `${commonsDatePrettyDate(timestamp, absolute, now, includeYear, iso)} ${commonsDatePrettyTime(timestamp, absolute, now, iso)}`;
|
|
742
|
+
}
|
|
743
|
+
export function commonsDatePrettyTimeRange(range, absolute = true, // inverse
|
|
744
|
+
now, iso = false) {
|
|
745
|
+
const sFrom = commonsDatePrettyTime(range.from, absolute, now, iso);
|
|
746
|
+
const sTo = commonsDatePrettyTime(range.to, absolute, now, iso);
|
|
747
|
+
if (commonsDateIsSameDate(range.from, range.to) && sFrom === sTo)
|
|
748
|
+
return sFrom;
|
|
749
|
+
return `${sFrom} - ${sTo}`;
|
|
750
|
+
}
|
|
751
|
+
export function commonsDatePrettyDateTimeRange(range, absolute, // not default value, so can apply the different defaults in prettyDate and prettyTimeRange
|
|
752
|
+
now, includeYear = true, iso = false) {
|
|
753
|
+
const absoluteDate = absolute === undefined ? false : absolute;
|
|
754
|
+
const absoluteTime = absolute === undefined ? true : absolute;
|
|
755
|
+
if (commonsDateIsSameDate(range.from, range.to)) {
|
|
756
|
+
return `${commonsDatePrettyDate(range.from, absoluteDate, now, includeYear, iso)} ${commonsDatePrettyTimeRange(range, absoluteTime, now, iso)}`;
|
|
757
|
+
}
|
|
758
|
+
return `${commonsDatePrettyDate(range.from, absoluteDate, now, includeYear, iso)} ${commonsDatePrettyTime(range.from, absoluteTime, now, iso)} - ${commonsDatePrettyDate(range.to, absoluteDate, now, includeYear, iso)} ${commonsDatePrettyTime(range.to, absoluteTime, now, iso)}`;
|
|
759
|
+
}
|
|
760
|
+
export function commonsDateSort(values) {
|
|
761
|
+
return values
|
|
762
|
+
.sort((a, b) => a.getTime() - b.getTime());
|
|
763
|
+
}
|
|
764
|
+
export function commonsDateFakeUTCNow() {
|
|
765
|
+
const date = new Date();
|
|
766
|
+
date.setUTCMilliseconds(date.getMilliseconds());
|
|
767
|
+
date.setUTCSeconds(date.getSeconds());
|
|
768
|
+
date.setUTCMinutes(date.getMinutes());
|
|
769
|
+
date.setUTCHours(date.getHours());
|
|
770
|
+
date.setUTCDate(date.getDate());
|
|
771
|
+
date.setUTCMonth(date.getMonth());
|
|
772
|
+
date.setUTCFullYear(date.getFullYear());
|
|
773
|
+
return date;
|
|
774
|
+
}
|
|
775
|
+
//# sourceMappingURL=commons-date.mjs.map
|