diff options
| author | Ben Sima <ben@bsima.me> | 2025-11-13 14:53:55 -0500 |
|---|---|---|
| committer | Ben Sima <ben@bsima.me> | 2025-11-13 14:53:55 -0500 |
| commit | bbb1e56e97ee5e899fc6deae497f06b3f13595d3 (patch) | |
| tree | ff0a5e68768e268ab96431f741afee1c60b74116 /Biz/PodcastItLater/UI.py | |
| parent | 55f676da16691c7eb23f1ffcb3b589dc3842b274 (diff) | |
Extract format_duration utility to UI module
Moved format_duration function from Web.py to UI.py for better code
organization. This is a UI utility function used for displaying
episode durations, so it belongs in the shared UI module rather than
the web-specific module.
Amp-Thread-ID:
https://ampcode.com/threads/T-8edacbeb-b343-49ca-b524-1c999272acb6
Co-authored-by: Amp <amp@ampcode.com>
Diffstat (limited to 'Biz/PodcastItLater/UI.py')
| -rw-r--r-- | Biz/PodcastItLater/UI.py | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/Biz/PodcastItLater/UI.py b/Biz/PodcastItLater/UI.py index 940abd5..99ac29b 100644 --- a/Biz/PodcastItLater/UI.py +++ b/Biz/PodcastItLater/UI.py @@ -9,6 +9,42 @@ Common UI components and utilities shared across web pages. import ludic.html as html +def format_duration(seconds: int | None) -> str: + """Format duration from seconds to human-readable format. + + Examples: + 300 -> "5m" + 3840 -> "1h 4m" + 11520 -> "3h 12m" + """ + if seconds is None or seconds <= 0: + return "Unknown" + + # Constants for time conversion + seconds_per_minute = 60 + minutes_per_hour = 60 + seconds_per_hour = 3600 + + # Round up to nearest minute + minutes = (seconds + seconds_per_minute - 1) // seconds_per_minute + + # Show as minutes only if under 60 minutes (exclusive) + # 3599 seconds rounds up to 60 minutes, which we keep as "60m" + if minutes <= minutes_per_hour: + # If exactly 3600 seconds (already 60 full minutes without rounding) + if seconds >= seconds_per_hour: + return "1h" + return f"{minutes}m" + + hours = minutes // minutes_per_hour + remaining_minutes = minutes % minutes_per_hour + + if remaining_minutes == 0: + return f"{hours}h" + + return f"{hours}h {remaining_minutes}m" + + def create_bootstrap_styles() -> html.style: """Load Bootstrap CSS and icons.""" return html.style( |
