. Advertisement .
..3..
. Advertisement .
..4..
I’m trying to run a new project. I do a couple of things like this:
NOW() AT TIME ZONE 'UTC'
But in my program, I am getting the warning:
TypeError: can't subtract offset-naive and offset-aware datetimes
Can someone explain why the “can’t subtract offset-naive and offset-aware datetimes” issue happened? Where have I gone wrong? Thank you!
The cause: Other timezones are frequently confused with each other (GMT, Greenwich, Universal, etc.). The definition of UTC differs from those of the other time zones, and they are not interchangeable. As a result, they won’t be able to compare the two in Python. Incompatibility is the cause of the problem
Solution: Remove the timezone awareness and replace it with time zone conversion to prevent the
error “can’t subtract offset-naive and offset-aware datetimes”.
The best solution is to Add the Timezone Info, e.g. to get the current hour as an aware Datetime object in Python 3
You can define
utc
.tzinfo objects in older Python versions (example: datetime docs).then: