import dayjs, { Dayjs } from 'dayjs'; /* eslint-disable no-nested-ternary */ function toFixedString(v: number) { return v.toString().split('.')[0]; } /** * 格式化时间段字符串 -> 17mins54s or 17 Minutes * @param param0 * @returns */ export function formatReadableTimeDuration({ start, end, showSecondUnit, t, }: { start?: number; end?: number; /** 是否显示第二个单位 */ showSecondUnit?: boolean; t: any; }): string | null { if (!start || !end) { return null; } const diff = end - start; const second = 1000; const minute = 60 * second; const hour = minute * 60; const day = hour * 24; let unit1 = ''; let unit2 = ''; if (diff < minute) { const seconds = diff / second; unit1 = `${toFixedString(seconds)} ${t( showSecondUnit ? 'common.time.seconds.short' : seconds >= 2 ? 'common.time.seconds' : 'common.time.second', )}`; } else if (diff < hour) { const minutes = diff / minute; unit1 = `${toFixedString(minutes)} ${t( minutes >= 2 ? showSecondUnit ? 'common.time.minutes.short' : 'common.time.minutes' : showSecondUnit ? 'common.time.minute.short' : 'common.time.minute', )}`; const seconds = (diff % minute) / second; unit2 = `${toFixedString(seconds)} ${t( showSecondUnit ? 'common.time.seconds.short' : seconds >= 2 ? 'common.time.seconds' : 'common.time.second', )}`; } else if (diff < day) { const hours = diff / hour; unit1 = `${toFixedString(hours)} ${t( hours >= 2 ? 'common.time.hours' : 'common.time.hour', )}`; const minutes = (diff % hour) / minute; unit2 = `${toFixedString(minutes)} ${t( minutes >= 2 ? 'common.time.minutes' : 'common.time.minute', )}`; } else { const days = diff / day; unit1 = `${toFixedString(days)} ${t( days >= 2 ? 'common.time.days' : 'common.time.day', )}`; const hours = (diff % day) / hour; unit2 = `${toFixedString(hours)} ${t( hours >= 2 ? 'common.time.hours' : 'common.time.hour', )}`; } if (showSecondUnit) { return `${unit1}${unit2}`.replaceAll(' ', '').toLowerCase(); } return unit1; } /** * 格式化消息时间段字符串 -> 17mins54s or 17min ago * @param param0 * @returns */ export function formatReadableTimeAgo({ time, t, }: { time: number; t: any; }): string { if (!time) { return ''; } const start = dayjs(time); const end = dayjs(); const diffHours = end.diff(start, 'h'); if (diffHours > 24) { return start.format('YYYY/MM/DD HH:MM'); } if (diffHours >= 1) { return t('common.time.hour-ago.short', { time: diffHours, }); } const diffMinute = end.diff(start, 'm'); if (diffMinute >= 1) { return t('common.time.minute-ago.short', { time: diffMinute, }); } return `${end.diff(start, 's')}${t('common.time.seconds.short')}`; } export function getMonthShortTextByMonthNumber(month: number) { switch (month) { case 1: return 'Jan'; case 2: return 'Feb'; case 3: return 'Mar'; case 4: return 'Apr'; case 5: return 'May'; case 6: return 'Jun'; case 7: return 'Jul'; case 8: return 'Aug'; case 9: return 'Sep'; case 10: return 'Oct'; case 11: return 'Nov'; case 12: return 'Dec'; default: return ''; } } export function getMonthShortText(date: Date | string | number | Dayjs) { const month = dayjs(date).month() + 1; return getMonthShortTextByMonthNumber(month); }