Authentication-Results: mail-b.sr.ht; dkim=none Received: from fortysixandtwo.eu (fortysixandtwo.eu [85.214.238.193]) by mail-b.sr.ht (Postfix) with ESMTPS id C4A7511EF15 for <~fabrixxm/confy-dev@lists.sr.ht>; Mon, 14 Jun 2021 08:32:28 +0000 (UTC) Received: from zeus.olympus (unknown [IPv6:2a02:590:313:e00:c184:e7c2:655:9e27]) by fortysixandtwo.eu (Postfix) with ESMTPA id D434F63A81; Mon, 14 Jun 2021 10:32:27 +0200 (CEST) From: Evangelos Ribeiro Tzaras To: ~fabrixxm/confy-dev@lists.sr.ht Cc: Evangelos Ribeiro Tzaras Subject: [PATCH v4] Fix division by zero for events with zero duration Date: Mon, 14 Jun 2021 10:32:21 +0200 Message-Id: <20210614083221.891567-1-devrtz@fortysixandtwo.eu> X-Mailer: git-send-email 2.30.2 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit When a event has duration zero because the start and end times are identical, ZeroDivisionError's are raised: ``` Traceback (most recent call last): File "/home/fortysixandtwo/install/lib/python3.9/site-packages/confy/widgets.py", line 130, in _on_draw prc = min(max(pos/dur, 0.0), 1.0) ZeroDivisionError: float division by zero ``` This patch ensures that prc is either 0 if the event is in the future or 1, if it's in the past. Closes #18 --- src/pages.py | 8 +++++++- src/widgets.py | 8 +++++++- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/src/pages.py b/src/pages.py index 5e6c09f..2162a93 100644 --- a/src/pages.py +++ b/src/pages.py @@ -507,7 +507,13 @@ class EventDetailPage(BasePage): dur = (obj.end.timestamp() - obj.start.timestamp()) pos = (now.timestamp() - obj.start.timestamp()) - prc = min(max(pos/dur, 0.0), 1.0) + if dur > 0: + prc = min(max(pos/dur, 0.0), 1.0) + else: + # not a valid event duration + # set prc to 0 if the event is in the future or 1 if in the past + prc = int(pos >= 0) + x = w * prc cr.set_source_rgb(0, 0, 0) diff --git a/src/widgets.py b/src/widgets.py index 528244d..2bd900b 100644 --- a/src/widgets.py +++ b/src/widgets.py @@ -127,7 +127,13 @@ class EventActionRow(Handy.ActionRow): dur = (obj.end.timestamp() - obj.start.timestamp()) pos = (now.timestamp() - obj.start.timestamp()) - prc = min(max(pos/dur, 0.0), 1.0) + if dur > 0: + prc = min(max(pos/dur, 0.0), 1.0) + else: + # not a valid event duration + # set prc to 0 if the event is in the future or 1 if in the past + prc = int(pos >= 0) + y = h * prc cr.set_source_rgb(0, 0, 0) -- 2.30.2