Skip to main content

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 Test result 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

Dmitry Belyaev (b4tman) Charles Becker (chasbecker) Arturi0

Become a sponsor

Metadata

Release files for DateTimeRange 2.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for DateTimeRange 2.2.0
File Size Uploaded Provenance Actions
DateTimeRange-2.2.0.tar.gz 15.9 kB Without provenance Details for DateTimeRange-2.2.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for DateTimeRange 2.2.0
File Interpreter ABI Platform Actions
DateTimeRange-2.2.0-py3-none-any.whl Python 3 none any Details for DateTimeRange-2.2.0-py3-none-any.whl

Total release size: 25.9 kB

Release files / DateTimeRange-2.2.0.tar.gz

Download URL DateTimeRange-2.2.0.tar.gz
Size 15.9 kB
Tags Source
SHA-256 checksum
How to use checksums
071e9dc89c6e44c344cd251a178910fb05c8df423d1c6bd3357e3296fe7b2d97
BLAKE2b-256 checksum
How to use checksums
f061528d586b6c844268ec351740688e4e28e441ae668684f9ac36a2c61f8ad9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.4

Release files / DateTimeRange-2.2.0-py3-none-any.whl

Download URL DateTimeRange-2.2.0-py3-none-any.whl
Size 10.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
a16c98a7240e5a18c96aa0afb31b2764eb5a49a14500c5d3d992d6ae08dfaa31
BLAKE2b-256 checksum
How to use checksums
e8fcc3141c53221c493d1daa1b3b93989e52e117d4a6a00a0be535f739ce114f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.4

Security for DateTimeRange 2.2.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

2.2.0 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page