-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathparse.js
87 lines (75 loc) · 1.55 KB
/
parse.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
'use strict'
function toString () {
const microseconds = this.microseconds
const milliseconds = this.milliseconds
const seconds = this.seconds
const minutes = this.minutes
const hours = this.hours
const days = this.days
const parts = [
{
name: 'day',
value: days
},
{
name: 'hour',
value: hours
},
{
name: 'minute',
value: minutes
},
{
name: 'second',
value: seconds
},
{
name: 'millisecond',
value: milliseconds
},
{
name: 'microsecond',
value: microseconds
}
]
const time = []
for (let i = 0; i < parts.length; i++) {
const part = parts[i]
if (part.value === 0) {
if (!time.length) continue // nothing was added yet
let broken = false
for (let j = i; j < parts.length; j++) {
const p = parts[j]
if (p.value) {
broken = true
break
}
}
if (!broken) break
}
time.push(part.value, part.value === 1 ? part.name : part.name + 's')
}
return time.join(' ')
}
module.exports = (micro) => {
const ms = micro / 1000
const ss = ms / 1000
const mm = ss / 60
const hh = mm / 60
const dd = hh / 24
const microseconds = Math.round((ms % 1) * 1000)
const milliseconds = Math.floor(ms % 1000)
const seconds = Math.floor(ss % 60)
const minutes = Math.floor(mm % 60)
const hours = Math.floor(hh % 24)
const days = Math.floor(dd)
return {
microseconds,
milliseconds,
seconds,
minutes,
hours,
days,
toString
}
}