Search code examples
pythonpython-3.xposixutcpython-datetime

Datetime's timestamp function not matching expected UNIX time


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?


Solution

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

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

    1563667200