BusinessTimeDelta is a small python module I wrote to help you calculate the amount of business time (hours, seconds) between two datetimes. It supports custom schedules, holidays, and time zones.


At PartMyRide we handle a significant volume of orders for body shops and mechanics all over the country. One of our key advantages we provide to our professional customers is a guaranteed delivery date for every order. In order to make good predictions of arrival dates, I needed a way to calculate exactly how much time it takes for an order to be confirmed, fulfilled and shipped by our network of suppliers. Our suppliers are located in different time zones and follow different holiday schedules. No existing python module was suited particularly well for this use case.

A Short Example

First, define a schedule.

import datetime
import pytz
import businesstimedelta
import holidays as pyholidays

# Define a working day
workday = businesstimedelta.WorkDayRule(
    working_days=[0, 1, 2, 3, 4])

# Take out the lunch break
lunchbreak = businesstimedelta.LunchTimeRule(
    working_days=[0, 1, 2, 3, 4])

# Take out holidays in California
ca_holidays = pyholidays.US(state='CA')
holidays = businesstimedelta.HolidayRule(ca_holidays)

# Combine the two
businesshrs = businesstimedelta.Rules([workday, lunchbreak, holidays])

Then you can calculate the business time between two datetimes,

start = datetime.datetime(2016, 1, 18, 9, 0, 0)
end = datetime.datetime(2016, 1, 25, 9, 0, 0)
bdiff = businesshrs.difference(start, end)

print bdiff
# <BusinessTimeDelta 40 hours 0 seconds>

print "%s hours and %s seconds" % (bdiff.hours, bdiff.seconds)
# 40 hours and 0 seconds

or do some arithmetic.

print start + businesstimedelta.BusinessTimeDelta(businesshrs, hours=40)
# 2016-01-25 09:00:00+00:00

print end - businesstimedelta.BusinessTimeDelta(businesshrs, hours=40)
# 2016-01-15 18:00:00+00:00