0

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
Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
Yas
  • 68
  • 2
  • 8
  • I am getting `ValueError: year 52871 is out of range` from the same line... – Tomerikoo Nov 25 '20 at 10:15
  • It looks like the input is too large, see: https://stackoverflow.com/questions/46133223/maximum-value-of-timestamp#:~:text=ctime(seconds)%20%2C%20seconds%20parameter,almost%20equals%20to%202%5E34.92135%20. – big_bad_bison Nov 25 '20 at 10:18
  • maybe you could try removing the slash 60 calculation and if larger calculation to reduce the complexity.where does webdriver come from ? – user3732793 Nov 25 '20 at 10:30

1 Answers1

1

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.

robinood
  • 1,138
  • 8
  • 16