bdateutil
Adds business day logic and improved data type flexibility to python-dateutil.
100% backwards compatible with python-dateutil, simply replace dateutil
imports with bdateutil.
Example Usage
# Test if a date is a business day
>>> from bdateutil import isbday
>>> isbday(date(2014, 1, 1))
True
# Date parameters are no longer limited to datetime objects
>>> isbday("2014-01-01")
True
>>> isbday("1/1/2014")
True
>>> isbday(1388577600) # Unix timestamp = Jan 1, 2014
True
# Take into account U.S. statutory holidays
>>> import holidays
>>> isbday("2014-01-01", holidays=holidays.US())
False
# Increment date by two business days
>>> from bdateutil import relativedelta
>>> date(2014, 7, 3) + relativedelta(bdays=+2)
datetime.date(2014, 7, 7)
# Any arguments that take a date/datetime object now accept
# strings/unicode/bytes in any encoding and integer/float timestamps.
# All dateutil functions now also take an optional `holidays` argument
# for helping to work with business days.
>>> "2014-07-03" + relativedelta(bdays=+2, holidays=holidays.US())
datetime.date(2014, 7, 8)
# Determine how many business days between two dates
>>> relativedelta("2014-07-07", date(2014, 7, 3))
relativedelta(days=+4, bdays=+2)
# Take into account Canadian statutory holidays
>>> from holidays import Canada
>>> relativedelta('2014-07-07', '07/03/2014', holidays=Canada())
relativedelta(days=+4, bdays=+1)
# Get a list of the next 10 business days starting 2014-01-01
>>> from bdateutil import rrule, BDAILY
>>> list(rrule(BDAILY, count=10, dtstart=date(2014, 1, 1)))
# Take into account British Columbia, Canada statutory holidays
>>> list(rrule(BDAILY, count=10, dtstart=date(2014, 1, 1),
holidays=Canada(prov='BC')))
Install
The latest stable version can always be installed or updated via pip:
$ pip install bdateutil
If the above fails, please use easy_install instead:
$ easy_install bdateutil
Documentation
This section will outline the additional functionality of bdateutil only. For full documentation on the features provided by python-dateutil please see its documentation at https://labix.org/python-dateutil.
bdateutil is 100% backwards compatible with python-dateutil. You can replace
dateutil with bdateutil across your entire project and
everything will continue to work the same but you will have access to the
following additional features:
A new, optional, keyword argument
bdaysis available when using relativedelta to add or remove time to a datetime object.
>>> date(2014, 1, 1) + relativedelta(bdays=+5)
date(2014, 1, 8)
When passing two datetime arguments to relativedelta, the resulting relativedelta object will contain a
bdaysattribute with the number of business days between the datetime arguments.
>>> relativedelta(date(2014, 7, 7), date(2014, 7, 3))
relativedelta(days=+4, bdays=+2)
Another new, optional, keyword argument
holidaysis available when using relativedelta to support thebdaysfeature. Without holidays business days are only calculated using weekdays. By passing a list of holidays a more accurate and useful business day calculation can be performed. The Python packageholidays.pyis installed as a requirement with bdateutil and that is the prefered way to generate holidays.
>>> from bdateutil import relativedelta
>>> from holidays import UnitedStates
>>> date(2014, 7, 3) + relativedelta(bdays=+2)
datetime.date(2014, 7, 7)
>>> date(2014, 7, 3) + relativedelta(bdays=+2, holidays=UnitedStates())
datetime.date(2014, 7, 8)
A new function
isbdaywhich returnsTrueif the argument passed to it falls on a business day andFalseif it is a weekend or holiday. Option keyword argumentholidaysadds the ability to take into account a specific set of holidays.
>>> from bdateutil import isbday
>>> isbday(date(2014, 1, 1))
True
>>> isbday("2014-01-01")
True
>>> isbday("1/1/2014")
True
>>> isbday(1388577600) # Unix timestamp = Jan 1, 2014
True
# Take into account U.S. statutory holidays
>>> import holidays
>>> isbday("2014-01-01", holidays=holidays.US())
False
In addition to
datetimeanddatetypes, relativedelta works with all strings/bytes regardless of encoding and integer/float timestamps. It does this by running all date/datetime parameters through theparsefunction which has been modified to accept many different types than strings, including date/datetime which will return without modifications. This allows you to callparse(dt)on an object regardless of type and ensure a datetime object is returned.
>>> parse(date(2014, 1, 1))
datetime.date(2014, 1, 1)
>>> parse(datetime(2014, 1, 1))
datetime.datetime(2014, 1, 1, 0, 0)
>>> parse("2014-01-01")
datetime.datetime(2014, 1, 1, 0, 0)
>>> parse("1/1/2014")
datetime.datetime(2014, 1, 1, 0, 0)
>>> parse(1388577600)
datetime.datetime(2014, 1, 1, 0, 0)
>>> relativedelta('2014-07-07', '2014-07-03')
relativedelta(days=+4, bdays=+2)
>>> 1388577600 + relativedelta(days=+2)
date(2014, 1, 3)
The
rrulefeature has a newBDAILYoption for use as thefreqargument. This will create a generator which yields business days. Rrule also will now accept an optionalholidayskeyword argument which affects theBDAILYfreq only. The existingdtstartanduntilarugments can now be passed as any type resembling a date/datetime.
# Get a list of the next 10 business days starting 2014-01-01
>>> from bdateutil import rrule, BDAILY
>>> list(rrule(BDAILY, count=10, dtstart=date(2014, 1, 1)))
# Get a list of all business days in January 2014, taking into account
# Canadian statutory holidays
>>> import holidays
>>> list(rrule(BDAILY, dtstart="2014-01-01", until="2014-01-31",
holidays=holidays.Canada()))
Import shortcuts are available that make importing the bdateutil features a little easier than python-dateutil. However, importing from bdateutil using the longer method used by python-dateutil still works to remain 100% backwards compatibility.
>>> # Importing relativedelta from the original python-dateutil package
>>> from dateutil.relativedelta import relativedelta
>>> # This method works with bdateutil
>>> from bdateutil.relativedelta import relativedelta
>>> # bdateutil also provides an easier way
>>> from bdateutil import relativedelta
Development Version
The latest development version can be installed directly from GitHub:
$ pip install --upgrade https://github.com/ryanss/bdateutil/tarball/master
Running Tests
$ pip install flake8
$ flake8 bdateutil/*.py tests.py --ignore=F401,F403
$ python tests.py
Coverage
$ pip install coverage
$ coverage run --omit=*site-packages* tests.py
$ coverage report
Contributions
Issues and Pull Requests are always welcome.
License
Code and documentation are available according to the MIT License (see LICENSE).
Metadata
Release files for bdateutil 0.1
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 | Actions |
|---|---|---|---|---|
| bdateutil-0.1.tar.gz | 24.1 kB | Without provenance | Details for bdateutil-0.1.tar.gz |
Release files / bdateutil-0.1.tar.gz
| Download URL | bdateutil-0.1.tar.gz |
|---|---|
| Size | 24.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
6dda0c79ed5d80f7c0972d62e6147e6d3a625e5d0ec2b77d0d69076b663ccc07
|
|
BLAKE2b-256 checksum How to use checksums |
39a444e289969e9826f25b3eeb30eacfdde6e175cc88bb54e1bada2431a8eb89
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
Security for bdateutil 0.1
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.