Reputation: 2601
I am working with an application that is storing a time in the format of "1619714600948", which is 13 characters long. I believe this to be an epoch date. Using the site https://www.epochconverter.com/ confirms this. I can enter the 13 character date in and get out:
When I try to create an epoch date in Python3, I end up with 1619703857, which is only 10 characters long. This causes problems when I am trying to compare the date that is stored vs. the date that I am generating. Why am I not getting the expected 13 character epoch date? Below is some of the things I have tried based on searching Stackoverflow
date_time2 = '04.29.2021' + " " + "13:44:17.000000"
pattern2 = '%m.%d.%Y %H:%M:%S.%f'
# get a time structure
tm = time.strptime(date_time2, pattern2)
# convert to gmt
gmt_epoch = int(calendar.timegm(tm))
print(gmt_epoch) #10 digit result
date_string = calendar.timegm(time.strptime(str(date.today()), '%Y-%m-%d'))
print(date_string) #10 digit result
Trying to convert from this gives me dates that have a 5 digit year.
print(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(1619714600948))) #Bogus date of 53296-09-16 11:29:08
print(time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime(1619714600948))) # Bogus date of 53296-09-16 18:29:08
What is causing this discrepancy and how can I fix it?
Upvotes: 0
Views: 530
Reputation: 169267
It's a UNIX time in milliseconds (common in JavaScript). Divide by 1000.
>>> import datetime
>>> datetime.datetime.fromtimestamp(1619714600948 / 1000.)
datetime.datetime(2021, 4, 29, 19, 43, 20, 948000)
Similarly, multiply by 1000 to get a millisecond timestamp:
>>> datetime.datetime.now().timestamp() * 1000
1619718337164.896
Upvotes: 1