Skip to 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.

License

Notifications You must be signed in to change notification settings

thombashi/DateTimeRange

Repository files navigation

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

pip install DateTimeRange
conda install -c conda-forge datetimerange

Features of DateTimeRange class include:

  • Supported operations:
    • Equation
    • Addition
    • Subtraction
    • Intersection
    • Union
    • Contains
    • Truncate
    • Split
    • Iteration
  • Timezone support
  • Daylight saving time support
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'
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'
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
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
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
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
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
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)

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

https://datetimerange.rtfd.io/

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

Become a sponsor

About

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.

Topics

Resources

License

Stars

Watchers

Forks

Sponsor this project

 

Packages

No packages published