from datetime import datetime


def spiffInterval(begin=None, end=None, duration=None):
    """
    Determine a datetime interval given zero or more of the parameters
    begin, end, duration.

    The begin and end parameter values should be instances of the
    datetime.datetime or datetime.date class, or None.  Any dates
    (datetime.date) are converted to datetimes (datetime.datetime)
    with time set to 00:00:00.

    The duration parameter value should be an instance of the
    datetime.timedelta class, or None.  The duration is used to
    calculate a missing endpoint in case begin or end is None.
    The duration parameter is ignored if the caller specifies
    both begin and end.

    Returns a pair (begin, end) in which each element is either
    an instance of the datetime.datetime class or None.
    """
    if begin and not hasattr(begin, 'hour'):
        begin = datetime(begin.year, begin.month, begin.day)
    if end and not hasattr(end, 'hour'):
        end = datetime(end.year, end.month, end.day)

    if (begin is None or end is None) and duration is not None:
        if begin:
            end = begin + duration
        elif end:
            begin = end - duration
        else:
            # Neither begin nor end was given.  Let default interval
            # begin at the current date and time minus the duration.
            # Let the interval remain unbounded at the high end.
            begin = datetime.now() - duration

    return begin, end