Fix datetime encoding