Time Window
Time Window is a small Python library that implements a representation for a
period of time, in the form of a half-open interval [since, until).
Installation
Install using pip from PyPI.
pip install time-window
Usage
Instantiate a new TimeWindow object using two datetime.datetime objects for
the boundaries.
>>> from datetime import datetime
>>> from time_window import TimeWindow
>>> since = datetime(2019, 1, 23)
>>> until = datetime(2019, 1, 29)
>>> tw = TimeWindow(since, until)
>>> tw
TimeWindow(datetime.datetime(2019, 1, 23, 0, 0), datetime.datetime(2019, 1, 29, 0, 0))
Alternatively, instantiate a new TimeWindow object using a datetime.datetime object and one
datetime.timedelta for the definition of the boundaries.
>>> from datetime import timedelta
>>> delta = timedelta(days=1)
>>> tw = TimeWindow.from_timedelta(since, delta)
>>> tw
TimeWindow(datetime.datetime(2019, 1, 23, 0, 0), datetime.datetime(2019, 1, 24, 0, 0))
Get the size of the window.
>>> tw.delta
datetime.timedelta(1)
Get the time that is in the middle of the window.
>>> tw.middle
datetime.datetime(2019, 1, 23, 12, 0)
You can also check if two TimeWindow objects overlap.
>>> tw = TimeWindow(datetime(2019, 1, 23), datetime(2019, 1, 29))
>>> tw2 = TimeWindow(datetime(2019, 1, 27), datetime(2019, 1, 30))
>>> tw.overlaps(tw2)
True
Complementary to the above action, you can check if two time windows are contiguous (i.e., adjacent, sharing one boundary).
>>> tw = TimeWindow(datetime(2019, 1, 23), datetime(2019, 1, 29))
>>> tw2 = TimeWindow(datetime(2019, 1, 20), datetime(2019, 1, 23))
>>> tw.contiguous(tw2)
[TimeWindow(datetime.datetime(2019, 1, 20, 0, 0), datetime.datetime(2019, 1, 23, 0, 0)),
TimeWindow(datetime.datetime(2019, 1, 23, 0, 0), datetime.datetime(2019, 1, 29, 0, 0))]
>>> tw3 = TimeWindow(datetime(2019, 1, 20), datetime(2019, 1, 21))
>>> tw.contiguous(tw3)
False
Any TimeWindow object offers some of the standard set operations.
>>> tw = TimeWindow(datetime(2019, 1, 23), datetime(2019, 1, 29))
>>> tw2 = TimeWindow(datetime(2019, 1, 27), datetime(2019, 1, 30))
>>> tw.intersection(tw2)
TimeWindow(datetime.datetime(2019, 1, 27, 0, 0), datetime.datetime(2019, 1, 29, 0, 0))
>>> tw.union(tw2)
TimeWindow(datetime.datetime(2019, 1, 23, 0, 0), datetime.datetime(2019, 1, 30, 0, 0))
>>> tw.complement(tw2)
TimeWindow(datetime.datetime(2019, 1, 23, 0, 0), datetime.datetime(2019, 1, 27, 0, 0))
Contributing
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
Please make sure to update tests as appropriate.
Metadata
Release files for time-window 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| time-window-0.1.0.tar.gz | 11.0 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| time_window-0.1.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 22.9 kB
Release files / time-window-0.1.0.tar.gz
| Download URL | time-window-0.1.0.tar.gz |
|---|---|
| Size | 11.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
c8e1c679746daa4bdbf354ee6c138b385c85634ca03846ce954e73bd073cdaa4
|
|
BLAKE2b-256 checksum How to use checksums |
923aa35c4a0cecd674c63896a1ea7ecd57e516cc8784b8d70efea141277858a9
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.1.3 requests-toolbelt/0.9.1 tqdm/4.45.0 CPython/3.6.9
|
Release files / time_window-0.1.0-py3-none-any.whl
| Download URL | time_window-0.1.0-py3-none-any.whl |
|---|---|
| Size | 11.9 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
7df99f7aa548ec7068a87be1754087639821d0a283d5d80250a9be9354bb6f72
|
|
BLAKE2b-256 checksum How to use checksums |
155df5510e72eb5556a7ab11cead681cc8820bd0feb145b3c0db126f9eddb1f0
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.1.3 requests-toolbelt/0.9.1 tqdm/4.45.0 CPython/3.6.9
|