Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / Python

Dates, instants and durations

Represent calendar dates separately from instants with a UTC offset and durations with timedelta.

Version: Python 3.14; repository image, production runtime not attested

Syntax

end.astimezone(UTC) - start.astimezone(UTC)

Represent calendar dates separately from instants with a UTC offset and durations with timedelta.

Normalize aware timestamps to UTC when calculating elapsed time across different offsets. Removing the offsets and subtracting wall-clock readings can give a different answer.

Use a monotonic clock to measure the duration of running code without relying on calendar-clock adjustments.

Examples

from datetime import UTC, datetime
from time import monotonic
start = datetime.fromisoformat("2026-03-29T01:30:00+01:00")
end = datetime.fromisoformat("2026-03-29T03:30:00+02:00")
print(end.astimezone(UTC) - start.astimezone(UTC))
print(end.replace(tzinfo=None) - start.replace(tzinfo=None))
tick = monotonic()
print(monotonic() >= tick)

Result

1:00:00
2:00:00
True

Use a monotonic clock to measure the duration of running code without relying on calendar-clock adjustments.

Sources