def dt_format_and_regex(fmt, no_date=False):
if fmt is None:<EOL><INDENT>return {'<STR_LIT>': None, '<STR_LIT>': None, '<STR_LIT>': None}<EOL><DEDENT>tz_marker = None<EOL>match = re.search('<STR_LIT>', fmt)<EOL>if match:<EOL><INDENT>tz_marker = match.group('<STR_LIT>')<EOL>if len(set(tz_marker.strip())) != <NUM_LIT:1>:  <EOL><INDENT>raise ValueError(fmt)<EOL><DEDENT>fmt = fmt[:match.start()]<EOL><DEDENT>date_patterns = {<EOL>"<STR_LIT>",  <EOL>"<STR_LIT>",  <EOL>"<STR_LIT>",  <EOL>"<STR_LIT>",  <EOL>"<STR_LIT>",  <EOL>"<STR_LIT>",  <EOL>"<STR_LIT>",  <EOL>"<STR_LIT>",  <EOL>"<STR_LIT>",  <EOL>"<STR_LIT>",  <EOL>"<STR_LIT>",  <EOL>"<STR_LIT>",  <EOL>"<STR_LIT>",  <EOL>"<STR_LIT>",  <EOL>}<EOL>time_patterns = {"<STR_LIT>", "<STR_LIT>", "<STR_LIT>", "<STR_LIT>"}<EOL>translate = {<EOL>'<STR_LIT>': ('<STR_LIT>', '<STR_LIT>'),<EOL>'<STR_LIT>': ('<STR_LIT>', '<STR_LIT>'),<EOL>'<STR_LIT>': ('<STR_LIT>', '<STR_LIT>'),<EOL>'<STR_LIT:M>': ('<STR_LIT>', '<STR_LIT>'),<EOL>'<STR_LIT:d>': ('<STR_LIT>', '<STR_LIT>'),<EOL>'<STR_LIT>': ('<STR_LIT>', '<STR_LIT>'),<EOL>'<STR_LIT>': ('<STR_LIT>', '<STR_LIT>'),<EOL>'<STR_LIT>': ('<STR_LIT>', '<STR_LIT>'),<EOL>}<EOL>for dt_sep in '<STR_LIT>':  <EOL><INDENT>if dt_sep in fmt:<EOL><INDENT>break<EOL><DEDENT><DEDENT>else:<EOL><INDENT>dt_sep = None<EOL><DEDENT>if dt_sep:<EOL><INDENT>dfmt, tfmt = fmt.split(dt_sep)<EOL><DEDENT>elif no_date:<EOL><INDENT>dfmt, tfmt = None, fmt<EOL><DEDENT>else:<EOL><INDENT>dfmt, tfmt = fmt, None<EOL><DEDENT>msecs = None  <EOL>if tfmt and '<STR_LIT:.>' in tfmt:  <EOL><INDENT>tfmt, msecs = tfmt.split('<STR_LIT:.>')  <EOL>if set(msecs) != {'<STR_LIT:S>'}:  <EOL><INDENT>raise ValueError(fmt)<EOL><DEDENT>msecs = len(msecs)   <EOL><DEDENT>if (dfmt and dfmt not in date_patterns) or (tfmt and tfmt not in time_patterns):<EOL><INDENT>raise ValueError(fmt)<EOL><DEDENT>regex, format = '<STR_LIT>', '<STR_LIT>'  <EOL>if dfmt:<EOL><INDENT>for d_sep in '<STR_LIT>':  <EOL><INDENT>if d_sep in dfmt:<EOL><INDENT>break<EOL><DEDENT><DEDENT>else:<EOL><INDENT>raise ValueError('<STR_LIT>')  <EOL><DEDENT>for i, part in enumerate(dfmt.split(d_sep)):<EOL><INDENT>if i > <NUM_LIT:0>:<EOL><INDENT>format += d_sep<EOL>regex += re.escape(d_sep)<EOL><DEDENT>f, r = translate[part]<EOL>format += f<EOL>regex += r<EOL><DEDENT><DEDENT>if dt_sep:<EOL><INDENT>format += dt_sep<EOL>regex += re.escape(dt_sep)<EOL><DEDENT>if tfmt:<EOL><INDENT>for i, part in enumerate(tfmt.split('<STR_LIT::>')):<EOL><INDENT>if i > <NUM_LIT:0>:<EOL><INDENT>format += '<STR_LIT::>'<EOL>regex += re.escape('<STR_LIT::>')<EOL><DEDENT>f, r = translate[part]<EOL>format += f<EOL>regex += r<EOL><DEDENT><DEDENT>if msecs:<EOL><INDENT>format += '<STR_LIT>' % msecs<EOL>regex += '<STR_LIT>' % msecs<EOL><DEDENT>return {'<STR_LIT>': re.compile(regex), '<STR_LIT>': format, '<STR_LIT>': tz_marker}<EOL>
.. seealso:: http://w3c.github.io/csvw/syntax/#formats-for-dates-and-times