<s> import re <EOL> import time <EOL> from datetime import datetime , time as dt_time <EOL> from dateutil import tz <EOL> ISO_DATE_RE = re . compile ( r'<STR_LIT>' ) <EOL> ISO_TIME_RE = re . compile ( r'<STR_LIT>' <EOL> r'<STR_LIT>' ) <EOL> ISO_DATETIME_RE = re . compile ( r'<STR_LIT>' <EOL> r'<STR_LIT>' <EOL> r'<STR_LIT>' ) <EOL> TZ_RE = re . compile ( r'<STR_LIT>' ) <EOL> class InvalidFormat ( Exception ) : <EOL> pass <EOL> class InvalidDate ( Exception ) : <EOL> pass <EOL> def parse_iso_date ( value ) : <EOL> if not ISO_DATE_RE . match ( value ) : <EOL> raise InvalidFormat ( '<STR_LIT>' . format ( value ) ) <EOL> try : <EOL> return datetime ( * time . strptime ( value , '<STR_LIT>' ) [ : <NUM_LIT:3> ] ) . date ( ) <EOL> except ValueError : <EOL> raise InvalidDate ( '<STR_LIT>' . format ( value ) ) <EOL> def parse_tz ( tzstr ) : <EOL> if tzstr is None : <EOL> tzinfo = None <EOL> elif tzstr == '<STR_LIT>' : <EOL> tzinfo = tz . tzutc ( ) <EOL> else : <EOL> s , h , m = TZ_RE . match ( tzstr ) . groups ( ) <EOL> tzseconds = int ( m and m or <NUM_LIT:0> ) * <NUM_LIT> <EOL> tzseconds += int ( h ) * <NUM_LIT> * <NUM_LIT> <EOL> if s == '<STR_LIT:->' : <EOL> tzseconds = tzseconds * - <NUM_LIT:1> <EOL> tzinfo = tz . tzoffset ( None , tzseconds ) <EOL> return tzinfo <EOL> def parse_iso_datetime ( value ) : <EOL> match = ISO_DATETIME_RE . match ( value ) <EOL> if not match : <EOL> raise InvalidFormat ( '<STR_LIT>' . format ( value ) ) <EOL> dtstr = match . group ( <NUM_LIT:1> ) <EOL> secs = match . group ( <NUM_LIT:3> ) <EOL> usecs = match . group ( <NUM_LIT:4> ) <EOL> tzstr = match . group ( <NUM_LIT:5> ) <EOL> dtstr = dtstr . replace ( '<STR_LIT:T>' , '<STR_LIT:U+0020>' ) <EOL> try : <EOL> dt_args = time . strptime ( dtstr , '<STR_LIT>' ) [ : <NUM_LIT:5> ] <EOL> except ValueError : <EOL> raise InvalidDate ( '<STR_LIT>' . format ( value ) ) <EOL> dt_args += ( int ( secs ) if secs else <NUM_LIT:0> , ) <EOL> dt_args += ( int ( usecs . lstrip ( '<STR_LIT:.>' ) ) if usecs else <NUM_LIT:0> , ) <EOL> dt_args += ( parse_tz ( tzstr ) , ) <EOL> try : <EOL> return datetime ( * dt_args ) <EOL> except ValueError : <EOL> raise InvalidDate ( '<STR_LIT>' . format ( value ) ) <EOL> def parse_iso_time ( value ) : <EOL> match = ISO_TIME_RE . match ( value ) <EOL> if not match : <EOL> raise InvalidFormat ( '<STR_LIT>' . format ( value ) ) <EOL> tmstr = match . group ( <NUM_LIT:1> ) <EOL> secs = match . group ( <NUM_LIT:3> ) <EOL> usecs = match . group ( <NUM_LIT:4> ) <EOL> tzstr = match . group ( <NUM_LIT:5> ) <EOL> try : <EOL> dt_args = time . strptime ( tmstr , '<STR_LIT>' ) [ <NUM_LIT:3> : <NUM_LIT:5> ] <EOL> except ValueError : <EOL> raise InvalidDate ( '<STR_LIT>' . format ( value ) ) <EOL> dt_args += ( int ( secs ) if secs else <NUM_LIT:0> , ) <EOL> dt_args += ( int ( usecs ) if usecs else <NUM_LIT:0> , ) <EOL> dt_args += ( parse_tz ( tzstr ) , ) <EOL> try : <EOL> return dt_time ( * dt_args ) <EOL> except ValueError : <EOL> raise InvalidDate ( '<STR_LIT>' . format ( value ) ) </s>
