Generic Commit; Most likely a fix or small feature
This commit is contained in:
42
dist/timehelper.js
vendored
Normal file
42
dist/timehelper.js
vendored
Normal file
@@ -0,0 +1,42 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.TimeSpan = void 0;
|
||||
exports.secondsToReadable = secondsToReadable;
|
||||
class TimeSpan {
|
||||
constructor(timeSpanStr) {
|
||||
const [startStr, endStr] = timeSpanStr.split("-");
|
||||
this.start = this.parseTime(startStr);
|
||||
this.end = this.parseTime(endStr);
|
||||
}
|
||||
parseTime(timeStr) {
|
||||
const [hours, minutes] = timeStr.split(":").map(Number);
|
||||
return { hours, minutes };
|
||||
}
|
||||
contains(timestamp = Date.now()) {
|
||||
const date = new Date(timestamp);
|
||||
const hours = date.getHours();
|
||||
const minutes = date.getMinutes();
|
||||
const currentMinutes = hours * 60 + minutes;
|
||||
const startMinutes = this.start.hours * 60 + this.start.minutes;
|
||||
const endMinutes = this.end.hours * 60 + this.end.minutes;
|
||||
if (startMinutes > endMinutes) {
|
||||
return currentMinutes >= startMinutes || currentMinutes < endMinutes;
|
||||
}
|
||||
else {
|
||||
return currentMinutes >= startMinutes && currentMinutes < endMinutes;
|
||||
}
|
||||
}
|
||||
}
|
||||
exports.TimeSpan = TimeSpan;
|
||||
function secondsToReadable(secs, roundToMinutes = false) {
|
||||
const totalSeconds = roundToMinutes ? Math.round(secs / 60) * 60 : secs;
|
||||
var days = Math.floor(totalSeconds / (3600 * 24));
|
||||
var hours = Math.floor((totalSeconds % (3600 * 24)) / 3600);
|
||||
var minutes = Math.floor((totalSeconds % 3600) / 60);
|
||||
var seconds = Math.floor(totalSeconds % 60);
|
||||
var dayDisplay = days > 0 ? days + (days === 1 ? " day, " : " days, ") : "";
|
||||
var hourDisplay = hours > 0 ? hours + (hours === 1 ? " hour, " : " hours, ") : "";
|
||||
var minuteDisplay = minutes > 0 ? minutes + (minutes === 1 ? " minute, " : " minutes, ") : "";
|
||||
var secondDisplay = seconds > 0 ? seconds + (seconds === 1 ? " second" : " seconds") : "";
|
||||
return (dayDisplay + hourDisplay + minuteDisplay + secondDisplay).replace(/,\s*$/, "");
|
||||
}
|
||||
Reference in New Issue
Block a user