import re


def trip_duration_udf(duration):
    """Convert trip duration to seconds. Return None if negative."""
    if not duration:
        return None

    time = re.match(r"\d*.\d*", duration)

    if not time:
        return None

    time = float(time[0])

    if time < 0:
        return None

    if "m" in duration:
        time *= 60
    elif "h" in duration:
        time *= 60 * 60

    return int(time)