-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtimePassed.js
More file actions
36 lines (31 loc) · 854 Bytes
/
timePassed.js
File metadata and controls
36 lines (31 loc) · 854 Bytes
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
const ONE_MINUTE = 60
function plurals(time) {
return time === 1 ? '' : 's'
}
function print(time, unit) {
return time > 0 ? `${time} ${unit}${plurals(time)}` : ''
}
function TimeFormatter(timeInSeconds) {
return {
format() {
const minutes = Math.floor(timeInSeconds / ONE_MINUTE)
const seconds = timeInSeconds % ONE_MINUTE
const hours = Math.floor(minutes / ONE_MINUTE)
const realMinutes = minutes % ONE_MINUTE
return `${print(hours, 'hour')} ${print(realMinutes, 'minute')} ${print(seconds, 'second')}`
.trim()
.replace(' ', ' ')
},
}
}
function TimePassed(then) {
return {
then,
count() {
const now = Math.floor(Date.now() / 1000)
return TimeFormatter(now - this.then).format()
},
}
}
exports.TimePassed = TimePassed
exports.TimeFormatter = TimeFormatter