getDurationStringFromSeconds static method

String getDurationStringFromSeconds(
  1. int seconds,
  2. AppLocalizations appLocalizations, {
  3. bool compact = false,
})

Implementation

static String getDurationStringFromSeconds(
  int seconds,
  AppLocalizations appLocalizations, {
  bool compact = false,
}) {
  final double minutes = seconds / 60;
  final int roundMinutes = minutes.round();
  if (roundMinutes == 0) {
    // TODO(monsieurtanuki): localize if relevant
    if (compact) {
      return '${seconds}s';
    }
  }
  if (roundMinutes < 60) {
    if (compact) {
      return '${roundMinutes}m';
    }
    return appLocalizations.plural_ago_minutes(roundMinutes);
  }

  final double hours = minutes / 60;
  final int roundHours = hours.round();
  if (roundHours < 24) {
    if (compact) {
      return '${roundHours}h';
    }
    return appLocalizations.plural_ago_hours(roundHours);
  }

  final double days = hours / 24;
  final int roundDays = days.round();
  if (roundDays < 7) {
    if (compact) {
      return '${roundDays}d';
    }
    return appLocalizations.plural_ago_days(roundDays);
  }
  final double weeks = days / 7;
  final int roundWeeks = weeks.round();
  if (compact) {
    return '${roundWeeks}w';
  }
  if (roundWeeks <= 4) {
    return appLocalizations.plural_ago_weeks(roundWeeks);
  }

  final double months = days / (365 / 12);
  final int roundMonths = months.round();
  return appLocalizations.plural_ago_months(roundMonths);
}