Reference

The business_calendar contains the main class Calendar.

This module doesn’t require any third-party package but will use dateutil for parsing if it is present. For testing however, nose and dateutil are required.

In this module we adopt weekdays() notation, so Monday corresponds to 0 and Sunday corresponds to 6, therefore there is a natural index of days of the week as a list.

As default, dateutil.parser.parse is used as parser if dateutil is found. Otherwise, a simple parser function expecting %Y-%m-%d is used. You may override the parse function by assigning to the module variable parsefun.

Classes:
Calendar
Constants:
MO, TU, WE, TH, FR, SA, SU, FOLLOWING, PREVIOUS, MODIFIEDFOLLOWING
Public Functions:
parsefun

Warning

CalendarHolidayWarning

The Calendar class

class business_calendar.Calendar(workdays=None, holidays=None)

Class that represents a calendar with work and rest days, as well as holidays (which of course are rest days).

Note

All functions will accept either a str, a datetime.datetime or a datetime.date, so this class is interactive enviroment-friendly. However, functions will return a proper datetime.datetime object whenever the argument is a str object.

addbusdays(date, offset)

Add business days to a given date, taking holidays into consideration.

Note

By definition, a zero offset causes the function to return the initial date, even it is not a business date. An offset of 1 represents the next business date, regardless of date being a business date or not.

Parameters:
  • date (date, datetime or str) – Date to be incremented.
  • offset (integer) – Number of business days to add. Positive values move the date forward and negative values move the date back.
Returns:

New incremented date.

Return type:

datetime

addworkdays(date, offset)

Add work days to a given date, ignoring holidays.

Note

By definition, a zero offset causes the function to return the initial date, even it is not a work date. An offset of 1 represents the next work date, regardless of date being a work date or not.

Parameters:
  • date (date, datetime or str) – Date to be incremented.
  • offset (integer) – Number of work days to add. Positive values move the date forward and negative values move the date back.
Returns:

New incremented date.

Return type:

datetime

adjust(date, mode)

Adjust the date to the closest work date.

Parameters:
  • date (date, datetime or str) – Date to be adjusted.
  • mode (integer) – FOLLOWING, PREVIOUS or MODIFIEDFOLLOWING.

Note

If date is already a business date than it is returned unchanged. How to use the adjustment constants:

FOLLOWING:
Adjust to the next business date.
PREVIOUS:
Adjust to the previous business date.
MODIFIEDFOLLOWING:
Adjust to the next business date unless it falls on a different month, in which case adjust to the previous business date.
Returns:Adjusted date.
Return type:datetime
busdaycount(date1, date2)

Count business days between two dates (private), taking holidays into consideration.

Parameters:
  • date1 (date, datetime or str) – Date start of interval.
  • date2 (date, datetime or str) – Date end of interval.

Note

The adopted notation is COB to COB, so effectively date1 is not included in the calculation result.

Example

>>> cal = Calendar()
>>> date1 = datetime.datetime.today()
>>> date2 = cal.addbusdays(date1, 1)
>>> cal.busdaycount(date1, date2)
1
Returns:
Number of business days between the two dates. If the dates
are equal the result is zero. If date1 > date2 the result is negative.
Return type:int
buseom(date)

Adjust date to last business day of the month, taking holidays into consideration.

Parameters:date (date, datetime or str) – Date to be adjusted.
Returns:Adjusted date.
Return type:datetime
static caleom(date)

Adjust date to last day of the month, regardless of work days.

Parameters:date (date, datetime or str) – Date to be adjusted.
Returns:Adjusted date.
Return type:datetime
isbusday(date)

Check if a given date is a business date, taking into consideration the work days and holidays.

Parameters:date (date, datetime or str) – Date to be checked.
Returns:True if the date is a business date, False otherwise.
Return type:bool
isholiday(date)

Check if a given date is a holiday.

Parameters:date (date, datetime or str) – Date to be checked.
Returns:True if the date is a holiday, False otherwise.
Return type:bool
isworkday(date)

Check if a given date is a work date, ignoring holidays.

Parameters:date (date, datetime or str) – Date to be checked.
Returns:True if the date is a work date, False otherwise.
Return type:bool
range(date1, date2)

Generate business days between two dates, taking holidays into consideration.

Parameters:
  • date1 (date, datetime or str) – Date start of interval.
  • date2 (date, datetime or str) – Date end of interval, not included.

Note

All business days between date1 (inc) and date2 (exc) are returned, and date2 must be bigger than date1.

Yields:datetime – Business days in the specified range.
workdaycount(date1, date2)

Count work days between two dates, ignoring holidays.

Parameters:
  • date1 (date, datetime or str) – Date start of interval.
  • date2 (date, datetime or str) – Date end of interval.

Note

The adopted notation is COB to COB, so effectively date1 is not included in the calculation result.

Example

>>> cal = Calendar()
>>> date1 = datetime.datetime.today()
>>> date2 = cal.addworkdays(date1, 1)
>>> cal.workdaycount(date1, date2)
1
Returns:
Number of work days between the two dates. If the dates
are equal the result is zero. If date1 > date2 the result is negative.
Return type:int