An exact solution for electrons in a time-dependent magnetic field