Skip to content

Commit

Permalink
fix: fix 1 week uptime formatting (#1759)
Browse files Browse the repository at this point in the history
  • Loading branch information
artemmufazalov authored Dec 17, 2024
1 parent 6abc16e commit e3de008
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
9 changes: 6 additions & 3 deletions src/utils/dataFormatters/__test__/formatUptime.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,13 @@ describe('formatUptimeInSeconds', () => {
const D = 24 * H;

it('should return days if value is more than 24h', () => {
expect(formatUptimeInSeconds(D)).toBe('1d' + UNBREAKABLE_GAP + '00:00:00');
expect(formatUptimeInSeconds(24 * H)).toBe('1d' + UNBREAKABLE_GAP + '00:00:00');
expect(formatUptimeInSeconds(D + H + M + 12)).toBe('1d' + UNBREAKABLE_GAP + '01:01:12');
expect(formatUptimeInSeconds(12 * D + 12 * H + 12 * M + 12)).toBe(
'12d' + UNBREAKABLE_GAP + '12:12:12',
// 1 week
expect(formatUptimeInSeconds(7 * D + H + M + 12)).toBe('7d' + UNBREAKABLE_GAP + '01:01:12');
// 1 month
expect(formatUptimeInSeconds(30 * D + 11 * H + 30 * M + 12)).toBe(
'30d' + UNBREAKABLE_GAP + '11:30:12',
);
expect(formatUptimeInSeconds(1234 * D + 12 * H + 12 * M + 12)).toBe(
'1234d' + UNBREAKABLE_GAP + '12:12:12',
Expand Down
4 changes: 3 additions & 1 deletion src/utils/dataFormatters/dataFormatters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,9 @@ export function formatUptimeInSeconds(seconds: number) {

let value: string;

if (d.days() > 0) {
// Do not use just d.days(), since days could be rescaled up to weeks and more
// So for 7d we will have d.weeks() = 1 and d.days() = 0
if (Math.floor(d.asDays()) > 0) {
value = d.format(`d[${i18n('d')}${UNBREAKABLE_GAP}]hh:mm:ss`);
} else if (d.hours() > 0) {
value = d.format('h:mm:ss');
Expand Down

0 comments on commit e3de008

Please sign in to comment.