Skip to content

Commit 0f78545

Browse files
committed
Update time formatting pipe to allow display of hours when needed
1 parent 5ad918c commit 0f78545

File tree

2 files changed

+19
-6
lines changed

2 files changed

+19
-6
lines changed

webapp/src/app/pipes/format-timer.pipe.spec.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,18 @@ describe('FormatTimerPipe', () => {
1111
const testCases = [
1212
{input: 'abc', expected: defaultFormat},
1313
{input: '', expected: defaultFormat},
14+
{input: undefined, expected: defaultFormat},
15+
{input: null, expected: defaultFormat},
1416
{input: '0', expected: '00:00'},
1517
{input: '1', expected: '00:01'},
1618
{input: '60', expected: '01:00'},
1719
{input: '119', expected: '01:59'},
20+
{input: '3600', expected: '1:00:00'},
21+
{input: '3661', expected: '1:01:01'},
1822
{input: '-1', expected: '-00:01'},
1923
{input: '-60', expected: '-01:00'},
20-
{input: '-119', expected: '-01:59'}
24+
{input: '-119', expected: '-01:59'},
25+
{input: '-3600', expected: '-1:00:00'},
2126
];
2227

2328
for (const testCase of testCases) {
+13-5
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,28 @@
11
import {Pipe, PipeTransform} from '@angular/core';
22

3+
const SEPARATOR = ":";
4+
const SECONDS_PER_MINUTE = 60;
5+
const SECONDS_PER_HOUR = 3600;
6+
37
@Pipe({
48
name: 'formatTimer',
59
standalone: true
610
})
711
export class FormatTimerPipe implements PipeTransform {
8-
912
transform(value: unknown, ..._args: unknown[]): unknown {
1013
const duration = parseInt(value as string, 10);
1114
if (isNaN(duration)) {
12-
return "--:--";
15+
return `--${SEPARATOR}--`;
1316
}
1417
const sign = duration < 0 ? "-" : "";
15-
const minutes = Math.floor(Math.abs(duration) / 60).toString().padStart(2, "0");
16-
const seconds = (Math.abs(duration) % 60).toString().padStart(2, "0");
17-
return `${sign}${minutes}:${seconds}`;
18+
const totalMinutes = Math.abs(duration);
19+
const nbHours = Math.floor(totalMinutes / SECONDS_PER_HOUR);
20+
const nbMinutes = Math.floor((totalMinutes - nbHours * SECONDS_PER_HOUR) / SECONDS_PER_MINUTE);
21+
const nbSeconds = totalMinutes % SECONDS_PER_MINUTE;
22+
const hours = nbHours > 0 ? nbHours.toString() + SEPARATOR : "";
23+
const minutes = nbMinutes.toString().padStart(2, "0") + SEPARATOR;
24+
const seconds = nbSeconds.toString().padStart(2, "0");
25+
return `${sign}${hours}${minutes}${seconds}`;
1826
}
1927

2028
}

0 commit comments

Comments
 (0)