No project description provided
Project description
OpenDateRange
This is a Python package that provides date ranges which allows open borders. Thereby it is possible to create date ranges that contain all dates up to a certain date or all dates from a certain date on. Of course date ranges with concrete dates as borders are supported as well.
Technical information
The package was developed with python 2.7.18 and only uses the build in python packages abc and datetime.
Installation
$ pip install OpenDateRange
Description of the provided Interface
This package provides only one class which is the DateRange class. The following sections describe how to use this class and how to interact with its instances.
- Date formats
- Creating date ranges
- Contains operator
- Intersection between date ranges
- Infinite date ranges
- Date range iterator
- Date range length
Date formats
The package supports all date formats that are supported by the build in datetime package. The date format can be changed via the static attribute DATE_FORMAT that belongs to the DateRange class and is predefined as "%Y-%m-%d". When the date format is changed all dates that are involved in interactions with instances of the DateRange class must match the new date format. Furthermore, all dates that are returned by any methods of the DateRange class match the new format as well.
Example
from openDateRange import DateRange
# change date format from %Y-%m-%d to %Y/%m/%d
DateRange.DATE_FORMAT = "%Y/%m/%d"
All DateRange instances now only accept and output dates in %Y/%m/%d format.
Creating date ranges
A DateRange instance takes two parameters. A date from where the range should start and a date up to which the range should go. If one of these parameters is set to None the corresponding border will be an open border. Both, the start and the end date are included in the date range.
Example
from openDateRange import DateRange
# holds all dates from 2000-12-12 up to 2001-12-12
dr1 = DateRange(date_from="2000-12-12", date_to="2001-12-12")
# holds all dates from 2000-12-12 on
dr2 = DateRange(date_from="2000-12-12", date_to=None)
# holds all dates up to 2001-12-12
dr3 = DateRange(date_from=None, date_to="2001-12-12")
# holds all dates
dr4 = DateRange(date_from=None, date_to=None)
Contains operator
Pythons in operator can be used to proof whether a date is contained in
a date range.
Example
from openDateRange import DateRange
dr = DateRange(date_from="2000-12-12", date_to=None)
# true
print("2000-12-12" in dr)
# true
print("2040-12-12" in dr)
# false
print("1990-12-12" in dr)
Intersection between date ranges
The intersects method of the DateRange class takes in
two borders that span a date range and proofs if the two date
ranges intersect. The method returns true if at least
one date is contained in both date ranges.
Example
from openDateRange import DateRange
dr = DateRange(date_from="2000-12-12", date_to="2001-12-12")
# true
dr.intersects(date_from="2001-05-05", date_to="2002-06-06")
# false
dr.intersects(date_from="2002-12-12", date_to="2003-12-12")
Infinite date ranges
Date ranges that have at least one open border are called to be infinite.
The following functions can not be called on infinite date ranges. These
include the iterator functionality and the len() function. Due to the fact
that an iterator on an infinite date range would be an endless loop and
the length of an infinite date range is not defined. The is_infinite()
method of the DateRange class proofs whether a DateRange instance is infinite.
Date range iterator
If a date range is finite it is possible to iterate over the date range using the build in iterator functionalities. The iterator returns each date as a String of the format that is currently set.
Example
from openDateRange import DateRange
dr = DateRange(date_from="2000-12-12", date_to="2000-12-15")
print([date for date in dr])
# output: ["2000-12-21", "2000-12-13", "2000-12-14", "2000-12-15"]
Date range length
A finite date range also has a length which is just the number of dates it contains. The length can be determined with the build in len() function. Note that the length of a date range can not change so the length of a date range is calculated in the first call of the len() function and is just read from memory in the following calls. Due to this implementation the first call of the len() function may be a little slower especially for very long date ranges.
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
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file OpenDateRange-1.0.1.tar.gz.
File metadata
- Download URL: OpenDateRange-1.0.1.tar.gz
- Upload date:
- Size: 5.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.8.10
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
07b50622950963d03f7f27c36e9b83682d5aed16193ed963340a8072fc6a4bae
|
|
| MD5 |
684a430053ad7f10cb425e8b73cb1200
|
|
| BLAKE2b-256 |
746a6bfff769ee8f7bcfa03479397f8668572da9481962f0b18510c41dbb9677
|
File details
Details for the file OpenDateRange-1.0.1-py3-none-any.whl.
File metadata
- Download URL: OpenDateRange-1.0.1-py3-none-any.whl
- Upload date:
- Size: 6.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.8.10
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4e5f320609a1e53d1a6bef71eeb101e6ad046696e911263bca7ea8e0bbedc882
|
|
| MD5 |
79487a8f59b2819f68a69d5d026b8e62
|
|
| BLAKE2b-256 |
356ac5c91b5dfacb03dd88a12e85f0a1dccb31f36a40aec5e2452871180ee5d7
|