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