2020-07-15 14:34:57 +01:00
|
|
|
import globalize from 'globalize';
|
2018-10-23 01:05:09 +03:00
|
|
|
|
2020-07-20 08:40:13 +01:00
|
|
|
/* eslint-disable indent */
|
2020-07-15 14:34:57 +01:00
|
|
|
|
|
|
|
export function parseISO8601Date(s, toLocal) {
|
2019-01-10 15:39:37 +03:00
|
|
|
// parenthese matches:
|
|
|
|
// year month day hours minutes seconds
|
|
|
|
// dotmilliseconds
|
|
|
|
// tzstring plusminus hours minutes
|
2020-07-20 08:40:13 +01:00
|
|
|
const re = /(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})(\.\d+)?(Z|([+-])(\d{2}):(\d{2}))?/;
|
2019-01-10 15:39:37 +03:00
|
|
|
|
2020-07-20 08:40:13 +01:00
|
|
|
const d = s.match(re);
|
2019-01-10 15:39:37 +03:00
|
|
|
|
|
|
|
// "2010-12-07T11:00:00.000-09:00" parses to:
|
|
|
|
// ["2010-12-07T11:00:00.000-09:00", "2010", "12", "07", "11",
|
|
|
|
// "00", "00", ".000", "-09:00", "-", "09", "00"]
|
|
|
|
// "2010-12-07T11:00:00.000Z" parses to:
|
|
|
|
// ["2010-12-07T11:00:00.000Z", "2010", "12", "07", "11",
|
|
|
|
// "00", "00", ".000", "Z", undefined, undefined, undefined]
|
|
|
|
|
|
|
|
if (!d) {
|
|
|
|
throw "Couldn't parse ISO 8601 date string '" + s + "'";
|
|
|
|
}
|
|
|
|
|
|
|
|
// parse strings, leading zeros into proper ints
|
2020-07-20 08:40:13 +01:00
|
|
|
const a = [1, 2, 3, 4, 5, 6, 10, 11];
|
2020-07-19 17:38:42 +02:00
|
|
|
for (const i in a) {
|
2019-01-10 15:39:37 +03:00
|
|
|
d[a[i]] = parseInt(d[a[i]], 10);
|
|
|
|
}
|
2018-10-23 01:05:09 +03:00
|
|
|
d[7] = parseFloat(d[7]);
|
2019-01-10 15:39:37 +03:00
|
|
|
|
|
|
|
// Date.UTC(year, month[, date[, hrs[, min[, sec[, ms]]]]])
|
|
|
|
// note that month is 0-11, not 1-12
|
|
|
|
// see https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Date/UTC
|
2020-07-20 08:40:13 +01:00
|
|
|
let ms = Date.UTC(d[1], d[2] - 1, d[3], d[4], d[5], d[6]);
|
2019-01-10 15:39:37 +03:00
|
|
|
|
|
|
|
// if there are milliseconds, add them
|
|
|
|
if (d[7] > 0) {
|
|
|
|
ms += Math.round(d[7] * 1000);
|
|
|
|
}
|
|
|
|
|
|
|
|
// if there's a timezone, calculate it
|
2020-05-04 12:44:12 +02:00
|
|
|
if (d[8] !== 'Z' && d[10]) {
|
2020-07-20 08:40:13 +01:00
|
|
|
let offset = d[10] * 60 * 60 * 1000;
|
2019-01-10 15:39:37 +03:00
|
|
|
if (d[11]) {
|
|
|
|
offset += d[11] * 60 * 1000;
|
|
|
|
}
|
2020-05-04 12:44:12 +02:00
|
|
|
if (d[9] === '-') {
|
2019-01-10 15:39:37 +03:00
|
|
|
ms -= offset;
|
|
|
|
} else {
|
|
|
|
ms += offset;
|
|
|
|
}
|
|
|
|
} else if (toLocal === false) {
|
|
|
|
ms += new Date().getTimezoneOffset() * 60000;
|
|
|
|
}
|
|
|
|
|
|
|
|
return new Date(ms);
|
2018-10-23 01:05:09 +03:00
|
|
|
}
|
|
|
|
|
2020-07-15 14:34:57 +01:00
|
|
|
export function getDisplayRunningTime(ticks) {
|
2020-07-20 08:40:13 +01:00
|
|
|
const ticksPerHour = 36000000000;
|
|
|
|
const ticksPerMinute = 600000000;
|
|
|
|
const ticksPerSecond = 10000000;
|
2019-01-10 15:39:37 +03:00
|
|
|
|
2020-07-20 08:40:13 +01:00
|
|
|
const parts = [];
|
2019-01-10 15:39:37 +03:00
|
|
|
|
2020-07-20 08:40:13 +01:00
|
|
|
let hours = ticks / ticksPerHour;
|
2019-01-10 15:39:37 +03:00
|
|
|
hours = Math.floor(hours);
|
|
|
|
|
|
|
|
if (hours) {
|
|
|
|
parts.push(hours);
|
|
|
|
}
|
|
|
|
|
|
|
|
ticks -= (hours * ticksPerHour);
|
|
|
|
|
2020-07-20 08:40:13 +01:00
|
|
|
let minutes = ticks / ticksPerMinute;
|
2019-01-10 15:39:37 +03:00
|
|
|
minutes = Math.floor(minutes);
|
|
|
|
|
|
|
|
ticks -= (minutes * ticksPerMinute);
|
|
|
|
|
|
|
|
if (minutes < 10 && hours) {
|
|
|
|
minutes = '0' + minutes;
|
|
|
|
}
|
|
|
|
parts.push(minutes);
|
|
|
|
|
2020-07-20 08:40:13 +01:00
|
|
|
let seconds = ticks / ticksPerSecond;
|
2019-01-10 15:39:37 +03:00
|
|
|
seconds = Math.floor(seconds);
|
|
|
|
|
|
|
|
if (seconds < 10) {
|
|
|
|
seconds = '0' + seconds;
|
|
|
|
}
|
|
|
|
parts.push(seconds);
|
|
|
|
|
|
|
|
return parts.join(':');
|
2018-10-23 01:05:09 +03:00
|
|
|
}
|
|
|
|
|
2020-07-20 08:40:13 +01:00
|
|
|
const toLocaleTimeStringSupportsLocales = function () {
|
2019-01-10 15:39:37 +03:00
|
|
|
try {
|
|
|
|
new Date().toLocaleTimeString('i');
|
|
|
|
} catch (e) {
|
|
|
|
return e.name === 'RangeError';
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}();
|
|
|
|
|
2018-10-23 01:05:09 +03:00
|
|
|
function getOptionList(options) {
|
2020-07-20 08:40:13 +01:00
|
|
|
const list = [];
|
2019-01-10 15:39:37 +03:00
|
|
|
|
2020-07-20 08:40:13 +01:00
|
|
|
for (const i in options) {
|
2019-01-10 15:39:37 +03:00
|
|
|
list.push({
|
|
|
|
name: i,
|
|
|
|
value: options[i]
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return list;
|
2018-10-23 01:05:09 +03:00
|
|
|
}
|
|
|
|
|
2020-07-15 14:34:57 +01:00
|
|
|
export function toLocaleString(date, options) {
|
2019-01-10 15:39:37 +03:00
|
|
|
if (!date) {
|
|
|
|
throw new Error('date cannot be null');
|
|
|
|
}
|
|
|
|
|
|
|
|
options = options || {};
|
|
|
|
|
|
|
|
if (toLocaleTimeStringSupportsLocales) {
|
2020-07-20 08:40:13 +01:00
|
|
|
const currentLocale = globalize.getCurrentDateTimeLocale();
|
2019-01-10 15:39:37 +03:00
|
|
|
|
|
|
|
if (currentLocale) {
|
|
|
|
return date.toLocaleString(currentLocale, options);
|
|
|
|
}
|
2018-10-23 01:05:09 +03:00
|
|
|
}
|
2019-01-10 15:39:37 +03:00
|
|
|
|
|
|
|
return date.toLocaleString();
|
2018-10-23 01:05:09 +03:00
|
|
|
}
|
|
|
|
|
2020-07-15 14:34:57 +01:00
|
|
|
export function toLocaleDateString(date, options) {
|
2019-01-10 15:39:37 +03:00
|
|
|
if (!date) {
|
|
|
|
throw new Error('date cannot be null');
|
|
|
|
}
|
|
|
|
|
|
|
|
options = options || {};
|
|
|
|
|
|
|
|
if (toLocaleTimeStringSupportsLocales) {
|
2020-07-20 08:40:13 +01:00
|
|
|
const currentLocale = globalize.getCurrentDateTimeLocale();
|
2019-01-10 15:39:37 +03:00
|
|
|
|
|
|
|
if (currentLocale) {
|
|
|
|
return date.toLocaleDateString(currentLocale, options);
|
|
|
|
}
|
2018-10-23 01:05:09 +03:00
|
|
|
}
|
2019-01-10 15:39:37 +03:00
|
|
|
|
|
|
|
// This is essentially a hard-coded polyfill
|
2020-07-20 08:40:13 +01:00
|
|
|
const optionList = getOptionList(options);
|
2019-01-10 15:39:37 +03:00
|
|
|
if (optionList.length === 1 && optionList[0].name === 'weekday') {
|
2020-07-20 08:40:13 +01:00
|
|
|
const weekday = [];
|
2020-05-04 12:44:12 +02:00
|
|
|
weekday[0] = 'Sun';
|
|
|
|
weekday[1] = 'Mon';
|
|
|
|
weekday[2] = 'Tue';
|
|
|
|
weekday[3] = 'Wed';
|
|
|
|
weekday[4] = 'Thu';
|
|
|
|
weekday[5] = 'Fri';
|
|
|
|
weekday[6] = 'Sat';
|
2019-01-10 15:39:37 +03:00
|
|
|
return weekday[date.getDay()];
|
2018-10-23 01:05:09 +03:00
|
|
|
}
|
2019-01-10 15:39:37 +03:00
|
|
|
|
|
|
|
return date.toLocaleDateString();
|
2018-10-23 01:05:09 +03:00
|
|
|
}
|
|
|
|
|
2020-07-15 14:34:57 +01:00
|
|
|
export function toLocaleTimeString(date, options) {
|
2019-01-10 15:39:37 +03:00
|
|
|
if (!date) {
|
|
|
|
throw new Error('date cannot be null');
|
|
|
|
}
|
|
|
|
|
|
|
|
options = options || {};
|
|
|
|
|
|
|
|
if (toLocaleTimeStringSupportsLocales) {
|
2020-07-20 08:40:13 +01:00
|
|
|
const currentLocale = globalize.getCurrentDateTimeLocale();
|
2019-01-10 15:39:37 +03:00
|
|
|
|
|
|
|
if (currentLocale) {
|
|
|
|
return date.toLocaleTimeString(currentLocale, options);
|
|
|
|
}
|
2018-10-23 01:05:09 +03:00
|
|
|
}
|
2019-01-10 15:39:37 +03:00
|
|
|
|
|
|
|
return date.toLocaleTimeString();
|
2018-10-23 01:05:09 +03:00
|
|
|
}
|
|
|
|
|
2020-07-15 14:34:57 +01:00
|
|
|
export function getDisplayTime(date) {
|
2019-01-10 15:39:37 +03:00
|
|
|
if (!date) {
|
|
|
|
throw new Error('date cannot be null');
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((typeof date).toString().toLowerCase() === 'string') {
|
|
|
|
try {
|
|
|
|
date = parseISO8601Date(date, true);
|
|
|
|
} catch (err) {
|
|
|
|
return date;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (toLocaleTimeStringSupportsLocales) {
|
|
|
|
return toLocaleTimeString(date, {
|
|
|
|
|
|
|
|
hour: 'numeric',
|
|
|
|
minute: '2-digit'
|
|
|
|
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-07-20 08:40:13 +01:00
|
|
|
let time = toLocaleTimeString(date);
|
2019-01-10 15:39:37 +03:00
|
|
|
|
2020-07-20 08:40:13 +01:00
|
|
|
const timeLower = time.toLowerCase();
|
2019-01-10 15:39:37 +03:00
|
|
|
|
|
|
|
if (timeLower.indexOf('am') !== -1 || timeLower.indexOf('pm') !== -1) {
|
2018-10-23 01:05:09 +03:00
|
|
|
time = timeLower;
|
2020-07-20 08:40:13 +01:00
|
|
|
let hour = date.getHours() % 12;
|
|
|
|
const suffix = date.getHours() > 11 ? 'pm' : 'am';
|
2019-01-10 15:39:37 +03:00
|
|
|
if (!hour) {
|
|
|
|
hour = 12;
|
|
|
|
}
|
2020-07-20 08:40:13 +01:00
|
|
|
let minutes = date.getMinutes();
|
2019-01-10 15:39:37 +03:00
|
|
|
|
|
|
|
if (minutes < 10) {
|
|
|
|
minutes = '0' + minutes;
|
|
|
|
}
|
|
|
|
|
|
|
|
minutes = ':' + minutes;
|
|
|
|
time = hour + minutes + suffix;
|
2018-10-23 01:05:09 +03:00
|
|
|
} else {
|
2020-07-20 08:40:13 +01:00
|
|
|
const timeParts = time.split(':');
|
2019-01-10 15:39:37 +03:00
|
|
|
|
|
|
|
// Trim off seconds
|
|
|
|
if (timeParts.length > 2) {
|
|
|
|
// setting to 2 also handles '21:00:28 GMT+9:30'
|
|
|
|
timeParts.length = 2;
|
|
|
|
time = timeParts.join(':');
|
|
|
|
}
|
2018-10-23 01:05:09 +03:00
|
|
|
}
|
2019-01-10 15:39:37 +03:00
|
|
|
|
|
|
|
return time;
|
2018-10-23 01:05:09 +03:00
|
|
|
}
|
|
|
|
|
2020-07-15 14:34:57 +01:00
|
|
|
export function isRelativeDay(date, offsetInDays) {
|
2019-01-10 15:39:37 +03:00
|
|
|
if (!date) {
|
|
|
|
throw new Error('date cannot be null');
|
2018-10-23 01:05:09 +03:00
|
|
|
}
|
2019-01-10 15:39:37 +03:00
|
|
|
|
2020-07-20 08:40:13 +01:00
|
|
|
const yesterday = new Date();
|
|
|
|
const day = yesterday.getDate() + offsetInDays;
|
2019-01-10 15:39:37 +03:00
|
|
|
|
|
|
|
yesterday.setDate(day); // automatically adjusts month/year appropriately
|
|
|
|
|
|
|
|
return date.getFullYear() === yesterday.getFullYear() && date.getMonth() === yesterday.getMonth() && date.getDate() === day;
|
|
|
|
}
|
|
|
|
|
2020-07-15 14:34:57 +01:00
|
|
|
export default {
|
2018-10-23 01:05:09 +03:00
|
|
|
parseISO8601Date: parseISO8601Date,
|
|
|
|
getDisplayRunningTime: getDisplayRunningTime,
|
|
|
|
toLocaleDateString: toLocaleDateString,
|
|
|
|
toLocaleString: toLocaleString,
|
|
|
|
getDisplayTime: getDisplayTime,
|
|
|
|
isRelativeDay: isRelativeDay,
|
|
|
|
toLocaleTimeString: toLocaleTimeString,
|
2019-01-10 15:39:37 +03:00
|
|
|
supportsLocalization: function () {
|
|
|
|
return toLocaleTimeStringSupportsLocales;
|
2018-10-23 01:05:09 +03:00
|
|
|
}
|
2019-01-10 15:39:37 +03:00
|
|
|
};
|
2020-07-15 14:34:57 +01:00
|
|
|
|
2020-07-20 08:40:13 +01:00
|
|
|
/* eslint-enable indent */
|