Backports #12290 to 4-0-stable. Closes #12459

I realized that https://github.com/rails/rails/pull/12290 fixes #12459 in `4-0-stable`.

I've also added a test to ensure that datetime attributes don't get marked as dirty when you pass a string with a different timezone but same time point.