Time.tsx 842 B

123456789101112131415161718192021222324252627282930313233343536
  1. import React, { FC } from 'react';
  2. import { toDuration } from '@grafana/data';
  3. export interface TimeProps {
  4. timeInMs: number;
  5. className?: string;
  6. humanize?: boolean;
  7. }
  8. export const Time: FC<TimeProps> = ({ timeInMs, className, humanize }) => {
  9. return <span className={`elapsed-time ${className}`}>{formatTime(timeInMs, humanize)}</span>;
  10. };
  11. const formatTime = (timeInMs: number, humanize = false): string => {
  12. const inSeconds = timeInMs / 1000;
  13. if (!humanize) {
  14. return `${inSeconds.toFixed(1)}s`;
  15. }
  16. const duration = toDuration(inSeconds, 'seconds');
  17. const hours = duration.hours();
  18. const minutes = duration.minutes();
  19. const seconds = duration.seconds();
  20. if (hours) {
  21. return `${hours}h ${minutes}m ${seconds}s`;
  22. }
  23. if (minutes) {
  24. return `${minutes}m ${seconds}s`;
  25. }
  26. return `${seconds}s`;
  27. };