def time_format(num, digits=1, align_unit=False):
    # type: (float, int, bool) -> str
    """Format and scale output according to standard time
    units.

    Example
    -------

    >>> time_format(0)
    '0.0 s'
    >>> time_format(0, align_unit=True)
    '0.0 s '
    >>> time_format(0.002)
    '2.0 ms'
    >>> time_format(2001)
    '33.4 m'
    >>> time_format(2001, digits=3)
    '33.350 m'

    """
    if num is None:
        return "<unknown>"
    unit = "s"
    if (num >= 1.0) or (num == 0.0):
        if num >= 60.0:
            num /= 60.0
            unit = "m"
            if num >= 60.0:
                num /= 60.0
                unit = "h"
                if num >= 24.0:
                    num /= 24.0
                    unit = "d"
    else:
        num *= 1000.0
        for p in ["ms", "us", "ns", "ps", "fs"]:
            unit = p
            if abs(round(num, digits + 3)) >= 1:
                break
            num *= 1000.0
    if (len(unit) == 1) and align_unit:
        return ("%." + str(digits) + "f %s ") % (num, unit)
    else:
        return ("%." + str(digits) + "f %s") % (num, unit)