UNPKG

1.7 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.extractTime = void 0;
4const MIN_MS = 60 * 1000;
5const HR_MS = MIN_MS * 60;
6const DAY_MS = HR_MS * 24;
7const ZERO = { days: 0, hours: 0, milliseconds: 0, minutes: 0, seconds: 0 };
8/** @internal */
9function add(a, b) {
10 return {
11 days: (a.days || 0) + b.days,
12 hours: (a.hours || 0) + b.hours,
13 milliseconds: (a.milliseconds || 0) + b.milliseconds,
14 minutes: (a.minutes || 0) + b.minutes,
15 seconds: (a.seconds || 0) + b.seconds
16 };
17}
18/** @internal */
19function extractSecs(ms) {
20 const s = ms / 1000;
21 if (s < 60) {
22 const seconds = ~~s;
23 return add({ seconds }, extractTime(ms - (seconds * 1000)));
24 }
25 const m = s / 60;
26 if (m < 60) {
27 const minutes = ~~m;
28 return add({ minutes }, extractTime(ms - (minutes * MIN_MS)));
29 }
30 const h = m / 60;
31 if (h < 24) {
32 const hours = ~~h;
33 return add({ hours }, extractTime(ms - (hours * HR_MS)));
34 }
35 const days = ~~(h / 24);
36 return add({ days }, extractTime(ms - (days * DAY_MS)));
37}
38/**
39 * @name extractTime
40 * @summary Convert a quantity of seconds to Time array representing accumulated {days, minutes, hours, seconds, milliseconds}
41 * @example
42 * <BR>
43 *
44 * ```javascript
45 * import { extractTime } from '@polkadot/util';
46 *
47 * const { days, minutes, hours, seconds, milliseconds } = extractTime(6000); // 0, 0, 10, 0, 0
48 * ```
49 */
50function extractTime(milliseconds) {
51 return !milliseconds
52 ? ZERO
53 : milliseconds < 1000
54 ? add({ milliseconds }, ZERO)
55 : extractSecs(milliseconds);
56}
57exports.extractTime = extractTime;