From bbb1e56e97ee5e899fc6deae497f06b3f13595d3 Mon Sep 17 00:00:00 2001 From: Ben Sima Date: Thu, 13 Nov 2025 14:53:55 -0500 Subject: 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 --- Biz/PodcastItLater/UI.py | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) (limited to 'Biz/PodcastItLater/UI.py') 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( -- cgit v1.2.3