From d7ded16239c4c39304af08ed4b029df3ddc571cc Mon Sep 17 00:00:00 2001 From: KnugiHK <24708955+KnugiHK@users.noreply.github.com> Date: Sat, 17 May 2025 17:35:30 +0800 Subject: [PATCH] Reimplement the convert_time_unit function to make it more human-readable --- Whatsapp_Chat_Exporter/utility.py | 42 ++++++++++++++++++------------- 1 file changed, 25 insertions(+), 17 deletions(-) diff --git a/Whatsapp_Chat_Exporter/utility.py b/Whatsapp_Chat_Exporter/utility.py index 6dcb40f..75744bb 100644 --- a/Whatsapp_Chat_Exporter/utility.py +++ b/Whatsapp_Chat_Exporter/utility.py @@ -44,23 +44,31 @@ def convert_time_unit(time_second: int) -> str: Returns: str: A human-readable string representing the time duration. """ - time = str(timedelta(seconds=time_second)) - if "day" not in time: - if time_second < 1: - time = "less than a second" - elif time_second == 1: - time = "a second" - elif time_second < 60: - time = time[5:][1 if time_second < 10 else 0:] + " seconds" - elif time_second == 60: - time = "a minute" - elif time_second < 3600: - time = time[2:] + " minutes" - elif time_second == 3600: - time = "an hour" - else: - time += " hour" - return time + if time_second < 1: + return "less than a second" + elif time_second == 1: + return "a second" + + delta = timedelta(seconds=time_second) + parts = [] + + days = delta.days + if days > 0: + parts.append(f"{days} day{'s' if days > 1 else ''}") + + hours = delta.seconds // 3600 + if hours > 0: + parts.append(f"{hours} hour{'s' if hours > 1 else ''}") + + minutes = (delta.seconds % 3600) // 60 + if minutes > 0: + parts.append(f"{minutes} minute{'s' if minutes > 1 else ''}") + + seconds = delta.seconds % 60 + if seconds > 0: + parts.append(f"{seconds} second{'s' if seconds > 1 else ''}") + + return " ".join(parts) def bytes_to_readable(size_bytes: int) -> str: