Featurecore
Naive datetimes
Also called naive datetime, aware datetime, datetime.utcnow, can't compare offset-naive and offset-aware datetimes, timezone.utc, zoneinfo, DTZ001, DTZ003, DTZ005.
A datetime with no tzinfo is naive: it does not say which zone it is in, and many methods treat it as local time. Use aware values, such as datetime.now(timezone.utc), and zoneinfo for local zones.
- Language
- Python
Synopsis
This code raises TypeError, on purpose.
from datetime import datetime, timezone
deadline = datetime(2026, 11, 18, 12, 0)
now = datetime(2026, 11, 18, 11, 0, tzinfo=timezone.utc)
print(deadline == now)
print(now < deadline)
The first datetime is naive and the second is aware. The == test gives False, and the < test raises TypeError, because Python cannot know which zone the naive value is in.
Semantics
r1. "Date and time objects may be categorized as “aware” or “naive” depending on whether or not they include time zone information."1 "A naive object does not contain enough information to unambiguously locate itself relative to other date/time objects. Whether a naive object represents Coordinated Universal Time (UTC), local time, or time in some other time zone is purely up to the program".1
r2. Naive and aware values do not mix: "Naive and aware datetime objects are never equal." and "Order comparison between naive and aware datetime objects raises TypeError."1 Example: mixed-compare.
r3. Many methods read a naive value as local time. For timestamp, "Naive datetime instances are assumed to represent local time".1 So a naive value that holds UTC gives a wrong instant on a computer whose zone is not UTC. Example: utcnow-timestamp.
r4. The documentation gives the fix: "Because naive datetime objects are treated by many datetime methods as local times, it is preferred to use aware datetimes to represent times in UTC. As such, the recommended way to create an object representing the current time in UTC is by calling datetime.now(timezone.utc)."1 Example: aware-now.
r5. For a named zone, use zoneinfo: "The zoneinfo module provides a concrete time zone implementation to support the IANA time zone database".2 Store and compute in UTC, and convert to a local zone for display. Example: zoneinfo-dst.
r6. Arithmetic in one zone ignores offset changes. "If both are naive, or both are aware and have the same tzinfo attribute, the tzinfo attributes are ignored, and the result is a timedelta object t such that datetime2 + t == datetime1. No time zone adjustments are done in this case."1 So a day in a local zone can be 23 or 25 hours long, and the subtraction still says one day. Example: same-zone-math.
r7. The linters:
- Ruff DTZ005 "Checks for usages of datetime.datetime.now() that do not specify a timezone."3
- Ruff DTZ003 "Checks for usage of datetime.datetime.utcnow()."4 Its reason: "datetime.datetime.utcnow() returns a naive datetime object; instead, use datetime.datetime.now(tz=...) to create a timezone-aware object."4
- Ruff DTZ001 "Checks for datetime instantiations that do not specify a timezone."5
Examples
from datetime import UTC, datetime, timezone
print(datetime.now().tzinfo)
print(datetime.now(timezone.utc).tzinfo)
print(UTC is timezone.utc)
stamp = datetime(2026, 11, 18, 9, 30, tzinfo=UTC)
print(stamp.isoformat())
None
UTC
True
2026-11-18T09:30:00+00:00
datetime.now() with no argument is naive. With a zone, it is aware, and isoformat writes the offset, so the text is clear to any reader (rule r4).
from datetime import UTC, datetime, timedelta
from zoneinfo import ZoneInfo
new_york = ZoneInfo("America/New_York")
start = datetime(2026, 3, 8, 6, 30, tzinfo=UTC)
for hours in [0, 1]:
moment = start + timedelta(hours=hours)
print(moment.astimezone(new_york))
2026-03-08 01:30:00-05:00
2026-03-08 03:30:00-04:00
One hour later in UTC is two hours later on the clock in New York, because daylight saving time starts in between. zoneinfo knows the rules of the zone (rule r5).
Versions
Since 3.11
is an "Alias for the UTC time zone singleton datetime.timezone.utc."1
Since 3.12
"datetime.datetime’s utcnow() and utcfromtimestamp() are deprecated and will be removed in a future version. Instead, use timezone-aware objects to represent datetimes in UTC: respectively, call now() and fromtimestamp() with the tz parameter set to datetime.UTC."6 Example:
utcnow-warns.
Pitfalls
This code runs but gives a wrong answer on purpose.
import subprocess
import sys
code = (
"import time, warnings\n"
"from datetime import datetime, timezone\n"
"warnings.simplefilter('ignore')\n"
"naive = datetime.utcnow().timestamp() - time.time()\n"
"aware = datetime.now(timezone.utc).timestamp() - time.time()\n"
"print(round(naive / 3600), round(aware / 3600))\n"
)
result = subprocess.run(
[sys.executable, "-c", code],
env={"TZ": "Asia/Ho_Chi_Minh"}, capture_output=True, text=True,
)
print(result.stdout.strip())
-7 0
In a zone at UTC+7, the naive value from utcnow was read as local time, so its timestamp is 7 hours off (rule r3). The aware value gives the right instant.
This code runs but gives a wrong answer on purpose.
from datetime import UTC, datetime
from zoneinfo import ZoneInfo
new_york = ZoneInfo("America/New_York")
start = datetime(2026, 3, 7, 12, 0, tzinfo=new_york)
end = datetime(2026, 3, 8, 12, 0, tzinfo=new_york)
print(end - start)
print(end.astimezone(UTC) - start.astimezone(UTC))
1 day, 0:00:00
23:00:00
Both values have the same tzinfo, so the first subtraction ignores the change of offset (rule r6). The time that really passed is 23 hours. Subtract in UTC to get it.
import warnings
from datetime import datetime
with warnings.catch_warnings(record=True) as caught:
warnings.simplefilter("always")
datetime.utcnow()
print(caught[0].category.__name__)
DeprecationWarning
From Python 3.12, utcnow emits a DeprecationWarning.6
Comparison
In Python, one datetime type holds both naive and aware values, and the type does not show which kind a value is (rule r1). So the bug shows only at run time, in a comparison or a conversion. Make all values aware at the edges of the program, where data comes in. For dates and times in general, see Dates and Times. For clocks and timestamps, see Time and clocks.
References
- datetime, Basic date and time types, Python 3.15 documentation, Aware and naive objects; datetime.utcnow(); datetime.timestamp(); Supported operations
- zoneinfo, IANA time zone support, Python 3.15 documentation, Introduction
- call-datetime-now-without-tzinfo (DTZ005), Ruff documentation, What it does; Why is this bad?
- call-datetime-utcnow (DTZ003), Ruff documentation, What it does; Why is this bad?
- call-datetime-without-tzinfo (DTZ001), Ruff documentation, What it does
- What’s New In Python 3.12, Deprecated, datetime
About this page
The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.
| Ladder | Level | What the next level needs |
|---|---|---|
| Human proof | H0 no proof |
|
| Lean | L0 no Lean part |
|
| Visual | V0 none |
|
| Writing | W3 standard |
|
Tag W7D4W0 Edit this page History Report an error Markdown