]> git.r.bdr.sh - rbdr/forum/blob - src/utils/readable_time.js
Port to sveltekit
[rbdr/forum] / src / utils / readable_time.js
1 const internals = {
2 magnitudes: {
3 day: 86400000,
4 hour: 3600000,
5 minute: 60000,
6 second: 1000
7 },
8 labels: {
9 day: 'time.days',
10 hour: 'time.hours',
11 minute: 'time.minutes',
12 second: 'time.seconds'
13 },
14
15 makeTimeReadable(time, magnitude) {
16
17 return {
18 count: Math.floor(time / internals.magnitudes[magnitude]),
19 label: internals.labels[magnitude]
20 };
21 }
22 };
23
24 export const readableTime = function readableTime(time) {
25
26 switch (true) {
27 case time >= internals.magnitudes.day:
28 return internals.makeTimeReadable(time, 'day');
29 case time >= internals.magnitudes.hour:
30 return internals.makeTimeReadable(time, 'hour');
31 case time >= internals.magnitudes.minute:
32 return internals.makeTimeReadable(time, 'minute');
33 case time < 0:
34 return internals.makeTimeReadable(0, 'second');
35 default:
36 return internals.makeTimeReadable(time, 'second');
37 }
38 };