def _zero_out_datetime(dt, unit):
    """To fix a super obnoxious issue where datetrunc (or SQLAlchemy) would
    break up resulting values if provided a datetime with nonzero values more
    granular than datetrunc expects. Ex. calling datetrunc('hour', ...) with
    a datetime such as 2016-09-20 08:12:12.

    Note that if any unit greater than an hour is provided, this method will
    zero hours and below, nothing more.

    :param dt: (datetime) to zero out
    :param unit: (str) from what unit of granularity do we zero
    :returns: (datetime) a well-behaved, non query-breaking datetime
    """
    units = ['year', 'month', 'day', 'hour', 'minute', 'second', 'microsecond']
    i = units.index(unit) + 1
    for zeroing_unit in units[i:]:
        try:
            dt = dt.replace(**{zeroing_unit: 0})
        except ValueError:
            pass
    return dt