-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhumanReadableTime.js
More file actions
60 lines (45 loc) · 1.67 KB
/
Copy pathhumanReadableTime.js
File metadata and controls
60 lines (45 loc) · 1.67 KB
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
function humanReadable(seconds) {
// TODO
// Guard clause: Ensure the input is within a reasonable range (0 to 99:59:59)
if (seconds < 0 || seconds > 359999) {
return null;
}
// 1. Calculate Hours: 3600 seconds in an hour
let hours = Math.floor(seconds / 3600);
// Remove the hours we just calculated from the total seconds pool
seconds -= hours * 3600;
// Manual Padding: Add leading zero if hours is a single digit
if (hours < 10) {
hours = '0' + hours;
}
// 2. Calculate Minutes: 60 seconds in a minute
let minutes = Math.floor(seconds / 60);
// Remove the minutes from the remaining seconds pool
seconds -= minutes * 60;
// Manual Padding: Add leading zero if minutes is a single digit
if (minutes < 10) {
minutes = '0' + minutes;
}
// 3. Remaining Seconds: What's left is our 'SS' value
// Manual Padding: Add leading zero if seconds is a single digit
if (seconds < 10) {
seconds = '0' + seconds;
}
// Combine into the final string format HH:MM:SS
return hours + ':' + minutes + ':' + seconds;
}
/**
Refactor - Modern, DRY version of the time formatter
DRY (Don't Repeat Yourself) using padStart(), and this avoids all those if statements
*/
function humanReadableModern(totalSeconds) {
// Basic bounds check
if (totalSeconds < 0 || totalSeconds > 359999) return "99:59:59";
const h = Math.floor(totalSeconds / 3600);
const m = Math.floor((totalSeconds % 3600) / 60); // Use modulo for cleaner math
const s = totalSeconds % 60;
// Helper to pad numbers to 2 digits
const format = (num) => String(num).padStart(2, '0');
// Return using Template Literals
return `${format(h)}:${format(m)}:${format(s)}`;
}