noob_23
noob_23

Reputation: 137

How to resolve Errno 22 in datetime

receiving at Errno 22 Invalida argument error when passing in normal integer timestammp

I have tried adding a normal integer timestamp

final_change_int = dictionary["timestamp"]
print(final_change_int)
datetime.fromtimestamp(final_change_int)

Result

1551766560457
---------------------------------------------------------------------------
OSError                                   Traceback (most recent call last)
<ipython-input-63-c6188e2f6967> in <module>
      1 final_change_int = dictionary["timestamp"]
      2 print(final_change_int)
----> 3 datetime.fromtimestamp(final_change_int)

OSError: [Errno 22] Invalid argument

Upvotes: 1

Views: 657

Answers (1)

Guillaume Lastecoueres
Guillaume Lastecoueres

Reputation: 215

I hope you are doing great,

Unfortunately in this case it seems the value of the timestamp is wrong.

The date indicated by your timestamp is 12/7/51143 at 2:47:37. Based on the operating system it will not support such a date.

The best you can do in this case is to wrap the code in a try...except

And do what is needed in case of wrong timestamp.

N.B: In your case it seems that the timestamp must be divided by 1000, before to reach a correct date.

In [3]: datetime.fromtimestamp(1551766560457/1000)                                                                                                                                                                                                                             
Out[3]: datetime.datetime(2019, 3, 5, 7, 16, 0, 457000)

Have a lovely day, My best regards.

G

Upvotes: 1

Related Questions