Sub topic 3
Naive and aware datetimes
This chapter assumes you can create datetime objects and use timedelta, and that you know what UTC and an offset such as +02:00 are. Time zone names come in a later chapter. Here the zones are UTC and fixed offsets.
Two kinds of datetime
Every datetime is either naive or aware.
- A naive datetime has no time zone information. It records a date and a clock reading, and nothing else.
- An aware datetime carries a time zone, so it identifies one exact moment in time.
The difference is the tzinfo attribute. Python treats a datetime as aware only if tzinfo is set and utcoffset() returns a value.
from datetime import datetime, timezone
naive = datetime(2026, 1, 1)
aware = datetime(2026, 1, 1, tzinfo=timezone.utc)
print(naive.tzinfo)
print(aware.tzinfo)
print(naive.utcoffset())
print(aware.utcoffset())This prints None, UTC, None and 0:00:00. A naive value answers "no information" to both questions.
Naive datetimes are not wrong. They suit values that really are local and abstract, such as "the shop opens at 09:00". They become a problem when you treat them as moments in time without saying which zone they belong to.
Python refuses to guess
Ordering comparisons and subtraction between a naive and an aware datetime raise TypeError. Equality comparison does not raise. It returns False.
from datetime import datetime, timezone
naive = datetime(2026, 1, 1)
aware = datetime(2026, 1, 1, tzinfo=timezone.utc)
try:
print(naive < aware)
except TypeError as e:
print(e)
print(naive == aware)The output is can't compare offset-naive and offset-aware datetimes, then False.
Python cannot know which zone the naive value was meant to be in, so it stops you instead of picking one. The silent False from == is the more dangerous case. A check like if stored == incoming can fail without any error when one side lost its zone.
Making a naive value aware
There are two different operations, and they do different things.
replace(tzinfo=...)keeps the clock reading and attaches a zone. You are stating a fact: "this reading was in this zone".astimezone(tz)keeps the moment and changes the zone. The clock reading changes to match.
from datetime import datetime, timedelta, timezone
naive = datetime(2026, 1, 1, 9, 0)
as_utc = naive.replace(tzinfo=timezone.utc)
plus2 = timezone(timedelta(hours=2))
print(as_utc)
print(as_utc.astimezone(plus2))This prints 2026-01-01 09:00:00+00:00 and 2026-01-01 11:00:00+02:00. Both lines are the same moment.
Use replace only when you know what zone the naive value was in. Using it with the wrong zone produces an aware value that is wrong by several hours, and nothing warns you.
Calling astimezone() on a naive datetime is also allowed. Python then assumes the value is in the machine's local zone. The result depends on the computer running the code, so a script can behave differently on a laptop and on a server set to UTC. Avoid it unless local machine time is exactly what you mean.
Common errors
- Mixing sources.
datetime.now()returns a naive local value, while a parsed timestamp such as2026-11-26T11:07:59+00:00is aware. Subtracting one from the other raisesTypeError. - Using
replaceto convert.dt.replace(tzinfo=other_zone)does not move the moment to another zone. It relabels the same clock reading. - Stripping the zone to make an error go away. Calling
replace(tzinfo=None)hides theTypeErrorand discards the information that made the value unambiguous. - Assuming
utcnow()is aware.datetime.utcnow()returns a naive datetime that happens to be in UTC. It is deprecated since Python 3.12. Usedatetime.now(timezone.utc)instead.
A good habit is to make values aware at the edge of your program, where data enters. Inside the program, compare and subtract only aware values. The final chapter turns this into a full set of rules.
Exercise
A function receives a = datetime(2026, 6, 1, 12, 0) with no zone, and b = datetime(2026, 6, 1, 12, 0, tzinfo=timezone.utc). You know a was recorded in UTC. Which expression tells you whether they are the same moment, and what does it return?
Show answer
a.replace(tzinfo=timezone.utc) == b returns True. You attach the known zone with replace and compare two aware values. Writing a == b directly would return False, because naive and aware values are never equal, and a < b would raise TypeError.