1. import datetime
  2. def extend_time(time_str, duration_str):
  3. """
  4. Extends a given time string by a specified duration.
  5. Args:
  6. time_str (str): A time string in HH:MM:SS format.
  7. duration_str (str): A duration string in DD:HH:MM:SS format.
  8. Returns:
  9. str: The extended time string in HH:MM:SS format, or None if there's an error.
  10. """
  11. try:
  12. # Parse the initial time string
  13. start_time = datetime.datetime.strptime(time_str, "%H:%M:%S").time()
  14. # Parse the duration string
  15. duration = datetime.timedelta(hours=int(duration_str.split(":")[0]),
  16. minutes=int(duration_str.split(":")[1]),
  17. seconds=int(duration_str.split(":")[2]))
  18. # Extend the time
  19. extended_time = datetime.datetime.combine(datetime.date.today(), start_time) + duration
  20. # Format the extended time back to HH:MM:SS
  21. extended_time_str = extended_time.strftime("%H:%M:%S")
  22. return extended_time_str
  23. except ValueError:
  24. return None # Handle invalid input format
  25. except Exception as e:
  26. return None # Handle other potential errors

Add your comment