feat: more readable time delta #193
4 changed files with 49 additions and 14 deletions
|
@ -67,7 +67,7 @@
|
||||||
"c_reminder5": "Reminders of",
|
"c_reminder5": "Reminders of",
|
||||||
"c_reminder6": "Page",
|
"c_reminder6": "Page",
|
||||||
"c_reminder7": "No message",
|
"c_reminder7": "No message",
|
||||||
"c_reminder8": "Expires",
|
"c_reminder8": "Expires in",
|
||||||
"c_reminder9": "Do on",
|
"c_reminder9": "Do on",
|
||||||
"c_reminder10": "The user has no pending reminders or page no.",
|
"c_reminder10": "The user has no pending reminders or page no.",
|
||||||
"c_reminder11": "empty",
|
"c_reminder11": "empty",
|
||||||
|
|
|
@ -67,7 +67,7 @@
|
||||||
"c_reminder5": "Rappels de",
|
"c_reminder5": "Rappels de",
|
||||||
"c_reminder6": "Page",
|
"c_reminder6": "Page",
|
||||||
"c_reminder7": "Pas de message",
|
"c_reminder7": "Pas de message",
|
||||||
"c_reminder8": "Expire",
|
"c_reminder8": "Expire dans",
|
||||||
"c_reminder9": "Fais le",
|
"c_reminder9": "Fais le",
|
||||||
"c_reminder10": "L'utilisateur n'a aucun rappel en attente ou page n°",
|
"c_reminder10": "L'utilisateur n'a aucun rappel en attente ou page n°",
|
||||||
"c_reminder11": "vide",
|
"c_reminder11": "vide",
|
||||||
|
|
|
@ -112,7 +112,7 @@ export const newReminder = async (client: Client, time: string, info: infoRemind
|
||||||
}
|
}
|
||||||
|
|
||||||
// Send confirmation to user
|
// 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()) {
|
if (channel?.isSendable()) {
|
||||||
let content = `<@${info.userId}>`;
|
let content = `<@${info.userId}>`;
|
||||||
embed.setFooter({
|
embed.setFooter({
|
||||||
text: timeDeltaToString(info.locale, info.createdAt).capitalize(),
|
text: timeDeltaToString(info.createdAt).capitalize(),
|
||||||
});
|
});
|
||||||
|
|
||||||
// Mention everybody if needed
|
// Mention everybody if needed
|
||||||
|
@ -426,7 +426,7 @@ export const embedListReminders = async (
|
||||||
if (text.length > 1024) {
|
if (text.length > 1024) {
|
||||||
text = `${text.substring(0, 1021)}...`;
|
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({
|
embed.addFields({
|
||||||
name: `#${remind.id} • ${loc.get("c_reminder9")} ${showDate(
|
name: `#${remind.id} • ${loc.get("c_reminder9")} ${showDate(
|
||||||
local,
|
local,
|
||||||
|
|
|
@ -99,17 +99,52 @@ export const strToSeconds = (time: string) => {
|
||||||
* @param time Time
|
* @param time Time
|
||||||
* @returns Delta between the time and now
|
* @returns Delta between the time and now
|
||||||
*/
|
*/
|
||||||
export const timeDeltaToString = (lang: string, time: number) => {
|
export const timeDeltaToString = (time: number) => {
|
||||||
const now = Date.now();
|
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" });
|
// Initialize variables to hold the time components
|
||||||
for (const [unit, value] of Object.entries(TimeSecond)) {
|
let years = 0,
|
||||||
const delta = secondsDifference / (value as number);
|
weeks = 0,
|
||||||
if (delta >= 1) {
|
days = 0,
|
||||||
return rtf.format(Math.round(delta), unit.toLowerCase() as Intl.RelativeTimeFormatUnit);
|
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(" ");
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in a new issue