<s> import datetime <EOL> import isodate <EOL> import parsedatetime <EOL> import six <EOL> from agate . data_types . base import DataType <EOL> from agate . exceptions import CastError <EOL> class DateTime ( DataType ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , datetime_format = None , timezone = None , ** kwargs ) : <EOL> super ( DateTime , self ) . __init__ ( ** kwargs ) <EOL> self . datetime_format = datetime_format <EOL> self . timezone = timezone <EOL> now = datetime . datetime . now ( ) <EOL> self . _source_time = datetime . datetime ( <EOL> now . year , now . month , now . day , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> , None <EOL> ) <EOL> self . _parser = parsedatetime . Calendar ( version = parsedatetime . VERSION_CONTEXT_STYLE ) <EOL> def __getstate__ ( self ) : <EOL> """<STR_LIT>""" <EOL> odict = self . __dict__ . copy ( ) <EOL> del odict [ '<STR_LIT>' ] <EOL> return odict <EOL> def __setstate__ ( self , dict ) : <EOL> """<STR_LIT>""" <EOL> self . __dict__ . update ( dict ) <EOL> self . _parser = parsedatetime . Calendar ( version = parsedatetime . VERSION_CONTEXT_STYLE ) <EOL> def cast ( self , d ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( d , datetime . datetime ) or d is None : <EOL> return d <EOL> elif isinstance ( d , datetime . date ) : <EOL> return datetime . datetime . combine ( d , datetime . time ( <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> ) ) <EOL> elif isinstance ( d , six . string_types ) : <EOL> d = d . strip ( ) <EOL> if d . lower ( ) in self . null_values : <EOL> return None <EOL> else : <EOL> raise CastError ( '<STR_LIT>' % d ) <EOL> if self . datetime_format : <EOL> try : <EOL> return datetime . datetime . strptime ( d , self . datetime_format ) <EOL> except : <EOL> raise CastError ( '<STR_LIT>' % d ) <EOL> value , ctx = self . _parser . parseDT ( <EOL> d , <EOL> sourceTime = self . _source_time , <EOL> tzinfo = self . timezone <EOL> ) <EOL> if ctx . hasDate and ctx . hasTime : <EOL> return value <EOL> elif ctx . hasDate and not ctx . hasTime : <EOL> return datetime . datetime . combine ( value . date ( ) , datetime . time . min ) <EOL> try : <EOL> dt = isodate . parse_datetime ( d ) <EOL> return dt <EOL> except : <EOL> pass <EOL> raise CastError ( '<STR_LIT>' % d ) <EOL> def csvify ( self , d ) : <EOL> if d is None : <EOL> return None <EOL> return d . isoformat ( ) <EOL> def jsonify ( self , d ) : <EOL> return self . csvify ( d ) </s>
