Clean merge of fixed mysql datetime handling

Rebased version of #317