export function getDurationString()

in libs/designer-ui/src/lib/utils/utils.ts [12:119]


export function getDurationString(milliseconds: number, abbreviated = true): string {
  const intl = getIntl();
  if (Number.isNaN(milliseconds)) {
    return '--';
  }

  const seconds = Math.round(Math.abs(milliseconds / 100)) / 10;
  if (seconds < 60) {
    if (abbreviated) {
      return intl.formatMessage(
        {
          defaultMessage: '{seconds}s',
          id: 'iql+jn',
          description: 'This is a period in time in seconds. {seconds} is replaced by the number and s is an abbreviation of seconds',
        },
        {
          seconds,
        }
      );
    }
    return intl.formatMessage(
      {
        defaultMessage: '{seconds, plural, one {# second} other {# seconds}}',
        id: 'hN7iBP',
        description: 'A duration of time shown in seconds',
      },
      {
        seconds,
      }
    );
  }

  const minutes = Math.round(Math.abs(milliseconds / 60 / 1000));
  if (minutes < 60) {
    if (abbreviated) {
      return intl.formatMessage(
        {
          defaultMessage: '{minutes}m',
          id: 'SXb47U',
          description: 'This is a period in time in seconds. {minutes} is replaced by the number and m is an abbreviation of minutes',
        },
        {
          minutes,
        }
      );
    }
    return intl.formatMessage(
      {
        defaultMessage: '{minutes, plural, one {# minute} other {# minutes}}',
        id: 'RhH4pF',
        description: 'A duration of time shown in minutes',
      },
      {
        minutes,
      }
    );
  }

  const hours = Math.round(Math.abs(milliseconds / 60 / 60 / 1000));
  if (hours < 24) {
    if (abbreviated) {
      return intl.formatMessage(
        {
          defaultMessage: '{hours}h',
          id: 'Qu1HkA',
          description: 'This is a period in time in hours. {hours} is replaced by the number and h is an abbreviation of hours',
        },
        {
          hours,
        }
      );
    }
    return intl.formatMessage(
      {
        defaultMessage: '{hours, plural, one {# hour} other {# hours}}',
        id: 'FXLR5M',
        description: 'A duration of time shown in hours',
      },
      {
        hours,
      }
    );
  }

  const days = Math.round(Math.abs(milliseconds / 24 / 60 / 60 / 1000));
  if (abbreviated) {
    return intl.formatMessage(
      {
        defaultMessage: '{days}d',
        id: 'YIBDSH',
        description: 'This is a period in time in days. {days} is replaced by the number and d is an abbreviation of days',
      },
      {
        days,
      }
    );
  }
  return intl.formatMessage(
    {
      defaultMessage: '{days, plural, one {# day} other {# days}}',
      id: 'qUWBUX',
      description: 'A duration of time shown in days',
    },
    {
      days,
    }
  );
}