Why is Datetime's `.timestamp()` method returning `OSError: [Errno 22] Invalid argument`?
Question:
I make use of the .timestamp()
function twice in my code, to convert datetime objects into epoch time. The first call to .timestamp()
looks like this:
import datetime
origin_epoch = origin.timestamp()
the contents of the variables origin
and origin_epoch
are:
Meanwhile, if I try to call the same method elsewhere in my code
import datetime
print(datetime.datetime(1900, 1, 1, 19, 6, 28).timestamp())
Then I get the following error: OSError: [Errno 22] Invalid argument
Why is this?
edit: this error occurred on Windows 10.
Answers:
The year 1900 was before the beginning of the UNIX epoch, which was in 1970, so the number of seconds returned by timestamp
must be negative. To be precise, should be negative, but apparently, not in your case. Looks like your OS just treats dates before the beginning of the UNIX epoch as errors.
This works fine for me on macOS, though:
>>> datetime.datetime(1900, 1, 1, 19, 6, 28).timestamp()
-2208929029.0
This appears to be a known issue which has been supposedly fixed, but I haven’t checked. On my Windows (Windows 10, GMT+2) any date before 1970-01-02 02:00:00 or beyond 3001-01-19 07:59:59 will give an OSError
when timestamp()
is called.
However, this does not happen to offset-aware datetime, instead, a call to timestamp()
gets computed as (from the docs):
(dt - datetime(1970, 1, 1, tzinfo=timezone.utc)).total_seconds()
So if you work with offset-naive datetimes, you can simply use:
(dt - datetime(1970, 1, 1)).total_seconds()
I replaced the year value with 2000 and now it works fine.
time_sec = datetime.strptime("00:00:13.345", "%H:%M:%S.%f")
print(time_sec.replace(year=2000).timestamp())
Yes, this is a bug. This code will work on Windows as well:
import datetime
year = 1952
print((datetime.date(year, 1, 1) - datetime.date(1970, 1, 1)).total_seconds())
If you want to be precise, make sure the year 2023 yields the same value as this code:
datetime.datetime(2023, 1, 1).timestamp()
Depending on your timezone add or subtract 3600 times the number of hours that your timezone differs.
I make use of the .timestamp()
function twice in my code, to convert datetime objects into epoch time. The first call to .timestamp()
looks like this:
import datetime
origin_epoch = origin.timestamp()
the contents of the variables origin
and origin_epoch
are:
Meanwhile, if I try to call the same method elsewhere in my code
import datetime
print(datetime.datetime(1900, 1, 1, 19, 6, 28).timestamp())
Then I get the following error: OSError: [Errno 22] Invalid argument
Why is this?
edit: this error occurred on Windows 10.
The year 1900 was before the beginning of the UNIX epoch, which was in 1970, so the number of seconds returned by timestamp
must be negative. To be precise, should be negative, but apparently, not in your case. Looks like your OS just treats dates before the beginning of the UNIX epoch as errors.
This works fine for me on macOS, though:
>>> datetime.datetime(1900, 1, 1, 19, 6, 28).timestamp()
-2208929029.0
This appears to be a known issue which has been supposedly fixed, but I haven’t checked. On my Windows (Windows 10, GMT+2) any date before 1970-01-02 02:00:00 or beyond 3001-01-19 07:59:59 will give an OSError
when timestamp()
is called.
However, this does not happen to offset-aware datetime, instead, a call to timestamp()
gets computed as (from the docs):
(dt - datetime(1970, 1, 1, tzinfo=timezone.utc)).total_seconds()
So if you work with offset-naive datetimes, you can simply use:
(dt - datetime(1970, 1, 1)).total_seconds()
I replaced the year value with 2000 and now it works fine.
time_sec = datetime.strptime("00:00:13.345", "%H:%M:%S.%f")
print(time_sec.replace(year=2000).timestamp())
Yes, this is a bug. This code will work on Windows as well:
import datetime
year = 1952
print((datetime.date(year, 1, 1) - datetime.date(1970, 1, 1)).total_seconds())
If you want to be precise, make sure the year 2023 yields the same value as this code:
datetime.datetime(2023, 1, 1).timestamp()
Depending on your timezone add or subtract 3600 times the number of hours that your timezone differs.