My code:
from datetime import datetime
def timedelta_calc(timeone, timetwo):
chars = ['AM', 'PM']
for i in chars:
timeone = timeone.replace(i, '')
timetwo = timetwo.replace(i, '')
FMT = '%H:%M'
tdelta = datetime.strptime(timetwo, FMT) - datetime.strptime(timeone, FMT)
return str(tdelta)
print(timedelta_calc('10:15PM', '11:15PM'))
In my code shown above, we are taking 10:15PM
and 11:15PM
, feeding them into the function and the output is 1:00:00
as expected(for the most part, the format is supposed to remove the seconds column). But with time delta functions like this the time you are subtracting from(timetwo
) is always expected to be higher. This caused issues for times such as
timeone = '10:15PM
and timetwo = '1:15AM'
output:
-1 day, 15:00:00
even though the delta should be 3 hours the output doesn't show that. The time string examples I'm using aren't usually what is given when using the datetime module. I'm using:
def get_time():
timezone = pytz.timezone('')#tz removed for privacy reasons
time = datetime.now(timezone)
time = time.strftime('%H:%M')
hours, minutes = time.split(":")
hours, minutes = int(hours), int(minutes)
setting = "AM"
if hours > 12:
setting = "PM"
hours -= 12
return str(("%02d:%02d" + setting) % (hours, minutes)).replace(' ', '')
This simply takes uses a datetime obj in 24h military time and converts it into normal time with AM or PM glued to the end as it should be with normal time. and what can easily be seen in my timedelta function is the use of
chars = ['AM', 'PM']
for i in chars:
timeone = timeone.replace(i, '')
timetwo = timetwo.replace(i, '')
to remove the non int characters and only feed the datetime.strptime
with the normal times.
Now from experience with the datetime module the same output issue would occur even when given 24h times(I assume. I could be totally wrong on this one) like 23:30
and 1:30
the next day. From what I know the datetime module does not contain anything to help fix this.
as you can see from the title I am at a lack of words for what to even call this issue. Is there an issue with my code? Or could anyone provide something that would solve this issue?
Looking into this more I could see how this isn't datetimes fault since the provided times don't have a date attached to them which might assist the issue. But Generally speaking it should be assumed what the operation should be based on how they are placed in the datetime.striptime
the first given time obj being timetwo
(or last depending on what you guys come up with) because its the larger of the two values. And timeone
being
the second given time obj in the strptime since it is smaller and assumed to be the start of the delta.
will update with any information need so please ask. I'm new around here so please be easy on me! Thanks for you time
in your example ""10:15PM" vs. "1:15AM"", you assume that the second, "smaller" time is from the next day - so actually, you assume occurance of these values / instances of time along a (strictly and) monotonically increasing time axis. You can account for this by adding a day:
from datetime import datetime, timedelta
def abs_timediff(timeone, timetwo):
t1, t2 = datetime.strptime(timeone, "%I:%M%p"), datetime.strptime(timetwo, "%I:%M%p")
if t2 > t1: # same day
return t2-t1
return (t2+timedelta(1))-t1 # assume t2 is on next day, so add one
for tone, ttwo in zip(('10:15PM', "10:15PM"), ('11:15PM', '1:15AM')):
print(tone, ttwo, "diff:", abs_timediff(tone, ttwo))
# 10:15PM 11:15PM diff: 1:00:00
# 10:15PM 1:15AM diff: 3:00:00
Related: Comparing two times without date in Python, and for the timedelta arithmetic on it's own: Python timedelta behaviour on subtraction.