Fast ISO8601 Datetime Parser

2.3.3 · active · verified Mon Apr 06

ciso8601 is a Python library providing a highly optimized C extension for parsing ISO 8601 and RFC 3339 datetime strings into Python `datetime` objects. It is designed for maximum performance, often outperforming other Python date parsing libraries. The current version is 2.3.3, and it maintains an active release cadence with regular updates for new Python versions and bug fixes.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates basic parsing of ISO 8601 strings, including naive and timezone-aware datetimes. It also shows how to explicitly parse as naive, ignoring timezone information, and how to use the strict RFC 3339 parser.

import ciso8601

# Parse a naive datetime string
date_str_naive = "2023-10-27T10:00:00"
dt_naive = ciso8601.parse_datetime(date_str_naive)
print(f"Naive: {dt_naive} (Type: {type(dt_naive)}) (TZ: {dt_naive.tzinfo})")

# Parse a timezone-aware datetime string
date_str_aware = "2023-10-27T10:00:00+01:00"
dt_aware = ciso8601.parse_datetime(date_str_aware)
print(f"Aware: {dt_aware} (Type: {type(dt_aware)}) (TZ: {dt_aware.tzinfo})")

# Parse as naive, even if timezone is present
date_str_aware_to_naive = "2023-10-27T10:00:00Z"
dt_forced_naive = ciso8601.parse_datetime_as_naive(date_str_aware_to_naive)
print(f"Forced Naive: {dt_forced_naive} (Type: {type(dt_forced_naive)}) (TZ: {dt_forced_naive.tzinfo})")

# Strict RFC 3339 parsing
# RFC 3339 requires a full date and time with a timezone offset or 'Z'
try:
    dt_rfc3339 = ciso8601.parse_rfc3339("2023-10-27T10:00:00Z")
    print(f"RFC3339: {dt_rfc3339} (TZ: {dt_rfc3339.tzinfo})")
    ciso8601.parse_rfc3339("2023-10-27") # This will raise ValueError
except ValueError as e:
    print(f"Error parsing non-RFC3339 string with parse_rfc3339: {e}")

view raw JSON →