relativeTime function

String relativeTime(
  1. DateTime time,
  2. DateTime now
)

Formats time as a compact relative age from now (e.g. 5m ago, 2h ago, 3d ago). Pure for unit testing — the live caller passes DateTime.now().

Implementation

String relativeTime(DateTime time, DateTime now) {
  final d = now.difference(time);
  if (d.isNegative) return 'just now';
  if (d.inSeconds < 60) return '${d.inSeconds}s ago';
  if (d.inMinutes < 60) return '${d.inMinutes}m ago';
  if (d.inHours < 24) return '${d.inHours}h ago';
  return '${d.inDays}d ago';
}