relative method

  1. @override
String relative(
  1. Duration duration, {
  2. bool dropPrefixOrSuffix = false,
  3. Abbreviation form = Abbreviation.none,
})
inherited

Toggle dropPrefixOrSuffix to get spanned duration without any prefix or suffix.

This will not return precise duration, for precise durations, use duration()

Note: When creating your own localization, please take a look at MomentLocalization.relativeThreshold function and MomentLocalization._relativeThresholds before implementing. Those will make your life slightly easier

form - Unit string form. For example, minute would look like "18 minutes ago", "18 min ago", "18m ago" in full, mid, short forms, respectively.

Implementation

@override
String relative(
  Duration duration, {
  bool dropPrefixOrSuffix = false,
  Abbreviation form = Abbreviation.none,
}) {
  final bool past = duration.isNegative;

  duration = duration.abs();

  DurationInterval interval = MomentLocalization.relativeThreshold(duration);

  String value =
      (units[interval]?.get(form, dropPrefixOrSuffix) ?? "¯\\_(ツ)_/¯");

  if (!interval.singular) {
    value = value.replaceAll(
      srDelta,
      DurationUnit.relativeDuration(duration, interval.unit).toString(),
    );
  }

  if (dropPrefixOrSuffix) return value;

  return past ? relativePast(value) : relativeFuture(value);
}