πΈ Rybbit - open-source and privacy-friendly alternative to Google Analytics that is 10x more intuitive.
chore: add code comments in dateTimeUtils for better clarity
Andrii Andrushchyshyn53ae9b372cab
diff --git a/client/src/lib/dateTimeUtils.ts b/client/src/lib/dateTimeUtils.tsindex a4f0834c6..825679484 100644--- a/client/src/lib/dateTimeUtils.ts+++ b/client/src/lib/dateTimeUtils.ts@@ -26,6 +26,7 @@ export function getLocalizedWeekdayNames(): string[] {const formatter = new Intl.DateTimeFormat(locale, { weekday: format });+ // Fallback in case Intl failsconst fallback: Record<"narrow" | "short" | "long", string[]> = {narrow: ["M", "T", "W", "T", "F", "S", "S"],short: ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"],@@ -40,16 +41,16 @@ export function getLocalizedWeekdayNames(const date = new Date(today);date.setDate(today.getDate() + ((day - today.getDay() + 7) % 7));- const formattedParts = formatter.formatToParts(date);- const weekdayPart = formattedParts.find((part) => part.type === "weekday");+ const parts = formatter.formatToParts(date);+ const weekday = parts.find((part) => part.type === "weekday");- return weekdayPart?.value ?? fallback[format][i];+ return weekday?.value ?? fallback[format][i];});}/**- * Returns a list of 24 localized hour labels (e.g., "0", "12 AM", "1 PM")- * based on the given locale and formatting options.+ * Returns an array of 24 localized hour labels (e.g., "0", "12 AM", "1 PM")+ * based on locale and given formatting options.** @param locale - The locale to use (e.g., "en-US", "uk-UA").* @param options - Intl.DateTimeFormat options (e.g., { hour: "numeric", hour12: true }).@@ -63,42 +64,26 @@ export function getLocalizedTimeLabels(return Array.from({ length: 24 }, (_, hour) => {const date = new Date();- date.setHours(hour, 0, 0, 0); // hour:00:00.000+ date.setHours(hour, 0, 0, 0);return formatter.format(date);});}/**- * Formats a duration given in minutes and seconds into a human-readable string.- *- * Uses `luxon.Duration` to normalize and format the result, omitting zero units- * and showing the output in a compact style (e.g., "1 min 30 sec").+ * Formats a duration from minutes and seconds into a human-readable string.** @param minutes - Number of minutes.* @param seconds - Number of seconds.- * @returns A human-readable duration string.+ * @returns A compact, readable string like "1 min 30 sec".*/export function formatDuration(minutes: number, seconds: number): string {const units: Record<string, number> = {};+ if (minutes > 0) units.minutes = minutes;+ if (seconds > 0 || minutes === 0) units.seconds = seconds;- if (minutes > 0) {- units.minutes = minutes;- }-- // Always show seconds if minutes is zero or seconds > 0- if (seconds > 0 || minutes === 0) {- units.seconds = seconds;- }+ const keys: (keyof DurationLikeObject)[] = minutes > 0 ? ["minutes", "seconds"] : ["seconds"];- const keys: (keyof DurationLikeObject)[] = [];- if (minutes > 0) {- keys.push("minutes");- }- keys.push("seconds");-- const duration = Duration.fromObject(units)- .shiftTo(...keys)- .normalize();+ const duration = Duration.fromObject(units).shiftTo(...keys).normalize();return duration.toHuman({listStyle: "narrow",@@ -106,55 +91,42 @@ export function formatDuration(minutes: number, seconds: number): string {});}---/**- * Formats a given hour and minute into a localized time string,- * respecting either 24-hour or 12-hour format based on the `hour12` flag.+ * Formats a given hour and minute into a localized time string+ * (e.g., "11:00 AM" or "23:00"), using the detected `hour12` preference.*- * @param hour - The hour of the day (0β23).- * @param minute - The minute of the hour (0β59).- * @returns A localized time string, e.g., "11:00 AM" or "23:00".+ * @param hour - Hour of the day (0β23).+ * @param minute - Minute of the hour (0β59).+ * @returns Localized time string.*/export const formatLocalTime = (hour: number, minute: number): string => {- // Create a DateTimeFormat instance with local settings:- // - hour: numeric (e.g., 1, 12, 23)- // - minute: 2-digit (e.g., 00, 59)- // - hour12: true for 12-hour format, false for 24-hour formatconst formatter = new Intl.DateTimeFormat(undefined, {hour: "numeric",minute: "2-digit",hour12: hour12,});- // Create a new Date object set to the given hour and minuteconst date = new Date();date.setHours(hour, minute, 0, 0);- // Format the date to a localized time stringreturn formatter.format(date);};+// --- Precomputed localized data ---+// Full weekday names (e.g., "Monday", "Tuesday")+export const longDayNames: string[] = getLocalizedWeekdayNames(userLocale, "long");-// localized full day names-// e.g., "Monday" "Tuesday" "Wednesday"-export const longDayNames:string[] = getLocalizedWeekdayNames(userLocale, "long");--// localized short day names-// e.g., "Mon" "Tue" "Wed"-export const shortDayNames:string[] = getLocalizedWeekdayNames(userLocale, "short");+// Short weekday names (e.g., "Mon", "Tue")+export const shortDayNames: string[] = getLocalizedWeekdayNames(userLocale, "short");-// localized time (hour witout minutes) labels-// e.g., "00" "01" "02"+// Hour-only labels (e.g., "00", "01", "02", "1 AM", "2 PM")export const hourLabels: string[] = getLocalizedTimeLabels(userLocale, {hour: "numeric",hour12: hour12,});-// localized time (hour with minutes) labels-// e.g., "00:00" "01:00" "02:00"+// Full hour:minute labels (e.g., "00:00", "01:00", "1:00 AM", "2:00 PM")export const hourMinuteLabels: string[] = getLocalizedTimeLabels(userLocale, {hour: "numeric",minute: "2-digit",