1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
5 import { PropertyValue } from 'models/search-bar';
10 } from 'models/vocabulary';
12 export const formatDate = (isoDate?: string | null, utc: boolean = false) => {
14 const date = new Date(isoDate);
17 text = date.toUTCString();
19 text = date.toLocaleString();
21 return text === 'Invalid Date' ? '(none)' : text;
26 export const formatFileSize = (size?: number | string) => {
27 if (typeof size === 'number') {
32 for (const { base, unit } of FILE_SIZES) {
34 return `${(size / base).toFixed(base === 1 ? 0 : 1)} ${unit}`;
38 if ((typeof size === 'string' && size === '') || size === undefined) {
44 export const formatTime = (time: number, seconds?: boolean) => {
45 const minutes = Math.floor((time / (1000 * 60)) % 60).toFixed(0);
46 const hours = Math.floor(time / (1000 * 60 * 60)).toFixed(0);
49 const seconds = Math.floor((time / 1000) % 60).toFixed(0);
50 return hours + 'h ' + minutes + 'm ' + seconds + 's';
53 return hours + 'h ' + minutes + 'm';
56 export const getTimeDiff = (endTime: string, startTime: string) => {
57 return new Date(endTime).getTime() - new Date(startTime).getTime();
60 export const formatProgress = (loaded: number, total: number) => {
61 const progress = loaded >= 0 && total > 0 ? (loaded * 100) / total : 0;
62 return `${progress.toFixed(2)}%`;
65 export function formatUploadSpeed(
72 loaded > prevLoaded && currentTime > prevTime
73 ? (loaded - prevLoaded) / (currentTime - prevTime)
76 return `${(speed / 1000).toFixed(2)} MB/s`;
102 export const formatPropertyValue = (
104 vocabulary?: Vocabulary
106 if (vocabulary && pv.keyID && pv.valueID) {
107 return `${getTagKeyLabel(pv.keyID, vocabulary)}: ${getTagValueLabel(
114 return pv.value ? `${pv.key}: ${pv.value}` : pv.key;
119 export const formatContainerCost = (cost: number): string => {
120 const decimalPlaces = 3;
122 const factor = Math.pow(10, decimalPlaces);
123 const rounded = Math.round(cost * factor) / factor;
124 if (cost > 0 && rounded === 0) {
125 // Display min value of 0.001
126 return `$${1 / factor}`;
128 // Otherwise use rounded value to proper decimal places
129 return `$${rounded}`;