isoduration is a Python library for working with ISO 8601 durations (e.g., 'P3Y6M4DT12H30M5S'). It provides functionality for parsing duration strings, formatting durations, and performing arithmetic operations with Python's `datetime` objects. The library is currently at version 20.11.0, released in 2020, and appears to be in maintenance mode with no recent major releases, though minor updates or repackaging may occur.
pip install isodurationVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates parsing an ISO 8601 duration string into a `Duration` object, accessing its components, and performing arithmetic by adding it to a `datetime` object.
Be aware that `parse_duration` may return a `Duration` object, not always a `timedelta`. The `Duration` object correctly implements addition/subtraction with `datetime` objects.
Consider its maintenance status when planning for long-term projects or needing new features/bug fixes. It appears stable for its current scope.
Access components via `duration.date.years`, `duration.time.hours`, etc. Perform arithmetic with `datetime` objects for correct temporal calculations. Avoid direct `timedelta` conversion if the `Duration` contains years or months, as `timedelta` cannot represent these precisely.
Ensure the duration string adheres to the ISO 8601 format (e.g., 'P3Y6M4DT12H30M5S', 'PT1H30M', 'P1W'). For instance, 'P3X' should be 'P3D' for three days or 'PT3S' for three seconds.
Identify where `DurationParsingException` is being referenced in your code or a dependent library. Replace it with a more general exception like `ValueError`, or specifically catch `iso8601.iso8601.ParseError` if that's the underlying exception being raised and you have `iso8601` installed as a dependency. If it's a dependency causing the issue, check for compatibility or try reinstalling the packages.
To get total seconds, you need to convert the `Duration` object to a `timedelta` first using `duration.to_timedelta()`. Note that `to_timedelta()` will raise a `ValueError` if the duration contains years or months, as these cannot be unambiguously converted to seconds without a specific context date. For durations with years/months, access the `years`, `months`, `days`, `hours`, `minutes`, `seconds` attributes from `duration.date` and `duration.time` directly.
No dependency data recorded yet.