An index theorem for Lorentzian manifolds