0

The following

import datetime as dt

dt.datetime(2019, 7, 21).timestamp()

returns

1563663600.0 .

However, if I look on https://www.unixtimeconverter.io/list/2019/july, then I see that the expected Unix time for this date is 1563667200.

Why the discrepancy?

2
  • 3
    what's your local timezone? 1563667200 is the UTC timestamp. Commented Jul 22, 2019 at 14:37
  • I was looking for the UTC timestamp. I didn't specify any local timezone when running .timestamp() Commented Jul 22, 2019 at 14:39

1 Answer 1

2

datetime doesn't assume UTC by default, so you need to specify it.

dt.datetime(2019, 7, 21, tzinfo=dt.timezone.utc).timestamp()

1563667200

Sign up to request clarification or add additional context in comments.

1 Comment

This behavior/solution is specifically documented for the timestamp method too: "Note: There is no method to obtain the POSIX timestamp directly from a naive datetime instance representing UTC time. If your application uses this convention and your system timezone is not set to UTC, you can obtain the POSIX timestamp by supplying tzinfo=timezone.utc" (followed by code sample)

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.