3

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!

2
  • Looks right to me. (1217099350 / 86400) + 29226 is about 43312, which is approximately the number of seconds in a twelve hour span, so it makes sense that the resulting datetime is about twelve hours after the Unix epoch. If anything, it's Libreoffice that's wrong. Commented Aug 2, 2018 at 18:04
  • I don't know because on Excel I have the same result. In the company where I am working, are using Excel to do this conversion, and I want to automatize the task. Commented Aug 2, 2018 at 18:20

2 Answers 2

7

Emmanuel,

Changing the Epoch in Python would likely be a lot of work. It might be easier to do some math on your LibreOffice timestamps to convert them to Posix timestamps before calling datetime.utfromtimestamp.

This won't work, however, if you use timestamps before Jan. 1, 1970.

from datetime import datetime
tt = 1217099350.0
tt2 = (tt / 86400.) + 29226.
# At this point tt2 has days since the LibreOffice Epoch.  Below, it
# is converted to seconds since Posix epoch.
tt3 = tt2 - 25569.  # There are 25569 days between LibreOffice and Posix epoch
tt4 = tt3 * 86400.  # Convert timestamp from days to seconds
tt5 = datetime.utcfromtimestamp(tt4).strftime("%Y-%m-%d %H:%M:%S")
print(tt5)

2018-07-31 19:09:10
Sign up to request clarification or add additional context in comments.

Comments

3

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.

1 Comment

Great!! How I could change the epoch day on Python's datetime module?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.