def to_sec(v):
    """
    Convert millisecond, microsecond or nanosecond to second.

    Args:
        v: timestamp in int, long, float or string.
            It can be a timestamp in second, millisecond(10e-3),
            microsecond(10e-6) or nanosecond(10e-9).

    Returns:
        int: timestamp in second.

    Raises:
        ValueError:  If `v` is not a valid timestamp.
    """

    v = float(str(v))

    if (not isinstance(v, float) or v < 0):
        raise ValueError('invalid time to convert to second: {v}'.format(v=v))

    l = len(str(int(v)))

    if l == 10:
        return int(v)
    elif l == 13:
        return int(v / 1000)
    elif l == 16:
        return int(v / (1000**2))
    elif l == 19:
        return int(v / (1000**3))
    else:
        raise ValueError(
            'invalid time length, not 10, 13, 16 or 19: {v}'.format(v=v))