better timeDeltaToString function

This commit is contained in:
Mylloon 2024-09-30 23:57:57 +02:00
parent f9cc154b04
commit 78be496eda
Signed by: Anri
GPG key ID: A82D63DFF8D1317F
4 changed files with 27 additions and 21 deletions

View file

@ -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 in", "c_reminder8": "Expires",
"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",
@ -76,7 +76,6 @@
"c_reminder14": "Message sent in DM as the channel is no longer available.", "c_reminder14": "Message sent in DM as the channel is no longer available.",
"c_reminder15": "Message sent in DM because you have left", "c_reminder15": "Message sent in DM because you have left",
"c_reminder16": "Message sent in DM because the Discord guild is no longer available.", "c_reminder16": "Message sent in DM because the Discord guild is no longer available.",
"c_reminder17": "Message from",
"c_reminder18": "Invalid time, try again.", "c_reminder18": "Invalid time, try again.",
"c_play_name": "play", "c_play_name": "play",

View file

@ -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 dans", "c_reminder8": "Expire",
"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",
@ -76,7 +76,6 @@
"c_reminder14": "Message envoyé en DM car le salon n'est plus disponible.", "c_reminder14": "Message envoyé en DM car le salon n'est plus disponible.",
"c_reminder15": "Message envoyé en DM car vous avez quitté", "c_reminder15": "Message envoyé en DM car vous avez quitté",
"c_reminder16": "Message envoyé en DM car le serveur Discord n'est plus disponible.", "c_reminder16": "Message envoyé en DM car le serveur Discord n'est plus disponible.",
"c_reminder17": "Message d'il y a",
"c_reminder18": "Temps invalide, réessayez.", "c_reminder18": "Temps invalide, réessayez.",
"c_play_name": "play", "c_play_name": "play",

View file

@ -191,7 +191,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: `${loc.get("c_reminder17")} ${timeDeltaToString(info.createdAt)}`, text: timeDeltaToString(info.locale, info.createdAt).capitalize(),
}); });
// Mention everybody if needed // Mention everybody if needed
@ -424,7 +424,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(remind.expiration_date)}`; const expiration = `${loc.get("c_reminder8")} ${timeDeltaToString(local, 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,

View file

@ -3,15 +3,15 @@ import { RegexC, RegExpFlags } from "./regex";
/** /**
* Parsed string adapted with TZ (locales) and format for the specified lang * Parsed string adapted with TZ (locales) and format for the specified lang
* @param tz Lang * @param lang Locale
* @param locale Locales * @param translation Translation for "at"
* @param date Date * @param date Date
* @returns String * @returns String
*/ */
export const showDate = (tz: string, locale: Map<string, unknown>, date: Date) => { export const showDate = (lang: string, translation: Map<string, unknown>, date: Date) => {
const localeInfo = new Intl.Locale(tz); const localeInfo = new Intl.Locale(lang);
const intlTimezone = moment.tz.zonesForCountry(localeInfo.region ?? localeInfo.baseName); const intlTimezone = moment.tz.zonesForCountry(localeInfo.region ?? localeInfo.baseName);
const formattedDate = new Intl.DateTimeFormat(tz, { const formattedDate = new Intl.DateTimeFormat(lang, {
timeZone: intlTimezone ? intlTimezone[0] : "Factory", timeZone: intlTimezone ? intlTimezone[0] : "Factory",
dateStyle: "short", dateStyle: "short",
timeStyle: "medium", timeStyle: "medium",
@ -19,14 +19,14 @@ export const showDate = (tz: string, locale: Map<string, unknown>, date: Date) =
.format(date) .format(date)
.split(" "); .split(" ");
return `${formattedDate[0]} ${locale.get("u_time_at")} ${formattedDate[1]}`; return `${formattedDate[0]} ${translation.get("u_time_at")} ${formattedDate[1]}`;
}; };
export enum TimeSecond { export enum TimeSecond {
Year = 31536000, Year = 60 * 60 * 24 * 365,
Week = 604800, Week = 60 * 60 * 24 * 7,
Day = 86400, Day = 60 * 60 * 24,
Hour = 3600, Hour = 60 * 60,
Minute = 60, Minute = 60,
Second = 1, Second = 1,
} }
@ -95,13 +95,21 @@ export const strToSeconds = (time: string) => {
/** /**
* Calculating the difference between a date and now * Calculating the difference between a date and now
* @param lang Locale
* @param time Time * @param time Time
* @returns Delta between the time and now * @returns Delta between the time and now
*/ */
export const timeDeltaToString = (time: number) => { export const timeDeltaToString = (lang: string, time: number) => {
const now = Date.now(); const now = Date.now();
// TODO: adapt the output and not always parse the time as seconds const secondsDifference = Math.round((time - now) / 1000) - 2;
// https://git.mylloon.fr/ConfrerieDuKassoulait/Botanique/issues/189
// Use Intl.RelativeTimeFormat ? const rtf = new Intl.RelativeTimeFormat(lang, { numeric: "auto" });
return `${strToSeconds(`${(now - time) / 1000}`)} secs`; 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);
}
}
return rtf.format(secondsDifference, "second");
}; };