Files
familienarchiv/frontend/src/lib/shared/relativeTime.ts
Marcel 567612761d refactor: move lib-root files to lib/shared/ and finalize domain structure
- Move api.server.ts, errors.ts, types.ts, utils.ts, relativeTime.ts to lib/shared/
- Move person relationship components to lib/person/relationship/
- Move Stammbaum components to lib/person/genealogy/
- Move HelpPopover to lib/shared/primitives/
- Update all import paths across routes, specs, and lib files
- Update vi.mock() paths in server-project test files
- Remove now-empty legacy directories (components/, hooks/, server/, etc.)
- Update vite.config.ts coverage include paths for new structure
- Update frontend/CLAUDE.md to reflect domain-based lib/ layout

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-05-05 14:53:31 +02:00

31 lines
1.6 KiB
TypeScript

import * as m from '$lib/paraglide/messages.js';
export function relativeTimeDe(from: Date, now: Date = new Date()): string {
const minutes = Math.round((now.getTime() - from.getTime()) / 60_000);
// Malformed input (e.g. Invalid Date from a broken backend string) must not
// crash the dashboard — fall back to "0 Minuten" rather than render NaN.
if (!Number.isFinite(minutes)) return m.comment_time_minutes({ count: 0 });
if (minutes < 60) return m.comment_time_minutes({ count: minutes });
if (minutes < 1440) return m.comment_time_hours({ count: Math.round(minutes / 60) });
return m.comment_time_days({ count: Math.round(minutes / 1440) });
}
// "vor N Jahren" for a historical letter date relative to now. Computed from
// calendar fields (not a constant ms-per-year) so that a letter from exactly
// one year ago reports "vor 1 Jahr" rather than falling on the wrong side of
// a leap-year rounding. Returns "" for invalid or future dates — the caller
// should then hide the relative-time label rather than render a misleading
// "vor 0 Jahren".
export function relativeYearsDe(from: Date, now: Date = new Date()): string {
if (Number.isNaN(from.getTime()) || Number.isNaN(now.getTime())) return '';
if (from.getTime() > now.getTime()) return '';
let years = now.getUTCFullYear() - from.getUTCFullYear();
const beforeAnniversary =
now.getUTCMonth() < from.getUTCMonth() ||
(now.getUTCMonth() === from.getUTCMonth() && now.getUTCDate() < from.getUTCDate());
if (beforeAnniversary) years -= 1;
if (years < 1) return 'vor weniger als 1 Jahr';
if (years === 1) return 'vor 1 Jahr';
return `vor ${years} Jahren`;
}