aniso8601 Library

10.0.1 · active · verified Thu Apr 09

aniso8601 is a Python library designed for robust parsing of ISO 8601 strings into native Python `datetime` and `timedelta` objects. It supports durations, dates, times, and datetimes, adhering strictly to the ISO 8601 standard. The current version is 10.0.1, and it maintains an active, though irregular, release cadence.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates parsing ISO 8601 duration and datetime strings using `aniso8601.parse.parse_duration` and `aniso8601.parse.parse_datetime`. It also shows how to catch `ISO8601Error` for invalid inputs.

import aniso8601.parse
import datetime

# Parse an ISO 8601 duration string
duration_str = "P3Y6M4DT12H30M5S"
duration = aniso8601.parse.parse_duration(duration_str)
print(f"Parsed Duration: {duration} (Type: {type(duration)})\n")

# Parse an ISO 8601 datetime string with timezone
datetime_str = "2023-01-15T10:00:00+01:00"
dt = aniso8601.parse.parse_datetime(datetime_str)
print(f"Parsed Datetime: {dt} (Type: {type(dt)}, TZ: {dt.tzinfo})\n")

# Parse an ISO 8601 datetime string (UTC)
utc_datetime_str = "2024-03-01T14:30:00Z"
utc_dt = aniso8601.parse.parse_datetime(utc_datetime_str)
print(f"Parsed UTC Datetime: {utc_dt} (Type: {type(utc_dt)}, TZ: {utc_dt.tzinfo})\n")

# Handle potential parsing errors
try:
    invalid_str = "invalid-date-string"
    aniso8601.parse.parse_datetime(invalid_str)
except aniso8601.ISO8601Error as e:
    print(f"Error parsing '{invalid_str}': {e}")

view raw JSON →