<s> """<STR_LIT>""" <EOL> from datetime import timedelta , time , date <EOL> from time import localtime <EOL> __all__ = [ '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' ] <EOL> def _number ( val ) : <EOL> try : <EOL> return int ( val ) <EOL> except : <EOL> return None <EOL> def parse_timedelta ( val ) : <EOL> """<STR_LIT>""" <EOL> if not val : <EOL> return None <EOL> val = val . lower ( ) <EOL> if "<STR_LIT:.>" in val : <EOL> val = float ( val ) <EOL> return timedelta ( hours = int ( val ) , minutes = <NUM_LIT> * ( val % <NUM_LIT:1.0> ) ) <EOL> fHour = ( "<STR_LIT:h>" in val or "<STR_LIT::>" in val ) <EOL> fMin = ( "<STR_LIT:m>" in val or "<STR_LIT::>" in val ) <EOL> for noise in "<STR_LIT>" : <EOL> val = val . replace ( noise , '<STR_LIT:U+0020>' ) <EOL> val = val . strip ( ) <EOL> val = val . split ( ) <EOL> hr = <NUM_LIT:0.0> <EOL> mi = <NUM_LIT:0> <EOL> val . reverse ( ) <EOL> if fHour : <EOL> hr = int ( val . pop ( ) ) <EOL> if fMin : <EOL> mi = int ( val . pop ( ) ) <EOL> if len ( val ) > <NUM_LIT:0> and not hr : <EOL> hr = int ( val . pop ( ) ) <EOL> return timedelta ( hours = hr , minutes = mi ) <EOL> def normalize_timedelta ( val ) : <EOL> """<STR_LIT>""" <EOL> if type ( val ) == str : <EOL> val = parse_timedelta ( val ) <EOL> if not val : <EOL> return '<STR_LIT>' <EOL> hr = val . seconds / <NUM_LIT> <EOL> mn = ( val . seconds % <NUM_LIT> ) / <NUM_LIT> <EOL> return "<STR_LIT>" % ( hr , mn * <NUM_LIT:100> / <NUM_LIT> ) <EOL> def parse_time ( val ) : <EOL> if not val : <EOL> return None <EOL> hr = mi = <NUM_LIT:0> <EOL> val = val . lower ( ) <EOL> amflag = ( - <NUM_LIT:1> != val . find ( '<STR_LIT:a>' ) ) <EOL> pmflag = ( - <NUM_LIT:1> != val . find ( '<STR_LIT:p>' ) ) <EOL> for noise in "<STR_LIT>" : <EOL> val = val . replace ( noise , '<STR_LIT:U+0020>' ) <EOL> val = val . split ( ) <EOL> if len ( val ) > <NUM_LIT:1> : <EOL> hr = int ( val [ <NUM_LIT:0> ] ) <EOL> mi = int ( val [ <NUM_LIT:1> ] ) <EOL> else : <EOL> val = val [ <NUM_LIT:0> ] <EOL> if len ( val ) < <NUM_LIT:1> : <EOL> pass <EOL> elif '<STR_LIT>' == val : <EOL> tm = localtime ( ) <EOL> hr = tm [ <NUM_LIT:3> ] <EOL> mi = tm [ <NUM_LIT:4> ] <EOL> elif '<STR_LIT>' == val : <EOL> hr = <NUM_LIT:12> <EOL> elif len ( val ) < <NUM_LIT:3> : <EOL> hr = int ( val ) <EOL> if not amflag and not pmflag and hr < <NUM_LIT:7> : <EOL> hr += <NUM_LIT:12> <EOL> elif len ( val ) < <NUM_LIT:5> : <EOL> hr = int ( val [ : - <NUM_LIT:2> ] ) <EOL> mi = int ( val [ - <NUM_LIT:2> : ] ) <EOL> else : <EOL> hr = int ( val [ : <NUM_LIT:1> ] ) <EOL> if amflag and hr >= <NUM_LIT:12> : <EOL> hr = hr - <NUM_LIT:12> <EOL> if pmflag and hr < <NUM_LIT:12> : <EOL> hr = hr + <NUM_LIT:12> <EOL> return time ( hr , mi ) <EOL> def normalize_time ( value , ampm ) : <EOL> if not value : <EOL> return '<STR_LIT>' <EOL> if type ( value ) == str : <EOL> value = parse_time ( value ) <EOL> if not ampm : <EOL> return "<STR_LIT>" % ( value . hour , value . minute ) <EOL> hr = value . hour <EOL> am = "<STR_LIT>" <EOL> if hr < <NUM_LIT:1> or hr > <NUM_LIT> : <EOL> hr = <NUM_LIT:12> <EOL> elif hr >= <NUM_LIT:12> : <EOL> am = "<STR_LIT>" <EOL> if hr > <NUM_LIT:12> : <EOL> hr = hr - <NUM_LIT:12> <EOL> return "<STR_LIT>" % ( hr , value . minute , am ) <EOL> _one_day = timedelta ( days = <NUM_LIT:1> ) <EOL> _str2num = { '<STR_LIT>' : <NUM_LIT:1> , '<STR_LIT>' : <NUM_LIT:2> , '<STR_LIT>' : <NUM_LIT:3> , '<STR_LIT>' : <NUM_LIT:4> , '<STR_LIT>' : <NUM_LIT:5> , '<STR_LIT>' : <NUM_LIT:6> , <EOL> '<STR_LIT>' : <NUM_LIT:7> , '<STR_LIT>' : <NUM_LIT:8> , '<STR_LIT>' : <NUM_LIT:9> , '<STR_LIT>' : <NUM_LIT:10> , '<STR_LIT>' : <NUM_LIT:11> , '<STR_LIT>' : <NUM_LIT:12> } <EOL> def _month ( val ) : <EOL> for ( key , mon ) in _str2num . items ( ) : <EOL> if key in val : <EOL> return mon <EOL> raise TypeError ( "<STR_LIT>" % val ) <EOL> _days_in_month = { <NUM_LIT:1> : <NUM_LIT> , <NUM_LIT:2> : <NUM_LIT> , <NUM_LIT:3> : <NUM_LIT> , <NUM_LIT:4> : <NUM_LIT:30> , <NUM_LIT:5> : <NUM_LIT> , <NUM_LIT:6> : <NUM_LIT:30> , <EOL> <NUM_LIT:7> : <NUM_LIT> , <NUM_LIT:8> : <NUM_LIT> , <NUM_LIT:9> : <NUM_LIT:30> , <NUM_LIT:10> : <NUM_LIT> , <NUM_LIT:11> : <NUM_LIT:30> , <NUM_LIT:12> : <NUM_LIT> , <EOL> } <EOL> _num2str = { <NUM_LIT:1> : '<STR_LIT>' , <NUM_LIT:2> : '<STR_LIT>' , <NUM_LIT:3> : '<STR_LIT>' , <NUM_LIT:4> : '<STR_LIT>' , <NUM_LIT:5> : '<STR_LIT>' , <NUM_LIT:6> : '<STR_LIT>' , <EOL> <NUM_LIT:7> : '<STR_LIT>' , <NUM_LIT:8> : '<STR_LIT>' , <NUM_LIT:9> : '<STR_LIT>' , <NUM_LIT:10> : '<STR_LIT>' , <NUM_LIT:11> : '<STR_LIT>' , <NUM_LIT:12> : '<STR_LIT>' , <EOL> } <EOL> _wkdy = ( "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ) <EOL> def parse_date ( val ) : <EOL> if not ( val ) : <EOL> return None <EOL> val = val . lower ( ) <EOL> now = None <EOL> strict = val . split ( "<STR_LIT:->" ) <EOL> if len ( strict ) == <NUM_LIT:3> : <EOL> ( y , m , d ) = strict <EOL> if "<STR_LIT:+>" in d : <EOL> d = d . split ( "<STR_LIT:+>" ) [ <NUM_LIT:0> ] <EOL> if "<STR_LIT:U+0020>" in d : <EOL> d = d . split ( "<STR_LIT:U+0020>" ) [ <NUM_LIT:0> ] <EOL> try : <EOL> now = date ( int ( y ) , int ( m ) , int ( d ) ) <EOL> val = "<STR_LIT>" + val [ <NUM_LIT:10> : ] <EOL> except ValueError : <EOL> pass <EOL> if not now : <EOL> chk = val [ : <NUM_LIT:3> ] <EOL> if chk in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> now = date . today ( ) <EOL> elif chk in _wkdy : <EOL> now = date . today ( ) <EOL> idx = list ( _wkdy ) . index ( chk ) + <NUM_LIT:1> <EOL> while now . isoweekday ( ) != idx : <EOL> now += _one_day <EOL> if now : <EOL> tail = val [ <NUM_LIT:3> : ] . strip ( ) <EOL> tail = tail . replace ( "<STR_LIT:+>" , "<STR_LIT>" ) . replace ( "<STR_LIT:->" , "<STR_LIT>" ) <EOL> for item in tail . split ( ) : <EOL> try : <EOL> days = int ( item ) <EOL> except ValueError : <EOL> pass <EOL> else : <EOL> now += timedelta ( days = days ) <EOL> return now <EOL> yr = mo = dy = None <EOL> for noise in ( '<STR_LIT:/>' , '<STR_LIT:->' , '<STR_LIT:U+002C>' , '<STR_LIT:*>' ) : <EOL> val = val . replace ( noise , '<STR_LIT:U+0020>' ) <EOL> for noise in _wkdy : <EOL> val = val . replace ( noise , '<STR_LIT:U+0020>' ) <EOL> out = [ ] <EOL> last = False <EOL> ldig = False <EOL> for ch in val : <EOL> if ch . isdigit ( ) : <EOL> if last and not ldig : <EOL> out . append ( '<STR_LIT:U+0020>' ) <EOL> last = ldig = True <EOL> else : <EOL> if ldig : <EOL> out . append ( '<STR_LIT:U+0020>' ) <EOL> ldig = False <EOL> last = True <EOL> out . append ( ch ) <EOL> val = "<STR_LIT>" . join ( out ) . split ( ) <EOL> if <NUM_LIT:3> == len ( val ) : <EOL> a = _number ( val [ <NUM_LIT:0> ] ) <EOL> b = _number ( val [ <NUM_LIT:1> ] ) <EOL> c = _number ( val [ <NUM_LIT:2> ] ) <EOL> if len ( val [ <NUM_LIT:0> ] ) == <NUM_LIT:4> : <EOL> yr = a <EOL> if b : <EOL> mo = b <EOL> dy = c <EOL> else : <EOL> mo = _month ( val [ <NUM_LIT:1> ] ) <EOL> dy = c <EOL> elif a is not None and a > <NUM_LIT:0> : <EOL> yr = c <EOL> if len ( val [ <NUM_LIT:2> ] ) < <NUM_LIT:4> : <EOL> raise TypeError ( "<STR_LIT>" ) <EOL> if b : <EOL> dy = b <EOL> mo = a <EOL> else : <EOL> dy = a <EOL> mo = _month ( val [ <NUM_LIT:1> ] ) <EOL> else : <EOL> dy = b <EOL> yr = c <EOL> if len ( val [ <NUM_LIT:2> ] ) < <NUM_LIT:4> : <EOL> raise TypeError ( "<STR_LIT>" ) <EOL> mo = _month ( val [ <NUM_LIT:0> ] ) <EOL> elif <NUM_LIT:2> == len ( val ) : <EOL> a = _number ( val [ <NUM_LIT:0> ] ) <EOL> b = _number ( val [ <NUM_LIT:1> ] ) <EOL> if a is not None and a > <NUM_LIT> : <EOL> yr = a <EOL> dy = <NUM_LIT:1> <EOL> if b is not None and b > <NUM_LIT:0> : <EOL> mo = b <EOL> else : <EOL> mo = _month ( val [ <NUM_LIT:1> ] ) <EOL> elif a is not None and a > <NUM_LIT:0> : <EOL> if b is not None and b > <NUM_LIT> : <EOL> mo = a <EOL> yr = b <EOL> dy = <NUM_LIT:1> <EOL> elif b is not None and b > <NUM_LIT:0> : <EOL> mo = a <EOL> dy = b <EOL> else : <EOL> dy = a <EOL> mo = _month ( val [ <NUM_LIT:1> ] ) <EOL> else : <EOL> if b > <NUM_LIT> : <EOL> yr = b <EOL> dy = <NUM_LIT:1> <EOL> else : <EOL> dy = b <EOL> mo = _month ( val [ <NUM_LIT:0> ] ) <EOL> elif <NUM_LIT:1> == len ( val ) : <EOL> val = val [ <NUM_LIT:0> ] <EOL> if not val . isdigit ( ) : <EOL> mo = _month ( val ) <EOL> if mo is not None : <EOL> dy = <NUM_LIT:1> <EOL> else : <EOL> v = _number ( val ) <EOL> val = str ( v ) <EOL> if <NUM_LIT:8> == len ( val ) : <EOL> yr = _number ( val [ : <NUM_LIT:4> ] ) <EOL> mo = _number ( val [ <NUM_LIT:4> : <NUM_LIT:6> ] ) <EOL> dy = _number ( val [ <NUM_LIT:6> : ] ) <EOL> elif len ( val ) in ( <NUM_LIT:3> , <NUM_LIT:4> ) : <EOL> if v is not None and v > <NUM_LIT> : <EOL> yr = v <EOL> mo = <NUM_LIT:1> <EOL> dy = <NUM_LIT:1> <EOL> else : <EOL> mo = _number ( val [ : - <NUM_LIT:2> ] ) <EOL> dy = _number ( val [ - <NUM_LIT:2> : ] ) <EOL> elif v < <NUM_LIT:32> : <EOL> dy = v <EOL> else : <EOL> raise TypeError ( "<STR_LIT>" ) <EOL> tm = localtime ( ) <EOL> if mo is None : <EOL> mo = tm [ <NUM_LIT:1> ] <EOL> if dy is None : <EOL> dy = tm [ <NUM_LIT:2> ] <EOL> if yr is None : <EOL> yr = tm [ <NUM_LIT:0> ] <EOL> return date ( yr , mo , dy ) <EOL> def normalize_date ( val , iso8601 = True ) : <EOL> if not val : <EOL> return '<STR_LIT>' <EOL> if type ( val ) == str : <EOL> val = parse_date ( val ) <EOL> if iso8601 : <EOL> return "<STR_LIT>" % ( val . year , val . month , val . day ) <EOL> return "<STR_LIT>" % ( val . day , _num2str [ val . month ] , val . year ) </s>
