-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathdateTimeUtils.js
433 lines (396 loc) · 13.1 KB
/
dateTimeUtils.js
1
2
3
4
5
6
7
8
9
10
11
12
13
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
import moment from 'moment-timezone';
import dayjs from 'dayjs';
import noop from 'lodash/noop';
import timezone from 'dayjs/plugin/timezone';
import localeData from 'dayjs/plugin/localeData';
import utc from 'dayjs/plugin/utc';
import customParseFormat from 'dayjs/plugin/customParseFormat';
import isoWeek from 'dayjs/plugin/isoWeek';
import weekOfYear from 'dayjs/plugin/weekOfYear';
import weekday from 'dayjs/plugin/weekday';
import arraySupport from 'dayjs/plugin/arraySupport';
import objectSupport from 'dayjs/plugin/objectSupport';
import isSameOrBefore from 'dayjs/plugin/isSameOrBefore';
import isSameOrAfter from 'dayjs/plugin/isSameOrAfter';
import isBetween from 'dayjs/plugin/isBetween';
import availableLocales from 'dayjs/locale'
dayjs.extend(timezone);
dayjs.extend(localeData);
dayjs.extend(utc);
dayjs.extend(objectSupport);
dayjs.extend(isoWeek);
dayjs.extend(weekOfYear);
dayjs.extend(weekday);
dayjs.extend(arraySupport);
dayjs.extend(customParseFormat);
dayjs.extend(isSameOrBefore);
dayjs.extend(isSameOrAfter);
dayjs.extend(isBetween);
// export a pre-extended dayjs for consumption.
export { dayjs };
const DEFAULT_LOCALE = 'en-US';
/** DayJS range utility class. */
export class DayRange {
/**
* Create a DayRange.
* @param {DayJS|dateString} start - the start value.
* @param {DayJS|dateString} end - the end value.
*/
constructor(start, end) {
this.start = dayjs(start);
this.end = dayjs(end);
this.isRange = true;
}
/**
* returns an array of contained dayjs day objects.
*
* @method
* @name DayRange#asDayJSArray
* @returns {Array} of dayjs objects.
*/
asDayJSArray = () => {
const range = [];
let current = dayjs(this.start);
while (current.isBefore(this.end)) {
range.push(current.clone());
current = current.add(1, 'day');
}
return range;
};
/**
* equality check.
*
* @method
* @name DayRange#isSame
* @param { DayRange }
* @returns {Bool}
*/
isSame = (candidate) => {
return this.start.isSame(candidate.start, 'day') && this.end.isSame(candidate.end, 'day');
};
/**
* returns true if candidate is fully within or equal to the range. Can be used with single dayjs objects
* or date strings as well.
*
* @method
* @name DayRange#contains
* @param { DayRange|Dayjs }
* @returns {Bool}
*/
contains = (candidate) => {
if (candidate instanceof DayRange) {
return this.isSame(candidate) ||
(this.contains(candidate.start) && this.contains(candidate.end));
} else {
return dayjs(candidate).isBetween(this.start, this.end);
}
};
/**
* returns true if candidate start or end is within the range, or if candidate is equal to the range.
*
* @method
* @name DayRange#overlaps
* @param { DayRange|Dayjs }
* @returns {Bool}
*/
overlaps = (candidate) => {
if (candidate instanceof DayRange) {
return this.isSame(candidate) ||
this.contains(candidate.start) ||
this.contains(candidate.end);
} else {
throw new Error('parameter should be a DayRange instance');
}
};
}
/**
* Since Moment is still in use, we can keep this for sake of easing the transition.
* @deprecated
*
* @export
* @param {*} intl
* @returns {String}
*/
export function getMomentLocalizedFormat(intl) {
moment.locale(intl.locale);
const format = moment.localeData()._longDateFormat.L;
return format;
}
/**
* getDayJSLocalizedFormat
* Fallback function in case getLocaleDateFormat is unable to perform.
*
* @export
* @param {*} intl
* @returns {String}
*/
export const getDayJSLocalizedFormat = (intl) => {
dayjs.locale(intl.locale);
return dayjs.localeData().longDateFormat('L');
};
/** dateCanBeParsed
* Due to some differentiating behavior between passing a single formats vs an array of formats to Dayjs.utc,
* we're implementing this utility function...
* We can probably remove this once https://github.com/iamkun/dayjs/pull/1914 is merged...
*
* @export
* @param {String} value - the date string to be validated.
* @param {Array.<String>} formats - an array of formats to attempt parsing the value with. The first to
* parse successfully will be returned as the validFormat.
* @returns {String}
*/
export const dateCanBeParsed = (value, formats) => ({
isValid: formats.some((f) => dayjs.utc(value, f).isValid()),
validFormat: formats[formats.findIndex((f) => dayjs(value, f, true).isValid())]
});
/**
* getCompatibleDayJSLocale -
* Function that returns an existing DayJS locale. Returns undefined if the static locale does not exist.
*
* @param {String} locale - locale string ex : 'en-SE'
* @param {String} parentLocale - 2 character language of the locale...ex parentLocale of
*/
export const getCompatibleDayJSLocale = (locale, parentLanguage) => {
/**
* Check for availability of locales - DayJS comes with a JSON list of available locales.
* We can check against that before attempting to load. We check for the full locale
* first, followed by the language if the full locale doesn't work.
*/
let localeToLoad;
let available = availableLocales.findIndex(l => l.key === locale);
if (available !== -1) {
localeToLoad = locale;
} else {
available = availableLocales.findIndex(l => l.key === parentLanguage);
if (available !== -1) {
localeToLoad = parentLanguage;
} else {
// eslint-disable-next-line no-console
console.error(`${locale}/${parentLanguage} unavailable for DayJS`);
}
}
return localeToLoad;
};
/**
* loadDayJSLocale
* dynamically loads a DayJS locale and sets the global DayJS locale.
* @param {string} locale
* */
export function loadDayJSLocale(locale, cb = noop) {
const parentLocale = locale.split('-')[0];
// Locale loading setup for DayJS
// 'en-US' is default and always loaded, so we don't even worry about loading another if the language is English.
if (locale !== DEFAULT_LOCALE) {
const localeToLoad = getCompatibleDayJSLocale(locale, parentLocale);
if (localeToLoad) {
import(
/* webpackChunkName: "dayjs-locale-[request]" */
/* webpackExclude: /\.d\.ts$/ */
`dayjs/locale/${localeToLoad}`
).then(() => {
dayjs.locale(localeToLoad);
cb(localeToLoad);
}).catch(e => {
// eslint-disable-next-line no-console
console.error(`Error loading locale ${localeToLoad} for DayJS`, e);
cb(localeToLoad, e);
});
} else {
// fall back to english in case a compatible locale can't be loaded.
dayjs.locale(DEFAULT_LOCALE);
cb(DEFAULT_LOCALE);
}
} else {
// set locale to english in case we're transitioning away from a non-english locale.
dayjs.locale(DEFAULT_LOCALE);
cb(DEFAULT_LOCALE);
}
}
/**
* Returns a localized format.
* Format will be a string similar to YYYY.MM.DD - something that can be
* passed to moment/dayjs for parsing/formatting purposes.
* @export
* @param {Object} settings -
* @param {Object} settings.intl - the intl object from context
* @param {config} settings.config - sets the options for IntlDateTimeFormat. See
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DateTimeFormat#using_options
* @returns {String} - something similar to 'YYYY-MM-DD' or 'MM/DD/YYYY' that you could provide to a date-parsing library.
*/
export const getLocaleDateFormat = ({ intl, config }) => {
const tempDate = new Date('Thu May 14 2020 14:39:25 GMT-0500');
let format = '';
// set up a locally formatted array of parts...
if (Intl?.DateTimeFormat()?.formatToParts) {
const intlFormatter = new Intl.DateTimeFormat(intl.locale, config || {
day: '2-digit',
year: 'numeric',
month: '2-digit',
});
const formatted = intlFormatter.formatToParts(tempDate);
// pull the localized format from the array of formatted parts.
formatted.forEach((p) => {
switch (p.type) {
case 'dayPeriod':
format += 'A';
format = format.replace('H', 'h');
break;
case 'minute':
format += 'mm';
break;
case 'hour':
format += 'H';
break;
case 'month':
format += 'MM';
break;
case 'day':
format += 'DD';
break;
case 'year':
format += 'YYYY';
break;
case 'literal':
// An ICU 72 update places a unicode character \u202f (non-breaking space) before day period (am/pm).
// This can make for differences that are imperceptible to humans, but automated tests know!
// the \u202f character is best detected via its charCode... 8239
if (p.value.charCodeAt(0) === 8239) {
format += ' ';
} else {
format += p.value;
}
break;
default:
break;
}
});
} else {
// if INTL api is not available, fall back to moment...
format = getMomentLocalizedFormat(intl);
}
return format;
};
/**
* getLocalizedTimeFormatInfo()
* Accepts a locale like 'ar' or 'de'
* Gets an array of dayPeriods for 12 hour modes of time. The
* array items can be used as values for the day period control,
* and the array length used to determin 12 hour (2 items) or 24 hour mode (0 items).
*
* example usage:
* getLocalizedTimeFormatInfo('en-SE') returns:
* {
* separator: ':',
* timeFormat: 'HH:mm',
* dayPeriods: [], // no dayPeriods means 24 hour time format.
* }
*
* getLocalizedTimeFormatInfo('en-US') returns:
* {
* separator: ':',
* timeFormat: 'hh:mm A',
* dayPeriods: ['am', 'pm'], // day periods: 12 hour time format.
* }
*
* @export
* @param {*} locale
* @returns {{ timeFormat: string; dayPeriods: {}; }}
*/
export function getLocalizedTimeFormatInfo(locale) {
// Build array of time stamps for convenience.
const dateArray = [];
while (dateArray.length < 24) {
dateArray.push(new Date(2022, 3, 10, dateArray.length));
}
const formatInfo = {};
const options = { hour: 'numeric', minute: 'numeric' };
const dpOptions = new Set();
const df = new Intl.DateTimeFormat(locale, options);
let timeFormat = '';
dateArray.forEach((d, i) => {
const dateFields = df.formatToParts(d);
dateFields.forEach((f) => {
if (f.type === 'dayPeriod') {
dpOptions.add(f.value);
}
});
// compile a local date-time format from the pieces of the last item.
// something in the form of HH:mm or HH:mm A that could be fed to a library like moment.
if (i === dateArray.length - 1) {
dateFields.forEach((p) => {
let adjustedValue;
switch (p.type) {
case 'hour':
timeFormat += 'HH';
break;
case 'minute':
timeFormat += 'mm';
break;
case 'literal':
adjustedValue = p.value;
// An ICU 72 update places a unicode character \u202f (non-breaking space) before day period (am/pm).
// This can make for differences that are imperceptible to humans, but automated tests know!
// the \u202f character is best detected via its charCode... 8239
if (adjustedValue.charCodeAt(0) === 8239) {
adjustedValue = ' ';
}
timeFormat += adjustedValue;
if (adjustedValue !== ' ' && !formatInfo.separator) {
formatInfo.separator = p.value;
}
break;
case 'dayPeriod':
timeFormat += 'A';
break;
default:
break;
}
});
}
});
if (timeFormat.includes('A')) {
timeFormat = timeFormat.replace('HH', 'hh');
}
return {
...formatInfo,
timeFormat,
dayPeriods: [...dpOptions],
};
}
/** Parses time without DST.
* DST moves time forward an hour - so +1 to the utc offset - but thankfully, it's not in use for majority ranges.
* given 2 static sample dates that are far enough apart, you'd get one that wasn't
* in DST if it's observed in your locale.
* so we can use the non-DST date to avoid off-by-1-hour time issues.
*
* @export
* @param {String} dateTime
* @param {String} timeFormat
* @returns {String}
*/
export function removeDST(dateTime, timeFormat) {
const julDate = '2022-07-20';
const janDate = '2022-01-01';
const julOffset = moment(julDate).utcOffset();
const janOffset = moment(janDate).utcOffset();
const offsetDate = janOffset < julOffset ? janDate : julDate;
const timestring = dateTime.includes('T') ? dateTime.split('T')[1] : dateTime;
return moment.utc(`${offsetDate}T${timestring}`).local().format(timeFormat);
}
/**
* Version of removeDST using DayJS
*
* @export
* @param {String} dateTime
* @param {String} timeFormat
* @returns {String}
*/
export function removeDSTDayJS(dateTime, timeFormat) {
const julDate = '2022-07-20';
const janDate = '2022-01-01';
const julOffset = dayjs(julDate).utcOffset();
const janOffset = dayjs(janDate).utcOffset();
const offsetDate = janOffset < julOffset ? janDate : julDate;
const timestring = dateTime.includes('T') ? dateTime.split('T')[1] : dateTime;
return dayjs.utc(`${offsetDate}T${timestring}`).local().format(timeFormat);
}