aniso8601 Library
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.
Common errors
-
ModuleNotFoundError: No module named 'aniso8601'
cause The 'aniso8601' library is not installed in the current Python environment.fixInstall the library using pip: 'pip install aniso8601'. -
ImportError: No module named aniso8601
cause The 'aniso8601' module is not found, possibly due to it not being installed or the virtual environment not being activated.fixEnsure the virtual environment is activated and install the module with 'pip install aniso8601'. -
aniso8601.exceptions.LeapSecondError: Leap seconds are not supported.
cause The 'aniso8601' library does not support parsing ISO 8601 strings that include leap seconds.fixAvoid parsing datetime strings that include leap seconds, as they are not supported by 'aniso8601'. -
ValueError: Fractional months and years are not defined for relative intervals.
cause Attempting to parse a duration with 'relative=True' that includes fractional months or years, which is not supported.fixEnsure that durations with 'relative=True' do not include fractional months or years. -
RuntimeError: dateutil must be installed for relative duration support.
cause The 'python-dateutil' library is required for parsing relative durations but is not installed.fixInstall the 'python-dateutil' library using pip: 'pip install python-dateutil'.
Warnings
- breaking Starting with v9.0.0, the return types for `parse_duration` and `parse_datetime` changed. `parse_duration` now returns `datetime.timedelta` (instead of `aniso8601.Duration`) and `parse_datetime` returns `datetime.datetime` (instead of `aniso8601.DateTime`). Code relying on the specific `aniso8601` custom objects will break.
- breaking In v10.0.0, Python 3.6 support was dropped. Additionally, the `parse_repeating_interval` function was removed entirely, and all exception classes were moved directly under the `aniso8601` package (e.g., `aniso8601.ISO8601Error` instead of `aniso8601.exceptions.ISO8601Error`).
- gotcha aniso8601 is strict about ISO 8601 compliance. It does not attempt to 'guess' or be lenient with non-standard formats. Input strings must strictly conform to the ISO 8601 specification to be parsed successfully. This can lead to unexpected `ISO8601Error` for subtly malformed strings.
- gotcha When parsing datetimes without an explicit timezone offset (e.g., '2023-01-15T10:00:00'), `parse_datetime` will return a naive `datetime` object (i.e., `dt.tzinfo` will be `None`). It does not assume UTC or any local timezone by default for such strings.
- gotcha The `aniso8601` library does not expose a submodule named `parse`. Core parsing functions (e.g., `parse_datetime`, `parse_duration`) are directly available under the `aniso8601` package. Attempting to import `aniso8601.parse` will result in a `ModuleNotFoundError`.
- breaking The `aniso8601.parse` submodule has been removed or restructured. Direct import of `aniso8601.parse` will result in a `ModuleNotFoundError`.
Install
-
pip install aniso8601
Imports
- parse_datetime
from aniso8601.parse import parse_datetime
- parse_date
from aniso8601.parse import parse_date
- parse_time
from aniso8601.parse import parse_time
- parse_duration
from aniso8601.parse import parse_duration
- ISO8601Error
from aniso8601.exceptions import ISO8601Error
from aniso8601 import ISO8601Error
Quickstart
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}")