Add or Subtract two Duration in Python
def main():
"""
Subtracts two time values.
"""
time_a = input("Enter time (hh:mm:ss): ")
time_b = input("Subtract (hh:mm:ss): ")
sec_a = get_total_seconds(time_a)
sec_b = get_total_seconds(time_b)
sec_diff = abs(sec_a - sec_b)
hr, remainder = divmod(sec_diff, 3600)
mn, sc = divmod(remainder, 60)
print(f"{hr:02d}:{mn:02d}:{sc:02d}")
def get_total_seconds(time_str: str) -> int:
units = time_str.split(":")
if len(units) != 3:
raise ValueError("Invalid format")
hr, mn, sc = map(int, units)
return hr * 3600 + mn * 60 + sc
if __name__ == "__main__":
main()
def main():
"""
Adds two time values.
"""
time_a = input("Enter time (hh:mm:ss): ")
time_b = input("Subtract (hh:mm:ss): ")
sec_a = get_total_seconds(time_a)
sec_b = get_total_seconds(time_b)
total_seconds = sec_a + sec_b
hours = total_seconds // 3600
total_seconds %= 3600
minutes = total_seconds // 60
seconds = total_seconds % 60
print(f"{hours:02d}:{minutes:02d}:{seconds:02d}")
def get_total_seconds(time_str: str):
units = time_str.split(":")
if len(units) != 3:
raise ValueError("Invalid format")
hr = int(units[0])
mn = int(units[1])
sc = int(units[2])
return hr * 3600 + mn * 60 + sc
if __name__ == "__main__":
main()