2018-03-09 17:49:35 +00:00
|
|
|
const moment = require("moment");
|
2017-06-23 22:32:24 +01:00
|
|
|
|
2017-11-28 18:02:54 +00:00
|
|
|
class Time {
|
|
|
|
constructor() {
|
2018-03-09 17:49:35 +00:00
|
|
|
this.dateFormat_ = "DD/MM/YYYY";
|
|
|
|
this.timeFormat_ = "HH:mm";
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
dateTimeFormat() {
|
2018-03-09 17:49:35 +00: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) {
|
2018-03-09 17:49:35 +00:00
|
|
|
return (
|
|
|
|
moment
|
|
|
|
.unix(ms / 1000)
|
|
|
|
.utc()
|
|
|
|
.format("YYYY-MM-DDTHH:mm:ss.SSS") + "Z"
|
|
|
|
);
|
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) {
|
2018-03-09 17:49:35 +00:00
|
|
|
return (
|
|
|
|
moment
|
|
|
|
.unix(ms / 1000)
|
|
|
|
.utc()
|
|
|
|
.format("YYYY-MM-DDTHH:mm:ss") + "Z"
|
|
|
|
);
|
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) {
|
2018-03-09 17:49:35 +00: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
|
|
|
|
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
|
|
|
|
2017-06-27 00:20:01 +01:00
|
|
|
msleep(ms) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
setTimeout(() => {
|
|
|
|
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
|
|
|
}
|
2017-06-13 23:39:45 +01:00
|
|
|
}
|
|
|
|
|
2017-11-28 18:02:54 +00:00
|
|
|
const time = new Time();
|
|
|
|
|
2018-03-09 17:49:35 +00:00
|
|
|
module.exports = { time };
|