feat: more readable time delta #193

Merged
Anri merged 6 commits from date into dev 2024-10-08 20:53:29 +02:00
4 changed files with 27 additions and 21 deletions
Showing only changes of commit 78be496eda - Show all commits

View file

@ -67,7 +67,7 @@
"c_reminder5": "Reminders of",
"c_reminder6": "Page",
"c_reminder7": "No message",
"c_reminder8": "Expires in",
"c_reminder8": "Expires",
"c_reminder9": "Do on",
"c_reminder10": "The user has no pending reminders or page no.",
"c_reminder11": "empty",
@ -76,7 +76,6 @@
"c_reminder14": "Message sent in DM as the channel is no longer available.",
"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_reminder17": "Message from",
"c_reminder18": "Invalid time, try again.",
"c_play_name": "play",

View file

@ -67,7 +67,7 @@
"c_reminder5": "Rappels de",
"c_reminder6": "Page",
"c_reminder7": "Pas de message",
"c_reminder8": "Expire dans",
"c_reminder8": "Expire",
"c_reminder9": "Fais le",
"c_reminder10": "L'utilisateur n'a aucun rappel en attente ou page n°",
"c_reminder11": "vide",
@ -76,7 +76,6 @@
"c_reminder14": "Message envoyé en DM car le salon n'est plus disponible.",
"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_reminder17": "Message d'il y a",
"c_reminder18": "Temps invalide, réessayez.",
"c_play_name": "play",

View file

@ -191,7 +191,7 @@ export const sendReminder = (client: Client, info: infoReminder, option: OptionR
if (channel?.isSendable()) {
let content = `<@${info.userId}>`;
embed.setFooter({
text: `${loc.get("c_reminder17")} ${timeDeltaToString(info.createdAt)}`,
text: timeDeltaToString(info.locale, info.createdAt).capitalize(),
});
// Mention everybody if needed
@ -424,7 +424,7 @@ export const embedListReminders = async (
if (text.length > 1024) {
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({
name: `#${remind.id}${loc.get("c_reminder9")} ${showDate(
local,

View file

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