<s> """<STR_LIT>""" <EOL> import time <EOL> import locale <EOL> import calendar <EOL> from re import compile as re_compile <EOL> from re import IGNORECASE <EOL> from re import escape as re_escape <EOL> from datetime import date as datetime_date <EOL> try : <EOL> from thread import allocate_lock as _thread_allocate_lock <EOL> except : <EOL> from dummy_thread import allocate_lock as _thread_allocate_lock <EOL> __all__ = [ ] <EOL> def _getlang ( ) : <EOL> return locale . getlocale ( locale . LC_TIME ) <EOL> class LocaleTime ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> """<STR_LIT>""" <EOL> self . lang = _getlang ( ) <EOL> self . __calc_weekday ( ) <EOL> self . __calc_month ( ) <EOL> self . __calc_am_pm ( ) <EOL> self . __calc_timezone ( ) <EOL> self . __calc_date_time ( ) <EOL> if _getlang ( ) != self . lang : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> def __pad ( self , seq , front ) : <EOL> seq = list ( seq ) <EOL> if front : <EOL> seq . insert ( <NUM_LIT:0> , '<STR_LIT>' ) <EOL> else : <EOL> seq . append ( '<STR_LIT>' ) <EOL> return seq <EOL> def __calc_weekday ( self ) : <EOL> a_weekday = [ calendar . day_abbr [ i ] . lower ( ) for i in range ( <NUM_LIT:7> ) ] <EOL> f_weekday = [ calendar . day_name [ i ] . lower ( ) for i in range ( <NUM_LIT:7> ) ] <EOL> self . a_weekday = a_weekday <EOL> self . f_weekday = f_weekday <EOL> def __calc_month ( self ) : <EOL> a_month = [ calendar . month_abbr [ i ] . lower ( ) for i in range ( <NUM_LIT> ) ] <EOL> f_month = [ calendar . month_name [ i ] . lower ( ) for i in range ( <NUM_LIT> ) ] <EOL> self . a_month = a_month <EOL> self . f_month = f_month <EOL> def __calc_am_pm ( self ) : <EOL> am_pm = [ ] <EOL> for hour in ( <NUM_LIT:0> <NUM_LIT:1> , <NUM_LIT> ) : <EOL> time_tuple = time . struct_time ( ( <NUM_LIT> , <NUM_LIT:3> , <NUM_LIT> , hour , <NUM_LIT> , <NUM_LIT> , <NUM_LIT:2> , <NUM_LIT> , <NUM_LIT:0> ) ) <EOL> am_pm . append ( time . strftime ( "<STR_LIT>" , time_tuple ) . lower ( ) ) <EOL> self . am_pm = am_pm <EOL> def __calc_date_time ( self ) : <EOL> time_tuple = time . struct_time ( ( <NUM_LIT> , <NUM_LIT:3> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT:2> , <NUM_LIT> , <NUM_LIT:0> ) ) <EOL> date_time = [ None , None , None ] <EOL> date_time [ <NUM_LIT:0> ] = time . strftime ( "<STR_LIT>" , time_tuple ) . lower ( ) <EOL> date_time [ <NUM_LIT:1> ] = time . strftime ( "<STR_LIT>" , time_tuple ) . lower ( ) <EOL> date_time [ <NUM_LIT:2> ] = time . strftime ( "<STR_LIT>" , time_tuple ) . lower ( ) <EOL> replacement_pairs = [ ( '<STR_LIT:%>' , '<STR_LIT>' ) , ( self . f_weekday [ <NUM_LIT:2> ] , '<STR_LIT>' ) , <EOL> ( self . f_month [ <NUM_LIT:3> ] , '<STR_LIT>' ) , ( self . a_weekday [ <NUM_LIT:2> ] , '<STR_LIT>' ) , <EOL> ( self . a_month [ <NUM_LIT:3> ] , '<STR_LIT>' ) , ( self . am_pm [ <NUM_LIT:1> ] , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , ( '<STR_LIT>' , '<STR_LIT>' ) , ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , ( '<STR_LIT>' , '<STR_LIT>' ) , ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , ( '<STR_LIT>' , '<STR_LIT>' ) , ( '<STR_LIT:3>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT:2>' , '<STR_LIT>' ) , ( '<STR_LIT>' , '<STR_LIT>' ) ] <EOL> replacement_pairs . extend ( [ ( tz , "<STR_LIT>" ) for tz_values in self . timezone <EOL> for tz in tz_values ] ) <EOL> for offset , directive in ( ( <NUM_LIT:0> , '<STR_LIT>' ) , ( <NUM_LIT:1> , '<STR_LIT>' ) , ( <NUM_LIT:2> , '<STR_LIT>' ) ) : <EOL> current_format = date_time [ offset ] <EOL> for old , new in replacement_pairs : <EOL> if old : <EOL> current_format = current_format . replace ( old , new ) <EOL> time_tuple = time . struct_time ( ( <NUM_LIT> , <NUM_LIT:1> , <NUM_LIT:3> , <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:6> , <NUM_LIT:3> , <NUM_LIT:0> ) ) <EOL> if '<STR_LIT>' in time . strftime ( directive , time_tuple ) : <EOL> U_W = '<STR_LIT>' <EOL> else : <EOL> U_W = '<STR_LIT>' <EOL> date_time [ offset ] = current_format . replace ( '<STR_LIT>' , U_W ) <EOL> self . LC_date_time = date_time [ <NUM_LIT:0> ] <EOL> self . LC_date = date_time [ <NUM_LIT:1> ] <EOL> self . LC_time = date_time [ <NUM_LIT:2> ] <EOL> def __calc_timezone ( self ) : <EOL> try : <EOL> time . tzset ( ) <EOL> except AttributeError : <EOL> pass <EOL> no_saving = frozenset ( [ "<STR_LIT>" , "<STR_LIT>" , time . tzname [ <NUM_LIT:0> ] . lower ( ) ] ) <EOL> if time . daylight : <EOL> has_saving = frozenset ( [ time . tzname [ <NUM_LIT:1> ] . lower ( ) ] ) <EOL> else : <EOL> has_saving = frozenset ( ) <EOL> self . timezone = ( no_saving , has_saving ) <EOL> class TimeRE ( dict ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , locale_time = None ) : <EOL> """<STR_LIT>""" <EOL> if locale_time : <EOL> self . locale_time = locale_time <EOL> else : <EOL> self . locale_time = LocaleTime ( ) <EOL> base = super ( TimeRE , self ) <EOL> base . __init__ ( { <EOL> '<STR_LIT:d>' : r"<STR_LIT>" , <EOL> '<STR_LIT:f>' : r"<STR_LIT>" , <EOL> '<STR_LIT:H>' : r"<STR_LIT>" , <EOL> '<STR_LIT:I>' : r"<STR_LIT>" , <EOL> '<STR_LIT>' : r"<STR_LIT>" , <EOL> '<STR_LIT:m>' : r"<STR_LIT>" , <EOL> '<STR_LIT:M>' : r"<STR_LIT>" , <EOL> '<STR_LIT:S>' : r"<STR_LIT>" , <EOL> '<STR_LIT>' : r"<STR_LIT>" , <EOL> '<STR_LIT:w>' : r"<STR_LIT>" , <EOL> '<STR_LIT:y>' : r"<STR_LIT>" , <EOL> '<STR_LIT:Y>' : r"<STR_LIT>" , <EOL> '<STR_LIT:A>' : self . __seqToRE ( self . locale_time . f_weekday , '<STR_LIT:A>' ) , <EOL> '<STR_LIT:a>' : self . __seqToRE ( self . locale_time . a_weekday , '<STR_LIT:a>' ) , <EOL> '<STR_LIT:B>' : self . __seqToRE ( self . locale_time . f_month [ <NUM_LIT:1> : ] , '<STR_LIT:B>' ) , <EOL> '<STR_LIT:b>' : self . __seqToRE ( self . locale_time . a_month [ <NUM_LIT:1> : ] , '<STR_LIT:b>' ) , <EOL> '<STR_LIT:p>' : self . __seqToRE ( self . locale_time . am_pm , '<STR_LIT:p>' ) , <EOL> '<STR_LIT>' : self . __seqToRE ( ( tz for tz_names in self . locale_time . timezone <EOL> for tz in tz_names ) , <EOL> '<STR_LIT>' ) , <EOL> '<STR_LIT:%>' : '<STR_LIT:%>' } ) <EOL> base . __setitem__ ( '<STR_LIT>' , base . __getitem__ ( '<STR_LIT>' ) . replace ( '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> base . __setitem__ ( '<STR_LIT:c>' , self . pattern ( self . locale_time . LC_date_time ) ) <EOL> base . __setitem__ ( '<STR_LIT:x>' , self . pattern ( self . locale_time . LC_date ) ) <EOL> base . __setitem__ ( '<STR_LIT:X>' , self . pattern ( self . locale_time . LC_time ) ) <EOL> def __seqToRE ( self , to_convert , directive ) : <EOL> """<STR_LIT>""" <EOL> to_convert = sorted ( to_convert , key = len , reverse = True ) <EOL> for value in to_convert : <EOL> if value != '<STR_LIT>' : <EOL> break <EOL> else : <EOL> return '<STR_LIT>' <EOL> regex = '<STR_LIT:|>' . join ( re_escape ( stuff ) for stuff in to_convert ) <EOL> regex = '<STR_LIT>' % ( directive , regex ) <EOL> return '<STR_LIT>' % regex <EOL> def pattern ( self , format ) : <EOL> """<STR_LIT>""" <EOL> processed_format = '<STR_LIT>' <EOL> regex_chars = re_compile ( r"<STR_LIT>" ) <EOL> format = regex_chars . sub ( r"<STR_LIT>" , format ) <EOL> whitespace_replacement = re_compile ( '<STR_LIT>' ) <EOL> format = whitespace_replacement . sub ( '<STR_LIT>' , format ) <EOL> while '<STR_LIT:%>' in format : <EOL> directive_index = format . index ( '<STR_LIT:%>' ) + <NUM_LIT:1> <EOL> processed_format = "<STR_LIT>" % ( processed_format , <EOL> format [ : directive_index - <NUM_LIT:1> ] , <EOL> self [ format [ directive_index ] ] ) <EOL> format = format [ directive_index + <NUM_LIT:1> : ] <EOL> return "<STR_LIT>" % ( processed_format , format ) <EOL> def compile ( self , format ) : <EOL> """<STR_LIT>""" <EOL> return re_compile ( self . pattern ( format ) , IGNORECASE ) <EOL> _cache_lock = _thread_allocate_lock ( ) <EOL> _TimeRE_cache = TimeRE ( ) <EOL> _CACHE_MAX_SIZE = <NUM_LIT:5> <EOL> _regex_cache = { } <EOL> def _calc_julian_from_U_or_W ( year , week_of_year , day_of_week , week_starts_Mon ) : <EOL> """<STR_LIT>""" <EOL> first_weekday = datetime_date ( year , <NUM_LIT:1> , <NUM_LIT:1> ) . weekday ( ) <EOL> if not week_starts_Mon : <EOL> first_weekday = ( first_weekday + <NUM_LIT:1> ) % <NUM_LIT:7> <EOL> day_of_week = ( day_of_week + <NUM_LIT:1> ) % <NUM_LIT:7> <EOL> week_0_length = ( <NUM_LIT:7> - first_weekday ) % <NUM_LIT:7> <EOL> if week_of_year == <NUM_LIT:0> : <EOL> return <NUM_LIT:1> + day_of_week - first_weekday <EOL> else : <EOL> days_to_week = week_0_length + ( <NUM_LIT:7> * ( week_of_year - <NUM_LIT:1> ) ) <EOL> return <NUM_LIT:1> + days_to_week + day_of_week <EOL> def _strptime ( data_string , format = "<STR_LIT>" ) : <EOL> """<STR_LIT>""" <EOL> global _TimeRE_cache , _regex_cache <EOL> with _cache_lock : <EOL> if _getlang ( ) != _TimeRE_cache . locale_time . lang : <EOL> _TimeRE_cache = TimeRE ( ) <EOL> _regex_cache . clear ( ) <EOL> if len ( _regex_cache ) > _CACHE_MAX_SIZE : <EOL> _regex_cache . clear ( ) <EOL> locale_time = _TimeRE_cache . locale_time <EOL> format_regex = _regex_cache . get ( format ) <EOL> if not format_regex : <EOL> try : <EOL> format_regex = _TimeRE_cache . compile ( format ) <EOL> except KeyError , err : <EOL> bad_directive = err . args [ <NUM_LIT:0> ] <EOL> if bad_directive == "<STR_LIT:\\>" : <EOL> bad_directive = "<STR_LIT:%>" <EOL> del err <EOL> raise ValueError ( "<STR_LIT>" % <EOL> ( bad_directive , format ) ) <EOL> except IndexError : <EOL> raise ValueError ( "<STR_LIT>" % format ) <EOL> _regex_cache [ format ] = format_regex <EOL> found = format_regex . match ( data_string ) <EOL> if not found : <EOL> raise ValueError ( "<STR_LIT>" % <EOL> ( data_string , format ) ) <EOL> if len ( data_string ) != found . end ( ) : <EOL> raise ValueError ( "<STR_LIT>" % <EOL> data_string [ found . end ( ) : ] ) <EOL> year = None <EOL> month = day = <NUM_LIT:1> <EOL> hour = minute = second = fraction = <NUM_LIT:0> <EOL> tz = - <NUM_LIT:1> <EOL> week_of_year = - <NUM_LIT:1> <EOL> week_of_year_start = - <NUM_LIT:1> <EOL> weekday = julian = - <NUM_LIT:1> <EOL> found_dict = found . groupdict ( ) <EOL> for group_key in found_dict . iterkeys ( ) : <EOL> if group_key == '<STR_LIT:y>' : <EOL> year = int ( found_dict [ '<STR_LIT:y>' ] ) <EOL> if year <= <NUM_LIT> : <EOL> year += <NUM_LIT> <EOL> else : <EOL> year += <NUM_LIT> <EOL> elif group_key == '<STR_LIT:Y>' : <EOL> year = int ( found_dict [ '<STR_LIT:Y>' ] ) <EOL> elif group_key == '<STR_LIT:m>' : <EOL> month = int ( found_dict [ '<STR_LIT:m>' ] ) <EOL> elif group_key == '<STR_LIT:B>' : <EOL> month = locale_time . f_month . index ( found_dict [ '<STR_LIT:B>' ] . lower ( ) ) <EOL> elif group_key == '<STR_LIT:b>' : <EOL> month = locale_time . a_month . index ( found_dict [ '<STR_LIT:b>' ] . lower ( ) ) <EOL> elif group_key == '<STR_LIT:d>' : <EOL> day = int ( found_dict [ '<STR_LIT:d>' ] ) <EOL> elif group_key == '<STR_LIT:H>' : <EOL> hour = int ( found_dict [ '<STR_LIT:H>' ] ) <EOL> elif group_key == '<STR_LIT:I>' : <EOL> hour = int ( found_dict [ '<STR_LIT:I>' ] ) <EOL> ampm = found_dict . get ( '<STR_LIT:p>' , '<STR_LIT>' ) . lower ( ) <EOL> if ampm in ( '<STR_LIT>' , locale_time . am_pm [ <NUM_LIT:0> ] ) : <EOL> if hour == <NUM_LIT:12> : <EOL> hour = <NUM_LIT:0> <EOL> elif ampm == locale_time . am_pm [ <NUM_LIT:1> ] : <EOL> if hour != <NUM_LIT:12> : <EOL> hour += <NUM_LIT:12> <EOL> elif group_key == '<STR_LIT:M>' : <EOL> minute = int ( found_dict [ '<STR_LIT:M>' ] ) <EOL> elif group_key == '<STR_LIT:S>' : <EOL> second = int ( found_dict [ '<STR_LIT:S>' ] ) <EOL> elif group_key == '<STR_LIT:f>' : <EOL> s = found_dict [ '<STR_LIT:f>' ] <EOL> s += "<STR_LIT:0>" * ( <NUM_LIT:6> - len ( s ) ) <EOL> fraction = int ( s ) <EOL> elif group_key == '<STR_LIT:A>' : <EOL> weekday = locale_time . f_weekday . index ( found_dict [ '<STR_LIT:A>' ] . lower ( ) ) <EOL> elif group_key == '<STR_LIT:a>' : <EOL> weekday = locale_time . a_weekday . index ( found_dict [ '<STR_LIT:a>' ] . lower ( ) ) <EOL> elif group_key == '<STR_LIT:w>' : <EOL> weekday = int ( found_dict [ '<STR_LIT:w>' ] ) <EOL> if weekday == <NUM_LIT:0> : <EOL> weekday = <NUM_LIT:6> <EOL> else : <EOL> weekday -= <NUM_LIT:1> <EOL> elif group_key == '<STR_LIT>' : <EOL> julian = int ( found_dict [ '<STR_LIT>' ] ) <EOL> elif group_key in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> week_of_year = int ( found_dict [ group_key ] ) <EOL> if group_key == '<STR_LIT>' : <EOL> week_of_year_start = <NUM_LIT:6> <EOL> else : <EOL> week_of_year_start = <NUM_LIT:0> <EOL> elif group_key == '<STR_LIT>' : <EOL> found_zone = found_dict [ '<STR_LIT>' ] . lower ( ) <EOL> for value , tz_values in enumerate ( locale_time . timezone ) : <EOL> if found_zone in tz_values : <EOL> if ( time . tzname [ <NUM_LIT:0> ] == time . tzname [ <NUM_LIT:1> ] and <EOL> time . daylight and found_zone not in ( "<STR_LIT>" , "<STR_LIT>" ) ) : <EOL> break <EOL> else : <EOL> tz = value <EOL> break <EOL> leap_year_fix = False <EOL> if year is None and month == <NUM_LIT:2> and day == <NUM_LIT> : <EOL> year = <NUM_LIT> <EOL> leap_year_fix = True <EOL> elif year is None : <EOL> year = <NUM_LIT> <EOL> if julian == - <NUM_LIT:1> and week_of_year != - <NUM_LIT:1> and weekday != - <NUM_LIT:1> : <EOL> week_starts_Mon = True if week_of_year_start == <NUM_LIT:0> else False <EOL> julian = _calc_julian_from_U_or_W ( year , week_of_year , weekday , <EOL> week_starts_Mon ) <EOL> if julian == - <NUM_LIT:1> : <EOL> julian = datetime_date ( year , month , day ) . toordinal ( ) - datetime_date ( year , <NUM_LIT:1> , <NUM_LIT:1> ) . toordinal ( ) + <NUM_LIT:1> <EOL> else : <EOL> datetime_result = datetime_date . fromordinal ( ( julian - <NUM_LIT:1> ) + datetime_date ( year , <NUM_LIT:1> , <NUM_LIT:1> ) . toordinal ( ) ) <EOL> year = datetime_result . year <EOL> month = datetime_result . month <EOL> day = datetime_result . day <EOL> if weekday == - <NUM_LIT:1> : <EOL> weekday = datetime_date ( year , month , day ) . weekday ( ) <EOL> if leap_year_fix : <EOL> year = <NUM_LIT> <EOL> return ( time . struct_time ( ( year , month , day , <EOL> hour , minute , second , <EOL> weekday , julian , tz ) ) , fraction ) <EOL> def _strptime_time ( data_string , format = "<STR_LIT>" ) : <EOL> return _strptime ( data_string , format ) [ <NUM_LIT:0> ] </s>
