2017-11-03 00:09:34 +00:00
|
|
|
const moment = require('moment');
|
2020-10-09 18:35:46 +01:00
|
|
|
const shim = require('lib/shim').default;
|
2017-06-23 22:32:24 +01:00
|
|
|
|
2017-11-28 18:02:54 +00:00
|
|
|
class Time {
|
|
|
|
constructor() {
|
|
|
|
this.dateFormat_ = 'DD/MM/YYYY';
|
|
|
|
this.timeFormat_ = 'HH:mm';
|
2019-08-29 10:38:24 -06:00
|
|
|
this.locale_ = 'en-us';
|
|
|
|
}
|
|
|
|
|
|
|
|
locale() {
|
|
|
|
return this.locale_;
|
|
|
|
}
|
|
|
|
|
|
|
|
setLocale(v) {
|
|
|
|
moment.locale(v);
|
2019-09-08 17:16:45 +01:00
|
|
|
this.locale_ = v;
|
2017-11-28 18:02:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
dateFormat() {
|
|
|
|
return this.dateFormat_;
|
|
|
|
}
|
|
|
|
|
|
|
|
setDateFormat(v) {
|
|
|
|
this.dateFormat_ = v;
|
|
|
|
}
|
|
|
|
|
|
|
|
timeFormat() {
|
|
|
|
return this.timeFormat_;
|
|
|
|
}
|
|
|
|
|
|
|
|
setTimeFormat(v) {
|
|
|
|
this.timeFormat_ = v;
|
|
|
|
}
|
|
|
|
|
2020-10-16 16:26:19 +01:00
|
|
|
use24HourFormat() {
|
|
|
|
return this.timeFormat() ? this.timeFormat().includes('HH') : true;
|
|
|
|
}
|
|
|
|
|
|
|
|
formatDateToLocal(date, format = null) {
|
|
|
|
return this.formatMsToLocal(date.getTime(), format);
|
|
|
|
}
|
|
|
|
|
2017-11-28 18:02:54 +00:00
|
|
|
dateTimeFormat() {
|
2019-09-19 22:51:18 +01:00
|
|
|
return `${this.dateFormat()} ${this.timeFormat()}`;
|
2017-11-28 18:02:54 +00:00
|
|
|
}
|
2017-06-13 23:39:45 +01:00
|
|
|
|
|
|
|
unix() {
|
2017-11-27 22:50:46 +00:00
|
|
|
return Math.floor(Date.now() / 1000);
|
2017-11-28 18:02:54 +00:00
|
|
|
}
|
2017-06-18 23:06:10 +01:00
|
|
|
|
|
|
|
unixMs() {
|
2017-11-27 22:50:46 +00:00
|
|
|
return Date.now();
|
2017-11-28 18:02:54 +00:00
|
|
|
}
|
2017-06-18 23:06:10 +01:00
|
|
|
|
2017-09-10 17:56:27 +01:00
|
|
|
unixMsToObject(ms) {
|
|
|
|
return new Date(ms);
|
2017-11-28 18:02:54 +00:00
|
|
|
}
|
2017-09-10 17:56:27 +01:00
|
|
|
|
2017-06-18 23:06:10 +01:00
|
|
|
unixMsToS(ms) {
|
2017-06-19 20:18:22 +01:00
|
|
|
return Math.floor(ms / 1000);
|
2017-11-28 18:02:54 +00:00
|
|
|
}
|
2017-06-23 22:32:24 +01:00
|
|
|
|
|
|
|
unixMsToIso(ms) {
|
2019-07-29 15:43:53 +02:00
|
|
|
return (
|
2019-09-19 22:51:18 +01:00
|
|
|
`${moment
|
2019-07-29 15:43:53 +02:00
|
|
|
.unix(ms / 1000)
|
|
|
|
.utc()
|
2019-09-19 22:51:18 +01:00
|
|
|
.format('YYYY-MM-DDTHH:mm:ss.SSS')}Z`
|
2019-07-29 15:43:53 +02:00
|
|
|
);
|
2017-11-28 18:02:54 +00:00
|
|
|
}
|
2017-06-13 23:39:45 +01:00
|
|
|
|
2017-07-07 18:19:24 +01:00
|
|
|
unixMsToIsoSec(ms) {
|
2019-07-29 15:43:53 +02:00
|
|
|
return (
|
2019-09-19 22:51:18 +01:00
|
|
|
`${moment
|
2019-07-29 15:43:53 +02:00
|
|
|
.unix(ms / 1000)
|
|
|
|
.utc()
|
2019-09-19 22:51:18 +01:00
|
|
|
.format('YYYY-MM-DDTHH:mm:ss')}Z`
|
2019-07-29 15:43:53 +02:00
|
|
|
);
|
2017-11-28 18:02:54 +00:00
|
|
|
}
|
2017-07-07 18:19:24 +01:00
|
|
|
|
2017-07-12 21:39:47 +01:00
|
|
|
unixMsToLocalDateTime(ms) {
|
|
|
|
return moment.unix(ms / 1000).format('DD/MM/YYYY HH:mm');
|
2017-11-28 18:02:54 +00:00
|
|
|
}
|
2017-07-12 21:39:47 +01:00
|
|
|
|
2019-09-25 18:58:15 +00:00
|
|
|
unixMsToLocalHms(ms) {
|
|
|
|
return moment.unix(ms / 1000).format('HH:mm:ss');
|
|
|
|
}
|
|
|
|
|
2017-11-28 20:31:14 +00:00
|
|
|
formatMsToLocal(ms, format = null) {
|
|
|
|
if (format === null) format = this.dateTimeFormat();
|
2017-07-15 00:12:32 +01:00
|
|
|
return moment(ms).format(format);
|
2017-11-28 18:02:54 +00:00
|
|
|
}
|
2017-07-13 18:47:31 +00:00
|
|
|
|
2018-09-16 19:37:31 +01:00
|
|
|
formatLocalToMs(localDateTime, format = null) {
|
|
|
|
if (format === null) format = this.dateTimeFormat();
|
|
|
|
const m = moment(localDateTime, format);
|
|
|
|
if (m.isValid()) return m.toDate().getTime();
|
2019-09-19 22:51:18 +01:00
|
|
|
throw new Error(`Invalid input for formatLocalToMs: ${localDateTime}`);
|
2018-09-16 19:37:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Mostly used as a utility function for the DateTime Electron component
|
|
|
|
anythingToDateTime(o, defaultValue = null) {
|
|
|
|
if (o && o.toDate) return o.toDate();
|
|
|
|
if (!o) return defaultValue;
|
|
|
|
let m = moment(o, time.dateTimeFormat());
|
|
|
|
if (m.isValid()) return m.toDate();
|
|
|
|
m = moment(o, time.dateFormat());
|
|
|
|
return m.isValid() ? m.toDate() : defaultValue;
|
|
|
|
}
|
|
|
|
|
2017-06-27 00:20:01 +01:00
|
|
|
msleep(ms) {
|
2019-09-12 22:16:42 +00:00
|
|
|
return new Promise((resolve) => {
|
2020-10-09 18:35:46 +01:00
|
|
|
shim.setTimeout(() => {
|
2017-06-27 00:20:01 +01:00
|
|
|
resolve();
|
|
|
|
}, ms);
|
|
|
|
});
|
2017-11-28 18:02:54 +00:00
|
|
|
}
|
2017-06-27 00:20:01 +01:00
|
|
|
|
|
|
|
sleep(seconds) {
|
|
|
|
return this.msleep(seconds * 1000);
|
2017-11-28 18:02:54 +00:00
|
|
|
}
|
2020-08-08 04:43:21 +05:30
|
|
|
|
|
|
|
|
|
|
|
goBackInTime(startDate, n, period) {
|
|
|
|
// period is a string (eg. "day", "week", "month", "year" ), n is an integer
|
|
|
|
return moment(startDate).startOf(period).subtract(n, period).format('x');
|
|
|
|
}
|
|
|
|
|
|
|
|
goForwardInTime(startDate, n, period) {
|
|
|
|
return moment(startDate).startOf(period).add(n, period).format('x');
|
|
|
|
}
|
|
|
|
|
2017-06-13 23:39:45 +01:00
|
|
|
}
|
|
|
|
|
2017-11-28 18:02:54 +00:00
|
|
|
const time = new Time();
|
|
|
|
|
2019-07-29 15:43:53 +02:00
|
|
|
module.exports = { time };
|