TimeFrame
Introduction
This package makes the following calculations on datetime:
- Adding two time frames, resulting in one bigger time frame or two disjoint one.
- Multiplying two time frames, resuling in either an overlapped time frame or an empty one, depending on the two time frames.
- Substracting two time frames, resuling in one or several time frames.
Install
Installing the package is as simple as running the following command inside your terminal:
pip install timeframe
Examples
NOTE: You can always take a look at the test cases in the tests directory to get a sense of how to use the package, but consider the below examples first, because it's fairly easy to use.
You need to import datetime as well as TimeFrame:
from datetime import datetime
from timeframe import TimeFrame
Inclusion
New API
>>> tf1 = TimeFrame(datetime(2021, 1, 1), datetime(2021, 1, 2))
>>> tf2 = TimeFrame(datetime(2021, 1, 1, 12), datetime(2021, 1, 1, 13))
>>> tf2 in tf1
True
Deprecated
This implies whether or not one time frame includes another; it can also be
used to check if a datetime is inside one TimeFrame.
When you want to check if a datetime is inside a TimeFrame:
tf1 = TimeFrame(datetime(2021, 1, 26, 19), datetime(2021, 1, 26, 20))
tf1.includes(datetime(2021, 1, 26, 19, 30))
# output: True
When You want to check if an instance of TimeFrame is inside another one:
tf2 = TimeFrame(datetime(2021, 1, 26, 19, 30), datetime(2021, 1, 26, 19, 40))
tf1.includes(tf2)
# output: True
tf3 = TimeFrame(datetime(2021, 1, 26, 19, 45), datetime(2021, 1, 26, 21, 30))
tf1.includes(tf3)
# output: False
Duration
TimeFrame has a property named duration which can be used to retrieve the
total amount of seconds that TimeFrame has:
tf1.duration
# output: 3600.0
tf2.duration
# output: 600.0
tf3.duration
# output: 6300.0
Comparison
You can always compare two TimeFrame to see if one is greater than the other or not.
This comparison is based on the end of one TimeFrame and the start of the other.
tf1 > tf2
# output: False
tf3 > tf2
# output: True
You can also compare equality using either greater-equal sign, or a simple equal.
tf1 == tf2
# output: False
tf3 >= tf2
# output: True
Overlap
When you want to know how much two time frames have in common, use multiply sign:
tf1 * tf2
# output: 2021-01-26T19:30:00#2021-01-26T19:40:00
tf2 * tf3
# output: Empty TimeFrame
You can also check their duration as well:
(tf1 * tf2).duration
# output: 600.0
(tf2 * tf3).duration
# output: 0.0
Summation (union)
The summation sign is used to get the union of two time frames:
tf1 + tf2
# output: 2021-01-26T19:00:00#2021-01-26T20:00:00
(tf1 + tf2).duration
# output: 3600.0
tf1 + tf3
# output: 2021-01-26T19:00:00#2021-01-26T21:30:00
(tf1 + tf3).duration
# output: 9000.0
Minus
You can also substract one time frame from the other, which will ultimately result in either two disjoint time frames, or one unified time frame, depending on the time frames.
tf1 - tf2
# output:
# 2021-01-26T19:00:00#2021-01-26T19:29:59.999999
# 2021-01-26T19:40:00.000001#2021-01-26T20:00:00
(tf1 - tf2).duration
# output: 2999.999998
Substracting two disjoint time frames will return the first time frame as a result.
tf2 - tf3
# output: 2021-01-26T19:30:00#2021-01-26T19:40:00
(tf2 - tf3).duration
# output: 600.0
(tf2 - tf3) == tf2
# output: True
Acknowledgment
Thank you for showing interest in this package. Feel free to contact me if you feel like it. 🥂
Contribution
Any contribution of any size is greatly appreciated. Feel free to open a PR or issue in the github page at any time. 🤗
Stargazers over time
Metadata
Release files for timeframe 3.0.3
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| timeframe-3.0.3.tar.gz | 13.0 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| timeframe-3.0.3-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 20.0 kB
Release files / timeframe-3.0.3.tar.gz
| Download URL | timeframe-3.0.3.tar.gz |
|---|---|
| Size | 13.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
515225c79e606e6cb6c39cc74fc265da876c37d59d47862fe797eceb735296ba
|
|
BLAKE2b-256 checksum How to use checksums |
88e24cc37698693e578981f186ee747479a6e3d3c30ec1323937832fd64b360f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.12.9
|
Release files / timeframe-3.0.3-py3-none-any.whl
| Download URL | timeframe-3.0.3-py3-none-any.whl |
|---|---|
| Size | 7.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
32f56b28addf7c40dd3ae53ef7f029247aaa49dd6405f6fab8a405854d2f4c66
|
|
BLAKE2b-256 checksum How to use checksums |
62ed3bef7419113436e2ba2b85727d0832c18776d359894e8a0365b8d54a0084
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.12.9
|
Security for timeframe 3.0.3
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.