GithubHelp home page GithubHelp logo

seatgeek / businesstime Goto Github PK

View Code? Open in Web Editor NEW
84.0 104.0 36.0 63 KB

A simple python utility for calculating business time aware timedeltas between two datetimes

License: BSD 2-Clause "Simplified" License

Python 100.00%

businesstime's Introduction

businesstime

https://badge.fury.io/py/businesstime.png https://travis-ci.org/seatgeek/businesstime.png?branch=master

BusinessTime is a simple utility for calculating business time aware timedeltas between two datetimes. BusinessTime's understanding of weekends, holidays and business hours can be easily configured in code.

>>> datetime(2013, 12, 26, 5) - datetime(2013, 12, 23, 12)
datetime.timedelta(2, 61200)
>>> bt = businesstime.BusinessTime(holidays=businesstime.holidays.usa.USFederalHolidays())
>>> bt.businesstimedelta(datetime(2013, 12, 23, 12), datetime(2013, 12, 26, 5))
datetime.timedelta(1, 18000)

Features

  • Simple, pythonic, business-time-aware datetime math
  • A simple declarative format for defining holidays
  • A number of useful iterators/predicate functions related to holidays/weekends/business hours

businesstime's People

Contributors

agans avatar airsource avatar christopherdcunha avatar csoare7 avatar danc86 avatar erwaller avatar fjcapdevila avatar garetht avatar jeremyklein avatar josegonzalez avatar jwilhelm-godaddy avatar seanmcfeely avatar zackkitzmiller avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

businesstime's Issues

Bad calculation

start = datetime(2015,2,23,hour=16, minute=0)
end = datetime(2015,2,24,hour=14, minute=20)
bt = BusinessTime(weekends=(SAT, SUN), holidays=holidays, \
            business_hours=(time(7), time(16)))
        business_time = bt.businesstimedelta(start, end)

Output is 7:20:00

start = datetime(2015,2,23,hour=16, minute=1)

Output is 0:00:00

Tested on Python 3.4.0

one case not working


import businesstime

bt = businesstime.BusinessTime((time(9), time(18)), (5, 6), holidays=None)
t = bt.businesstimedelta(datetime.now(), datetime.now() + timedelta(days=1, hours=-4))
#t = bt.businesstimedelta(datetime.now() - timedelta(days=1), datetime.now() + timedelta(days=0, hours=0))
t = bt.businesstimedelta(datetime.now() - timedelta(hours=2), datetime.now() + timedelta(days=1, hours=0))
m = (float(t.days) * 9 * 60 + float(t.seconds) / 60)
h = m / 60

print h

Not working well if the two times are after business with one day of difference. It should return one business day.

Holiday generator misses previous holidays

When isholiday is called, the holiday generator is invoked. But the holiday generator maintains a cache, and it assumes that future calls to isholiday will be in the future (while len(self._holidays) == 0 or dt > self._holidays[-1]). However, my code iterates backward and breaks this, because the generator is "forward-only".

Timedelta to businesshours support

First of all, thank you very much for your work on this library.

I found myself slightly confused when trying to extract business hours from the timedelta result.

>>> d1 = datetime.datetime(2017, 5, 23, 9, 0)
>>> d2 = datetime.datetime(2017, 5, 24, 9, 0)
>>> r = BusinessTime().businesstimedelta(d1, d2)
datetime.timedelta(1)

My immediate thought to extract the business hours (which in this case it's 8) was to call
timedelta.total_seconds() method on the result, but this will return the equivalent of 24 hours in seconds, not 8 hours - given it has no knowledge of the business_hours in the BusinessTime object.

This experience proved a bit confusing and was wondering if you might be able to suggest a solution to this? Would a PR would be accepted for this sort of functionality?

Time delta calculation bug when both start and end times are after hours

This piece of code returns the wrong time delta:

>>> bt=BusinessTime()
>>> bt.businesstimedelta(datetime(2016, 6, 17, 18), datetime(2016, 6, 20, 18))
datetime.timedelta(0)

The start time is after hours on a Friday at 6pm, while the end time is after hours on the following Monday at 6pm. The correct answer should be datetime.timedelta(1), right?

Code breaks with TZ aware datetimes

Everything breaks with tz-aware datetimes. It would be good to take an optional tzinfo and to cast the datetimes into that TZ automatically.

More flexible specification for business hours

Currently you can only specify a pair of open and close times which apply on non-weekend, non-holiday days. It'd be useful to have open and close times which vary by day of the week, time of the year, etc.

Also, the concept of weekend days is fairly arbitrary. It probably makes sense to take this extension of the concept of flexible business hours to support days with no business hours, and have that replace the concept of weekends.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.