Show "1 day" not "1 days"

Closes: 25
This commit is contained in:
Edward Betts 2023-10-21 07:16:21 +01:00
parent 7e439c1fdf
commit 6927cab4ef

View file

@ -2,10 +2,11 @@
"""Web page to show upcoming events.""" """Web page to show upcoming events."""
import functools
import inspect import inspect
import sys import sys
import traceback import traceback
from datetime import date, datetime, timezone from datetime import date, datetime
import flask import flask
import werkzeug import werkzeug
@ -44,22 +45,27 @@ def exception_handler(e: werkzeug.exceptions.InternalServerError) -> tuple[str,
) )
def delta_days(today: date, when: date) -> str:
"""Return number of days from today as a string."""
delta = (when - today).days
match delta:
case 0:
return "today"
case 1:
return "1 day"
case _:
return f"{delta:,d} days"
@app.route("/") @app.route("/")
def index() -> str: def index() -> str:
"""Index page.""" """Index page."""
now = datetime.now() now = datetime.now()
today = now.date()
data = get_data(now) data = get_data(now)
now_utc = datetime.now(timezone.utc) days = functools.partial(delta_days, now.date())
def days_hours(when: datetime) -> str: return flask.render_template("index.html", days=days, **data)
delta = when - (now if when.tzinfo is None else now_utc)
return f"{delta.days:>5,d} days {delta.seconds // 3600:>2.0f} hours"
def days(when: date) -> str:
return "today" if when == today else f"{(when - today).days:,d} days"
return flask.render_template("index.html", days=days, days_hours=days_hours, **data)
if __name__ == "__main__": if __name__ == "__main__":