How to find average time with datetime?

I'm trying to find the average to a list that stores my datetime times.

I've tried getting the sum of the list except that doesn't work since it is a datetime module.

finaltimes = []
for i in range(len(times)):
    try:
        a = times[i]
        b = prior_times[i]

        da = datetime.datetime.strptime(a, '%H:%M:%S')
        db = datetime.datetime.strptime(b, '%H:%M:%S')

        print(db - da)
        finaltimes.append(db - da)

I'm expecting the output to be something along the lines of 00:01:34. Any help?

Upvotes: 1

Views: 4949

Answers (3)

Deepstop
Deepstop

Reputation: 3807

Another method as follows. This time, if you have just HH:MM:SS format, you can just use arithmetic on the strings converted to seconds. The final times in this example are strings. You can convert them to something else, like datetime or time objects, depending on what you require. Or just leave it as the number of seconds 'ts, etc.

from operator import mul
finaltimes = []
for i in range(len(times)):
    a = times[i]
    b = prior_times[i]

    as = sum(map(operator.mul, (int(x) for x in a.split(":")), (3600, 60, 1)))
    bs = sum(map(operator.mul, (int(x) for x in b.split(":")), (3600, 60, 1)))
    ts = (as + bs) // 2

    finaltimes.append(f"{ts // 3600:02d}:{(ts % 3600) // 60:02d}:{ts % 60:02d}")

Upvotes: 0

Deepstop
Deepstop

Reputation: 3807

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

Upvotes: 0

Sushant
Sushant

Reputation: 3669

One way to do this would be to convert datetime objects to utc, average them out, covert the final utc back to datetime object like so -

import datetime
datetime.datetime.fromtimestamp((datetime.datetime(2012,4,1,0,0).timestamp() + datetime.datetime(2013,4,1,0,0).timestamp()) / 2 )
# OP datetime.datetime(2012, 9, 30, 11, 0)

Upvotes: 1

Related Questions