Skip to content

Commit 1327317

Browse files
committed
Fix time rounding bug (fixes #154)
1 parent 67f7a5a commit 1327317

File tree

2 files changed

+12
-3
lines changed

2 files changed

+12
-3
lines changed

src/format-time.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,13 @@ const hmsTime = (d: number) => {
4646
return joinWithColons(h, m, s);
4747
}
4848
const hmTime = (d: number) => {
49-
const h = Math.trunc(d / 3600);
50-
const m = Math.ceil((d % 3600) / 60); // Round up the minutes (#86)
49+
let h = Math.trunc(d / 3600);
50+
let m = Math.ceil((d % 3600) / 60); // Round up the minutes (#86)
51+
if (m == 60) {
52+
// Edge case: minutes rounded up to 60, so I need to carry to the hours
53+
h += 1;
54+
m = 0;
55+
}
5156
return joinWithColons(0, h, m);
5257
}
5358

test/format-time.test.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,4 +62,8 @@ it('can render negative times', () => {
6262
expect(formatTime(-30, 'hms')).toBe('-30')
6363
expect(formatTime(-60, 'hms')).toBe('-1:00')
6464
expect(formatTime(-90, 'hms')).toBe('-1:30')
65-
});
65+
});
66+
67+
it('can render tricky times', () => {
68+
expect(formatTime(59 + 59*60 + 1*60*60, 'hm')).toBe('2:00')
69+
});

0 commit comments

Comments
 (0)