Flask: Improved datetime calculation