2020-11-05 16:58:23 +00:00
|
|
|
import shim from './shim';
|
2017-11-03 00:09:34 +00:00
|
|
|
const moment = require('moment');
|
2017-06-23 22:32:24 +01:00
|
|
|
|
2021-01-29 18:45:11 +00:00
|
|
|
type ConditionHandler = ()=> boolean;
|
|
|
|
|
2017-11-28 18:02:54 +00:00
|
|
|
class Time {
|
2020-11-05 16:58:23 +00:00
|
|
|
|
2020-11-12 19:13:28 +00:00
|
|
|
private dateFormat_: string = 'DD/MM/YYYY';
|
|
|
|
private timeFormat_: string = 'HH:mm';
|
|
|
|
private locale_: string = 'en-us';
|
2019-08-29 10:38:24 -06:00
|
|
|
|
2021-01-29 18:45:11 +00:00
|
|
|
public locale() {
|
2019-08-29 10:38:24 -06:00
|
|
|
return this.locale_;
|
|
|
|
}
|
|
|
|
|
2021-01-29 18:45:11 +00:00
|
|
|
public setLocale(v: string) {
|
2019-08-29 10:38:24 -06:00
|
|
|
moment.locale(v);
|
2019-09-08 17:16:45 +01:00
|
|
|
this.locale_ = v;
|
2017-11-28 18:02:54 +00:00
|
|
|
}
|
|
|
|
|
2021-01-29 18:45:11 +00:00
|
|
|
public dateFormat() {
|
2017-11-28 18:02:54 +00:00
|
|
|
return this.dateFormat_;
|
|
|
|
}
|
|
|
|
|
2021-01-29 18:45:11 +00:00
|
|
|
public setDateFormat(v: string) {
|
2017-11-28 18:02:54 +00:00
|
|
|
this.dateFormat_ = v;
|
|
|
|
}
|
|
|
|
|
2021-01-29 18:45:11 +00:00
|
|
|
public timeFormat() {
|
2017-11-28 18:02:54 +00:00
|
|
|
return this.timeFormat_;
|
|
|
|
}
|
|
|
|
|
2021-01-29 18:45:11 +00:00
|
|
|
public setTimeFormat(v: string) {
|
2017-11-28 18:02:54 +00:00
|
|
|
this.timeFormat_ = v;
|
|
|
|
}
|
|
|
|
|
2021-01-29 18:45:11 +00:00
|
|
|
public use24HourFormat() {
|
2020-10-16 16:26:19 +01:00
|
|
|
return this.timeFormat() ? this.timeFormat().includes('HH') : true;
|
|
|
|
}
|
|
|
|
|
2021-01-29 18:45:11 +00:00
|
|
|
public formatDateToLocal(date: Date, format: string = null) {
|
2020-10-16 16:26:19 +01:00
|
|
|
return this.formatMsToLocal(date.getTime(), format);
|
|
|
|
}
|
|
|
|
|
2021-01-29 18:45:11 +00:00
|
|
|
public 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
|
|
|
|
2021-01-29 18:45:11 +00:00
|
|
|
public 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
|
|
|
|
2021-01-29 18:45:11 +00:00
|
|
|
public 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
|
|
|
|
2021-01-29 18:45:11 +00:00
|
|
|
public unixMsToObject(ms: number) {
|
2017-09-10 17:56:27 +01:00
|
|
|
return new Date(ms);
|
2017-11-28 18:02:54 +00:00
|
|
|
}
|
2017-09-10 17:56:27 +01:00
|
|
|
|
2021-01-29 18:45:11 +00:00
|
|
|
public unixMsToS(ms: number) {
|
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
|
|
|
|
2021-01-29 18:45:11 +00:00
|
|
|
public unixMsToIso(ms: number) {
|
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
|
|
|
|
2021-01-29 18:45:11 +00:00
|
|
|
public unixMsToIsoSec(ms: number) {
|
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
|
|
|
|
2021-10-16 01:59:37 -07:00
|
|
|
public unixMsToRfc3339Sec(ms: number) {
|
|
|
|
return (
|
|
|
|
`${moment
|
|
|
|
.unix(ms / 1000)
|
|
|
|
.utc()
|
|
|
|
.format('YYYY-MM-DD HH:mm:ss')}Z`
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-01-29 18:45:11 +00:00
|
|
|
public unixMsToLocalDateTime(ms: number) {
|
2017-07-12 21:39:47 +01:00
|
|
|
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
|
|
|
|
2021-01-29 18:45:11 +00:00
|
|
|
public unixMsToLocalHms(ms: number) {
|
2019-09-25 18:58:15 +00:00
|
|
|
return moment.unix(ms / 1000).format('HH:mm:ss');
|
|
|
|
}
|
|
|
|
|
2021-01-29 18:45:11 +00:00
|
|
|
public formatMsToLocal(ms: number, format: string = null) {
|
2017-11-28 20:31:14 +00:00
|
|
|
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
|
|
|
|
2021-01-29 18:45:11 +00:00
|
|
|
public formatLocalToMs(localDateTime: any, format: string = null) {
|
2018-09-16 19:37:31 +01:00
|
|
|
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
|
2021-01-29 18:45:11 +00:00
|
|
|
public anythingToDateTime(o: any, defaultValue: Date = null) {
|
2018-09-16 19:37:31 +01: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-10-16 01:59:37 -07:00
|
|
|
public anythingToMs(o: any, defaultValue: number = null) {
|
|
|
|
if (o && o.toDate) return o.toDate();
|
|
|
|
if (!o) return defaultValue;
|
|
|
|
// There are a few date formats supported by Joplin that are not supported by
|
|
|
|
// moment without an explicit format specifier. The typical case is that a user
|
|
|
|
// has a preferred data format. This means we should try the currently assigned
|
|
|
|
// date first, and then attempt to load a generic date string.
|
|
|
|
const m = moment(o, this.dateTimeFormat());
|
|
|
|
if (m.isValid()) return m.toDate().getTime();
|
|
|
|
const d = moment(o);
|
|
|
|
return d.isValid() ? d.toDate().getTime() : defaultValue;
|
|
|
|
}
|
|
|
|
|
2021-01-29 18:45:11 +00:00
|
|
|
public msleep(ms: number) {
|
2020-12-28 11:48:47 +00:00
|
|
|
return new Promise((resolve: Function) => {
|
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
|
|
|
|
2021-01-29 18:45:11 +00:00
|
|
|
public sleep(seconds: number) {
|
2017-06-27 00:20:01 +01:00
|
|
|
return this.msleep(seconds * 1000);
|
2017-11-28 18:02:54 +00:00
|
|
|
}
|
2020-08-08 04:43:21 +05:30
|
|
|
|
|
|
|
|
2021-01-29 18:45:11 +00:00
|
|
|
public goBackInTime(startDate: any, n: number, period: any) {
|
2020-08-08 04:43:21 +05:30
|
|
|
// 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 18:45:11 +00:00
|
|
|
public goForwardInTime(startDate: any, n: number, period: any) {
|
2020-08-08 04:43:21 +05:30
|
|
|
return moment(startDate).startOf(period).add(n, period).format('x');
|
|
|
|
}
|
|
|
|
|
2021-01-29 18:45:11 +00: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-13 23:39:45 +01:00
|
|
|
}
|
|
|
|
|
2017-11-28 18:02:54 +00:00
|
|
|
const time = new Time();
|
|
|
|
|
2020-11-05 16:58:23 +00:00
|
|
|
export default time;
|