Fix encoding of timestamps and simplify date logic.