changeset 43d123a4877d in trytond:default
details: https://hg.tryton.org/trytond?cmd=changeset&node=43d123a4877d
description:
        Use fromisoformat method from datetime module

        issue11637
        review411551003
diffstat:

 trytond/ir/trigger.py        |  18 +++---------------
 trytond/model/fields/date.py |  17 +++--------------
 2 files changed, 6 insertions(+), 29 deletions(-)

diffs (83 lines):

diff -r 08d07aa63e73 -r 43d123a4877d trytond/ir/trigger.py
--- a/trytond/ir/trigger.py     Thu Sep 08 13:05:06 2022 +0200
+++ b/trytond/ir/trigger.py     Thu Sep 08 13:10:15 2022 +0200
@@ -229,19 +229,6 @@
                         new_ids.append(record_id)
             ids = new_ids
 
-        def cast_datetime(value):
-            datepart, timepart = value.split(" ")
-            year, month, day = map(int, datepart.split("-"))
-            timepart_full = timepart.split(".")
-            hours, minutes, seconds = map(
-                int, timepart_full[0].split(":"))
-            if len(timepart_full) == 2:
-                microseconds = int(timepart_full[1])
-            else:
-                microseconds = 0
-            return datetime.datetime(
-                year, month, day, hours, minutes, seconds, microseconds)
-
         # Filter on minimum_time_delay
         if self.minimum_time_delay:
             new_ids = []
@@ -250,7 +237,7 @@
             cursor.execute(*Select([timestamp_cast(CurrentTimestamp())]))
             now, = cursor.fetchone()
             if isinstance(now, str):
-                now = cast_datetime(now)
+                now = datetime.datetime.fromisoformat(now)
             for sub_ids in grouped_slice(ids):
                 sub_ids = list(sub_ids)
                 red_sql = reduce_ids(trigger_log.record_id, sub_ids)
@@ -265,7 +252,8 @@
                         continue
                     # SQLite return string for MAX
                     if isinstance(delay[record_id], str):
-                        delay[record_id] = cast_datetime(delay[record_id])
+                        delay[record_id] = datetime.datetime.fromisoformat(
+                            delay[record_id])
                     if now - delay[record_id] >= self.minimum_time_delay:
                         new_ids.append(record_id)
             ids = new_ids
diff -r 08d07aa63e73 -r 43d123a4877d trytond/model/fields/date.py
--- a/trytond/model/fields/date.py      Thu Sep 08 13:05:06 2022 +0200
+++ b/trytond/model/fields/date.py      Thu Sep 08 13:10:15 2022 +0200
@@ -36,8 +36,7 @@
 
     def sql_format(self, value):
         if isinstance(value, str):
-            year, month, day = list(map(int, value.split("-", 2)))
-            value = datetime.date(year, month, day)
+            value = datetime.date.fromisoformat(value)
         elif isinstance(value, datetime.datetime):
             if value.time() != datetime.time():
                 raise ValueError("Date field can not have time")
@@ -86,16 +85,7 @@
 
     def sql_format(self, value):
         if isinstance(value, str):
-            datepart, timepart = value.split(" ")
-            year, month, day = map(int, datepart.split("-", 2))
-            timepart_full = timepart.split(".", 1)
-            hours, minutes, seconds = map(int, timepart_full[0].split(":"))
-            if len(timepart_full) == 2:
-                microseconds = int(timepart_full[1])
-            else:
-                microseconds = 0
-            value = datetime.datetime(
-                year, month, day, hours, minutes, seconds, microseconds)
+            value = datetime.datetime.fromisoformat(value)
         return super().sql_format(value)
 
     def sql_cast(self, expression):
@@ -156,8 +146,7 @@
 
     def sql_format(self, value):
         if isinstance(value, str):
-            hours, minutes, seconds = map(int, value.split(":"))
-            value = datetime.time(hours, minutes, seconds)
+            value = datetime.time.fromisoformat(value)
         value = super().sql_format(value)
         if isinstance(value, datetime.time):
             value = value.replace(microsecond=0)

Reply via email to