All files / src/utils formatDuration.ts

0% Statements 0/8
0% Branches 0/4
0% Functions 0/1
0% Lines 0/7

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22                                           
/**
 * Format a time duration like `HH:MM:SS.mmm`.
 *
 * @param durationMs time duration in milliseconds.
 * @return formatted time string.
 */
export default function formatDuration(durationMs: number): string {
  const isNegative = durationMs < 0;
  if (isNegative) durationMs = -durationMs;
 
  const hours = Math.floor(durationMs / 3600000);
  const minutes = Math.floor((durationMs % 3600000) / 60000);
  const seconds = Math.floor((durationMs % 60000) / 1000);
  const milliseconds = Math.floor(durationMs % 1000);
 
  return (isNegative ? '-' : '') +
    `${hours.toString().padStart(2, '0')}:` +
    `${minutes.toString().padStart(2, '0')}:` +
    `${seconds.toString().padStart(2, '0')}.` +
    `${milliseconds.toString().padStart(3, '0')}`;
}