Turns a datetime.time object into a string. The string must have one of the
    formats from VALID_TIME_FORMATS_TEXT to make it compatible with 
    timestr2time.

    Args:
        time (datetime.time) the time to be translated
        fmt (str) a format string.
    Returns:
        (str) that represents a time.
    Raises:
        ValueError if the format is not valid.

def time2timestr(time, fmt='hhmmss'):
    '''
    Turns a datetime.time object into a string. The string must have one of the
    formats from VALID_TIME_FORMATS_TEXT to make it compatible with 
    timestr2time.

    Args:
        time (datetime.time) the time to be translated
        fmt (str) a format string.
    Returns:
        (str) that represents a time.
    Raises:
        ValueError if the format is not valid.
    '''
    if fmt.count(':') == 2:
        if not fmt.index('h') < fmt.index('m') < fmt.index('s'):
            raise ValueError('Invalid format string. {}'.format(
                    VALID_TIME_FORMATS_TEXT))
        h, m, s = fmt.split(':')
    elif fmt.count(':') == 1:
        if not fmt.index('h') < fmt.index('m'):
            raise ValueError('Invalid format string. {}'.format(
                    VALID_TIME_FORMATS_TEXT))
        h, m = fmt.split(':')
        s = None
    elif any(c not in 'hms' for c in fmt) or len(fmt) != 6:
        raise ValueError('Invalid character in format string. {}'.format(
                VALID_TIME_FORMATS_TEXT))
    else:
        if not fmt.index('h') < fmt.index('m') < fmt.index('s'):
            raise ValueError('Invalid format string. {}'.format(
                    VALID_TIME_FORMATS_TEXT))
        h, m, s = fmt[:-4], fmt[-4:-2], fmt[-2:]
    for string, char in ((h, 'h'), (m, 'm'), (s, 's')):
        if string is not None and any(c != char for c in string):
            raise ValueError('Invalid date format: {} is not {}'.\
                    format(char, string))
    if len(h) == 2:
        fmt = fmt.replace('hh', '%H', 1)
    elif len(h) == 1:
        fmt = fmt.replace('h', 'X%H', 1)
    else:
        raise ValueError('Invalid format string, hour must have 1 or 2 digits')
    if len(m) == 2:
        fmt = fmt.replace('mm', '%M', 1)
    else:
        raise ValueError('Invalid format string, minutes must have 2 digits')
    if s is not None and len(s) == 2:
        fmt = fmt. replace('ss', '%S', 1)
    elif s is not None:
        raise ValueError('Invalid format string, seconds must have 2 digits')
    return time.strftime(fmt).replace('X0','X').replace('X','')