When an application is used in several countries, "10:00" does not mean the same to everyone. Time zones are the most common source of errors with dates, and it is worth knowing the basic principles.
"Naive" and "aware" dates
A datetime without time zone information is called naive: it does not know what place in the world it is for. One with a zone (*aware*) does. With naive ones, all the operations in this lesson work; problems appear when mixing both types.
from datetime import datetime, timezone
naive = datetime(2026, 3, 15, 12, 0)
aware = datetime(2026, 3, 15, 12, 0, tzinfo=timezone.utc)
print(naive.tzinfo) # None
print(aware.tzinfo) # UTC
print(aware) # 2026-03-15 12:00:00+00:00
try:
naive < aware
except TypeError as e:
print(e) # can't compare offset-naive and offset-aware datetimes
UTC and offsets
UTC is the world reference time. Any zone is described as an offset from UTC: Spain in winter is UTC+1 and in summer UTC+2. timezone(timedelta(hours=...)) creates a zone with a fixed offset.
from datetime import datetime, timedelta, timezone
utc = datetime(2026, 3, 15, 12, 0, tzinfo=timezone.utc)
madrid_winter = timezone(timedelta(hours=1))
new_york = timezone(timedelta(hours=-5))
print(utc.astimezone(madrid_winter)) # 2026-03-15 13:00:00+01:00
print(utc.astimezone(new_york)) # 2026-03-15 07:00:00-05:00
print(utc.astimezone(madrid_winter) == utc) # True (it is the SAME instant)
Look at the last result: 13:00 in Madrid and 12:00 UTC are the same instant; only the way it is written changes. Comparing and subtracting aware dates works correctly.
zoneinfo: zones with daylight saving time
A fixed offset knows nothing about clock changes (daylight saving time). For that there is the zoneinfo module, which uses the system's time zone database ("Europe/Madrid", "America/New_York"...). On some systems you must install the tzdata package for it to work. Its use is:
# from zoneinfo import ZoneInfo
# from datetime import datetime
#
# m = datetime(2026, 7, 1, 12, 0, tzinfo=ZoneInfo("Europe/Madrid"))
# print(m.utcoffset()) # 2:00:00 (summer)
# print(datetime(2026, 1, 1, 12, 0, tzinfo=ZoneInfo("Europe/Madrid")).utcoffset()) # 1:00:00
Golden rules
- Always store in UTC (or ISO with zone) and convert to local time only when displaying.
- Use ISO 8601 to store and transmit dates.
- Do not subtract or compare aware and naive dates: decide whether your program works with naive or aware ones and be consistent.
- Do not rely on
datetime.now()in tests: its value changes. Pass it as a parameter to your functions so you can test them with fixed dates. - Beware of 29 February and month ends: "one month after 31 January" does not exist, you must decide a rule.
- Do not add "30 days" when you mean "one month".
from datetime import date
def is_weekend(day):
return day.weekday() >= 5
# The function does NOT call date.today(): it receives the date. That way it can always be tested the same.
assert is_weekend(date(2026, 11, 14)) is True
assert is_weekend(date(2026, 11, 13)) is False
print("Tests passed") # Tests passed
Timestamps (epoch)
Many systems store time as the seconds elapsed since 1 January 1970 (UTC). It is called a *timestamp* or *epoch*.
from datetime import datetime, timezone
m = datetime(2026, 3, 15, 12, 0, tzinfo=timezone.utc)
ts = m.timestamp()
print(ts) # 1773576000.0
print(datetime.fromtimestamp(ts, tz=timezone.utc)) # 2026-03-15 12:00:00+00:00
With this you have seen the essentials for working with dates in Python: creating them, operating with them, converting them to and from text, using the calendar and being careful with zones. In the exercises the results depend only on the input data, so they are always the same.