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.
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
Installation
Installation: pip
pip install DateTimeRange
Installation: conda
conda install -c conda-forge datetimerange
Dependencies
Documentation
Sponsors
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file DateTimeRange-2.0.0.tar.gz.
File metadata
- Download URL: DateTimeRange-2.0.0.tar.gz
- Upload date:
- Size: 14.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.10.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2aa7bc652ed2dbe0750860c564a79f930c6e3337f937eeee428884130aeff262
|
|
| MD5 |
4e1bcd5dcc47876a2321566296d0303d
|
|
| BLAKE2b-256 |
7083c8ce0fd619ed0d692762ea095e3bcba42f997bf08384cc37dd58b19048bc
|
File details
Details for the file DateTimeRange-2.0.0-py3-none-any.whl.
File metadata
- Download URL: DateTimeRange-2.0.0-py3-none-any.whl
- Upload date:
- Size: 8.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.10.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
11cdf58f8bf0acc1969eb525cab88c0e7f68e0f9e1ee69e651be38e6ecdd0dbb
|
|
| MD5 |
ce50f6e3740dc53c5d4ed9a2f96698ca
|
|
| BLAKE2b-256 |
b472b32611afebc95626f59100278a6110a1f2169764436c1e3fa517653f94ab
|