I am interfacing with an API which gives year-naive RFC 3339 datetime strings for representing a users birthday. Naturally, I want to interpret this as some sort of datetime object - However, the python datetime library doesn't support datetime strings with values less than one.
Here's an example datetime string given by the API: 0000-09-01T00:00:00-00:00 (Notice the year is set to 0000). If I were to just throw this into datetime.fromisoformat, it unsuprisingly raises an error:
In [1]: from datetime import datetime
In [2]: datetime.fromisoformat("0000-09-01T00:00:00-00:00")
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-2-e1d8a5624d92> in <module>
----> 1 datetime.fromisoformat("0000-09-01T00:00:00-00:00")
ValueError: year 0 is out of range
If I were to entirely remove the year section of the string, It gives the following:
In [1]: from datetime import datetime
In [2]: datetime.fromisoformat("09-01T00:00:00-00:00")
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-2-a027335f00c1> in <module>
----> 1 datetime.fromisoformat("09-01T00:00:00-00:00")
ValueError: Invalid isoformat string: '09-01T00:00:00-00:00'
At first, I thought this was a bug or limitation. But after a little research, I found that the RFC3339 Standard states the following in its introduction:
All dates and times are assumed to be in the "current era", somewhere between 0000AD and 9999AD.
Assuming that this range is inclusive (This is based on the other uses of the term "between" within the standard, although it is never strictly specified), it is implied that the datetime module does not conform to the RFC3339 standard as it hard codes a minimum and maximum year value and also makes it a required value. However, it never claims that it does conform to the standard. So the new issue is that if the included library doesn't support RFC3339, what does?
My question is: Is there a method of interpreting this string as some kind of datetime object or use a third-party library?
datetimelibrary.replace('0000', '2996')? hoping this code won't be still in use in a few hundred years...datetime.fromisoformat()isn't. But it doesn't claim to be.