METADATA 5.6 KB

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