1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 | import * as metric from './metric.js'
|
8 | import * as math from './math.js'
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 | export const getDate = () => new Date()
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 | export const getUnixTime = Date.now
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 | export const humanizeDuration = d => {
|
31 | if (d < 60000) {
|
32 | const p = metric.prefix(d, -1)
|
33 | return math.round(p.n * 100) / 100 + p.prefix + 's'
|
34 | }
|
35 | d = math.floor(d / 1000)
|
36 | const seconds = d % 60
|
37 | const minutes = math.floor(d / 60) % 60
|
38 | const hours = math.floor(d / 3600) % 24
|
39 | const days = math.floor(d / 86400)
|
40 | if (days > 0) {
|
41 | return days + 'd' + ((hours > 0 || minutes > 30) ? ' ' + (minutes > 30 ? hours + 1 : hours) + 'h' : '')
|
42 | }
|
43 | if (hours > 0) {
|
44 |
|
45 | return hours + 'h' + ((minutes > 0 || seconds > 30) ? ' ' + (seconds > 30 ? minutes + 1 : minutes) + 'min' : '')
|
46 | }
|
47 | return minutes + 'min' + (seconds > 0 ? ' ' + seconds + 's' : '')
|
48 | }
|