2021-08-01 21:47:39 -07:00
|
|
|
from dateutil import parser
|
|
|
|
|
import json
|
|
|
|
|
|
2022-03-20 21:01:35 -07:00
|
|
|
IGNORE = object()
|
|
|
|
|
SET_NULL = object()
|
2021-08-01 21:47:39 -07:00
|
|
|
|
|
|
|
|
|
2022-03-20 21:01:35 -07:00
|
|
|
def parsedate(value, dayfirst=False, yearfirst=False, errors=None):
|
|
|
|
|
"""
|
|
|
|
|
Parse a date and convert it to ISO date format: yyyy-mm-dd
|
|
|
|
|
\b
|
|
|
|
|
- dayfirst=True: treat xx as the day in xx/yy/zz
|
|
|
|
|
- yearfirst=True: treat xx as the year in xx/yy/zz
|
|
|
|
|
- errors=r.IGNORE to ignore values that cannot be parsed
|
|
|
|
|
- errors=r.SET_NULL to set values that cannot be parsed to null
|
|
|
|
|
"""
|
|
|
|
|
try:
|
|
|
|
|
return (
|
|
|
|
|
parser.parse(value, dayfirst=dayfirst, yearfirst=yearfirst)
|
|
|
|
|
.date()
|
|
|
|
|
.isoformat()
|
|
|
|
|
)
|
|
|
|
|
except parser.ParserError:
|
|
|
|
|
if errors is IGNORE:
|
|
|
|
|
return value
|
|
|
|
|
elif errors is SET_NULL:
|
|
|
|
|
return None
|
|
|
|
|
else:
|
|
|
|
|
raise
|
2021-08-01 21:47:39 -07:00
|
|
|
|
2022-03-20 21:01:35 -07:00
|
|
|
|
|
|
|
|
def parsedatetime(value, dayfirst=False, yearfirst=False, errors=None):
|
|
|
|
|
"""
|
|
|
|
|
Parse a datetime and convert it to ISO datetime format: yyyy-mm-ddTHH:MM:SS
|
|
|
|
|
\b
|
|
|
|
|
- dayfirst=True: treat xx as the day in xx/yy/zz
|
|
|
|
|
- yearfirst=True: treat xx as the year in xx/yy/zz
|
|
|
|
|
- errors=r.IGNORE to ignore values that cannot be parsed
|
|
|
|
|
- errors=r.SET_NULL to set values that cannot be parsed to null
|
|
|
|
|
"""
|
|
|
|
|
try:
|
|
|
|
|
return parser.parse(value, dayfirst=dayfirst, yearfirst=yearfirst).isoformat()
|
|
|
|
|
except parser.ParserError:
|
|
|
|
|
if errors is IGNORE:
|
|
|
|
|
return value
|
|
|
|
|
elif errors is SET_NULL:
|
|
|
|
|
return None
|
|
|
|
|
else:
|
|
|
|
|
raise
|
2021-08-01 21:47:39 -07:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def jsonsplit(value, delimiter=",", type=str):
|
2022-03-20 21:01:35 -07:00
|
|
|
"""
|
|
|
|
|
Convert a string like a,b,c into a JSON array ["a", "b", "c"]
|
|
|
|
|
"""
|
2021-08-01 21:47:39 -07:00
|
|
|
return json.dumps([type(s.strip()) for s in value.split(delimiter)])
|