Yas
Yas

Reputation: 68

Having trouble trying to get the duration between 2 timestamps

I'm trying to get the difference between two time-stamps: 1606294772889 and 1606295867656. But I keep getting OSError: [Errno 22] Invalid argument

Here is my code:

from datetime import datetime

def get_duration(start , end):
    fmt = '%Y-%m-%d %H:%M:%S'
    start = datetime.utcfromtimestamp(start).strftime(fmt)
    end = datetime.utcfromtimestamp(end).strftime(fmt)
    tstamp1 = datetime.strptime(start, fmt)
    tstamp2 = datetime.strptime(end, fmt)

    if tstamp1 > tstamp2:
        td = tstamp1 - tstamp2
    else:
        td = tstamp2 - tstamp1
    td_mins = int(round(td.total_seconds() / 60))

    print('The difference is approx. %s minutes' % td_mins)

get_duration(start = 1606294772889 , end = 1606295867656)

Traceback:

Traceback (most recent call last):
  File "c:/Users/Yas_!_ru/Documents/GitHub/Mindustry-Ranked/webdriver.py", line 220, in <module>    
    Match.get_duration(start = 1606294772889 , end = 1606295867656)
  File "c:/Users/Yas_!_ru/Documents/GitHub/Mindustry-Ranked/webdriver.py", line 207, in get_duration
    start = datetime.utcfromtimestamp(start).strftime(fmt)
OSError: [Errno 22] Invalid argument

Upvotes: 0

Views: 47

Answers (1)

robinood
robinood

Reputation: 1248

The problem here is that your timestamps seems to be in some subdivision of a second. Probably milliseconds.

When I tried your code I had the same error as Tomerikoo in the comments, I divided the timestamps by 1000 it gave me 182 minutes.

Be sure to check in which format the timestamp are given (miliseconds, tenth of a second etc...), in order to convert them back to seconds to use the datetime functions.

Upvotes: 1

Related Questions