2023-08-19 16:56:22 +01:00
|
|
|
import StringTools from "./StringTools";
|
|
|
|
|
|
|
|
export default class TimeLengthInput {
|
|
|
|
public readonly value: string;
|
|
|
|
|
|
|
|
constructor(input: string) {
|
2024-01-05 19:26:44 +00:00
|
|
|
this.value = StringTools.ReplaceAll(input, ",", "");
|
2023-08-19 16:56:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public GetDays(): number {
|
2024-01-05 19:26:44 +00:00
|
|
|
return this.GetValue("d");
|
2023-08-19 16:56:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public GetHours(): number {
|
2024-01-05 19:26:44 +00:00
|
|
|
return this.GetValue("h");
|
2023-08-19 16:56:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public GetMinutes(): number {
|
2024-01-05 19:26:44 +00:00
|
|
|
return this.GetValue("m");
|
2023-08-19 16:56:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public GetSeconds(): number {
|
2024-01-05 19:26:44 +00:00
|
|
|
return this.GetValue("s");
|
2023-08-19 16:56:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public GetMilliseconds(): number {
|
|
|
|
const days = this.GetDays();
|
|
|
|
const hours = this.GetHours();
|
|
|
|
const minutes = this.GetMinutes();
|
|
|
|
const seconds = this.GetSeconds();
|
|
|
|
|
|
|
|
let milliseconds = 0;
|
|
|
|
|
|
|
|
milliseconds += seconds * 1000;
|
|
|
|
milliseconds += minutes * 60 * 1000;
|
|
|
|
milliseconds += hours * 60 * 60 * 1000;
|
|
|
|
milliseconds += days * 24 * 60 * 60 * 1000;
|
|
|
|
|
|
|
|
return milliseconds;
|
|
|
|
}
|
|
|
|
|
|
|
|
public GetDateFromNow(): Date {
|
|
|
|
const now = Date.now();
|
|
|
|
|
|
|
|
const dateFromNow = now
|
|
|
|
+ (1000 * this.GetSeconds())
|
|
|
|
+ (1000 * 60 * this.GetMinutes())
|
|
|
|
+ (1000 * 60 * 60 * this.GetHours())
|
|
|
|
+ (1000 * 60 * 60 * 24 * this.GetDays());
|
|
|
|
|
|
|
|
return new Date(dateFromNow);
|
|
|
|
}
|
|
|
|
|
|
|
|
public GetLength(): string {
|
|
|
|
const days = this.GetDays();
|
|
|
|
const hours = this.GetHours();
|
|
|
|
const minutes = this.GetMinutes();
|
|
|
|
const seconds = this.GetSeconds();
|
|
|
|
|
|
|
|
const value = [];
|
|
|
|
|
|
|
|
if (days) {
|
|
|
|
value.push(`${days} days`);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (hours) {
|
|
|
|
value.push(`${hours} hours`);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (minutes) {
|
|
|
|
value.push(`${minutes} minutes`);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (seconds) {
|
|
|
|
value.push(`${seconds} seconds`);
|
|
|
|
}
|
|
|
|
|
|
|
|
return value.join(", ");
|
|
|
|
}
|
|
|
|
|
|
|
|
public GetLengthShort(): string {
|
|
|
|
const days = this.GetDays();
|
|
|
|
const hours = this.GetHours();
|
|
|
|
const minutes = this.GetMinutes();
|
|
|
|
const seconds = this.GetSeconds();
|
|
|
|
|
|
|
|
const value = [];
|
|
|
|
|
|
|
|
if (days) {
|
|
|
|
value.push(`${days}d`);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (hours) {
|
|
|
|
value.push(`${hours}h`);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (minutes) {
|
|
|
|
value.push(`${minutes}m`);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (seconds) {
|
|
|
|
value.push(`${seconds}s`);
|
|
|
|
}
|
|
|
|
|
|
|
|
return value.join(" ");
|
|
|
|
}
|
|
|
|
|
|
|
|
private GetValue(designation: string): number {
|
2024-01-05 19:26:44 +00:00
|
|
|
const valueSplit = this.value.split(" ");
|
2023-08-19 16:56:22 +01:00
|
|
|
|
|
|
|
const desString = valueSplit.find(x => x.charAt(x.length - 1) == designation);
|
|
|
|
|
|
|
|
if (!desString) return 0;
|
|
|
|
|
|
|
|
const desNumber = Number(desString.substring(0, desString.length - 1));
|
|
|
|
|
|
|
|
if (!desNumber) return 0;
|
|
|
|
|
|
|
|
return desNumber;
|
|
|
|
}
|
|
|
|
}
|