friendlyAgoFormat function

String friendlyAgoFormat(
  1. Duration elapsed, {
  2. bool short = false,
})

Formats an elapsed duration as a coarse, human-friendly relative-time phrase like "2 hours ago" or "just now".

Only the largest applicable time unit is shown, and durations under 5 seconds are treated as "just now".

With short, the unit is abbreviated, like "2h ago".

Implementation

String friendlyAgoFormat(Duration elapsed, {bool short = false}) {
  if (elapsed.inSeconds < 5) {
    return 'just now';
  }
  final ago = short ? _shortAgo : _pluralizedAgo;
  if (elapsed.inMinutes < 1) {
    return ago(elapsed.inSeconds, 'second');
  }
  if (elapsed.inHours < 1) {
    return ago(elapsed.inMinutes, 'minute');
  }
  if (elapsed.inDays < 1) {
    return ago(elapsed.inHours, 'hour');
  }
  return ago(elapsed.inDays, 'day');
}