Skip to main content

DateTimeRange

Summary

DateTimeRange is a Python library to handle routine work related to a time range, such as test whether a time is within the time range, get time range intersection, truncating the time range, and so forth.

https://badge.fury.io/py/DateTimeRange.svg https://img.shields.io/pypi/pyversions/DateTimeRange.svg https://img.shields.io/travis/thombashi/DateTimeRange/master.svg?label=Linux/macOS https://img.shields.io/appveyor/ci/thombashi/datetimerange/master.svg?label=Windows https://coveralls.io/repos/github/thombashi/DateTimeRange/badge.svg?branch=master https://img.shields.io/github/stars/thombashi/DateTimeRange.svg?style=social&label=Star

Examples

Create and convert to string

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'

Get 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 intersect 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)
time_range
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)
time_range
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 IPython Notebook is also available at https://nbviewer.jupyter.org/github/thombashi/DateTimeRange/tree/master/ipynb/DateTimeRange.ipynb

Installation

pip install DateTimeRange

Dependencies

Python 2.7 or 3.4+

Test dependencies

Documentation

https://datetimerange.rtfd.io/

Metadata

Release files for DateTimeRange 0.4.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 0.4.0
File Size Uploaded Provenance Actions
DateTimeRange-0.4.0.tar.gz 11.3 kB Without provenance Details for DateTimeRange-0.4.0.tar.gz

Built distribution (wheel)

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

Total release size: 18.9 kB

Release files / DateTimeRange-0.4.0.tar.gz

Download URL DateTimeRange-0.4.0.tar.gz
Size 11.3 kB
Tags Source
SHA-256 checksum
How to use checksums
a4c96f0613f432f652f71faa23f1c2d65062f9a9220975eaf8ee55c096a5a840
BLAKE2b-256 checksum
How to use checksums
e706945bc33f6dded8685fc57cbc5405df1f7af1c955830b858d2598c80b0932
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.12.1 pkginfo/1.4.2 requests/2.20.0 setuptools/40.5.0 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.6.5

Release files / DateTimeRange-0.4.0-py2.py3-none-any.whl

Download URL DateTimeRange-0.4.0-py2.py3-none-any.whl
Size 7.6 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
1a9c0e678d9f66d98142ed25f616e8eb58a64a7704ad9bce04b44c8e240d3b41
BLAKE2b-256 checksum
How to use checksums
7bef84cd8acb152d6796b02db28edbdc701fd8af86575d3569c218f4e408f5ac
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.12.1 pkginfo/1.4.2 requests/2.20.0 setuptools/40.5.0 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.6.5

Security for DateTimeRange 0.4.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

0.4.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