From a834e1905d9d9827b5a391dd3d2c21b5922ff09b Mon Sep 17 00:00:00 2001
From: Pierre-Antoine Rouby <pierre-antoine.rouby@inrae.fr>
Date: Mon, 8 Apr 2024 16:10:05 +0200
Subject: [PATCH] BC: Add DMY datetime support.

---
 src/Model/BoundaryCondition/BoundaryCondition.py | 11 ++++++++++-
 src/View/Tools/ASubWindow.py                     |  6 +++---
 src/tools.py                                     | 16 ++++++++++++++++
 3 files changed, 29 insertions(+), 4 deletions(-)

diff --git a/src/Model/BoundaryCondition/BoundaryCondition.py b/src/Model/BoundaryCondition/BoundaryCondition.py
index ba23cb7d..918af8b3 100644
--- a/src/Model/BoundaryCondition/BoundaryCondition.py
+++ b/src/Model/BoundaryCondition/BoundaryCondition.py
@@ -18,7 +18,12 @@
 
 import logging
 
-from tools import trace, timer, old_pamhyr_date_to_timestamp
+from tools import (
+    trace, timer,
+    old_pamhyr_date_to_timestamp,
+    date_iso_to_timestamp,
+    date_dmy_to_timestamp,
+)
 
 from Model.Tools.PamhyrDB import SQLSubModel
 from Model.Except import NotImplementedMethodeError
@@ -189,6 +194,10 @@ class BoundaryCondition(SQLSubModel):
     @classmethod
     def time_convert(cls, data):
         if type(data) is str:
+            if data.count("-") == 2:
+                return date_iso_to_timestamp(data)
+            if data.count("/") == 2:
+                return date_dmy_to_timestamp(data)
             if data.count(":") == 3:
                 return old_pamhyr_date_to_timestamp(data)
             if data.count(":") == 2:
diff --git a/src/View/Tools/ASubWindow.py b/src/View/Tools/ASubWindow.py
index 7dc57b0d..e568aa25 100644
--- a/src/View/Tools/ASubWindow.py
+++ b/src/View/Tools/ASubWindow.py
@@ -68,11 +68,11 @@ class WindowToolKit(object):
         header = []
         values = []
 
-        delimiter = '\t'
+        delimiter = ' '
         if ';' in data:
             delimiter = ';'
-        if ' ' in data:
-            delimiter = ' '
+        if '\t' in data:
+            delimiter = '\t'
 
         stream = StringIO(data)
         rows = csv.reader(stream, delimiter=delimiter)
diff --git a/src/tools.py b/src/tools.py
index b8a4c0b3..35ed7b40 100644
--- a/src/tools.py
+++ b/src/tools.py
@@ -234,6 +234,22 @@ def timestamp(dt: datetime):
         return (dt - datetime(1970, 1, 1)).total_seconds()
     return dt.timestamp()
 
+def date_iso_to_timestamp(date: str):
+    return datetime.isoformat(date)
+
+def date_dmy_to_timestamp(date: str):
+    if date.count(":") == 0:
+        ret = datetime.strptime(date, "%d/%m/%y")
+    elif date.count(".") == 1:
+        ret=  datetime.strptime(date, "%d/%m/%y %H:%M:%S.%f")
+    elif date.count(":") == 1:
+        ret = datetime.strptime(date, "%d/%m/%y %H:%M")
+    elif date.count(":") == 2:
+        ret = datetime.strptime(date, "%d/%m/%y %H:%M:%S")
+    else:
+        ret = datetime.now()
+
+    return ret.timestamp()
 
 def old_pamhyr_date_to_timestamp(date: str):
     v = date.split(":")
-- 
GitLab