# 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](https://travis-ci.org/seppemans/businesstimedelta.svg?branch=master)](https://travis-ci.org/seppemans/businesstimedelta)
## Installation
Use pip to install BusinessTimeDelta.
```shell
pip install businesstimedelta
```
## Example Use
Define your business hours
```python
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
```python
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
```python
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](https://pypi.python.org/pypi/holidays) package
```python
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:
```python
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
```python
# 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>
```
Raw data
{
"_id": null,
"home_page": "http://github.com/seppemans/businesstimedelta",
"name": "businesstimedelta",
"maintainer": "",
"docs_url": null,
"requires_python": "",
"maintainer_email": "",
"keywords": "business working time timedelta hours businesstime businesshours",
"author": "seppemans",
"author_email": "",
"download_url": "https://files.pythonhosted.org/packages/55/cd/b7d60a91db23d2de3e7e3e915949dbf4e4ddd02ea2a6b8b8ed27ce9adfac/businesstimedelta-1.0.1.tar.gz",
"platform": "",
"description": "# BusinessTimeDelta\nPython'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.\n\n[![Build Status](https://travis-ci.org/seppemans/businesstimedelta.svg?branch=master)](https://travis-ci.org/seppemans/businesstimedelta)\n\n## Installation\nUse pip to install BusinessTimeDelta.\n\n```shell\npip install businesstimedelta\n```\n\n## Example Use\nDefine your business hours\n\n```python\nimport datetime\nimport pytz\nimport businesstimedelta\n\n# Define a working day\nworkday = businesstimedelta.WorkDayRule(\n start_time=datetime.time(9),\n end_time=datetime.time(18),\n working_days=[0, 1, 2, 3, 4])\n\n# Take out the lunch break\nlunchbreak = businesstimedelta.LunchTimeRule(\n start_time=datetime.time(12),\n end_time=datetime.time(13),\n working_days=[0, 1, 2, 3, 4])\n\n# Combine the two\nbusinesshrs = businesstimedelta.Rules([workday, lunchbreak])\n```\n\nCalculate the business time between two datetimes\n\n```python\nstart = datetime.datetime(2016, 1, 18, 9, 0, 0)\nend = datetime.datetime(2016, 1, 22, 18, 0, 0)\nbdiff = businesshrs.difference(start, end)\n\nprint bdiff\n# <BusinessTimeDelta 40 hours 0 seconds>\n\nprint \"%s hours and %s seconds\" % (bdiff.hours, bdiff.seconds)\n# 40 hours and 0 seconds\n```\n\nBusiness time arithmetic\n\n```python\nprint start + businesstimedelta.BusinessTimeDelta(businesshrs, hours=40)\n# 2016-01-22 18:00:00+00:00\n\nprint end - businesstimedelta.BusinessTimeDelta(businesshrs, hours=40)\n# 2016-01-18 09:00:00+00:00\n```\n\nTo define holidays, simply use the [Holidays](https://pypi.python.org/pypi/holidays) package\n\n```python\nimport holidays as pyholidays\n\nca_holidays = pyholidays.US(state='CA')\nholidays = businesstimedelta.HolidayRule(ca_holidays)\nbusinesshrs = businesstimedelta.Rules([workday, lunchbreak, holidays])\n\n# Christmas is on Friday 2015/12/25\nstart = datetime.datetime(2015, 12, 21, 9, 0, 0)\nend = datetime.datetime(2015, 12, 28, 9, 0, 0)\nprint businesshrs.difference(start, end)\n# <BusinessTimeDelta 32 hours 0 seconds>\n```\n\n## Timezones\nIf your datetimes are not timezone aware, they will be localized to UTC (see example above).\n\nLet's say you want to calculate the business time overlap between a working day in San Francisco and in Santiago, Chile:\n```python\nsantiago_workday = businesstimedelta.WorkDayRule(\n start_time=datetime.time(9),\n end_time=datetime.time(18),\n working_days=[0, 1, 2, 3, 4],\n tz=pytz.timezone('America/Santiago'))\n\nsantiago_lunchbreak = businesstimedelta.LunchTimeRule(\n start_time=datetime.time(12),\n end_time=datetime.time(13),\n working_days=[0, 1, 2, 3, 4],\n tz=pytz.timezone('America/Santiago'))\n\nsantiago_businesshrs = businesstimedelta.Rules([santiago_workday, santiago_lunchbreak])\n\nsf_tz = pytz.timezone('America/Los_Angeles')\nsf_start = sf_tz.localize(datetime.datetime(2016, 1, 18, 9, 0, 0))\nsf_end = sf_tz.localize(datetime.datetime(2016, 1, 18, 18, 0, 0))\n\nprint santiago_businesshrs.difference(sf_start, sf_end)\n# <BusinessTimeDelta 4 hours 0 seconds>\n```\n\n## Overnight Shifts\n```python\n# Day shift\nworkday = WorkDayRule(\n start_time=datetime.time(9),\n end_time=datetime.time(17),\n working_days=[0, 1, 2, 3, 4],\n tz=pytz.utc)\n\n# Night shift\nnightshift = businesstimedelta.WorkDayRule(\n start_time=datetime.time(23),\n end_time=datetime.time(7),\n working_days=[0, 1, 2, 3, 4])\n\nbusinesshrs = businesstimedelta.Rules([workday, nightshift])\n\nstart = datetime.datetime(2016, 1, 18, 9, 0, 0)\nend = datetime.datetime(2016, 1, 22, 18, 0, 0)\nbdiff = businesshrs.difference(start, end)\n\nprint bdiff\n# <BusinessTimeDelta 80 hours 0 seconds>\n```\n\n\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "Timedelta for business time. Supports exact amounts of time (hours, seconds), custom schedules, holidays, and time zones.",
"version": "1.0.1",
"project_urls": {
"Homepage": "http://github.com/seppemans/businesstimedelta"
},
"split_keywords": [
"business",
"working",
"time",
"timedelta",
"hours",
"businesstime",
"businesshours"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "93bd509890e3652e713186638b4784f6f325392ce8699ed1c16ddac5ffc20f11",
"md5": "24e417314dafd66f15c935d8000b1b9a",
"sha256": "3c90ecfc1e11c3493fd3dfebb0b35e998f29d45ea3ad54cbec5dc2c78ebc70d5"
},
"downloads": -1,
"filename": "businesstimedelta-1.0.1-py3-none-any.whl",
"has_sig": false,
"md5_digest": "24e417314dafd66f15c935d8000b1b9a",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": null,
"size": 8582,
"upload_time": "2019-09-07T17:27:07",
"upload_time_iso_8601": "2019-09-07T17:27:07.638176Z",
"url": "https://files.pythonhosted.org/packages/93/bd/509890e3652e713186638b4784f6f325392ce8699ed1c16ddac5ffc20f11/businesstimedelta-1.0.1-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "55cdb7d60a91db23d2de3e7e3e915949dbf4e4ddd02ea2a6b8b8ed27ce9adfac",
"md5": "9216818cc3513bce93ea940ecb88d78e",
"sha256": "93f41276f7a05592a8d6aa1e20ae0c013d504e6f346bfc433df2ac6ca65f03b4"
},
"downloads": -1,
"filename": "businesstimedelta-1.0.1.tar.gz",
"has_sig": false,
"md5_digest": "9216818cc3513bce93ea940ecb88d78e",
"packagetype": "sdist",
"python_version": "source",
"requires_python": null,
"size": 5780,
"upload_time": "2019-09-07T17:27:09",
"upload_time_iso_8601": "2019-09-07T17:27:09.238819Z",
"url": "https://files.pythonhosted.org/packages/55/cd/b7d60a91db23d2de3e7e3e915949dbf4e4ddd02ea2a6b8b8ed27ce9adfac/businesstimedelta-1.0.1.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2019-09-07 17:27:09",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "seppemans",
"github_project": "businesstimedelta",
"travis_ci": true,
"coveralls": false,
"github_actions": false,
"lcname": "businesstimedelta"
}