Seconds between datestimes excluding weekends and evenings The basic idea here is that I want to measure the number of seconds between two python datetime objects. However, I only want to count hours between 8:00 and 17:00, as well as skipping weekends (saturday and sunday). This works, but I wondered if anyone had clever ideas to make it cleaner.

START_HOUR = 8
STOP_HOUR = 17
KEEP = (STOP_HOUR - START_HOUR)/24.0

def seconds_between(a, b):

    weekend_seconds = 0

    current = a
    while current &lt; b:
        current += timedelta(days = 1)
        if current.weekday() in (5,6):
            weekend_seconds += 24*60*60*KEEP



    a_stop_hour = datetime(a.year, a.month, a.day, STOP_HOUR)
    seconds = max(0, (a_stop_hour - a).total_seconds())
    b_stop_hour = datetime(b.year, b.month, b.day, STOP_HOUR)
    if b_stop_hour &gt; b:
        b_stop_hour = datetime(b.year, b.month, b.day-1, STOP_HOUR)

    seconds += (b - b_stop_hour).total_seconds()

    return (b_stop_hour - a_stop_hour).total_seconds() * KEEP + seconds - weekend_seconds
1. Issues

Your code fails in the following corner cases:


a and b on the same day, for example:

&gt;&gt;&gt; a = datetime(2012, 11, 22, 8)
&gt;&gt;&gt; a.weekday()
3          # Thursday
&gt;&gt;&gt; seconds_between(a, a + timedelta(seconds = 100))
54100.0    # Expected 100

a or b at the weekend, for example:

&gt;&gt;&gt; a = datetime(2012, 11, 17, 8)
&gt;&gt;&gt; a.weekday()
5          # Saturday
&gt;&gt;&gt; seconds_between(a, a + timedelta(seconds = 100))
21700.0    # Expected 0

a after STOP_HOUR or b before START_HOUR, for example:

&gt;&gt;&gt; a = datetime(2012, 11, 19, 23)
&gt;&gt;&gt; a.weekday()
0          # Monday
&gt;&gt;&gt; seconds_between(a, a + timedelta(hours = 2))
28800.0    # Expected 0



Also, you count the weekdays by looping over all the days between the start and end of the interval. That means that the computation time is proportional to the size of the interval:

&gt;&gt;&gt; from timeit import timeit
&gt;&gt;&gt; a = datetime(1, 1, 1)
&gt;&gt;&gt; timeit(lambda:seconds_between(a, a + timedelta(days=999999)), number=1)
1.7254137992858887


For comparison, in this extreme case the revised code below is about 100,000 times faster:

&gt;&gt;&gt; timeit(lambda:office_time_between(a, a + timedelta(days=999999)), number=100000)
1.6366889476776123


The break even point is about 4 days:

&gt;&gt;&gt; timeit(lambda:seconds_between(a, a + timedelta(days=4)), number=100000)
1.5806620121002197
&gt;&gt;&gt; timeit(lambda:office_time_between(a, a + timedelta(days=4)), number=100000)
1.5950188636779785


2. Improvements

barracel's answer has two very good ideas, which I adopted:


compute the sum in seconds rather than days;
add up whole days and subtract part days if necessary.


and I made the following additional improvements:


handle corner cases correctly;
run in constant time regardless of how far apart a and b are;
compute the sum as a timedelta object rather than an integer;
move common code out into functions for clarity;
docstrings!


3. Revised code

from datetime import datetime, timedelta

def clamp(t, start, end):
    "Return `t` clamped to the range [`start`, `end`]."
    return max(start, min(end, t))

def day_part(t):
    "Return timedelta between midnight and `t`."
    return t - t.replace(hour = 0, minute = 0, second = 0)

def office_time_between(a, b, start = timedelta(hours = 8),
                        stop = timedelta(hours = 17)):
    """
    Return the total office time between `a` and `b` as a timedelta
    object. Office time consists of weekdays from `start` to `stop`
    (default: 08:00 to 17:00).
    """
    zero = timedelta(0)
    assert(zero &lt;= start &lt;= stop &lt;= timedelta(1))
    office_day = stop - start
    days = (b - a).days + 1
    weeks = days // 7
    extra = (max(0, 5 - a.weekday()) + min(5, 1 + b.weekday())) % 5
    weekdays = weeks * 5 + extra
    total = office_day * weekdays
    if a.weekday() &lt; 5:
        total -= clamp(day_part(a) - start, zero, office_day)
    if b.weekday() &lt; 5:
        total -= clamp(stop - day_part(b), zero, office_day)
    return total