Skip to main content

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.

Project description

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.

PyPI package version conda-forge package version Supported Python versions CI status of Linux/macOS/Windows Test coverage CodeQL

Installation

Installation: pip

pip install DateTimeRange

Installation: conda

conda install -c conda-forge datetimerange

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:
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:
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:
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:
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:
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:
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:
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:
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:
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

Documentation

https://datetimerange.rtfd.io/

Sponsors

ex-sponsor: Charles Becker (chasbecker) ex-sponsor: 時雨堂 (shiguredo) onetime: Dmitry Belyaev (b4tman) onetime: Arturi0 onetime: GitHub (github)

Become a sponsor

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

datetimerange-2.3.2.tar.gz (42.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

datetimerange-2.3.2-py3-none-any.whl (10.5 kB view details)

Uploaded Python 3

File details

Details for the file datetimerange-2.3.2.tar.gz.

File metadata

  • Download URL: datetimerange-2.3.2.tar.gz
  • Upload date:
  • Size: 42.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for datetimerange-2.3.2.tar.gz
Algorithm Hash digest
SHA256 f36e3f9f937aadfd6ac9dbbf542bccbfecf2091046ffe829fbc75109633e7e8d
MD5 7e8ef1f2b3876ad2cc1ab5883d926abd
BLAKE2b-256 538fac328b99eb3b5783f69cabdbb2cd2413e3156ab1c69cd995a129dc4725f4

See more details on using hashes here.

Provenance

The following attestation bundles were made for datetimerange-2.3.2.tar.gz:

Publisher: publish.yml on thombashi/DateTimeRange

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file datetimerange-2.3.2-py3-none-any.whl.

File metadata

  • Download URL: datetimerange-2.3.2-py3-none-any.whl
  • Upload date:
  • Size: 10.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for datetimerange-2.3.2-py3-none-any.whl
Algorithm Hash digest
SHA256 eb96950a179a4c99f8fd4016398bd9a836d60bf690a3264b27977b9eda1e7f99
MD5 1ea2043d87ad5fd1751c9083b99d13c7
BLAKE2b-256 9c582a00cf3bd4bac431e9dc6e2ec56c6449bc10f5748724e82acbedcf656491

See more details on using hashes here.

Provenance

The following attestation bundles were made for datetimerange-2.3.2-py3-none-any.whl:

Publisher: publish.yml on thombashi/DateTimeRange

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page