Skip to content

Commit

Permalink
Prevent negative timestamps, warn users when dates look odd (#495)
Browse files Browse the repository at this point in the history
  • Loading branch information
almarklein authored Jul 18, 2024
1 parent ec2fbb6 commit a1accba
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 8 deletions.
27 changes: 25 additions & 2 deletions timetagger/app/dialogs.py
Original file line number Diff line number Diff line change
Expand Up @@ -665,6 +665,7 @@ def __init__(self, node, callback, t1, t2, mode):
text_finished = "Stopped"

self.node.innerHTML = f"""
<div style='color:#955;'></div>
<div>
<label style='user-select:none;'><input type='radio' name='runningornot' />&nbsp;{text_startnow}&nbsp;&nbsp;</label>
<label style='user-select:none;'><input type='radio' name='runningornot' />&nbsp;{text_startrlr}&nbsp;&nbsp;</label>
Expand Down Expand Up @@ -696,8 +697,9 @@ def __init__(self, node, callback, t1, t2, mode):
"""

# Unpack children
self.radionode = self.node.children[0]
self.gridnode = self.node.children[1]
self.warningnode = self.node.children[0]
self.radionode = self.node.children[1]
self.gridnode = self.node.children[2]
self.radio_startnow = self.radionode.children[0].children[0]
self.radio_startrlr = self.radionode.children[1].children[0]
self.radio_finished = self.radionode.children[2].children[0]
Expand Down Expand Up @@ -938,6 +940,26 @@ def render(self):
else:
self.date2input.style.color = None

# Warn about some basic validity checks
warnings = []
if "1970" in t1_date:
warnings.append(f"Invalid date, clipped to 1970")
elif self.t1 < self.initial_t1 - 86400:
diff = dt.duration_string(self.initial_t1 - self.t1, False, "dhms")
warnings.append(f"moving start back {diff}")
if self.t2 > self.initial_t2 + 86400:
diff = dt.duration_string(self.t2 - self.initial_t2, False, "dhms")
warnings.append(f"moving end forward {diff}")
if self.t2 - self.t1 > 86400:
diff = dt.duration_string(self.t2 - self.t1, False)
warnings.append(f"duration is {diff}")
if warnings:
self.warningnode.innerHTML = "<i class='fas'>\uf071</i> " + ", ".join(
warnings
)
else:
self.warningnode.innerHTML = ""

def onchanged(self, action):
# step size used for time buttons
_stepsize = 5
Expand Down Expand Up @@ -1736,6 +1758,7 @@ def _on_times_change(self):
self._record.t1 = self._time_edit.t1
self._record.t2 = self._time_edit.t2
is_running = self._record.t1 == self._record.t2

self._mark_as_edited()
# Swap mode?
if was_running and not is_running:
Expand Down
11 changes: 6 additions & 5 deletions timetagger/app/dt.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ def to_time_int(t):
t = datetime.datetime.strptime(t, "%Y-%m-%d %H:%M:%S").timestamp()
if not isinstance(t, (int, float)):
raise RuntimeError(f"Time must be a number, not {t!r}")
return int(t)
return max(int(t), 0) # for dates before the epoch (1970) just clip


def get_timezone_indicator(t, sep="", utc_offset=None):
Expand Down Expand Up @@ -290,11 +290,12 @@ def duration_string_colon(t, show_secs=False):
return f"{sign}{m//60:.0f}:{m%60:02.0f}"


def duration_string(t, show_secs=False):
def duration_string(t, show_secs=False, repr=None):
PSCRIPT_OVERLOAD = False # noqa
repr = "hms"
if window.simplesettings:
repr = window.simplesettings.get("duration_repr", "hms")
if not repr:
repr = "hms"
if window.simplesettings:
repr = window.simplesettings.get("duration_repr", "hms")
if repr == "hms" or repr == "dhms":
sign = "-" if t < 0 else ""
t = abs(t)
Expand Down
2 changes: 1 addition & 1 deletion timetagger/app/stores.py
Original file line number Diff line number Diff line change
Expand Up @@ -477,7 +477,7 @@ def _put(self, *records):

def _update_bins(self, level, changed_bins):
"""Update bins of the given layer."""
# This uses a loop, because with recursion we too easily reach the recursion depth limit
# This uses a loop to avoid eaching the recursion depth limit
PSCRIPT_OVERLOAD = False # noqa

while True:
Expand Down

0 comments on commit a1accba

Please sign in to comment.