A More Time Zone Tolerant datetime Class
Yesterday I signed in to the billtracker, and got an error page when trying to display my bill list:
[ ... ]
File "/var/www/nmbilltracker/billtracker/app/models.py", line 766, in location_html
if self.last_action_date > self.scheduled_date.replace(tzinfo=None):
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: can't compare offset-naive and offset-aware datetimes
Python's datetime class drives me crazy.
Any given datetime object might or might not have a timezone.
Those that do are called "timezone aware" or just "aware" datetimes;
those without a timezone are called "unaware" or "naive".
Any given function might or might not return a timezone-aware datetime.
If you ever mess up and call a function that returns a timezone when you
didn't expect one, or vice versa, or if a function you call changes in
that respect, now you have a hidden time bomb that will crash your
program the next time you do any sort of comparison with or
subtraction from another datetime, and by then, you may have no idea
way of finding out where the problematic time came from so you can
guard against it happening again.
[ 18:53 Feb 21, 2026 More programming | permalink to this entry | ]