Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase datetime convert error on python

Can someone explain how i can calculate the time in "%Y%m%d%H%M%S%f" from my Firebase timestamp? I am receiving an error

signed integer is greater than maximum

when i do the following:

from datetime import datetime
def formatdatetime(sfd):
  ts = datetime(sfd)
  print(datetime.strftime("%Y%m%d%H%M%S%f", ts))

formatdatetime(1521508504583)

I'm guessing that I'm not taking in the milliseconds correctly?

like image 975
ben Avatar asked Aug 30 '26 08:08

ben


1 Answers

First, you need to use datetime.datetime.fromtimestamp or datetime.datetime.utcfromtimestamp to parse the timestamp.

Second, I believe the timestamp you shared is in milliseconds rather than seconds, so you should divide it by 1000.

Example:

>>> dt = 1521508504583

>>> datetime.datetime.fromtimestamp(dt / 1000)
datetime.datetime(2018, 3, 19, 18, 15, 4)

>>> datetime.datetime.utcfromtimestamp(dt / 1000)
datetime.datetime(2018, 3, 20, 1, 15, 4)
like image 180
erichiggins Avatar answered Aug 31 '26 21:08

erichiggins



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!