def count_interval_times(start_event, end_event, messages):
    """
    Count the time in all [start_event,end_event] intervals and return the sum.
    """
    interval_times = 0.0

    last_start_stamp = None
    for message in messages:
        if message.event == start_event:
            last_start_stamp = message.header.stamp
            continue
        elif message.event == end_event:
            if last_start_stamp == None:
                print("count_interval_time(): {0} not preceded by a {1}!".format(
                    end_event,
                    start_event))
                continue
            else:
                interval_time = message.header.stamp - last_start_stamp
                interval_secs = interval_time.secs + (interval_time.nsecs/1000000000.)
                print("{0}--{1}=={2}".format(start_event, end_event, interval_secs))
                interval_times += interval_secs
                last_start_stamp = None

    return interval_times