Your code works if you make the datetime object aware, i.e. set a time zone:
from datetime import datetime, timezone
date_string = "16:30"
date = datetime.strptime(date_string, "%H:%M").replace(tzinfo=timezone.utc)
print(date)
# 1900-01-01 16:30:00+00:00
print(date.timestamp())
# -2208929400.0
(Tested on Windows 10, Python 3.9.12)
A bit of background, from the docs:
datetime.timestamp() ... Naive datetime instances are assumed to
represent local time and this method relies on the platform C mktime()
function to perform the conversion.
So, if you call the .timestamp()
method on a naive datetime object, local time (i.e. the UTC offset at given date/time) must be determined. If your platform's mktime
doesn't support this for a given Unix time, you see OSError: [Errno 22]
. In contrast, if you set the time zone (i.e. specify the UTC offset), that call to mktime is not necessary and the error is avoided.