From 586f17dc2a40da099ed72b87c5c81bee8269b49d Mon Sep 17 00:00:00 2001
From: Pierre-Antoine Rouby <pierre-antoine.rouby@inrae.fr>
Date: Tue, 20 Feb 2024 15:12:33 +0100
Subject: [PATCH] Results, tools: Fix negative timestamp on Windows and some
 others fixes.

---
 src/View/Results/PlotH.py   | 2 +-
 src/View/Results/PlotKPC.py | 1 +
 src/tools.py                | 7 ++++++-
 3 files changed, 8 insertions(+), 2 deletions(-)

diff --git a/src/View/Results/PlotH.py b/src/View/Results/PlotH.py
index fd07b45a..5dacb2a8 100644
--- a/src/View/Results/PlotH.py
+++ b/src/View/Results/PlotH.py
@@ -148,7 +148,7 @@ class PlotH(PamhyrPlot):
     def set_reach(self, reach_id):
         self._current_reach_id = reach_id
         self._current_profile_id = 0
-        self.update()
+        self.draw()
 
     def set_profile(self, profile_id):
         self._current_profile_id = profile_id
diff --git a/src/View/Results/PlotKPC.py b/src/View/Results/PlotKPC.py
index f7c071a4..35f0b211 100644
--- a/src/View/Results/PlotKPC.py
+++ b/src/View/Results/PlotKPC.py
@@ -348,6 +348,7 @@ class PlotKPC(PamhyrPlot):
         if not self._init:
             self.draw()
 
+        reach = self.results.river.reach(self._current_reach_id)
         if reach.has_sediment():
             self.update_bottom_with_bedload()
 
diff --git a/src/tools.py b/src/tools.py
index 84aca848..817ac5fa 100644
--- a/src/tools.py
+++ b/src/tools.py
@@ -254,7 +254,12 @@ def old_pamhyr_date_to_timestamp(date: str):
 def timestamp_to_old_pamhyr_date(time: int):
     logger.debug(f"timestamp_to_old_pamhyr_date({time}: {type(time)})")
     t0 = datetime.fromtimestamp(0)
-    t = datetime.fromtimestamp(int(time))
+
+    # HACK: Windows do not accept negative timestamps
+    if time < 0:
+        t = t0 + timedelta(seconds=int(time))
+    else:
+        t = datetime.fromtimestamp(int(time))
 
     dt = t - t0
     hours = dt.seconds // 3600
-- 
GitLab