Skip to main content

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.

Project description

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)
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 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/

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

DateTimeRange-0.5.2.tar.gz (11.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

DateTimeRange-0.5.2-py2.py3-none-any.whl (7.6 kB view details)

Uploaded Python 2Python 3

File details

Details for the file DateTimeRange-0.5.2.tar.gz.

File metadata

  • Download URL: DateTimeRange-0.5.2.tar.gz
  • Upload date:
  • Size: 11.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.21.0 setuptools/40.6.3 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.7.1

File hashes

Hashes for DateTimeRange-0.5.2.tar.gz
Algorithm Hash digest
SHA256 d4bf645d0508475348de67f3714e8a299e7cd98e31f04ca58b15021d4af002db
MD5 d0e5e40f0b3d42013e0af2a5ab46e088
BLAKE2b-256 78dcff4ad67419725cd02e71875a8b50fa71794a3f35a699d59a48d316aa0254

See more details on using hashes here.

File details

Details for the file DateTimeRange-0.5.2-py2.py3-none-any.whl.

File metadata

  • Download URL: DateTimeRange-0.5.2-py2.py3-none-any.whl
  • Upload date:
  • Size: 7.6 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.21.0 setuptools/40.6.3 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.7.1

File hashes

Hashes for DateTimeRange-0.5.2-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 0d0fcbcc2c9d6504411fdb8d1ce0631779a06e5ea78efc693e6fa890c89177c2
MD5 efd56c2ceda1e010683992802899e660
BLAKE2b-256 429f5bb298ffe844a0dc3c3ba83a0dd6865a5f68f1d9545af7124c0cecd8e505

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page