Skip to main content

Timedelta for business time. Supports exact amounts of time (hours, seconds), custom schedules, holidays, and time zones.

Project description

BusinessTimeDelta

Python's timedelta for business time. This module helps you calculate the exact working time between two datetimes. It supports common scenarios such as custom schedules, holidays, and time zones.

Build Status

Installation

Use pip to install BusinessTimeDelta.

pip install businesstimedelta

Example Use

Define your business hours

import datetime
import pytz
import businesstimedelta

# Define a working day
workday = businesstimedelta.WorkDayRule(
    start_time=datetime.time(9),
    end_time=datetime.time(18),
    working_days=[0, 1, 2, 3, 4])

# Take out the lunch break
lunchbreak = businesstimedelta.LunchTimeRule(
    start_time=datetime.time(12),
    end_time=datetime.time(13),
    working_days=[0, 1, 2, 3, 4])

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

Calculate the business time between two datetimes

start = datetime.datetime(2016, 1, 18, 9, 0, 0)
end = datetime.datetime(2016, 1, 22, 18, 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

Business time arithmetic

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

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

To define holidays, simply use the Holidays package

import holidays as pyholidays

ca_holidays = pyholidays.US(state='CA')
holidays = businesstimedelta.HolidayRule(ca_holidays)
businesshrs = businesstimedelta.Rules([workday, lunchbreak, holidays])

# Christmas is on Friday 2015/12/25
start = datetime.datetime(2015, 12, 21, 9, 0, 0)
end = datetime.datetime(2015, 12, 28, 9, 0, 0)
print businesshrs.difference(start, end)
# <BusinessTimeDelta 32 hours 0 seconds>

Timezones

If your datetimes are not timezone aware, they will be localized to UTC (see example above).

Let's say you want to calculate the business time overlap between a working day in San Francisco and in Santiago, Chile:

santiago_workday = businesstimedelta.WorkDayRule(
    start_time=datetime.time(9),
    end_time=datetime.time(18),
    working_days=[0, 1, 2, 3, 4],
    tz=pytz.timezone('America/Santiago'))

santiago_lunchbreak = businesstimedelta.LunchTimeRule(
    start_time=datetime.time(12),
    end_time=datetime.time(13),
    working_days=[0, 1, 2, 3, 4],
    tz=pytz.timezone('America/Santiago'))

santiago_businesshrs = businesstimedelta.Rules([santiago_workday, santiago_lunchbreak])

sf_tz = pytz.timezone('America/Los_Angeles')
sf_start = sf_tz.localize(datetime.datetime(2016, 1, 18, 9, 0, 0))
sf_end = sf_tz.localize(datetime.datetime(2016, 1, 18, 18, 0, 0))

print santiago_businesshrs.difference(sf_start, sf_end)
# <BusinessTimeDelta 4 hours 0 seconds>

Overnight Shifts

# Day shift
workday = WorkDayRule(
    start_time=datetime.time(9),
    end_time=datetime.time(17),
    working_days=[0, 1, 2, 3, 4],
    tz=pytz.utc)

# Night shift
nightshift = businesstimedelta.WorkDayRule(
    start_time=datetime.time(23),
    end_time=datetime.time(7),
    working_days=[0, 1, 2, 3, 4])

businesshrs = businesstimedelta.Rules([workday, nightshift])

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

print bdiff
# <BusinessTimeDelta 80 hours 0 seconds>

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

businesstimedelta-1.0.1.tar.gz (5.8 kB view details)

Uploaded Source

Built Distribution

businesstimedelta-1.0.1-py3-none-any.whl (8.6 kB view details)

Uploaded Python 3

File details

Details for the file businesstimedelta-1.0.1.tar.gz.

File metadata

  • Download URL: businesstimedelta-1.0.1.tar.gz
  • Upload date:
  • Size: 5.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.35.0 CPython/3.6.5

File hashes

Hashes for businesstimedelta-1.0.1.tar.gz
Algorithm Hash digest
SHA256 93f41276f7a05592a8d6aa1e20ae0c013d504e6f346bfc433df2ac6ca65f03b4
MD5 9216818cc3513bce93ea940ecb88d78e
BLAKE2b-256 55cdb7d60a91db23d2de3e7e3e915949dbf4e4ddd02ea2a6b8b8ed27ce9adfac

See more details on using hashes here.

File details

Details for the file businesstimedelta-1.0.1-py3-none-any.whl.

File metadata

  • Download URL: businesstimedelta-1.0.1-py3-none-any.whl
  • Upload date:
  • Size: 8.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.35.0 CPython/3.6.5

File hashes

Hashes for businesstimedelta-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 3c90ecfc1e11c3493fd3dfebb0b35e998f29d45ea3ad54cbec5dc2c78ebc70d5
MD5 24e417314dafd66f15c935d8000b1b9a
BLAKE2b-256 93bd509890e3652e713186638b4784f6f325392ce8699ed1c16ddac5ffc20f11

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page