From 7e51a3221071eadbb589f2f18cca22bc9c6b1117 Mon Sep 17 00:00:00 2001 From: Edward Betts Date: Mon, 22 Jan 2024 13:12:33 +0000 Subject: [PATCH] Trip page to show how many days/weeks/months until trip Closes #118 --- templates/trip_page.html | 5 +++++ web_view.py | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+) diff --git a/templates/trip_page.html b/templates/trip_page.html index e7c9ce2..a7a6169 100644 --- a/templates/trip_page.html +++ b/templates/trip_page.html @@ -65,6 +65,11 @@
Start: {{ display_date_no_year(trip.start) }} (end date missing)
{% endif %} + {% set delta = human_readable_delta(trip.start) %} + {% if delta %} +
{{ delta }} time
+ {% endif %} +
{% for conf in trip.conferences %} {{ conference_row(conf, "going") }} {% endfor %}
diff --git a/web_view.py b/web_view.py index 91e9cc2..ea84364 100755 --- a/web_view.py +++ b/web_view.py @@ -219,6 +219,39 @@ def trip_list() -> str: ) +def human_readable_delta(future_date: date) -> str | None: + """ + Calculate the human-readable time delta for a given future date. + + Args: + future_date (date): The future date as a datetime.date object. + + Returns: + str: Human-readable time delta. + """ + # Ensure the input is a future date + if future_date <= date.today(): + return None + + # Calculate the delta + delta = future_date - date.today() + + # Convert delta to a more human-readable format + months, days = divmod(delta.days, 30) + weeks, days = divmod(days, 7) + + # Formatting the output + parts = [] + if months > 0: + parts.append(f"{months} months") + if weeks > 0: + parts.append(f"{weeks} weeks") + if days > 0: + parts.append(f"{days} days") + + return " ".join(parts) if parts else None + + @app.route("/trip/") def trip_page(start: str) -> str: """Individual trip page.""" @@ -282,6 +315,7 @@ def trip_page(start: str) -> str: get_country=agenda.get_country, format_list_with_ampersand=format_list_with_ampersand, holidays=holidays, + human_readable_delta=human_readable_delta, )