You can use this. I have imported just datetime
from datetime
for brevity in the first line. When you use datetime.strptime
on only the time, it will return a date of January 1st, 1970. The datetime
methods are much richer than the time
methods, so the input and arithmetic is done with datetime
here. You can just use strftime
to output the time without the dates so they are ignored throughout. (Note: In Windows, the timestamp method will not work for January 1st, 1970, so I have added a hack to force the date to January 2nd. As long as this is done for both times, it will work fine).
from datetime import datetime
finaltimes = []
for i in range(len(times)):
try:
a = times[i]
b = prior_times[i]
da = datetime.strptime(a, '%H:%M:%S').replace(day=2)
db = datetime.strptime(b, '%H:%M:%S').replace(day=2)
ft = datetime.fromtimestamp((da.timestamp() + db.timestamp()) / 2)
print(ft)
finaltimes.append(ft)
Here's the core code using your example times, run in interactive mode, with the inputs already done.
>>> ft = datetime.fromtimestamp((da.timestamp() + db.timestamp()) / 2)
>>> print(ft)
2019-07-23 00:02:52.500000
>>> print(ft.strftime("%H:%M:%S.%f"))
00:02:52.500000
>>> print(ft.strftime("%H:%M:%S.%f")[:11])
00:02:52.50