Use fromisoformat method from datetime module
issue11637 review411551003
This commit is contained in:
+3
-15
@@ -229,19 +229,6 @@ class Trigger(DeactivableMixin, ModelSQL, ModelView):
|
||||
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 @@ class Trigger(DeactivableMixin, ModelSQL, ModelView):
|
||||
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 @@ class Trigger(DeactivableMixin, ModelSQL, ModelView):
|
||||
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
|
||||
|
||||
@@ -36,8 +36,7 @@ class Date(Field):
|
||||
|
||||
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 @@ class Timestamp(FormatMixin, Field):
|
||||
|
||||
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 @@ class Time(FormatMixin, Field):
|
||||
|
||||
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)
|
||||
|
||||
Reference in New Issue
Block a user