31 lines
1.2 KiB
TypeScript
31 lines
1.2 KiB
TypeScript
import { parseDbDateTime } from './timezone';
|
|
|
|
// Parses a MySQL DATETIME string correctly regardless of the Node
|
|
// process's own timezone — see lib/timezone.ts.
|
|
export function parseDbDate(s: string): Date {
|
|
return parseDbDateTime(s);
|
|
}
|
|
|
|
export function diffMinutes(startStr: string, endStr: string | null): number | null {
|
|
if (!endStr) return null;
|
|
const start = parseDbDate(startStr);
|
|
const end = parseDbDate(endStr);
|
|
return Math.round((end.getTime() - start.getTime()) / 60000);
|
|
}
|
|
|
|
// "2h ago", "3d ago", etc. — used for "last connected" displays.
|
|
export function formatRelativeTime(dbDateStr: string | null): string {
|
|
if (!dbDateStr) return '—';
|
|
const diffMs = Date.now() - parseDbDate(dbDateStr).getTime();
|
|
const diffMin = Math.round(diffMs / 60_000);
|
|
if (diffMin < 1) return 'just now';
|
|
if (diffMin < 60) return `${diffMin}m ago`;
|
|
const diffHr = Math.round(diffMin / 60);
|
|
if (diffHr < 24) return `${diffHr}h ago`;
|
|
const diffDay = Math.round(diffHr / 24);
|
|
if (diffDay < 30) return `${diffDay}d ago`;
|
|
const diffMonth = Math.round(diffDay / 30);
|
|
if (diffMonth < 12) return `${diffMonth}mo ago`;
|
|
return `${Math.round(diffMonth / 12)}y ago`;
|
|
}
|