README.rst 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. .. contents:: **DateTimeRange**
  2. :backlinks: top
  3. :depth: 2
  4. Summary
  5. =========
  6. `DateTimeRange <https://github.com/thombashi/DateTimeRange>`__ is a Python library to handle a time range. e.g. check whether a time is within the time range, get the intersection of time ranges, truncating a time range, iterate through a time range, and so forth.
  7. .. image:: https://badge.fury.io/py/DateTimeRange.svg
  8. :target: https://badge.fury.io/py/DateTimeRange
  9. :alt: PyPI package version
  10. .. image:: https://anaconda.org/conda-forge/datetimerange/badges/version.svg
  11. :target: https://anaconda.org/conda-forge/datetimerange
  12. :alt: conda-forge package version
  13. .. image:: https://img.shields.io/pypi/pyversions/DateTimeRange.svg
  14. :target: https://pypi.org/project/DateTimeRange
  15. :alt: Supported Python versions
  16. .. image:: https://github.com/thombashi/DateTimeRange/workflows/Tests/badge.svg
  17. :target: https://github.com/thombashi/DateTimeRange/actions/workflows/tests.yml
  18. :alt: Test result of Linux/macOS/Windows
  19. .. image:: https://coveralls.io/repos/github/thombashi/DateTimeRange/badge.svg?branch=master
  20. :target: https://coveralls.io/github/thombashi/DateTimeRange?branch=master
  21. :alt: Test coverage
  22. .. image:: https://github.com/thombashi/DateTimeRange/actions/workflows/codeql-analysis.yml/badge.svg
  23. :target: https://github.com/thombashi/DateTimeRange/actions/workflows/codeql-analysis.yml
  24. :alt: CodeQL
  25. .. image:: https://img.shields.io/github/stars/thombashi/DateTimeRange.svg?style=social&label=Star
  26. :target: https://github.com/thombashi/DateTimeRange
  27. :alt: GitHub stars
  28. Examples
  29. ==========
  30. Create a DateTimeRange instance from start and end datetime
  31. -----------------------------------------------------------------
  32. :Sample Code:
  33. .. code:: python
  34. from datetimerange import DateTimeRange
  35. time_range = DateTimeRange("2015-03-22T10:00:00+0900", "2015-03-22T10:10:00+0900")
  36. str(time_range)
  37. :Output:
  38. ::
  39. '2015-03-22T10:00:00+0900 - 2015-03-22T10:10:00+0900'
  40. Create a DateTimeRange instance from a range text
  41. -----------------------------------------------------------------
  42. :Sample Code:
  43. .. code:: python
  44. from datetimerange import DateTimeRange
  45. time_range = DateTimeRange.from_range_text("2015-03-22T10:00:00+0900 - 2015-03-22T10:10:00+0900")
  46. str(time_range)
  47. :Output:
  48. ::
  49. '2015-03-22T10:00:00+0900 - 2015-03-22T10:10:00+0900'
  50. Get an iterator
  51. ------------------------
  52. :Sample Code 1:
  53. .. code:: python
  54. import datetime
  55. from datetimerange import DateTimeRange
  56. time_range = DateTimeRange("2015-01-01T00:00:00+0900", "2015-01-04T00:00:00+0900")
  57. for value in time_range.range(datetime.timedelta(days=1)):
  58. print(value)
  59. :Output 1:
  60. ::
  61. 2015-01-01 00:00:00+09:00
  62. 2015-01-02 00:00:00+09:00
  63. 2015-01-03 00:00:00+09:00
  64. 2015-01-04 00:00:00+09:00
  65. :Sample Code 2:
  66. .. code:: python
  67. from datetimerange import DateTimeRange
  68. from dateutil.relativedelta import relativedelta
  69. time_range = DateTimeRange("2015-01-01T00:00:00+0900", "2016-01-01T00:00:00+0900")
  70. for value in time_range.range(relativedelta(months=+4)):
  71. print(value)
  72. :Output 2:
  73. ::
  74. 2015-01-01 00:00:00+09:00
  75. 2015-05-01 00:00:00+09:00
  76. 2015-09-01 00:00:00+09:00
  77. 2016-01-01 00:00:00+09:00
  78. Test whether a value within the time range
  79. ------------------------------------------
  80. :Sample Code:
  81. .. code:: python
  82. from datetimerange import DateTimeRange
  83. time_range = DateTimeRange("2015-03-22T10:00:00+0900", "2015-03-22T10:10:00+0900")
  84. print("2015-03-22T10:05:00+0900" in time_range)
  85. print("2015-03-22T10:15:00+0900" in time_range)
  86. time_range_smaller = DateTimeRange("2015-03-22T10:03:00+0900", "2015-03-22T10:07:00+0900")
  87. print(time_range_smaller in time_range)
  88. :Output:
  89. ::
  90. True
  91. False
  92. True
  93. Test whether a value intersect the time range
  94. ---------------------------------------------
  95. :Sample Code:
  96. .. code:: python
  97. from datetimerange import DateTimeRange
  98. time_range = DateTimeRange("2015-03-22T10:00:00+0900", "2015-03-22T10:10:00+0900")
  99. x = DateTimeRange("2015-03-22T10:05:00+0900", "2015-03-22T10:15:00+0900")
  100. time_range.is_intersection(x)
  101. :Output:
  102. ::
  103. True
  104. Make an intersected time range
  105. ------------------------------
  106. :Sample Code:
  107. .. code:: python
  108. from datetimerange import DateTimeRange
  109. time_range = DateTimeRange("2015-03-22T10:00:00+0900", "2015-03-22T10:10:00+0900")
  110. x = DateTimeRange("2015-03-22T10:05:00+0900", "2015-03-22T10:15:00+0900")
  111. time_range.intersection(x)
  112. :Output:
  113. ::
  114. 2015-03-22T10:05:00+0900 - 2015-03-22T10:10:00+0900
  115. Make an encompassed time range
  116. ------------------------------
  117. :Sample Code:
  118. .. code:: python
  119. from datetimerange import DateTimeRange
  120. time_range = DateTimeRange("2015-03-22T10:00:00+0900", "2015-03-22T10:10:00+0900")
  121. x = DateTimeRange("2015-03-22T10:05:00+0900", "2015-03-22T10:15:00+0900")
  122. time_range.encompass(x)
  123. :Output:
  124. ::
  125. 2015-03-22T10:00:00+0900 - 2015-03-22T10:15:00+0900
  126. Truncate time range
  127. -------------------
  128. :Sample Code:
  129. .. code:: python
  130. from datetimerange import DateTimeRange
  131. time_range = DateTimeRange("2015-03-22T10:00:00+0900", "2015-03-22T10:10:00+0900")
  132. time_range.is_output_elapse = True
  133. print("before truncate: ", time_range)
  134. time_range.truncate(10)
  135. print("after truncate: ", time_range)
  136. :Output:
  137. ::
  138. before truncate: 2015-03-22T10:00:00+0900 - 2015-03-22T10:10:00+0900 (0:10:00)
  139. after truncate: 2015-03-22T10:00:30+0900 - 2015-03-22T10:09:30+0900 (0:09:00)
  140. For more information
  141. ----------------------
  142. More examples are available at
  143. https://datetimerange.rtfd.io/en/latest/pages/examples/index.html
  144. Examples with Jupyter Notebook is also available at `DateTimeRange.ipynb <https://nbviewer.jupyter.org/github/thombashi/DateTimeRange/tree/master/examples/DateTimeRange.ipynb>`__
  145. Installation
  146. ============
  147. Installation: pip
  148. ------------------------------
  149. ::
  150. pip install DateTimeRange
  151. Installation: conda
  152. ------------------------------
  153. ::
  154. conda install -c conda-forge dataproperty
  155. Dependencies
  156. ============
  157. - Python 3.6+
  158. - `Python package dependencies (automatically installed) <https://github.com/thombashi/DateTimeRange/network/dependencies>`__
  159. Documentation
  160. ===============
  161. https://datetimerange.rtfd.io/
  162. Sponsors
  163. ====================================
  164. .. image:: https://avatars.githubusercontent.com/u/3658062?s=48&v=4
  165. :target: https://github.com/b4tman
  166. :alt: Dmitry Belyaev (b4tman)
  167. .. image:: https://avatars.githubusercontent.com/u/44389260?s=48&u=6da7176e51ae2654bcfd22564772ef8a3bb22318&v=4
  168. :target: https://github.com/chasbecker
  169. :alt: Charles Becker (chasbecker)
  170. .. image:: https://avatars.githubusercontent.com/u/46711571?s=48&u=57687c0e02d5d6e8eeaf9177f7b7af4c9f275eb5&v=4
  171. :target: https://github.com/Arturi0
  172. :alt: Arturi0
  173. `Become a sponsor <https://github.com/sponsors/thombashi>`__