import time
import logging


def getSeconds(time_str):
    """
    Convert a string of the form '%d-%H:%M:%S', '%H:%M:%S' or '%M:%S'
    to seconds.
    """

    if not time_str:
        return 0

    # sometimes the timestamp includs a fractional second part
    time_str = time_str.split('.')[0]

    if '-' in time_str:
        days, time_str = time_str.split('-')
        st = time.strptime(time_str, '%H:%M:%S')
        sec = int(days)*86400+st.tm_hour*3600+st.tm_min*60+st.tm_sec
    else:
        try:
            st = time.strptime(time_str, '%H:%M:%S')
            sec = st.tm_hour*3600+st.tm_min*60+st.tm_sec
        except ValueError:
            try:
                st = time.strptime(time_str, '%M:%S')
                sec = st.tm_min*60+st.tm_sec
            except ValueError:
                logging.error('String: %s does not match time format.' % time_str)
                return -1

    return sec