CKTIS / Python
datetime.datetime
Combines calendar fields with time-of-day fields and an optional time zone.
Version: Python 3.14; repository image, production runtime not attested
Syntax
datetime.datetime(year, month, day, hour=0, minute=0, second=0, microsecond=0, tzinfo=None, *, fold=0)Combines calendar fields with time-of-day fields and an optional time zone.
Converting an aware timestamp across a fixed offset may change its calendar date while preserving the instant.
A datetime combines date and wall-clock fields at microsecond resolution. It is aware when tzinfo supplies a non-None UTC offset. astimezone on an aware value changes the displayed clock while keeping the instant; replace(tzinfo=...) only relabels the fields. Here 01:00 UTC on January 1 becomes 22:00 the previous day at UTC−03:00. When New York's clock repeated 01:30 on 2024-11-03, fold=0 meant 05:30 UTC and fold=1 meant 06:30 UTC. Subtracting with the same tzinfo object ignores fold and yields zero; converting both to UTC first gives an hour.
from datetime import datetime, timedelta, timezone
stamp = datetime(2025, 1, 1, 1, tzinfo=timezone.utc)
zone = timezone(timedelta(hours=-3))
local = stamp.astimezone(zone)
assert local.isoformat() == "2024-12-31T22:00:00-03:00" and local == stamp
assert stamp.replace(tzinfo=zone) != stamp
from datetime import tzinfo
class RepeatedHour(tzinfo):
def utcoffset(self, dt): return timedelta(hours=-4-dt.fold)
def dst(self, dt): return timedelta(hours=1-dt.fold)
z = RepeatedHour()
a = datetime(2024,11,3,1,30,tzinfo=z)
b = a.replace(fold=1)
assert b-a == timedelta(0)
assert (a.astimezone(timezone.utc).hour, b.astimezone(timezone.utc).hour) == (5,6)
assert b.astimezone(timezone.utc)-a.astimezone(timezone.utc) == timedelta(hours=1)
datetime.datetime.strptime · datetime.datetime.date · datetime.datetime.combine · datetime.datetime.replace · datetime.datetime.strftime · datetime.datetime.isoformat
strptime(text, fmt) parses fields by format, and date() extracts the date. combine(date, time) joins them using the time's tzinfo by default. replace returns a new value with selected fields changed, without converting zones, and rejects an invalid date. strftime(fmt) renders directives, while isoformat() emits ISO text with the offset of this aware datetime.
from datetime import datetime,time,timezone
d=datetime.strptime('2024-02-29','%Y-%m-%d')
t=time(8,30,tzinfo=timezone.utc)
combined=datetime.combine(d.date(),t)
later=combined.replace(hour=9)
assert combined.hour==8 and later.strftime('%Y/%m/%d %H:%M')=='2024/02/29 09:30'
assert later.isoformat()=='2024-02-29T09:30:00+00:00'
try: d.replace(year=2023)
except ValueError: pass
else: raise AssertionError
Examples
import datetime
utc = datetime.timezone.utc
t = datetime.datetime(2025, 1, 1, 1, tzinfo=utc)
local = t.astimezone(datetime.timezone(datetime.timedelta(hours=-3)))
print(local.isoformat())
print(local == t)
Result
2024-12-31T22:00:00-03:00 True
A datetime combines date and wall-clock fields at microsecond resolution. It is aware when tzinfo supplies a non-None UTC offset. astimezone on an aware value changes the displayed clock while keeping the instant; replace(tzinfo=...) only relabels the fields. Here 01:00 UTC on January 1 becomes 22:00 the previous day at UTC−03:00. When New York's clock repeated 01:30 on 2024-11-03, fold=0 meant 05:30 UTC and fold=1 meant 06:30 UTC. Subtracting with the same tzinfo object ignores fold and yields zero; converting both to UTC first gives an hour.