-6

I want to do a time minus: 24Hours - 14Hours 20 minutes 10 seconds,write a function:

def time_minus(start,end):
    def convert_sec(time):
        hours = int(time.split(":")[0])
        mins = int(time.split(":")[1])
        secs = int(time.split(":")[2])
        return hours*60*60 + mins*60 + secs
    start_time = convert_sec(start)
    end_time = convert_sec(end)   
    st = end_time-start_time
    hours = str(st//3600).rjust(2,'0')
    mins = str((st%3600)//60).rjust(2,'0')
    secs = str((st%3600)%60).rjust(2,'0')
    result = hours +  " hours " + mins +  " minutes " + secs + " seconds"
    print(result)
    return  hours + ":" + mins +  ":" + secs 

The output:

x=time_minus("14:20:10","24:00:00")  
09 hours 39 minutes 50 seconds  
x
'09:39:50'
                     

Is there a some python's lib to do the minus?How can call it?

>>> from datetime import datetime
>>> s1 = '14:20:10'
>>> s2 = '24:00:00'  
>>> FMT = '%H:%M:%S'
>>> tdelta = datetime.strptime(s2, FMT) - datetime.strptime(s1, FMT)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3.9/_strptime.py", line 568, in _strptime_datetime
    tt, fraction, gmtoff_fraction = _strptime(data_string, format)
  File "/usr/lib/python3.9/_strptime.py", line 349, in _strptime
    raise ValueError("time data %r does not match format %r" %
ValueError: time data '24:00:00' does not match format '%H:%M:%S'

I have to customize a function because of time data '24:00:00' does not match format '%H:%M:%S'!!!

newview
  • 648
  • 6
  • Does this answer your question? [How do I find the time difference between two datetime objects in python?](https://stackoverflow.com/q/1345827/2745495) – Gino Mempin Nov 06 '22 at 09:19
  • You can use standard `datetime` module to read the time strings and get the 9, 39, 50 values, but you'll need to format the output into your desired `__ hours __ minutes __ seconds` (or to whichever format you want). – Gino Mempin Nov 06 '22 at 09:21
  • Please show me how can? – newview Nov 06 '22 at 09:27
  • 1
    `time data '24:00:00' does not match format '%H:%M:%S'` => It's because `datetime`'s `%H` can only parse up to 23 since `24:00:00` is equivalent to `0:00:00` (there is technically no "24 o'clock", it technically means "the next day"). You'll have to convert it to `0:00:00`. – Gino Mempin Nov 06 '22 at 09:30
  • `show me how` => You are already computing the timedelta. You are almost there. Once you have it, search the datetime docs and/or related posts on how to format it to a string, like this [Format timedelta to string](https://stackoverflow.com/a/539360/2745495) – Gino Mempin Nov 06 '22 at 09:33

2 Answers2

2

Since 24:00:00 is not a valid time of the day, I think you need to convert it into a format including a day indicator:

from datetime import datetime
s1     = '0-14:20:10'
s2     = '1-00:00:00'  
FMT    = '%w-%H:%M:%S'
tdelta = datetime.strptime(s2, FMT) - datetime.strptime(s1, FMT)

Source: https://docs.python.org/3/library/datetime.html#strftime-and-strptime-format-codes

C-3PO
  • 1,181
  • 9
  • 17
0

The convertion part seems a bit of uggly,but it is so short!

from datetime import datetime
s1 = '14:20:10'
s2 = '00:00:00'  
FMT = '%H:%M:%S'
tdelta = datetime.strptime(s2, FMT) - datetime.strptime(s1, FMT)
str(tdelta).split(",")[1].replace(" ","0")
'09:39:50'
newview
  • 648
  • 6