# -*- coding: ansi -*- def timetosec(time_str): parts = time_str.split(':') parts = [int(p) for p in parts] if len(parts) == 3: # HH:MM:SS hours, minutes, seconds = parts elif len(parts) == 2: # MM:SS hours = 0 minutes, seconds = parts elif len(parts) == 1: # SS hours = 0 minutes = 0 seconds = parts[0] else: raise ValueError("Zeitformat") total_seconds = hours * 3600 + minutes * 60 + seconds return total_seconds print(timetosec("02:15:30")) # 8130 print(timetosec("15:30")) # 930 print(timetosec("45")) # 45 def format_seconds(seconds: int) -> str: """ Meine Interpretation von dem Moodle eintrag. Der leer ist. Wandelt Sekunden in das nächst beste Format um :param seconds -> int :returns str """ if seconds < 0: return "falsche eingable" intervals = ( ('Tag', 86400), # 60*60*24 ('Stunde', 3600), # 60*60 ('Minute', 60), ('Sekunde', 1), ) result = [] for name, count in intervals: value = seconds // count if value: seconds -= value * count if value == 1: result.append(f"{value} {name}") else: result.append(f"{value} {name}en") return ', '.join(result) if result else "0 Sekunden" print(format_seconds(3661)) # 1std 1m 1s print(format_seconds(86465)) # 1t 1m 5s print(format_seconds(59)) # 59s