Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions src/utils/__tests__/usage.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,16 +134,28 @@ describe('usage window helpers', () => {
});

it('formats duration in block timer style', () => {
expect(formatUsageDuration(0)).toBe('0hr');
expect(formatUsageDuration(0)).toBe('0m');
expect(formatUsageDuration(3 * 60 * 60 * 1000)).toBe('3hr');
expect(formatUsageDuration(3.5 * 60 * 60 * 1000)).toBe('3hr 30m');
expect(formatUsageDuration(4 * 60 * 60 * 1000 + 5 * 60 * 1000)).toBe('4hr 5m');
});

it('formats duration with days when >= 24h', () => {
expect(formatUsageDuration(25 * 60 * 60 * 1000)).toBe('1d 1hr');
expect(formatUsageDuration(36.5 * 60 * 60 * 1000)).toBe('1d 12hr 30m');
expect(formatUsageDuration(168 * 60 * 60 * 1000)).toBe('7d');
});

it('formats duration in compact style', () => {
expect(formatUsageDuration(0, true)).toBe('0h');
expect(formatUsageDuration(0, true)).toBe('0m');
expect(formatUsageDuration(3 * 60 * 60 * 1000, true)).toBe('3h');
expect(formatUsageDuration(3.5 * 60 * 60 * 1000, true)).toBe('3h30m');
expect(formatUsageDuration(4 * 60 * 60 * 1000 + 5 * 60 * 1000, true)).toBe('4h5m');
});

it('formats duration with days in compact style when >= 24h', () => {
expect(formatUsageDuration(25 * 60 * 60 * 1000, true)).toBe('1d1h');
expect(formatUsageDuration(36.5 * 60 * 60 * 1000, true)).toBe('1d12h30m');
expect(formatUsageDuration(168 * 60 * 60 * 1000, true)).toBe('7d');
});
});
20 changes: 8 additions & 12 deletions src/utils/usage-windows.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,18 +91,14 @@ export function resolveWeeklyUsageWindow(usageData: UsageData, nowMs = Date.now(

export function formatUsageDuration(durationMs: number, compact = false): string {
const clampedMs = Math.max(0, durationMs);
const elapsedHours = Math.floor(clampedMs / (1000 * 60 * 60));
const elapsedMinutes = Math.floor((clampedMs % (1000 * 60 * 60)) / (1000 * 60));

if (compact) {
return elapsedMinutes === 0 ? `${elapsedHours}h` : `${elapsedHours}h${elapsedMinutes}m`;
}

if (elapsedMinutes === 0) {
return `${elapsedHours}hr`;
}

return `${elapsedHours}hr ${elapsedMinutes}m`;
const d = Math.floor(clampedMs / (1000 * 60 * 60 * 24));
const h = Math.floor((clampedMs % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
const m = Math.floor((clampedMs % (1000 * 60 * 60)) / (1000 * 60));

const hLabel = compact ? 'h' : 'hr';
const sep = compact ? '' : ' ';
const parts = [d > 0 && `${d}d`, h > 0 && `${h}${hLabel}`, m > 0 && `${m}m`].filter(Boolean);
return parts.length > 0 ? parts.join(sep) : '0m';
}

export function getUsageErrorMessage(error: UsageError): string {
Expand Down
Loading