more precise calculation
All checks were successful
PR Check / lint-and-format (pull_request) Successful in 18s

This commit is contained in:
Mylloon 2024-10-08 20:11:19 +02:00
parent 208dbf0813
commit 5bf6b48d21
Signed by: Anri
GPG key ID: A82D63DFF8D1317F
4 changed files with 49 additions and 14 deletions

View file

@ -67,7 +67,7 @@
"c_reminder5": "Reminders of",
"c_reminder6": "Page",
"c_reminder7": "No message",
"c_reminder8": "Expires",
"c_reminder8": "Expires in",
"c_reminder9": "Do on",
"c_reminder10": "The user has no pending reminders or page no.",
"c_reminder11": "empty",

View file

@ -67,7 +67,7 @@
"c_reminder5": "Rappels de",
"c_reminder6": "Page",
"c_reminder7": "Pas de message",
"c_reminder8": "Expire",
"c_reminder8": "Expire dans",
"c_reminder9": "Fais le",
"c_reminder10": "L'utilisateur n'a aucun rappel en attente ou page n°",
"c_reminder11": "vide",

View file

@ -112,7 +112,7 @@ export const newReminder = async (client: Client, time: string, info: infoRemind
}
// Send confirmation to user
ok(`${loc.get("c_reminder1")} ${timeDeltaToString(info.locale, expiration_date)}.`);
ok(`${loc.get("c_reminder1")} ${timeDeltaToString(expiration_date)}.`);
},
);
});
@ -193,7 +193,7 @@ export const sendReminder = (client: Client, info: infoReminder, option: OptionR
if (channel?.isSendable()) {
let content = `<@${info.userId}>`;
embed.setFooter({
text: timeDeltaToString(info.locale, info.createdAt).capitalize(),
text: timeDeltaToString(info.createdAt).capitalize(),
});
// Mention everybody if needed
@ -426,7 +426,7 @@ export const embedListReminders = async (
if (text.length > 1024) {
text = `${text.substring(0, 1021)}...`;
}
const expiration = `${loc.get("c_reminder8")} ${timeDeltaToString(local, remind.expiration_date)}`;
const expiration = `${loc.get("c_reminder8")} ${timeDeltaToString(remind.expiration_date)}`;
embed.addFields({
name: `#${remind.id}${loc.get("c_reminder9")} ${showDate(
local,

View file

@ -99,17 +99,52 @@ export const strToSeconds = (time: string) => {
* @param time Time
* @returns Delta between the time and now
*/
export const timeDeltaToString = (lang: string, time: number) => {
export const timeDeltaToString = (time: number) => {
const now = Date.now();
const secondsDifference = Math.round((time - now) / 1000) - 2;
let secondsDifference = Math.round((time - now) / 1000);
const rtf = new Intl.RelativeTimeFormat(lang, { numeric: "auto" });
for (const [unit, value] of Object.entries(TimeSecond)) {
const delta = secondsDifference / (value as number);
if (delta >= 1) {
return rtf.format(Math.round(delta), unit.toLowerCase() as Intl.RelativeTimeFormatUnit);
}
// Initialize variables to hold the time components
let years = 0,
weeks = 0,
days = 0,
hours = 0,
minutes = 0,
seconds = 0;
// Calculate the time components
if (secondsDifference >= TimeSecond.Year) {
years = Math.floor(secondsDifference / TimeSecond.Year);
secondsDifference -= years * TimeSecond.Year;
}
if (secondsDifference >= TimeSecond.Week) {
weeks = Math.floor(secondsDifference / TimeSecond.Week);
secondsDifference -= weeks * TimeSecond.Week;
}
if (secondsDifference >= TimeSecond.Day) {
days = Math.floor(secondsDifference / TimeSecond.Day);
secondsDifference -= days * TimeSecond.Day;
}
if (secondsDifference >= TimeSecond.Hour) {
hours = Math.floor(secondsDifference / TimeSecond.Hour);
secondsDifference -= hours * TimeSecond.Hour;
}
if (secondsDifference >= TimeSecond.Minute) {
minutes = Math.floor(secondsDifference / TimeSecond.Minute);
secondsDifference -= minutes * TimeSecond.Minute;
}
return rtf.format(secondsDifference, "second");
// Remaining seconds
seconds = secondsDifference;
// Create the detailed time string
const detailedTimeParts = [];
if (years > 0) detailedTimeParts.push(`${years}y`);
if (weeks > 0) detailedTimeParts.push(`${weeks}w`);
if (days > 0) detailedTimeParts.push(`${days}d`);
if (hours > 0) detailedTimeParts.push(`${hours}h`);
if (minutes > 0) detailedTimeParts.push(`${minutes}m`);
if (seconds > 0) detailedTimeParts.push(`${seconds}s`);
// Return the formatted string
return detailedTimeParts.join(" ");
};