Improve support of date_trunc for SQLite backend

issue7030
review40891002
This commit is contained in:
Cédric Krier committed 2018-01-08 19:24:39 +01:00
1 parent 43617c4d3a
commit f1c4e19155
1 file changed
+20 -11
+20 -11
View File
@@ -75,19 +75,28 @@ class SQLiteExtract(Function):
def date_trunc(_type, date):
if _type == 'second':
if not _type:
return date
try:
tm_tuple = time.strptime(date, '%Y-%m-%d %H:%M:%S')
except Exception:
for format_ in [
'%Y-%m-%d %H:%M:%S.%f',
'%Y-%m-%d %H:%M:%S',
'%Y-%m-%d',
'%H:%M:%S',
]:
try:
value = datetime.datetime.strptime(date, format_)
except ValueError:
continue
else:
break
else:
return None
if _type == 'year':
return "%i-01-01 00:00:00" % tm_tuple.tm_year
elif _type == 'month':
return "%i-%02i-01 00:00:00" % (tm_tuple.tm_year, tm_tuple.tm_mon)
elif _type == 'day':
return "%i-%02i-%02i 00:00:00" % (tm_tuple.tm_year, tm_tuple.tm_mon,
tm_tuple.tm_mday)
for attribute in [
'microsecond', 'second', 'minute', 'hour', 'day', 'month', 'year']:
if _type.startswith(attribute):
break
value = value.replace(**{attribute: 0})
return str(value)
def split_part(text, delimiter, count):