Trip page to show how many days/weeks/months until trip

Closes #118
This commit is contained in:
Edward Betts 2024-01-22 13:12:33 +00:00
parent b7d655a21e
commit 7e51a32210
2 changed files with 39 additions and 0 deletions

View file

@ -65,6 +65,11 @@
<div>Start: {{ display_date_no_year(trip.start) }} (end date missing)</div>
{% endif %}
{% set delta = human_readable_delta(trip.start) %}
{% if delta %}
<div>{{ delta }} time</div>
{% endif %}
<div class="conferences">
{% for conf in trip.conferences %} {{ conference_row(conf, "going") }} {% endfor %}
</div>

View file

@ -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/<start>")
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,
)