Search code examples
pythoniso8601

How can I parse and compare ISO 8601 durations in Python?


I'm looking for a library in Python (v2) that will allow me to parse and compare ISO 8601 durations that may be in different units

Ideally it would work with the standard operators (a < b) but I would be cool with something like a.compare(b) or however.

so something like:

duration('P23M') < duration('P2Y') //True
duration('P25M') < duration('P2Y') //False

I've installed isodate from PyPi, but it has its own class for periods that include month and years and they don't compare with either themselves or with timedeltas


Solution

  • The way I eventually "solved" it was by comparing the resulting values if the period was added to a value holding the current UTC time. This of course means that during February, P30D is longer than P1M, in March it is shorter and in April it is equal. Not ideal, but met my needs well enough.