<s> """<STR_LIT>""" <EOL> from datetime import datetime , timedelta <EOL> from time import daylight <EOL> from django . conf import settings <EOL> try : <EOL> import pytz <EOL> except ImportError : <EOL> from graphite . thirdparty import pytz <EOL> months = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> weekdays = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> tzinfo = pytz . timezone ( settings . TIME_ZONE ) <EOL> def parseATTime ( s ) : <EOL> s = s . strip ( ) . lower ( ) . replace ( '<STR_LIT:_>' , '<STR_LIT>' ) . replace ( '<STR_LIT:U+002C>' , '<STR_LIT>' ) . replace ( '<STR_LIT:U+0020>' , '<STR_LIT>' ) <EOL> if s . isdigit ( ) : <EOL> if len ( s ) == <NUM_LIT:8> and int ( s [ : <NUM_LIT:4> ] ) > <NUM_LIT> and int ( s [ <NUM_LIT:4> : <NUM_LIT:6> ] ) < <NUM_LIT> and int ( s [ <NUM_LIT:6> : ] ) < <NUM_LIT:32> : <EOL> pass <EOL> else : <EOL> return datetime . fromtimestamp ( int ( s ) , tzinfo ) <EOL> if '<STR_LIT:+>' in s : <EOL> ref , offset = s . split ( '<STR_LIT:+>' , <NUM_LIT:1> ) <EOL> offset = '<STR_LIT:+>' + offset <EOL> elif '<STR_LIT:->' in s : <EOL> ref , offset = s . split ( '<STR_LIT:->' , <NUM_LIT:1> ) <EOL> offset = '<STR_LIT:->' + offset <EOL> else : <EOL> ref , offset = s , '<STR_LIT>' <EOL> return tzinfo . localize ( parseTimeReference ( ref ) , daylight ) + parseTimeOffset ( offset ) <EOL> def parseTimeReference ( ref ) : <EOL> if not ref or ref == '<STR_LIT>' : return datetime . now ( ) <EOL> i = ref . find ( '<STR_LIT::>' ) <EOL> hour , min = <NUM_LIT:0> , <NUM_LIT:0> <EOL> if i != - <NUM_LIT:1> : <EOL> hour = int ( ref [ : i ] ) <EOL> min = int ( ref [ i + <NUM_LIT:1> : i + <NUM_LIT:3> ] ) <EOL> ref = ref [ i + <NUM_LIT:3> : ] <EOL> if ref [ : <NUM_LIT:2> ] == '<STR_LIT>' : ref = ref [ <NUM_LIT:2> : ] <EOL> elif ref [ : <NUM_LIT:2> ] == '<STR_LIT>' : <EOL> hour = ( hour + <NUM_LIT:12> ) % <NUM_LIT> <EOL> ref = ref [ <NUM_LIT:2> : ] <EOL> if ref . startswith ( '<STR_LIT>' ) : <EOL> hour , min = <NUM_LIT:12> , <NUM_LIT:0> <EOL> ref = ref [ <NUM_LIT:4> : ] <EOL> elif ref . startswith ( '<STR_LIT>' ) : <EOL> hour , min = <NUM_LIT:0> , <NUM_LIT:0> <EOL> ref = ref [ <NUM_LIT:8> : ] <EOL> elif ref . startswith ( '<STR_LIT>' ) : <EOL> hour , min = <NUM_LIT:16> , <NUM_LIT:0> <EOL> ref = ref [ <NUM_LIT:7> : ] <EOL> refDate = datetime . now ( ) . replace ( hour = hour , minute = min , second = <NUM_LIT:0> ) <EOL> if ref in ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) : <EOL> if ref == '<STR_LIT>' : <EOL> refDate = refDate - timedelta ( days = <NUM_LIT:1> ) <EOL> if ref == '<STR_LIT>' : <EOL> refDate = refDate + timedelta ( days = <NUM_LIT:1> ) <EOL> elif ref . count ( '<STR_LIT:/>' ) == <NUM_LIT:2> : <EOL> m , d , y = map ( int , ref . split ( '<STR_LIT:/>' ) ) <EOL> if y < <NUM_LIT> : y += <NUM_LIT> <EOL> if y < <NUM_LIT> : y += <NUM_LIT:100> <EOL> refDate = refDate . replace ( year = y ) <EOL> try : <EOL> refDate = refDate . replace ( month = m ) <EOL> refDate = refDate . replace ( day = d ) <EOL> except : <EOL> refDate = refDate . replace ( day = d ) <EOL> refDate = refDate . replace ( month = m ) <EOL> elif len ( ref ) == <NUM_LIT:8> and ref . isdigit ( ) : <EOL> refDate = refDate . replace ( year = int ( ref [ : <NUM_LIT:4> ] ) ) <EOL> try : <EOL> refDate = refDate . replace ( month = int ( ref [ <NUM_LIT:4> : <NUM_LIT:6> ] ) ) <EOL> refDate = refDate . replace ( day = int ( ref [ <NUM_LIT:6> : <NUM_LIT:8> ] ) ) <EOL> except : <EOL> refDate = refDate . replace ( day = int ( ref [ <NUM_LIT:6> : <NUM_LIT:8> ] ) ) <EOL> refDate = refDate . replace ( month = int ( ref [ <NUM_LIT:4> : <NUM_LIT:6> ] ) ) <EOL> elif ref [ : <NUM_LIT:3> ] in months : <EOL> refDate = refDate . replace ( month = months . index ( ref [ : <NUM_LIT:3> ] ) + <NUM_LIT:1> ) <EOL> if ref [ - <NUM_LIT:2> : ] . isdigit ( ) : <EOL> refDate = refDate . replace ( day = int ( ref [ - <NUM_LIT:2> : ] ) ) <EOL> elif ref [ - <NUM_LIT:1> : ] . isdigit ( ) : <EOL> refDate = refDate . replace ( day = int ( ref [ - <NUM_LIT:1> : ] ) ) <EOL> else : <EOL> raise Exception , "<STR_LIT>" <EOL> elif ref [ : <NUM_LIT:3> ] in weekdays : <EOL> todayDayName = refDate . strftime ( "<STR_LIT>" ) . lower ( ) [ : <NUM_LIT:3> ] <EOL> today = weekdays . index ( todayDayName ) <EOL> twoWeeks = weekdays * <NUM_LIT:2> <EOL> dayOffset = today - twoWeeks . index ( ref [ : <NUM_LIT:3> ] ) <EOL> if dayOffset < <NUM_LIT:0> : dayOffset += <NUM_LIT:7> <EOL> refDate -= timedelta ( days = dayOffset ) <EOL> elif ref : <EOL> raise Exception , "<STR_LIT>" <EOL> return refDate <EOL> def parseTimeOffset ( offset ) : <EOL> if not offset : <EOL> return timedelta ( ) <EOL> t = timedelta ( ) <EOL> if offset [ <NUM_LIT:0> ] . isdigit ( ) : <EOL> sign = <NUM_LIT:1> <EOL> else : <EOL> sign = { '<STR_LIT:+>' : <NUM_LIT:1> , '<STR_LIT:->' : - <NUM_LIT:1> } [ offset [ <NUM_LIT:0> ] ] <EOL> offset = offset [ <NUM_LIT:1> : ] <EOL> while offset : <EOL> i = <NUM_LIT:1> <EOL> while offset [ : i ] . isdigit ( ) and i <= len ( offset ) : i += <NUM_LIT:1> <EOL> num = int ( offset [ : i - <NUM_LIT:1> ] ) <EOL> offset = offset [ i - <NUM_LIT:1> : ] <EOL> i = <NUM_LIT:1> <EOL> while offset [ : i ] . isalpha ( ) and i <= len ( offset ) : i += <NUM_LIT:1> <EOL> unit = offset [ : i - <NUM_LIT:1> ] <EOL> offset = offset [ i - <NUM_LIT:1> : ] <EOL> unitString = getUnitString ( unit ) <EOL> if unitString == '<STR_LIT>' : <EOL> unitString = '<STR_LIT>' <EOL> num = num * <NUM_LIT:30> <EOL> if unitString == '<STR_LIT>' : <EOL> unitString = '<STR_LIT>' <EOL> num = num * <NUM_LIT> <EOL> t += timedelta ( ** { unitString : sign * num } ) <EOL> return t <EOL> def getUnitString ( s ) : <EOL> if s . startswith ( '<STR_LIT:s>' ) : return '<STR_LIT>' <EOL> if s . startswith ( '<STR_LIT>' ) : return '<STR_LIT>' <EOL> if s . startswith ( '<STR_LIT:h>' ) : return '<STR_LIT>' <EOL> if s . startswith ( '<STR_LIT:d>' ) : return '<STR_LIT>' <EOL> if s . startswith ( '<STR_LIT:w>' ) : return '<STR_LIT>' <EOL> if s . startswith ( '<STR_LIT>' ) : return '<STR_LIT>' <EOL> if s . startswith ( '<STR_LIT:y>' ) : return '<STR_LIT>' <EOL> raise Exception , "<STR_LIT>" % s </s>
