| 1 | # isoduration: Operations with ISO 8601 durations. |
| 2 | |
| 3 | [](https://pypi.org/project/isoduration/) |
| 4 | |
| 5 | ## What is this. |
| 6 | |
| 7 | ISO 8601 is most commonly known as a way to exchange datetimes in textual format. A |
| 8 | lesser known aspect of the standard is the representation of durations. They have a |
| 9 | shape similar to this: |
| 10 | |
| 11 | ``` |
| 12 | P3Y6M4DT12H30M5S |
| 13 | ``` |
| 14 | |
| 15 | This string represents a duration of 3 years, 6 months, 4 days, 12 hours, 30 minutes, |
| 16 | and 5 seconds. |
| 17 | |
| 18 | The state of the art of ISO 8601 duration handling in Python is more or less limited to |
| 19 | what's offered by [`isodate`](https://pypi.org/project/isodate/). What we are trying to |
| 20 | achieve here is to address the shortcomings of `isodate` (as described in their own |
| 21 | [_Limitations_](https://github.com/gweis/isodate/#limitations) section), and a few of |
| 22 | our own annoyances with their interface, such as the lack of uniformity in their |
| 23 | handling of types, and the use of regular expressions for parsing. |
| 24 | |
| 25 | ## How to use it. |
| 26 | |
| 27 | This package revolves around the [`Duration`](src/isoduration/types.py) type. |
| 28 | |
| 29 | Given a ISO duration string we can produce such a type by using the `parse_duration()` |
| 30 | function: |
| 31 | |
| 32 | ```py |
| 33 | >>> from isoduration import parse_duration |
| 34 | >>> duration = parse_duration("P3Y6M4DT12H30M5S") |
| 35 | >>> duration.date |
| 36 | DateDuration(years=Decimal('3'), months=Decimal('6'), days=Decimal('4'), weeks=Decimal('0')) |
| 37 | >>> duration.time |
| 38 | TimeDuration(hours=Decimal('12'), minutes=Decimal('30'), seconds=Decimal('5')) |
| 39 | ``` |
| 40 | |
| 41 | The `date` and `time` portions of the parsed duration are just regular |
| 42 | [dataclasses](https://docs.python.org/3/library/dataclasses.html), so their members can |
| 43 | be accessed in a non-surprising way. |
| 44 | |
| 45 | Besides just parsing them, a number of additional operations are available: |
| 46 | |
| 47 | - Durations can be compared and negated: |
| 48 | ```py |
| 49 | >>> parse_duration("P3Y4D") == parse_duration("P3Y4DT0H") |
| 50 | True |
| 51 | >>> -parse_duration("P3Y4D") |
| 52 | Duration(DateDuration(years=Decimal('-3'), months=Decimal('0'), days=Decimal('-4'), weeks=Decimal('0')), TimeDuration(hours=Decimal('0'), minutes=Decimal('0'), seconds=Decimal('0'))) |
| 53 | ``` |
| 54 | - Durations can be added to, or subtracted from, Python datetimes: |
| 55 | ```py |
| 56 | >>> from datetime import datetime |
| 57 | >>> datetime(2020, 3, 15) + parse_duration("P2Y") |
| 58 | datetime.datetime(2022, 3, 15, 0, 0) |
| 59 | >>> datetime(2020, 3, 15) - parse_duration("P33Y1M4D") |
| 60 | datetime.datetime(1987, 2, 11, 0, 0) |
| 61 | ``` |
| 62 | - Durations are hashable, so they can be used as dictionary keys or as part of sets. |
| 63 | - Durations can be formatted back to a ISO 8601-compliant duration string: |
| 64 | ```py |
| 65 | >>> from isoduration import parse_duration, format_duration |
| 66 | >>> format_duration(parse_duration("P11YT2H")) |
| 67 | 'P11YT2H' |
| 68 | >>> str(parse_duration("P11YT2H")) |
| 69 | 'P11YT2H' |
| 70 | ``` |
| 71 | |
| 72 | ## How to improve it. |
| 73 | |
| 74 | These steps, in this order, should land you in a development environment: |
| 75 | |
| 76 | ```sh |
| 77 | git clone [email protected]:bolsote/isoduration.git |
| 78 | cd isoduration/ |
| 79 | python -m venv ve |
| 80 | . ve/bin/activate |
| 81 | pip install -U pip |
| 82 | pip install -e . |
| 83 | pip install -r requirements/dev.txt |
| 84 | ``` |
| 85 | |
| 86 | Adapt to your own likings and/or needs. |
| 87 | |
| 88 | Testing is driven by [tox](https://tox.readthedocs.io). The output of `tox -l` and a |
| 89 | careful read of [tox.ini](tox.ini) should get you there. |
| 90 | |
| 91 | ## FAQs. |
| 92 | |
| 93 | ### How come `P1Y != P365D`? |
| 94 | Some years have 366 days. If it's not always the same, then it's not the same. |
| 95 | |
| 96 | ### Why do you create your own types, instead of somewhat shoehorning a `timedelta`? |
| 97 | `timedelta` cannot represent certain durations, such as those involving years or months. |
| 98 | Since it cannot represent all possible durations without dangerous arithmetic, then it |
| 99 | must not be the right type. |
| 100 | |
| 101 | ### Why don't you use regular expressions to parse duration strings? |
| 102 | [Regular expressions should only be used to parse regular languages.](https://stackoverflow.com/a/1732454) |
| 103 | |
| 104 | ### Why is parsing the inverse of formatting, but the converse is not true? |
| 105 | Because this wonderful representation is not unique. |
| 106 | |
| 107 | ### Why do you support `<insert here a weird case>`? |
| 108 | Probably because the standard made me to. |
| 109 | |
| 110 | ### Why do you not support `<insert here a weird case>`? |
| 111 | Probably because the standard doesn't allow me to. |
| 112 | |
| 113 | ### Why is it not possible to subtract a datetime from a duration? |
| 114 | I'm confused. |
| 115 | |
| 116 | ### Why should I use this over some other thing? |
| 117 | You shouldn't do what people on the Internet tell you to do. |
| 118 | |
| 119 | ### Why are ISO standards so strange? |
| 120 | Yes. |
| 121 | |
| 122 | ## References. |
| 123 | |
| 124 | - [XML Schema Part 2: Datatypes, Appendix D](https://www.w3.org/TR/xmlschema-2/#isoformats): |
| 125 | This excitingly named document contains more details about ISO 8601 than any human |
| 126 | should be allowed to understand. |
| 127 | - [`isodate`](https://pypi.org/project/isodate/): The original implementation of ISO |
| 128 | durations in Python. Worth a look. But ours is cooler. |