2026-06-10 19:19:30 +02:00
|
|
|
import { getCurrentIntlLocale } from '@/lib/i18n';
|
|
|
|
|
import { formatMessage, useI18nStore } from '@/lib/i18n/store';
|
2026-06-03 18:20:35 +03:00
|
|
|
import { formatTimeForPreference } from '@/lib/timeFormat';
|
|
|
|
|
import type { TimeFormatPreference } from '@/stores/useUIStore';
|
2026-02-28 15:16:31 -03:00
|
|
|
|
|
|
|
|
const isSameDay = (left: Date, right: Date): boolean => {
|
|
|
|
|
return (
|
|
|
|
|
left.getFullYear() === right.getFullYear() &&
|
|
|
|
|
left.getMonth() === right.getMonth() &&
|
|
|
|
|
left.getDate() === right.getDate()
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
2026-03-04 20:08:52 +02:00
|
|
|
const isYesterday = (date: Date, now: Date): boolean => {
|
|
|
|
|
const yesterday = new Date(now);
|
|
|
|
|
yesterday.setDate(now.getDate() - 1);
|
|
|
|
|
return isSameDay(date, yesterday);
|
|
|
|
|
};
|
|
|
|
|
|
2026-02-28 15:16:31 -03:00
|
|
|
const isValidTimestamp = (timestamp: number): boolean => {
|
|
|
|
|
return Number.isFinite(timestamp) && !Number.isNaN(new Date(timestamp).getTime());
|
|
|
|
|
};
|
|
|
|
|
|
2026-06-03 18:20:35 +03:00
|
|
|
export const formatTimestampForDisplay = (timestamp: number, timeFormatPreference: TimeFormatPreference): string => {
|
2026-02-28 15:16:31 -03:00
|
|
|
if (!isValidTimestamp(timestamp)) {
|
|
|
|
|
return '';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const date = new Date(timestamp);
|
|
|
|
|
const now = new Date();
|
2026-06-03 18:20:35 +03:00
|
|
|
const timePart = formatTimeForPreference(date, timeFormatPreference);
|
2026-06-10 19:19:30 +02:00
|
|
|
const locale = getCurrentIntlLocale();
|
|
|
|
|
const dictionary = useI18nStore.getState().dictionary;
|
2026-02-28 15:16:31 -03:00
|
|
|
|
|
|
|
|
if (isSameDay(date, now)) {
|
|
|
|
|
return timePart;
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-04 20:08:52 +02:00
|
|
|
if (isYesterday(date, now)) {
|
2026-06-10 19:19:30 +02:00
|
|
|
return formatMessage(dictionary, 'common.date.yesterdayWithTime', { time: timePart });
|
2026-03-04 20:08:52 +02:00
|
|
|
}
|
|
|
|
|
|
2026-06-10 19:19:30 +02:00
|
|
|
const monthPart = date.toLocaleString(locale, { month: 'short' });
|
2026-03-04 20:08:52 +02:00
|
|
|
const dayPart = date.getDate();
|
|
|
|
|
const datePart = `${monthPart} ${dayPart}`;
|
|
|
|
|
|
|
|
|
|
if (date.getFullYear() === now.getFullYear()) {
|
|
|
|
|
return `${datePart}, ${timePart}`;
|
|
|
|
|
}
|
2026-02-28 15:16:31 -03:00
|
|
|
|
2026-03-04 20:08:52 +02:00
|
|
|
return `${datePart}, ${date.getFullYear()}, ${timePart}`;
|
2026-02-28 15:16:31 -03:00
|
|
|
};
|