Some time ago, I had to write a small script in python that, among others, parsed ISO8601-formatted date string and compared it with the current datetime.
As I do not code in Python, I was looking around for a solution. To my big disappointment, all suggested solutions that I found suggested using third-party libraries to accomplish the job. Essentially, I ended up doing this (simplified for brevity):
import iso8601
import pytz
utc = pytz.UTC()
event_date = iso8601.parse_date('2002-10-02T15:00:00.05Z')
now = utc.localize(datetime.utcnow())
# Only now I was able to compare the dates
if event_date - timedelta(hours=3) < now:
<do something>
I am not comfortable with the fact that I had to import two third-party libraries for such common task.
How can I accomplish the above with only what's only available in the Python standard lib and avoid errors like:
- can't compare offset-naive and offset-aware datetimes
- cannot compare tz-naive and tz-aware datetime-like objects