Skip to content

Commit

Permalink
fix: correct duration formatting logic (#643)
Browse files Browse the repository at this point in the history
* fix: correct duration formatting logic

* test: add unit test for formatting 3600 seconds as "1h 0m 0s"

* test: fix expected output for formatting 3600 seconds as "1h 0m 0s"
  • Loading branch information
BelKed authored Feb 21, 2025
1 parent 1860f15 commit 2b14364
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 3 deletions.
12 changes: 9 additions & 3 deletions src/util/time.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,15 @@ export function seconds_to_duration(seconds: number) {
const min = Math.floor((seconds / 60) % 60);
const sec = Math.floor(seconds % 60);
const l = [];
if (hrs != 0) l.push(hrs + 'h');
if (min != 0) l.push(min + 'm');
if (sec != 0 || l.length == 0) l.push(sec + 's');

if (hrs > 0) {
l.push(hrs + 'h');
l.push(min + 'm');
} else if (min > 0) {
l.push(min + 'm');
}
l.push(sec + 's');

return l.join(' ');
}

Expand Down
27 changes: 27 additions & 0 deletions test/unit/time.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { seconds_to_duration } from '~/util/time';

describe('seconds_to_duration', () => {
test('should format 8145 seconds as "2h 15m 45s"', () => {
expect(seconds_to_duration(8145)).toBe('2h 15m 45s');
});

test('should format 3630 seconds as "1h 0m 30s"', () => {
expect(seconds_to_duration(3630)).toBe('1h 0m 30s');
});

test('should format 3600 seconds as "1h 0m 0s"', () => {
expect(seconds_to_duration(3600)).toBe('1h 0m 0s');
});

test('should format 1830 seconds as "30m 30s"', () => {
expect(seconds_to_duration(1830)).toBe('30m 30s');
});

test('should format 60 seconds as "1m 0s"', () => {
expect(seconds_to_duration(60)).toBe('1m 0s');
});

test('should format 30 seconds as "30s"', () => {
expect(seconds_to_duration(30)).toBe('30s');
});
});

1 comment on commit 2b14364

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here are screenshots of this commit:

Screenshots using aw-server v0.12.3b18 (click to expand)

Screenshots using aw-server-rust master (click to expand)

Screenshots using aw-server-rust v0.12.3b18 (click to expand)

Please sign in to comment.