optimization in datetime function