Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TimeStamp to date Python

I am trying to convert a timestamp (that I don't know how it is convert) to datetime.

I have this input: 1217099350.0

If I write this on Libreoffice calc (1217099350.0/86400) + 29226 and format the cell to date time. I have a correct output:

31/07/2018 19:09:10

But if I make this on python:

tt = 1217099350.0
tt2 = (tt / 86400.) + 29226.
tt3 = datetime.fromtimestamp(tt2).strftime("%Y-%M-%d %H:%m:%S"
print(tt3)

I have the next output:

1970-01-01 09:01:52

What is the problem on my code?

Thanks! Regards!

like image 219
Emmanuel Arias Avatar asked Feb 11 '26 13:02

Emmanuel Arias


1 Answers

It appears that LibreOffice epoch is not the same as the Posix epoch. I found this article that might be helpful.

https://syslog.me/2012/08/28/dates-from-unix-timestamps-in-openoffice-libreoffice/

The Posix epoch is midnight Jan. 1, 1970 UTC.

>>> datetime.utcfromtimestamp(0).strftime("%Y-%m-%d %H:%M:%S")
'1970-01-01 00:00:00'

The LibreOffice epoch is Dec. 30, 1899.

The divide by 86400 suggests that you are trying to convert seconds to days. However, the datetime.fromtimestamp function in Python expects a timestamp in seconds.

Also, in your call to strftime you reversed months and minutes. %M gives minutes and %m gives months.

Finally, you may want to use utcfromtimestamp instead of fromttimestamp to avoid time-zone issues.

like image 94
Jeffrey Harper Avatar answered Feb 13 '26 03:02

Jeffrey Harper



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!