datetime.timestamp()
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
:
timestamp = dt.replace(tzinfo=timezone.utc).timestamp()
or by calculating the timestamp directly:
timestamp = (dt - datetime(1970, 1, 1)) / timedelta(seconds=1)
Applied to your code:
from datetime import datetime, timezone, timedelta
date = datetime(1969, 1, 1)
value = -31539600.0 # value from the question
# my workaround
stamp0 = (date - datetime(1970, 1, 1)).total_seconds()
print('stamp0', stamp0, stamp0 - value)
# from the docs
stamp1 = date.replace(tzinfo=timezone.utc).timestamp()
print('stamp1', stamp1, stamp1 - value)
# from the docs (alternative)
stamp2 = (date - datetime(1970, 1, 1)) / timedelta(seconds=1)
print('stamp2', stamp2, stamp2 - value)
# compensate the one-hour difference (example)
stampx = (date - datetime(1970, 1, 1, 1)) / timedelta(seconds=1)
print('stampx', stampx, stampx - value)
Output: .\SO\72660402.py
stamp0 -31536000.0 3600.0
stamp1 -31536000.0 3600.0
stamp2 -31536000.0 3600.0
stampx -31539600.0 0.0