Rigorous and flexible statistical tests for correlations between stationary or nonstationary time series