import type { Context } from "../index.js" const units = { year: 24 * 60 * 60 * 1000 * 365, month: 24 * 60 * 60 * 1000 * 365 / 12, day: 24 * 60 * 60 * 1000, hour: 60 * 60 * 1000, minute: 60 * 1000, second: 1000 } as const const rtf = new Intl.RelativeTimeFormat('en', { numeric: 'auto' }) export function RelativeTime({ timestamp }: Context): { relativeTime: string | null } { const elapsed = timestamp.getTime() - new Date().getTime() for (const u in units) if (Math.abs(elapsed) > units[u as keyof typeof units] || u == 'second') return { relativeTime: rtf.format(Math.round(elapsed / units[u as keyof typeof units]), u as keyof typeof units) } return { relativeTime: null } }