.. contents:: **DateTimeRange**
:backlinks: top
:depth: 2
Summary
=========
`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, truncate a time range, iterate through a time range, and so forth.
|PyPI pkg ver| |conda pkg ver| |Supported Python versions| |CI status| |Test coverage| |CodeQL|
.. |PyPI pkg ver| image:: https://badge.fury.io/py/DateTimeRange.svg
:target: https://badge.fury.io/py/DateTimeRange
:alt: PyPI package version
.. |conda pkg ver| image:: https://anaconda.org/conda-forge/datetimerange/badges/version.svg
:target: https://anaconda.org/conda-forge/datetimerange
:alt: conda-forge package version
.. |Supported Python versions| image:: https://img.shields.io/pypi/pyversions/DateTimeRange.svg
:target: https://pypi.org/project/DateTimeRange
:alt: Supported Python versions
.. |CI status| image:: https://github.com/thombashi/DateTimeRange/actions/workflows/ci.yml/badge.svg
:target: https://github.com/thombashi/DateTimeRange/actions/workflows/ci.yml
:alt: CI status of Linux/macOS/Windows
.. |Test coverage| image:: https://coveralls.io/repos/github/thombashi/DateTimeRange/badge.svg?branch=master
:target: https://coveralls.io/github/thombashi/DateTimeRange?branch=master
:alt: Test coverage
.. |CodeQL| image:: https://github.com/thombashi/DateTimeRange/actions/workflows/github-code-scanning/codeql/badge.svg
:target: https://github.com/thombashi/DateTimeRange/actions/workflows/github-code-scanning/codeql
:alt: CodeQL
Installation
============
Installation: pip
------------------------------
::
pip install DateTimeRange
Installation: conda
------------------------------
::
conda install -c conda-forge datetimerange
Dependencies
============
- Python 3.7+
- `Python package dependencies (automatically installed) <https://github.com/thombashi/DateTimeRange/network/dependencies>`__
Features
============
Features of ``DateTimeRange`` class include:
- Supported operations:
- Equation
- Addition
- Subtraction
- Intersection
- Union
- Contains
- Truncate
- Split
- Iteration
- Timezone support
- Daylight saving time support
Examples
==========
Create a DateTimeRange instance from start and end datetime
-----------------------------------------------------------------
:Sample Code:
.. code:: python
from datetimerange import DateTimeRange
time_range = DateTimeRange("2015-03-22T10:00:00+0900", "2015-03-22T10:10:00+0900")
str(time_range)
:Output:
::
'2015-03-22T10:00:00+0900 - 2015-03-22T10:10:00+0900'
Create a DateTimeRange instance from a range text
-----------------------------------------------------------------
:Sample Code:
.. code:: python
from datetimerange import DateTimeRange
time_range = DateTimeRange.from_range_text("2015-03-22T10:00:00+0900 - 2015-03-22T10:10:00+0900")
str(time_range)
:Output:
::
'2015-03-22T10:00:00+0900 - 2015-03-22T10:10:00+0900'
Get an iterator
------------------------
:Sample Code 1:
.. code:: python
import datetime
from datetimerange import DateTimeRange
time_range = DateTimeRange("2015-01-01T00:00:00+0900", "2015-01-04T00:00:00+0900")
for value in time_range.range(datetime.timedelta(days=1)):
print(value)
:Output 1:
::
2015-01-01 00:00:00+09:00
2015-01-02 00:00:00+09:00
2015-01-03 00:00:00+09:00
2015-01-04 00:00:00+09:00
:Sample Code 2:
.. code:: python
from datetimerange import DateTimeRange
from dateutil.relativedelta import relativedelta
time_range = DateTimeRange("2015-01-01T00:00:00+0900", "2016-01-01T00:00:00+0900")
for value in time_range.range(relativedelta(months=+4)):
print(value)
:Output 2:
::
2015-01-01 00:00:00+09:00
2015-05-01 00:00:00+09:00
2015-09-01 00:00:00+09:00
2016-01-01 00:00:00+09:00
Test whether a value within the time range
------------------------------------------
:Sample Code:
.. code:: python
from datetimerange import DateTimeRange
time_range = DateTimeRange("2015-03-22T10:00:00+0900", "2015-03-22T10:10:00+0900")
print("2015-03-22T10:05:00+0900" in time_range)
print("2015-03-22T10:15:00+0900" in time_range)
time_range_smaller = DateTimeRange("2015-03-22T10:03:00+0900", "2015-03-22T10:07:00+0900")
print(time_range_smaller in time_range)
:Output:
::
True
False
True
Test whether a value intersects the time range
----------------------------------------------
:Sample Code:
.. code:: python
from datetimerange import DateTimeRange
time_range = DateTimeRange("2015-03-22T10:00:00+0900", "2015-03-22T10:10:00+0900")
x = DateTimeRange("2015-03-22T10:05:00+0900", "2015-03-22T10:15:00+0900")
time_range.is_intersection(x)
:Output:
::
True
Make an intersected time range
------------------------------
:Sample Code:
.. code:: python
from datetimerange import DateTimeRange
time_range = DateTimeRange("2015-03-22T10:00:00+0900", "2015-03-22T10:10:00+0900")
x = DateTimeRange("2015-03-22T10:05:00+0900", "2015-03-22T10:15:00+0900")
time_range.intersection(x)
:Output:
::
2015-03-22T10:05:00+0900 - 2015-03-22T10:10:00+0900
Make an encompassed time range
------------------------------
:Sample Code:
.. code:: python
from datetimerange import DateTimeRange
time_range = DateTimeRange("2015-03-22T10:00:00+0900", "2015-03-22T10:10:00+0900")
x = DateTimeRange("2015-03-22T10:05:00+0900", "2015-03-22T10:15:00+0900")
time_range.encompass(x)
:Output:
::
2015-03-22T10:00:00+0900 - 2015-03-22T10:15:00+0900
Truncate time range
-------------------
:Sample Code:
.. code:: python
from datetimerange import DateTimeRange
time_range = DateTimeRange("2015-03-22T10:00:00+0900", "2015-03-22T10:10:00+0900")
time_range.is_output_elapse = True
print("before truncate: ", time_range)
time_range.truncate(10)
print("after truncate: ", time_range)
:Output:
::
before truncate: 2015-03-22T10:00:00+0900 - 2015-03-22T10:10:00+0900 (0:10:00)
after truncate: 2015-03-22T10:00:30+0900 - 2015-03-22T10:09:30+0900 (0:09:00)
For more information
----------------------
More examples are available at
https://datetimerange.rtfd.io/en/latest/pages/examples/index.html
Examples with Jupyter Notebook are also available at `DateTimeRange.ipynb <https://nbviewer.jupyter.org/github/thombashi/DateTimeRange/tree/master/examples/DateTimeRange.ipynb>`__
Documentation
===============
https://datetimerange.rtfd.io/
Sponsors
====================================
.. image:: https://avatars.githubusercontent.com/u/3658062?s=48&v=4
:target: https://github.com/b4tman
:alt: Dmitry Belyaev (b4tman)
.. image:: https://avatars.githubusercontent.com/u/44389260?s=48&u=6da7176e51ae2654bcfd22564772ef8a3bb22318&v=4
:target: https://github.com/chasbecker
:alt: Charles Becker (chasbecker)
.. image:: https://avatars.githubusercontent.com/u/46711571?s=48&u=57687c0e02d5d6e8eeaf9177f7b7af4c9f275eb5&v=4
:target: https://github.com/Arturi0
:alt: Arturi0
`Become a sponsor <https://github.com/sponsors/thombashi>`__
Raw data
{
"_id": null,
"home_page": "https://github.com/thombashi/DateTimeRange",
"name": "DateTimeRange",
"maintainer": null,
"docs_url": null,
"requires_python": ">=3.7",
"maintainer_email": null,
"keywords": "datetimerange, datetime, time range",
"author": "Tsuyoshi Hombashi",
"author_email": "tsuyoshi.hombashi@gmail.com",
"download_url": "https://files.pythonhosted.org/packages/d0/00/4c0aa0436a740058a3f541c973d38ce25af604b869fa436173b3359397bb/DateTimeRange-2.2.1.tar.gz",
"platform": null,
"description": ".. contents:: **DateTimeRange**\n :backlinks: top\n :depth: 2\n\nSummary\n=========\n`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, truncate a time range, iterate through a time range, and so forth.\n\n|PyPI pkg ver| |conda pkg ver| |Supported Python versions| |CI status| |Test coverage| |CodeQL|\n\n.. |PyPI pkg ver| image:: https://badge.fury.io/py/DateTimeRange.svg\n :target: https://badge.fury.io/py/DateTimeRange\n :alt: PyPI package version\n\n.. |conda pkg ver| image:: https://anaconda.org/conda-forge/datetimerange/badges/version.svg\n :target: https://anaconda.org/conda-forge/datetimerange\n :alt: conda-forge package version\n\n.. |Supported Python versions| image:: https://img.shields.io/pypi/pyversions/DateTimeRange.svg\n :target: https://pypi.org/project/DateTimeRange\n :alt: Supported Python versions\n\n.. |CI status| image:: https://github.com/thombashi/DateTimeRange/actions/workflows/ci.yml/badge.svg\n :target: https://github.com/thombashi/DateTimeRange/actions/workflows/ci.yml\n :alt: CI status of Linux/macOS/Windows\n\n.. |Test coverage| image:: https://coveralls.io/repos/github/thombashi/DateTimeRange/badge.svg?branch=master\n :target: https://coveralls.io/github/thombashi/DateTimeRange?branch=master\n :alt: Test coverage\n\n.. |CodeQL| image:: https://github.com/thombashi/DateTimeRange/actions/workflows/github-code-scanning/codeql/badge.svg\n :target: https://github.com/thombashi/DateTimeRange/actions/workflows/github-code-scanning/codeql\n :alt: CodeQL\n\nInstallation\n============\n\nInstallation: pip\n------------------------------\n::\n\n pip install DateTimeRange\n\n\nInstallation: conda\n------------------------------\n::\n\n conda install -c conda-forge datetimerange\n\n\nDependencies\n============\n- Python 3.7+\n- `Python package dependencies (automatically installed) <https://github.com/thombashi/DateTimeRange/network/dependencies>`__\n\nFeatures\n============\nFeatures of ``DateTimeRange`` class include:\n\n- Supported operations:\n - Equation\n - Addition\n - Subtraction\n - Intersection\n - Union\n - Contains\n - Truncate\n - Split\n - Iteration\n- Timezone support\n- Daylight saving time support\n\nExamples\n==========\nCreate a DateTimeRange instance from start and end datetime\n-----------------------------------------------------------------\n:Sample Code:\n .. code:: python\n\n from datetimerange import DateTimeRange\n time_range = DateTimeRange(\"2015-03-22T10:00:00+0900\", \"2015-03-22T10:10:00+0900\")\n str(time_range)\n\n:Output:\n ::\n\n '2015-03-22T10:00:00+0900 - 2015-03-22T10:10:00+0900'\n\nCreate a DateTimeRange instance from a range text\n-----------------------------------------------------------------\n:Sample Code:\n .. code:: python\n\n from datetimerange import DateTimeRange\n time_range = DateTimeRange.from_range_text(\"2015-03-22T10:00:00+0900 - 2015-03-22T10:10:00+0900\")\n str(time_range)\n\n:Output:\n ::\n\n '2015-03-22T10:00:00+0900 - 2015-03-22T10:10:00+0900'\n\nGet an iterator\n------------------------\n:Sample Code 1:\n .. code:: python\n\n import datetime\n from datetimerange import DateTimeRange\n\n time_range = DateTimeRange(\"2015-01-01T00:00:00+0900\", \"2015-01-04T00:00:00+0900\")\n for value in time_range.range(datetime.timedelta(days=1)):\n print(value)\n\n:Output 1:\n ::\n\n 2015-01-01 00:00:00+09:00\n 2015-01-02 00:00:00+09:00\n 2015-01-03 00:00:00+09:00\n 2015-01-04 00:00:00+09:00\n\n:Sample Code 2:\n .. code:: python\n\n from datetimerange import DateTimeRange\n from dateutil.relativedelta import relativedelta\n\n time_range = DateTimeRange(\"2015-01-01T00:00:00+0900\", \"2016-01-01T00:00:00+0900\")\n for value in time_range.range(relativedelta(months=+4)):\n print(value)\n\n:Output 2:\n ::\n\n 2015-01-01 00:00:00+09:00\n 2015-05-01 00:00:00+09:00\n 2015-09-01 00:00:00+09:00\n 2016-01-01 00:00:00+09:00\n\nTest whether a value within the time range\n------------------------------------------\n:Sample Code:\n .. code:: python\n\n from datetimerange import DateTimeRange\n\n time_range = DateTimeRange(\"2015-03-22T10:00:00+0900\", \"2015-03-22T10:10:00+0900\")\n print(\"2015-03-22T10:05:00+0900\" in time_range)\n print(\"2015-03-22T10:15:00+0900\" in time_range)\n\n time_range_smaller = DateTimeRange(\"2015-03-22T10:03:00+0900\", \"2015-03-22T10:07:00+0900\")\n print(time_range_smaller in time_range)\n\n:Output:\n ::\n\n True\n False\n True\n\nTest whether a value intersects the time range\n----------------------------------------------\n:Sample Code:\n .. code:: python\n\n from datetimerange import DateTimeRange\n time_range = DateTimeRange(\"2015-03-22T10:00:00+0900\", \"2015-03-22T10:10:00+0900\")\n x = DateTimeRange(\"2015-03-22T10:05:00+0900\", \"2015-03-22T10:15:00+0900\")\n time_range.is_intersection(x)\n\n:Output:\n ::\n\n True\n\nMake an intersected time range\n------------------------------\n:Sample Code:\n .. code:: python\n\n from datetimerange import DateTimeRange\n time_range = DateTimeRange(\"2015-03-22T10:00:00+0900\", \"2015-03-22T10:10:00+0900\")\n x = DateTimeRange(\"2015-03-22T10:05:00+0900\", \"2015-03-22T10:15:00+0900\")\n time_range.intersection(x)\n\n:Output:\n ::\n\n 2015-03-22T10:05:00+0900 - 2015-03-22T10:10:00+0900\n\nMake an encompassed time range\n------------------------------\n:Sample Code:\n .. code:: python\n\n from datetimerange import DateTimeRange\n time_range = DateTimeRange(\"2015-03-22T10:00:00+0900\", \"2015-03-22T10:10:00+0900\")\n x = DateTimeRange(\"2015-03-22T10:05:00+0900\", \"2015-03-22T10:15:00+0900\")\n time_range.encompass(x)\n\n:Output:\n ::\n\n 2015-03-22T10:00:00+0900 - 2015-03-22T10:15:00+0900\n\nTruncate time range\n-------------------\n:Sample Code:\n .. code:: python\n\n from datetimerange import DateTimeRange\n\n time_range = DateTimeRange(\"2015-03-22T10:00:00+0900\", \"2015-03-22T10:10:00+0900\")\n time_range.is_output_elapse = True\n print(\"before truncate: \", time_range)\n\n time_range.truncate(10)\n print(\"after truncate: \", time_range)\n\n:Output:\n ::\n\n before truncate: 2015-03-22T10:00:00+0900 - 2015-03-22T10:10:00+0900 (0:10:00)\n after truncate: 2015-03-22T10:00:30+0900 - 2015-03-22T10:09:30+0900 (0:09:00)\n\nFor more information\n----------------------\nMore examples are available at \nhttps://datetimerange.rtfd.io/en/latest/pages/examples/index.html\n\nExamples with Jupyter Notebook are also available at `DateTimeRange.ipynb <https://nbviewer.jupyter.org/github/thombashi/DateTimeRange/tree/master/examples/DateTimeRange.ipynb>`__\n\nDocumentation\n===============\nhttps://datetimerange.rtfd.io/\n\nSponsors\n====================================\n.. image:: https://avatars.githubusercontent.com/u/3658062?s=48&v=4\n :target: https://github.com/b4tman\n :alt: Dmitry Belyaev (b4tman)\n.. image:: https://avatars.githubusercontent.com/u/44389260?s=48&u=6da7176e51ae2654bcfd22564772ef8a3bb22318&v=4\n :target: https://github.com/chasbecker\n :alt: Charles Becker (chasbecker)\n.. image:: https://avatars.githubusercontent.com/u/46711571?s=48&u=57687c0e02d5d6e8eeaf9177f7b7af4c9f275eb5&v=4\n :target: https://github.com/Arturi0\n :alt: Arturi0\n\n`Become a sponsor <https://github.com/sponsors/thombashi>`__\n\n",
"bugtrack_url": null,
"license": "MIT License",
"summary": "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, truncate a time range, iterate through a time range, and so forth.",
"version": "2.2.1",
"project_urls": {
"Changlog": "https://github.com/thombashi/DateTimeRange/releases",
"Documentation": "https://datetimerange.rtfd.io/",
"Funding": "https://github.com/sponsors/thombashi",
"Homepage": "https://github.com/thombashi/DateTimeRange",
"Source": "https://github.com/thombashi/DateTimeRange",
"Tracker": "https://github.com/thombashi/DateTimeRange/issues"
},
"split_keywords": [
"datetimerange",
" datetime",
" time range"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "6ea606ec6c74b641e3487eaa02566d83989661f4b9dea49f188898922fea31f5",
"md5": "9831a86151f45188fecfe7c40e2f9cd5",
"sha256": "c77cf6c59903a09d2328d08eda1a92ca764a79bdbdfb2a4195d82e97beee1c4e"
},
"downloads": -1,
"filename": "DateTimeRange-2.2.1-py3-none-any.whl",
"has_sig": false,
"md5_digest": "9831a86151f45188fecfe7c40e2f9cd5",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.7",
"size": 10102,
"upload_time": "2024-04-07T14:56:50",
"upload_time_iso_8601": "2024-04-07T14:56:50.875339Z",
"url": "https://files.pythonhosted.org/packages/6e/a6/06ec6c74b641e3487eaa02566d83989661f4b9dea49f188898922fea31f5/DateTimeRange-2.2.1-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "d0004c0aa0436a740058a3f541c973d38ce25af604b869fa436173b3359397bb",
"md5": "0be14f160e9ba8875ed183b20bd26d0b",
"sha256": "70df1751c05f1738ed6766fb1b66550359fa557d71a5e35f8cbdcd3e459f0670"
},
"downloads": -1,
"filename": "DateTimeRange-2.2.1.tar.gz",
"has_sig": false,
"md5_digest": "0be14f160e9ba8875ed183b20bd26d0b",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.7",
"size": 16236,
"upload_time": "2024-04-07T14:56:53",
"upload_time_iso_8601": "2024-04-07T14:56:53.112594Z",
"url": "https://files.pythonhosted.org/packages/d0/00/4c0aa0436a740058a3f541c973d38ce25af604b869fa436173b3359397bb/DateTimeRange-2.2.1.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-04-07 14:56:53",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "thombashi",
"github_project": "DateTimeRange",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"tox": true,
"lcname": "datetimerange"
}