def parse(timestamp, utc=False, produce_naive=False):
parse_re = re.compile(r'''<STR_LIT>''',<EOL>re.I | re.X)<EOL>match = parse_re.match(timestamp)<EOL>if match is not None:<EOL><INDENT>if match.group('<STR_LIT>') in ["<STR_LIT>", "<STR_LIT:z>", "<STR_LIT>", "<STR_LIT>"]:<EOL><INDENT>if produce_naive is True:<EOL><INDENT>tzinfo = None<EOL><DEDENT>else:<EOL><INDENT>tzinfo = pytz.utc<EOL><DEDENT><DEDENT>else:<EOL><INDENT>if produce_naive is True:<EOL><INDENT>raise ValueError("<STR_LIT>" +<EOL>"<STR_LIT>")<EOL><DEDENT>else:<EOL><INDENT>tzinfo = FixedOffset(int(match.group('<STR_LIT>')),<EOL>int(match.group('<STR_LIT>')))<EOL><DEDENT><DEDENT>secfrac = match.group('<STR_LIT>')<EOL>if secfrac is None:<EOL><INDENT>microsecond = <NUM_LIT:0><EOL><DEDENT>else:<EOL><INDENT>microsecond = int(round(float(secfrac) * <NUM_LIT>))<EOL><DEDENT>dt_out = datetime(year=int(match.group('<STR_LIT>')),<EOL>month=int(match.group('<STR_LIT>')),<EOL>day=int(match.group('<STR_LIT>')),<EOL>hour=int(match.group('<STR_LIT>')),<EOL>minute=int(match.group('<STR_LIT>')),<EOL>second=int(match.group('<STR_LIT>')),<EOL>microsecond=microsecond,<EOL>tzinfo=tzinfo)<EOL>if utc:<EOL><INDENT>dt_out = dt_out.astimezone(pytz.utc)<EOL><DEDENT>return dt_out<EOL><DEDENT>else:<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT>
Parse an :RFC:`3339`-formatted timestamp and return a
`datetime.datetime`.

If the timestamp is presented in UTC, then the `tzinfo` parameter of the
returned `datetime` will be set to `pytz.utc`.

>>> parse('2009-01-01T10:01:02Z')
datetime.datetime(2009, 1, 1, 10, 1, 2, tzinfo=<UTC>)

Otherwise, a `tzinfo` instance is created with the appropriate offset, and
the `tzinfo` parameter of the returned `datetime` is set to that value.

>>> parse('2009-01-01T14:01:02-04:00')
datetime.datetime(2009, 1, 1, 14, 1, 2, tzinfo=<UTC-04:00>)

However, if `parse()`  is called with `utc=True`, then the returned
`datetime` will be normalized to UTC (and its tzinfo parameter set to
`pytz.utc`), regardless of the input timezone.

>>> parse('2009-01-01T06:01:02-04:00', utc=True)
datetime.datetime(2009, 1, 1, 10, 1, 2, tzinfo=<UTC>)

The input is strictly required to conform to :RFC:`3339`, and appropriate
exceptions are thrown for invalid input.

>>> parse('2009-01-01T06:01:02')
Traceback (most recent call last):
...
ValueError: timestamp does not conform to RFC 3339

>>> parse('2009-01-01T25:01:02Z')
Traceback (most recent call last):
...
ValueError: hour must be in 0..23