UNPKG

671 BJavaScriptView Raw
1// Copyright 2017-2022 @polkadot/util authors & contributors
2// SPDX-License-Identifier: Apache-2.0
3import { bnToBn } from "../bn/toBn.js";
4
5function formatValue(elapsed) {
6 if (elapsed < 15) {
7 return `${elapsed.toFixed(1)}s`;
8 } else if (elapsed < 60) {
9 return `${elapsed | 0}s`;
10 } else if (elapsed < 3600) {
11 return `${elapsed / 60 | 0}m`;
12 }
13
14 return `${elapsed / 3600 | 0}h`;
15}
16
17export function formatElapsed(now, value) {
18 const tsNow = now && now.getTime() || 0;
19 const tsValue = value instanceof Date ? value.getTime() : bnToBn(value).toNumber();
20 return tsNow && tsValue ? formatValue(Math.max(Math.abs(tsNow - tsValue), 0) / 1000) : '0.0s';
21}
\No newline at end of file