1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
5 export const formatDate = (isoDate?: string) => {
7 const date = new Date(isoDate);
8 const text = date.toLocaleString();
9 return text === 'Invalid Date' ? "" : text;
14 export const formatFileSize = (size?: number) => {
15 if (typeof size === "number") {
16 for (const { base, unit } of FILE_SIZES) {
18 return `${(size / base).toFixed()} ${unit}`;
25 export const formatTime = (time: number) => {
26 const minutes = Math.floor(time / (1000 * 60) % 60).toFixed(0);
27 const hours = Math.floor(time / (1000 * 60 * 60)).toFixed(0);
29 return hours + "h " + minutes + "m";
32 export const getTimeDiff = (endTime: string, startTime: string) => {
33 return new Date(endTime).getTime() - new Date(startTime).getTime();
36 export const formatProgress = (loaded: number, total: number) => {
37 const progress = loaded >= 0 && total > 0 ? loaded * 100 / total : 0;
38 return `${progress.toFixed(2)}%`;
41 export function formatUploadSpeed(prevLoaded: number, loaded: number, prevTime: number, currentTime: number) {
42 const speed = loaded > prevLoaded && currentTime > prevTime
43 ? (loaded - prevLoaded) / (currentTime - prevTime)
45 return `${(speed / 1000).toFixed(2)} KB/s`;