From ed04a9bc89afed2e774d4d386475f8c792d9547f Mon Sep 17 00:00:00 2001 From: rzuasti Date: Mon, 8 Jun 2026 11:15:20 -0400 Subject: [PATCH] Scale duration formatting up to months formatSeconds previously capped at minutes; extend it to dynamically show the two largest relevant units up through months for the passive scanners' "Listening for ..." line and other duration displays. Co-Authored-By: Claude Opus 4.8 --- frontend/lib/utils/duration_formatter.dart | 21 +++++++++++++++---- .../test/unit/duration_formatter_test.dart | 20 ++++++++++++++++++ 2 files changed, 37 insertions(+), 4 deletions(-) diff --git a/frontend/lib/utils/duration_formatter.dart b/frontend/lib/utils/duration_formatter.dart index 15b5c85..69463eb 100644 --- a/frontend/lib/utils/duration_formatter.dart +++ b/frontend/lib/utils/duration_formatter.dart @@ -1,7 +1,20 @@ +/// Formats a duration given in [seconds] into a compact, human-readable string. +/// +/// The format scales with the magnitude of the duration, always showing the two +/// largest relevant units: seconds for sub-minute spans, then minutes, hours, +/// days, weeks and finally months for longer ones. String formatSeconds(double seconds) { final total = seconds.round().clamp(0, double.maxFinite.toInt()); - if (total < 60) return '${total}s'; - final m = total ~/ 60; - final s = total % 60; - return '${m}m ${s}s'; + const minute = 60; + const hour = 60 * minute; + const day = 24 * hour; + const week = 7 * day; + const month = 30 * day; + + if (total < minute) return '${total}s'; + if (total < hour) return '${total ~/ minute}m ${total % minute}s'; + if (total < day) return '${total ~/ hour}h ${(total % hour) ~/ minute}m'; + if (total < week) return '${total ~/ day}d ${(total % day) ~/ hour}h'; + if (total < month) return '${total ~/ week}w ${(total % week) ~/ day}d'; + return '${total ~/ month}mo ${(total % month) ~/ week}w'; } diff --git a/frontend/test/unit/duration_formatter_test.dart b/frontend/test/unit/duration_formatter_test.dart index 676fa54..33dd468 100644 --- a/frontend/test/unit/duration_formatter_test.dart +++ b/frontend/test/unit/duration_formatter_test.dart @@ -21,6 +21,26 @@ void main() { expect(formatSeconds(59.6), '1m 0s'); }); + test('renders hour-and-minute values past 60 minutes', () { + expect(formatSeconds(3600), '1h 0m'); + expect(formatSeconds(3600 + 125), '1h 2m'); + }); + + test('renders day-and-hour values past 24 hours', () { + expect(formatSeconds(86400), '1d 0h'); + expect(formatSeconds(86400 + 3 * 3600), '1d 3h'); + }); + + test('renders week-and-day values past 7 days', () { + expect(formatSeconds(604800), '1w 0d'); + expect(formatSeconds(604800 + 2 * 86400), '1w 2d'); + }); + + test('renders month-and-week values past 30 days', () { + expect(formatSeconds(2592000), '1mo 0w'); + expect(formatSeconds(2592000 + 14 * 86400), '1mo 2w'); + }); + test('clamps negative values to zero', () { expect(formatSeconds(-10), '0s'); });