export function getDurationStringPanelMode()

in libs/designer-ui/src/lib/utils/utils.ts [140:283]


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

  const intl = getIntl();
  if (milliseconds < 1000) {
    const millisecondsRounded = Math.round(Math.abs(milliseconds / 1000) * 10) / 10;
    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: millisecondsRounded,
        }
      );
    }
    return intl.formatMessage(
      {
        defaultMessage: '{seconds, plural, one {# second} other {# seconds}}',
        id: 'hN7iBP',
        description: 'A duration of time shown in seconds',
      },
      {
        seconds: millisecondsRounded,
      }
    );
  }

  const seconds = Math.round(Math.abs(milliseconds / 1000));
  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.floor(Math.abs(milliseconds / 60 / 1000));
  const millisecondsCarry = Math.abs(milliseconds - minutes * 60 * 1000);
  const secondsCarry = Math.round(Math.abs(millisecondsCarry / 1000));
  if (minutes < 60) {
    if (abbreviated) {
      return intl.formatMessage(
        {
          defaultMessage: '{minutes}m {seconds}s',
          id: 'kHcCxH',
          description: 'This is a time duration in abbreviated format',
        },
        {
          seconds: secondsCarry,
          minutes,
        }
      );
    }
    return intl.formatMessage(
      {
        defaultMessage: '{minutes} minutes {seconds} seconds',
        id: 'XTuxmH',
        description: 'This is a time duration in full non abbreviated format',
      },
      {
        seconds: secondsCarry,
        minutes,
      }
    );
  }

  const hours = Math.floor(Math.abs(milliseconds / 60 / 60 / 1000));
  const minutesCarry = Math.round(Math.abs(milliseconds - hours * 3600000) / 60 / 1000);
  if (hours < 24) {
    if (abbreviated) {
      return intl.formatMessage(
        {
          defaultMessage: '{hours}h {minutes}m',
          id: 'Oib1mL',
          description: 'This is a time duration in abbreviated format',
        },
        {
          hours,
          minutes: minutesCarry,
        }
      );
    }
    return intl.formatMessage(
      {
        defaultMessage: '{hours} hours {minutes} minutes',
        id: 'l36V56',
        description: 'This is a time duration in full non abbreviated format',
      },
      {
        hours,
        minutes: minutesCarry,
      }
    );
  }

  const days = Math.floor(Math.abs(milliseconds / 24 / 60 / 60 / 1000));
  const hoursCarry = Math.round(Math.abs(milliseconds - days * 86400000) / 60 / 60 / 1000);
  if (abbreviated) {
    return intl.formatMessage(
      {
        defaultMessage: '{days}d {hours}h',
        id: 'tImHz/',
        description: 'This is a time duration in abbreviated format',
      },
      {
        hours: hoursCarry,
        days,
      }
    );
  }
  return intl.formatMessage(
    {
      defaultMessage: '{days} days {hours} hours',
      id: 'X8JjjT',
      description: 'This is a time duration in full non abbreviated format',
    },
    {
      hours: hoursCarry,
      days,
    }
  );
}