Parse and format ISO 8601 dates in UTC
Convert ISO timestamps to timezone-aware UTC datetimes and back, avoiding naive datetime bugs.
- Python
- GreenSnippets
- MIT
- Executed with Python 3.11
from datetime import datetime, timezone
def parse_utc(text):
value = datetime.fromisoformat(text.replace("Z", "+00:00"))
if value.tzinfo is None:
value = value.replace(tzinfo=timezone.utc)
return value.astimezone(timezone.utc)
def to_iso_utc(value):
return value.astimezone(timezone.utc).isoformat(timespec="seconds").replace("+00:00", "Z")Example / expected output
parse_utc("2025-03-01T12:00:00+02:00") # 2025-03-01 10:00:00+00:00
to_iso_utc(parse_utc("2025-03-01T12:00:00+02:00")) # '2025-03-01T10:00:00Z'How it works
Store and exchange times in UTC and convert only for display. datetime.fromisoformat handles offsets (and Z since Python 3.11). Naive inputs are assumed to be UTC here, which is a decision you should make explicitly for your data.
Related snippets
- Python Tested
Token bucket rate limiter
Allow bursts but cap the sustained request rate with a small thread-safe token bucket.
- Python Tested
Time a block of code with a context manager
Measure elapsed wall-clock time of a with-block using time.perf_counter.
- Python Tested
Slugify a string
Turn text into a lowercase, hyphenated, ASCII-only slug using only the standard library.
- Python Tested
Sliding window over an iterable
Yield overlapping windows of fixed size, useful for moving averages and n-grams.

