Improve duration display

This commit is contained in:
Edward Betts 2024-05-16 16:58:07 +02:00
parent dd59c809e1
commit 3ec7f5c18a
2 changed files with 23 additions and 2 deletions

View file

@ -268,6 +268,26 @@ class Event:
else None
)
def display_duration(self) -> str | None:
"""Duration for display."""
if self.end_as_date != self.as_date or not self.has_time:
return None
assert isinstance(self.date, datetime.datetime)
assert isinstance(self.end_date, datetime.datetime)
secs: int = int((self.end_date - self.date).total_seconds())
hours: int = secs // 3600
mins: int = (secs % 3600) // 60
if mins == 0:
return f"{hours:d}h"
if hours == 0:
return f"{mins:d} mins"
return f"{hours:d}h {mins:02d} mins"
def delta_days(self, today: datetime.date) -> str:
"""Return number of days from today as a string."""
delta = (self.as_date - today).days

View file

@ -174,9 +174,10 @@
<div class="col-md-2{{ cell_bg }}">
{% if event.end_date %}
{% if event.end_as_date == event.as_date and event.has_time %}
{% set duration = event.display_duration() %}
{% if duration %}
end: {{event.end_date.strftime("%H:%M") }}
(duration: {{event.end_date - event.date}})
(duration: {{duration}})
{% elif event.end_date != event.date %}
{{event.end_date}}
{% endif %}