2022-07-27 13:00:23 +02:00
|
|
|
/**
|
|
|
|
* Parsed string adapted with TZ (locales) and format for the specified lang.
|
|
|
|
* @param tz Lang
|
|
|
|
* @param locale Locales
|
|
|
|
* @param date Date
|
|
|
|
* @returns String
|
|
|
|
*/
|
2023-01-17 23:11:22 +01:00
|
|
|
export const showDate = (tz: string, locale: Map<string, unknown>, date: Date) => {
|
|
|
|
return date.toLocaleString(tz).replace(" ", ` ${locale.get("u_time_at")} `);
|
2023-01-17 12:15:14 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
enum TimeSecond {
|
2023-01-17 23:11:22 +01:00
|
|
|
Year = 31536000,
|
|
|
|
Week = 604800,
|
|
|
|
Day = 86400,
|
|
|
|
Hour = 3600,
|
|
|
|
Minute = 60,
|
|
|
|
Second = 1,
|
2023-01-17 12:15:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Take a cooldown, for example 2min and transform it to seconds, here: 120s
|
|
|
|
* @param time time in human format
|
|
|
|
* @returns time in seconds
|
|
|
|
*/
|
|
|
|
export const strToSeconds = (time: string) => {
|
2023-01-17 23:11:22 +01:00
|
|
|
const regex = new RegExp(
|
|
|
|
`(?<${TimeSecond[TimeSecond.Year]}>[0-9]+(?=[y|a]))|(?<${
|
|
|
|
TimeSecond[TimeSecond.Week]
|
|
|
|
}>[0-9]+(?=[w]))|(?<${TimeSecond[TimeSecond.Day]}>[0-9]+(?=[d|j]))|(?<${
|
|
|
|
TimeSecond[TimeSecond.Hour]
|
|
|
|
}>[0-9]+(?=[h]))|(?<${TimeSecond[TimeSecond.Minute]}>[0-9]+(?=[m]))|(?<${
|
|
|
|
TimeSecond[TimeSecond.Second]
|
|
|
|
}>[0-9]+(?=[s]?))`
|
|
|
|
);
|
2023-01-17 12:15:14 +01:00
|
|
|
|
2023-01-17 23:11:22 +01:00
|
|
|
const data = Object.assign({}, regex.exec(time)?.groups);
|
2023-01-17 12:15:14 +01:00
|
|
|
|
2023-01-17 23:11:22 +01:00
|
|
|
let res = 0;
|
|
|
|
Object.entries(data).forEach(([key, value]) => {
|
|
|
|
if (value) {
|
|
|
|
res += +value * TimeSecond[key as keyof typeof TimeSecond];
|
|
|
|
}
|
|
|
|
});
|
2023-01-17 12:15:14 +01:00
|
|
|
|
2023-01-17 23:11:22 +01:00
|
|
|
return res;
|
2023-01-17 12:15:14 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Calculating the difference between a date and now
|
|
|
|
* @param time Time
|
|
|
|
* @returns Delta between the time and now
|
|
|
|
*/
|
|
|
|
export const timeDeltaToString = (time: number) => {
|
2023-01-17 23:11:22 +01:00
|
|
|
const now = Date.now();
|
|
|
|
// TODO adapt the output and not always parse the time as seconds
|
|
|
|
return `${strToSeconds(`${(now - time) / 1000}`)} secs`;
|
2022-07-27 13:00:23 +02:00
|
|
|
};
|