DateTimeRange is a python library to handle routine work associated with a time range, such as test whether a time is within the time range, get time range intersection, truncating the time range etc.
Project description
DateTimeRange
Summary
DateTimeRange is a python library to handle routine work associated with a time range, such as test whether a time is within the time range, get time range intersection, truncating the time range, etc.
Usage
Create and convert to string
from datetimerange import DateTimeRange
time_range = DateTimeRange("2015-03-22T10:00:00+0900", "2015-03-22T10:10:00+0900")
str(time_range)
'2015-03-22T10:00:00+0900 - 2015-03-22T10:10:00+0900'
Get iterator
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
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
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
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
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
True False True
Test whether a value intersect the time range
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)
True
Make an intersected time range
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
2015-03-22T10:05:00+0900 - 2015-03-22T10:10:00+0900
Make an encompassed time range
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
2015-03-22T10:00:00+0900 - 2015-03-22T10:15:00+0900
Truncate time range
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
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 http://datetimerange.readthedocs.org/en/latest/pages/examples/index.html
Examples with IPython Notebook is also available at http://nbviewer.jupyter.org/github/thombashi/DateTimeRange/tree/master/ipynb/DateTimeRange.ipynb
Installation
pip install DateTimeRange
Dependencies
Python 2.5+ or 3.3+
Test dependencies
Documentation
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
Hashes for DateTimeRange-0.2.1-py2.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2a3ef23117b9b15aab940a0a48f679cc47e2656bddd7fcb00427195b5c3375a7 |
|
MD5 | 7d3a96093c15bdf4c7ec61416702aa44 |
|
BLAKE2b-256 | b85b9b77f51d62d1dbbdaa2cfbc1e865c3b587142fec8a29e31ee63040b1e114 |