Support datetime precision

Backported:

    * https://github.com/rails/rails/pull/17673
    * https://github.com/rails/rails/pull/18067