<s> """<STR_LIT>""" <EOL> from __future__ import division <EOL> import re <EOL> import pytz as _pytz <EOL> from datetime import date , datetime , time , timedelta <EOL> from bisect import bisect_right <EOL> from babel . core import default_locale , get_global , Locale <EOL> from babel . util import UTC , LOCALTZ <EOL> from babel . _compat import string_types , integer_types , number_types <EOL> LC_TIME = default_locale ( '<STR_LIT>' ) <EOL> date_ = date <EOL> datetime_ = datetime <EOL> time_ = time <EOL> def get_timezone ( zone = None ) : <EOL> """<STR_LIT>""" <EOL> if zone is None : <EOL> return LOCALTZ <EOL> if not isinstance ( zone , string_types ) : <EOL> return zone <EOL> try : <EOL> return _pytz . timezone ( zone ) <EOL> except _pytz . UnknownTimeZoneError : <EOL> raise LookupError ( '<STR_LIT>' % zone ) <EOL> def get_next_timezone_transition ( zone = None , dt = None ) : <EOL> """<STR_LIT>""" <EOL> zone = get_timezone ( zone ) <EOL> if dt is None : <EOL> dt = datetime . utcnow ( ) <EOL> else : <EOL> dt = dt . replace ( tzinfo = None ) <EOL> if not hasattr ( zone , '<STR_LIT>' ) : <EOL> raise TypeError ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> try : <EOL> idx = max ( <NUM_LIT:0> , bisect_right ( zone . _utc_transition_times , dt ) ) <EOL> old_trans = zone . _transition_info [ idx - <NUM_LIT:1> ] <EOL> new_trans = zone . _transition_info [ idx ] <EOL> old_tz = zone . _tzinfos [ old_trans ] <EOL> new_tz = zone . _tzinfos [ new_trans ] <EOL> except ( LookupError , ValueError ) : <EOL> return None <EOL> return TimezoneTransition ( <EOL> activates = zone . _utc_transition_times [ idx ] , <EOL> from_tzinfo = old_tz , <EOL> to_tzinfo = new_tz , <EOL> reference_date = dt <EOL> ) <EOL> class TimezoneTransition ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , activates , from_tzinfo , to_tzinfo , reference_date = None ) : <EOL> self . activates = activates <EOL> self . from_tzinfo = from_tzinfo <EOL> self . to_tzinfo = to_tzinfo <EOL> self . reference_date = reference_date <EOL> @ property <EOL> def from_tz ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . from_tzinfo . _tzname <EOL> @ property <EOL> def to_tz ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . to_tzinfo . _tzname <EOL> @ property <EOL> def from_offset ( self ) : <EOL> """<STR_LIT>""" <EOL> return int ( self . from_tzinfo . _utcoffset . total_seconds ( ) ) <EOL> @ property <EOL> def to_offset ( self ) : <EOL> """<STR_LIT>""" <EOL> return int ( self . to_tzinfo . _utcoffset . total_seconds ( ) ) <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' % ( <EOL> self . from_tz , <EOL> self . to_tz , <EOL> self . activates , <EOL> ) <EOL> def get_period_names ( locale = LC_TIME ) : <EOL> """<STR_LIT>""" <EOL> return Locale . parse ( locale ) . periods <EOL> def get_day_names ( width = '<STR_LIT>' , context = '<STR_LIT>' , locale = LC_TIME ) : <EOL> """<STR_LIT>""" <EOL> return Locale . parse ( locale ) . days [ context ] [ width ] <EOL> def get_month_names ( width = '<STR_LIT>' , context = '<STR_LIT>' , locale = LC_TIME ) : <EOL> """<STR_LIT>""" <EOL> return Locale . parse ( locale ) . months [ context ] [ width ] <EOL> def get_quarter_names ( width = '<STR_LIT>' , context = '<STR_LIT>' , locale = LC_TIME ) : <EOL> """<STR_LIT>""" <EOL> return Locale . parse ( locale ) . quarters [ context ] [ width ] <EOL> def get_era_names ( width = '<STR_LIT>' , locale = LC_TIME ) : <EOL> """<STR_LIT>""" <EOL> return Locale . parse ( locale ) . eras [ width ] <EOL> def get_date_format ( format = '<STR_LIT>' , locale = LC_TIME ) : <EOL> """<STR_LIT>""" <EOL> return Locale . parse ( locale ) . date_formats [ format ] <EOL> def get_datetime_format ( format = '<STR_LIT>' , locale = LC_TIME ) : <EOL> """<STR_LIT>""" <EOL> patterns = Locale . parse ( locale ) . datetime_formats <EOL> if format not in patterns : <EOL> format = None <EOL> return patterns [ format ] <EOL> def get_time_format ( format = '<STR_LIT>' , locale = LC_TIME ) : <EOL> """<STR_LIT>""" <EOL> return Locale . parse ( locale ) . time_formats [ format ] <EOL> def get_timezone_gmt ( datetime = None , width = '<STR_LIT>' , locale = LC_TIME ) : <EOL> """<STR_LIT>""" <EOL> if datetime is None : <EOL> datetime = datetime_ . utcnow ( ) <EOL> elif isinstance ( datetime , integer_types ) : <EOL> datetime = datetime_ . utcfromtimestamp ( datetime ) . time ( ) <EOL> if datetime . tzinfo is None : <EOL> datetime = datetime . replace ( tzinfo = UTC ) <EOL> locale = Locale . parse ( locale ) <EOL> offset = datetime . tzinfo . utcoffset ( datetime ) <EOL> seconds = offset . days * <NUM_LIT> * <NUM_LIT> * <NUM_LIT> + offset . seconds <EOL> hours , seconds = divmod ( seconds , <NUM_LIT> ) <EOL> if width == '<STR_LIT>' : <EOL> pattern = u'<STR_LIT>' <EOL> else : <EOL> pattern = locale . zone_formats [ '<STR_LIT>' ] % '<STR_LIT>' <EOL> return pattern % ( hours , seconds // <NUM_LIT> ) <EOL> def get_timezone_location ( dt_or_tzinfo = None , locale = LC_TIME ) : <EOL> """<STR_LIT>""" <EOL> if dt_or_tzinfo is None : <EOL> dt = datetime . now ( ) <EOL> tzinfo = LOCALTZ <EOL> elif isinstance ( dt_or_tzinfo , string_types ) : <EOL> dt = None <EOL> tzinfo = get_timezone ( dt_or_tzinfo ) <EOL> elif isinstance ( dt_or_tzinfo , integer_types ) : <EOL> dt = None <EOL> tzinfo = UTC <EOL> elif isinstance ( dt_or_tzinfo , ( datetime , time ) ) : <EOL> dt = dt_or_tzinfo <EOL> if dt . tzinfo is not None : <EOL> tzinfo = dt . tzinfo <EOL> else : <EOL> tzinfo = UTC <EOL> else : <EOL> dt = None <EOL> tzinfo = dt_or_tzinfo <EOL> locale = Locale . parse ( locale ) <EOL> if hasattr ( tzinfo , '<STR_LIT>' ) : <EOL> zone = tzinfo . zone <EOL> else : <EOL> zone = tzinfo . tzname ( dt or datetime . utcnow ( ) ) <EOL> zone = get_global ( '<STR_LIT>' ) . get ( zone , zone ) <EOL> info = locale . time_zones . get ( zone , { } ) <EOL> region_format = locale . zone_formats [ '<STR_LIT>' ] <EOL> territory = get_global ( '<STR_LIT>' ) . get ( zone ) <EOL> if territory not in locale . territories : <EOL> territory = '<STR_LIT>' <EOL> territory_name = locale . territories [ territory ] <EOL> if territory and len ( get_global ( '<STR_LIT>' ) . get ( territory , [ ] ) ) == <NUM_LIT:1> : <EOL> return region_format % ( territory_name ) <EOL> fallback_format = locale . zone_formats [ '<STR_LIT>' ] <EOL> if '<STR_LIT>' in info : <EOL> city_name = info [ '<STR_LIT>' ] <EOL> else : <EOL> metazone = get_global ( '<STR_LIT>' ) . get ( zone ) <EOL> metazone_info = locale . meta_zones . get ( metazone , { } ) <EOL> if '<STR_LIT>' in metazone_info : <EOL> city_name = metazone_info [ '<STR_LIT>' ] <EOL> elif '<STR_LIT:/>' in zone : <EOL> city_name = zone . split ( '<STR_LIT:/>' , <NUM_LIT:1> ) [ <NUM_LIT:1> ] . replace ( '<STR_LIT:_>' , '<STR_LIT:U+0020>' ) <EOL> else : <EOL> city_name = zone . replace ( '<STR_LIT:_>' , '<STR_LIT:U+0020>' ) <EOL> return region_format % ( fallback_format % { <EOL> '<STR_LIT:0>' : city_name , <EOL> '<STR_LIT:1>' : territory_name <EOL> } ) <EOL> def get_timezone_name ( dt_or_tzinfo = None , width = '<STR_LIT>' , uncommon = False , <EOL> locale = LC_TIME , zone_variant = None ) : <EOL> r"""<STR_LIT>""" <EOL> if dt_or_tzinfo is None : <EOL> dt = datetime . now ( ) <EOL> tzinfo = LOCALTZ <EOL> elif isinstance ( dt_or_tzinfo , string_types ) : <EOL> dt = None <EOL> tzinfo = get_timezone ( dt_or_tzinfo ) <EOL> elif isinstance ( dt_or_tzinfo , integer_types ) : <EOL> dt = None <EOL> tzinfo = UTC <EOL> elif isinstance ( dt_or_tzinfo , ( datetime , time ) ) : <EOL> dt = dt_or_tzinfo <EOL> if dt . tzinfo is not None : <EOL> tzinfo = dt . tzinfo <EOL> else : <EOL> tzinfo = UTC <EOL> else : <EOL> dt = None <EOL> tzinfo = dt_or_tzinfo <EOL> locale = Locale . parse ( locale ) <EOL> if hasattr ( tzinfo , '<STR_LIT>' ) : <EOL> zone = tzinfo . zone <EOL> else : <EOL> zone = tzinfo . tzname ( dt ) <EOL> if zone_variant is None : <EOL> if dt is None : <EOL> zone_variant = '<STR_LIT>' <EOL> else : <EOL> dst = tzinfo . dst ( dt ) <EOL> if dst : <EOL> zone_variant = '<STR_LIT>' <EOL> else : <EOL> zone_variant = '<STR_LIT>' <EOL> else : <EOL> if zone_variant not in ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> zone = get_global ( '<STR_LIT>' ) . get ( zone , zone ) <EOL> info = locale . time_zones . get ( zone , { } ) <EOL> if width in info : <EOL> if zone_variant in info [ width ] : <EOL> return info [ width ] [ zone_variant ] <EOL> metazone = get_global ( '<STR_LIT>' ) . get ( zone ) <EOL> if metazone : <EOL> metazone_info = locale . meta_zones . get ( metazone , { } ) <EOL> if width in metazone_info : <EOL> if zone_variant in metazone_info [ width ] : <EOL> return metazone_info [ width ] [ zone_variant ] <EOL> if dt is not None : <EOL> return get_timezone_gmt ( dt , width = width , locale = locale ) <EOL> return get_timezone_location ( dt_or_tzinfo , locale = locale ) <EOL> def format_date ( date = None , format = '<STR_LIT>' , locale = LC_TIME ) : <EOL> """<STR_LIT>""" <EOL> if date is None : <EOL> date = date_ . today ( ) <EOL> elif isinstance ( date , datetime ) : <EOL> date = date . date ( ) <EOL> locale = Locale . parse ( locale ) <EOL> if format in ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) : <EOL> format = get_date_format ( format , locale = locale ) <EOL> pattern = parse_pattern ( format ) <EOL> return pattern . apply ( date , locale ) <EOL> def format_datetime ( datetime = None , format = '<STR_LIT>' , tzinfo = None , <EOL> locale = LC_TIME ) : <EOL> r"""<STR_LIT>""" <EOL> if datetime is None : <EOL> datetime = datetime_ . utcnow ( ) <EOL> elif isinstance ( datetime , number_types ) : <EOL> datetime = datetime_ . utcfromtimestamp ( datetime ) <EOL> elif isinstance ( datetime , time ) : <EOL> datetime = datetime_ . combine ( date . today ( ) , datetime ) <EOL> if datetime . tzinfo is None : <EOL> datetime = datetime . replace ( tzinfo = UTC ) <EOL> if tzinfo is not None : <EOL> datetime = datetime . astimezone ( get_timezone ( tzinfo ) ) <EOL> if hasattr ( tzinfo , '<STR_LIT>' ) : <EOL> datetime = tzinfo . normalize ( datetime ) <EOL> locale = Locale . parse ( locale ) <EOL> if format in ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) : <EOL> return get_datetime_format ( format , locale = locale ) . replace ( "<STR_LIT:'>" , "<STR_LIT>" ) . replace ( '<STR_LIT>' , format_time ( datetime , format , tzinfo = None , <EOL> locale = locale ) ) . replace ( '<STR_LIT>' , format_date ( datetime , format , locale = locale ) ) <EOL> else : <EOL> return parse_pattern ( format ) . apply ( datetime , locale ) <EOL> def format_time ( time = None , format = '<STR_LIT>' , tzinfo = None , locale = LC_TIME ) : <EOL> r"""<STR_LIT>""" <EOL> if time is None : <EOL> time = datetime . utcnow ( ) <EOL> elif isinstance ( time , number_types ) : <EOL> time = datetime . utcfromtimestamp ( time ) <EOL> if time . tzinfo is None : <EOL> time = time . replace ( tzinfo = UTC ) <EOL> if isinstance ( time , datetime ) : <EOL> if tzinfo is not None : <EOL> time = time . astimezone ( tzinfo ) <EOL> if hasattr ( tzinfo , '<STR_LIT>' ) : <EOL> time = tzinfo . normalize ( time ) <EOL> time = time . timetz ( ) <EOL> elif tzinfo is not None : <EOL> time = time . replace ( tzinfo = tzinfo ) <EOL> locale = Locale . parse ( locale ) <EOL> if format in ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) : <EOL> format = get_time_format ( format , locale = locale ) <EOL> return parse_pattern ( format ) . apply ( time , locale ) <EOL> TIMEDELTA_UNITS = ( <EOL> ( '<STR_LIT>' , <NUM_LIT> * <NUM_LIT> * <NUM_LIT> ) , <EOL> ( '<STR_LIT>' , <NUM_LIT> * <NUM_LIT> * <NUM_LIT:30> ) , <EOL> ( '<STR_LIT>' , <NUM_LIT> * <NUM_LIT> * <NUM_LIT:7> ) , <EOL> ( '<STR_LIT>' , <NUM_LIT> * <NUM_LIT> ) , <EOL> ( '<STR_LIT>' , <NUM_LIT> ) , <EOL> ( '<STR_LIT>' , <NUM_LIT> ) , <EOL> ( '<STR_LIT>' , <NUM_LIT:1> ) <EOL> ) <EOL> def format_timedelta ( delta , granularity = '<STR_LIT>' , threshold = <NUM_LIT> , <EOL> add_direction = False , format = '<STR_LIT>' , <EOL> locale = LC_TIME ) : <EOL> """<STR_LIT>""" <EOL> if format not in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> raise TypeError ( '<STR_LIT>' ) <EOL> if isinstance ( delta , timedelta ) : <EOL> seconds = int ( ( delta . days * <NUM_LIT> ) + delta . seconds ) <EOL> else : <EOL> seconds = delta <EOL> locale = Locale . parse ( locale ) <EOL> def _iter_choices ( unit ) : <EOL> if add_direction : <EOL> if seconds >= <NUM_LIT:0> : <EOL> yield unit + '<STR_LIT>' <EOL> else : <EOL> yield unit + '<STR_LIT>' <EOL> yield unit + '<STR_LIT::>' + format <EOL> yield unit <EOL> for unit , secs_per_unit in TIMEDELTA_UNITS : <EOL> value = abs ( seconds ) / secs_per_unit <EOL> if value >= threshold or unit == granularity : <EOL> if unit == granularity and value > <NUM_LIT:0> : <EOL> value = max ( <NUM_LIT:1> , value ) <EOL> value = int ( round ( value ) ) <EOL> plural_form = locale . plural_form ( value ) <EOL> pattern = None <EOL> for choice in _iter_choices ( unit ) : <EOL> patterns = locale . _data [ '<STR_LIT>' ] . get ( choice ) <EOL> if patterns is not None : <EOL> pattern = patterns [ plural_form ] <EOL> break <EOL> if pattern is None : <EOL> return u'<STR_LIT>' <EOL> return pattern . replace ( '<STR_LIT>' , str ( value ) ) <EOL> return u'<STR_LIT>' <EOL> def parse_date ( string , locale = LC_TIME ) : <EOL> """<STR_LIT>""" <EOL> format = get_date_format ( locale = locale ) . pattern . lower ( ) <EOL> year_idx = format . index ( '<STR_LIT:y>' ) <EOL> month_idx = format . index ( '<STR_LIT:m>' ) <EOL> if month_idx < <NUM_LIT:0> : <EOL> month_idx = format . index ( '<STR_LIT:l>' ) <EOL> day_idx = format . index ( '<STR_LIT:d>' ) <EOL> indexes = [ ( year_idx , '<STR_LIT:Y>' ) , ( month_idx , '<STR_LIT:M>' ) , ( day_idx , '<STR_LIT:D>' ) ] <EOL> indexes . sort ( ) <EOL> indexes = dict ( [ ( item [ <NUM_LIT:1> ] , idx ) for idx , item in enumerate ( indexes ) ] ) <EOL> numbers = re . findall ( '<STR_LIT>' , string ) <EOL> year = numbers [ indexes [ '<STR_LIT:Y>' ] ] <EOL> if len ( year ) == <NUM_LIT:2> : <EOL> year = <NUM_LIT> + int ( year ) <EOL> else : <EOL> year = int ( year ) <EOL> month = int ( numbers [ indexes [ '<STR_LIT:M>' ] ] ) <EOL> day = int ( numbers [ indexes [ '<STR_LIT:D>' ] ] ) <EOL> if month > <NUM_LIT:12> : <EOL> month , day = day , month <EOL> return date ( year , month , day ) <EOL> def parse_time ( string , locale = LC_TIME ) : <EOL> """<STR_LIT>""" <EOL> format = get_time_format ( locale = locale ) . pattern . lower ( ) <EOL> hour_idx = format . index ( '<STR_LIT:h>' ) <EOL> if hour_idx < <NUM_LIT:0> : <EOL> hour_idx = format . index ( '<STR_LIT:k>' ) <EOL> min_idx = format . index ( '<STR_LIT:m>' ) <EOL> sec_idx = format . index ( '<STR_LIT:s>' ) <EOL> indexes = [ ( hour_idx , '<STR_LIT:H>' ) , ( min_idx , '<STR_LIT:M>' ) , ( sec_idx , '<STR_LIT:S>' ) ] <EOL> indexes . sort ( ) <EOL> indexes = dict ( [ ( item [ <NUM_LIT:1> ] , idx ) for idx , item in enumerate ( indexes ) ] ) <EOL> numbers = re . findall ( '<STR_LIT>' , string ) <EOL> hour = int ( numbers [ indexes [ '<STR_LIT:H>' ] ] ) <EOL> minute = int ( numbers [ indexes [ '<STR_LIT:M>' ] ] ) <EOL> second = int ( numbers [ indexes [ '<STR_LIT:S>' ] ] ) <EOL> return time ( hour , minute , second ) <EOL> class DateTimePattern ( object ) : <EOL> def __init__ ( self , pattern , format ) : <EOL> self . pattern = pattern <EOL> self . format = format <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' % ( type ( self ) . __name__ , self . pattern ) <EOL> def __unicode__ ( self ) : <EOL> return self . pattern <EOL> def __mod__ ( self , other ) : <EOL> if type ( other ) is not DateTimeFormat : <EOL> return NotImplemented <EOL> return self . format % other <EOL> def apply ( self , datetime , locale ) : <EOL> return self % DateTimeFormat ( datetime , locale ) <EOL> class DateTimeFormat ( object ) : <EOL> def __init__ ( self , value , locale ) : <EOL> assert isinstance ( value , ( date , datetime , time ) ) <EOL> if isinstance ( value , ( datetime , time ) ) and value . tzinfo is None : <EOL> value = value . replace ( tzinfo = UTC ) <EOL> self . value = value <EOL> self . locale = Locale . parse ( locale ) <EOL> def __getitem__ ( self , name ) : <EOL> char = name [ <NUM_LIT:0> ] <EOL> num = len ( name ) <EOL> if char == '<STR_LIT>' : <EOL> return self . format_era ( char , num ) <EOL> elif char in ( '<STR_LIT:y>' , '<STR_LIT:Y>' , '<STR_LIT:u>' ) : <EOL> return self . format_year ( char , num ) <EOL> elif char in ( '<STR_LIT>' , '<STR_LIT:q>' ) : <EOL> return self . format_quarter ( char , num ) <EOL> elif char in ( '<STR_LIT:M>' , '<STR_LIT:L>' ) : <EOL> return self . format_month ( char , num ) <EOL> elif char in ( '<STR_LIT:w>' , '<STR_LIT>' ) : <EOL> return self . format_week ( char , num ) <EOL> elif char == '<STR_LIT:d>' : <EOL> return self . format ( self . value . day , num ) <EOL> elif char == '<STR_LIT:D>' : <EOL> return self . format_day_of_year ( num ) <EOL> elif char == '<STR_LIT:F>' : <EOL> return self . format_day_of_week_in_month ( ) <EOL> elif char in ( '<STR_LIT:E>' , '<STR_LIT:e>' , '<STR_LIT:c>' ) : <EOL> return self . format_weekday ( char , num ) <EOL> elif char == '<STR_LIT:a>' : <EOL> return self . format_period ( char ) <EOL> elif char == '<STR_LIT:h>' : <EOL> if self . value . hour % <NUM_LIT:12> == <NUM_LIT:0> : <EOL> return self . format ( <NUM_LIT:12> , num ) <EOL> else : <EOL> return self . format ( self . value . hour % <NUM_LIT:12> , num ) <EOL> elif char == '<STR_LIT:H>' : <EOL> return self . format ( self . value . hour , num ) <EOL> elif char == '<STR_LIT>' : <EOL> return self . format ( self . value . hour % <NUM_LIT:12> , num ) <EOL> elif char == '<STR_LIT:k>' : <EOL> if self . value . hour == <NUM_LIT:0> : <EOL> return self . format ( <NUM_LIT> , num ) <EOL> else : <EOL> return self . format ( self . value . hour , num ) <EOL> elif char == '<STR_LIT:m>' : <EOL> return self . format ( self . value . minute , num ) <EOL> elif char == '<STR_LIT:s>' : <EOL> return self . format ( self . value . second , num ) <EOL> elif char == '<STR_LIT:S>' : <EOL> return self . format_frac_seconds ( num ) <EOL> elif char == '<STR_LIT:A>' : <EOL> return self . format_milliseconds_in_day ( num ) <EOL> elif char in ( '<STR_LIT:z>' , '<STR_LIT>' , '<STR_LIT:v>' , '<STR_LIT>' ) : <EOL> return self . format_timezone ( char , num ) <EOL> else : <EOL> raise KeyError ( '<STR_LIT>' % char ) <EOL> def format_era ( self , char , num ) : <EOL> width = { <NUM_LIT:3> : '<STR_LIT>' , <NUM_LIT:4> : '<STR_LIT>' , <NUM_LIT:5> : '<STR_LIT>' } [ max ( <NUM_LIT:3> , num ) ] <EOL> era = int ( self . value . year >= <NUM_LIT:0> ) <EOL> return get_era_names ( width , self . locale ) [ era ] <EOL> def format_year ( self , char , num ) : <EOL> value = self . value . year <EOL> if char . isupper ( ) : <EOL> week = self . get_week_number ( self . get_day_of_year ( ) ) <EOL> if week == <NUM_LIT:0> : <EOL> value -= <NUM_LIT:1> <EOL> year = self . format ( value , num ) <EOL> if num == <NUM_LIT:2> : <EOL> year = year [ - <NUM_LIT:2> : ] <EOL> return year <EOL> def format_quarter ( self , char , num ) : <EOL> quarter = ( self . value . month - <NUM_LIT:1> ) // <NUM_LIT:3> + <NUM_LIT:1> <EOL> if num <= <NUM_LIT:2> : <EOL> return ( '<STR_LIT>' % num ) % quarter <EOL> width = { <NUM_LIT:3> : '<STR_LIT>' , <NUM_LIT:4> : '<STR_LIT>' , <NUM_LIT:5> : '<STR_LIT>' } [ num ] <EOL> context = { '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT:q>' : '<STR_LIT>' } [ char ] <EOL> return get_quarter_names ( width , context , self . locale ) [ quarter ] <EOL> def format_month ( self , char , num ) : <EOL> if num <= <NUM_LIT:2> : <EOL> return ( '<STR_LIT>' % num ) % self . value . month <EOL> width = { <NUM_LIT:3> : '<STR_LIT>' , <NUM_LIT:4> : '<STR_LIT>' , <NUM_LIT:5> : '<STR_LIT>' } [ num ] <EOL> context = { '<STR_LIT:M>' : '<STR_LIT>' , '<STR_LIT:L>' : '<STR_LIT>' } [ char ] <EOL> return get_month_names ( width , context , self . locale ) [ self . value . month ] <EOL> def format_week ( self , char , num ) : <EOL> if char . islower ( ) : <EOL> day_of_year = self . get_day_of_year ( ) <EOL> week = self . get_week_number ( day_of_year ) <EOL> if week == <NUM_LIT:0> : <EOL> date = self . value - timedelta ( days = day_of_year ) <EOL> week = self . get_week_number ( self . get_day_of_year ( date ) , <EOL> date . weekday ( ) ) <EOL> return self . format ( week , num ) <EOL> else : <EOL> week = self . get_week_number ( self . value . day ) <EOL> if week == <NUM_LIT:0> : <EOL> date = self . value - timedelta ( days = self . value . day ) <EOL> week = self . get_week_number ( date . day , date . weekday ( ) ) <EOL> pass <EOL> return '<STR_LIT>' % week <EOL> def format_weekday ( self , char , num ) : <EOL> if num < <NUM_LIT:3> : <EOL> if char . islower ( ) : <EOL> value = <NUM_LIT:7> - self . locale . first_week_day + self . value . weekday ( ) <EOL> return self . format ( value % <NUM_LIT:7> + <NUM_LIT:1> , num ) <EOL> num = <NUM_LIT:3> <EOL> weekday = self . value . weekday ( ) <EOL> width = { <NUM_LIT:3> : '<STR_LIT>' , <NUM_LIT:4> : '<STR_LIT>' , <NUM_LIT:5> : '<STR_LIT>' } [ num ] <EOL> context = { <NUM_LIT:3> : '<STR_LIT>' , <NUM_LIT:4> : '<STR_LIT>' , <NUM_LIT:5> : '<STR_LIT>' } [ num ] <EOL> return get_day_names ( width , context , self . locale ) [ weekday ] <EOL> def format_day_of_year ( self , num ) : <EOL> return self . format ( self . get_day_of_year ( ) , num ) <EOL> def format_day_of_week_in_month ( self ) : <EOL> return '<STR_LIT>' % ( ( self . value . day - <NUM_LIT:1> ) // <NUM_LIT:7> + <NUM_LIT:1> ) <EOL> def format_period ( self , char ) : <EOL> period = { <NUM_LIT:0> : '<STR_LIT>' , <NUM_LIT:1> : '<STR_LIT>' } [ int ( self . value . hour >= <NUM_LIT:12> ) ] <EOL> return get_period_names ( locale = self . locale ) [ period ] <EOL> def format_frac_seconds ( self , num ) : <EOL> value = str ( self . value . microsecond ) <EOL> return self . format ( round ( float ( '<STR_LIT>' % value ) , num ) * <NUM_LIT:10> ** num , num ) <EOL> def format_milliseconds_in_day ( self , num ) : <EOL> msecs = self . value . microsecond // <NUM_LIT:1000> + self . value . second * <NUM_LIT:1000> + self . value . minute * <NUM_LIT> + self . value . hour * <NUM_LIT> <EOL> return self . format ( msecs , num ) <EOL> def format_timezone ( self , char , num ) : <EOL> width = { <NUM_LIT:3> : '<STR_LIT>' , <NUM_LIT:4> : '<STR_LIT>' } [ max ( <NUM_LIT:3> , num ) ] <EOL> if char == '<STR_LIT:z>' : <EOL> return get_timezone_name ( self . value , width , locale = self . locale ) <EOL> elif char == '<STR_LIT>' : <EOL> return get_timezone_gmt ( self . value , width , locale = self . locale ) <EOL> elif char == '<STR_LIT:v>' : <EOL> return get_timezone_name ( self . value . tzinfo , width , <EOL> locale = self . locale ) <EOL> elif char == '<STR_LIT>' : <EOL> if num == <NUM_LIT:1> : <EOL> return get_timezone_name ( self . value . tzinfo , width , <EOL> uncommon = True , locale = self . locale ) <EOL> return get_timezone_location ( self . value . tzinfo , locale = self . locale ) <EOL> def format ( self , value , length ) : <EOL> return ( '<STR_LIT>' % length ) % value <EOL> def get_day_of_year ( self , date = None ) : <EOL> if date is None : <EOL> date = self . value <EOL> return ( date - date . replace ( month = <NUM_LIT:1> , day = <NUM_LIT:1> ) ) . days + <NUM_LIT:1> <EOL> def get_week_number ( self , day_of_period , day_of_week = None ) : <EOL> """<STR_LIT>""" <EOL> if day_of_week is None : <EOL> day_of_week = self . value . weekday ( ) <EOL> first_day = ( day_of_week - self . locale . first_week_day - <EOL> day_of_period + <NUM_LIT:1> ) % <NUM_LIT:7> <EOL> if first_day < <NUM_LIT:0> : <EOL> first_day += <NUM_LIT:7> <EOL> week_number = ( day_of_period + first_day - <NUM_LIT:1> ) // <NUM_LIT:7> <EOL> if <NUM_LIT:7> - first_day >= self . locale . min_week_days : <EOL> week_number += <NUM_LIT:1> <EOL> return week_number <EOL> PATTERN_CHARS = { <EOL> '<STR_LIT>' : [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> , <NUM_LIT:5> ] , <EOL> '<STR_LIT:y>' : None , '<STR_LIT:Y>' : None , '<STR_LIT:u>' : None , <EOL> '<STR_LIT>' : [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> ] , '<STR_LIT:q>' : [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> ] , <EOL> '<STR_LIT:M>' : [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> , <NUM_LIT:5> ] , '<STR_LIT:L>' : [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> , <NUM_LIT:5> ] , <EOL> '<STR_LIT:w>' : [ <NUM_LIT:1> , <NUM_LIT:2> ] , '<STR_LIT>' : [ <NUM_LIT:1> ] , <EOL> '<STR_LIT:d>' : [ <NUM_LIT:1> , <NUM_LIT:2> ] , '<STR_LIT:D>' : [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] , '<STR_LIT:F>' : [ <NUM_LIT:1> ] , '<STR_LIT:g>' : None , <EOL> '<STR_LIT:E>' : [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> , <NUM_LIT:5> ] , '<STR_LIT:e>' : [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> , <NUM_LIT:5> ] , '<STR_LIT:c>' : [ <NUM_LIT:1> , <NUM_LIT:3> , <NUM_LIT:4> , <NUM_LIT:5> ] , <EOL> '<STR_LIT:a>' : [ <NUM_LIT:1> ] , <EOL> '<STR_LIT:h>' : [ <NUM_LIT:1> , <NUM_LIT:2> ] , '<STR_LIT:H>' : [ <NUM_LIT:1> , <NUM_LIT:2> ] , '<STR_LIT>' : [ <NUM_LIT:1> , <NUM_LIT:2> ] , '<STR_LIT:k>' : [ <NUM_LIT:1> , <NUM_LIT:2> ] , <EOL> '<STR_LIT:m>' : [ <NUM_LIT:1> , <NUM_LIT:2> ] , <EOL> '<STR_LIT:s>' : [ <NUM_LIT:1> , <NUM_LIT:2> ] , '<STR_LIT:S>' : None , '<STR_LIT:A>' : None , <EOL> '<STR_LIT:z>' : [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> ] , '<STR_LIT>' : [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> ] , '<STR_LIT:v>' : [ <NUM_LIT:1> , <NUM_LIT:4> ] , '<STR_LIT>' : [ <NUM_LIT:1> , <NUM_LIT:4> ] <EOL> } <EOL> def parse_pattern ( pattern ) : <EOL> """<STR_LIT>""" <EOL> if type ( pattern ) is DateTimePattern : <EOL> return pattern <EOL> result = [ ] <EOL> quotebuf = None <EOL> charbuf = [ ] <EOL> fieldchar = [ '<STR_LIT>' ] <EOL> fieldnum = [ <NUM_LIT:0> ] <EOL> def append_chars ( ) : <EOL> result . append ( '<STR_LIT>' . join ( charbuf ) . replace ( '<STR_LIT:%>' , '<STR_LIT>' ) ) <EOL> del charbuf [ : ] <EOL> def append_field ( ) : <EOL> limit = PATTERN_CHARS [ fieldchar [ <NUM_LIT:0> ] ] <EOL> if limit and fieldnum [ <NUM_LIT:0> ] not in limit : <EOL> raise ValueError ( '<STR_LIT>' <EOL> % ( fieldchar [ <NUM_LIT:0> ] * fieldnum [ <NUM_LIT:0> ] ) ) <EOL> result . append ( '<STR_LIT>' % ( fieldchar [ <NUM_LIT:0> ] * fieldnum [ <NUM_LIT:0> ] ) ) <EOL> fieldchar [ <NUM_LIT:0> ] = '<STR_LIT>' <EOL> fieldnum [ <NUM_LIT:0> ] = <NUM_LIT:0> <EOL> for idx , char in enumerate ( pattern . replace ( "<STR_LIT>" , '<STR_LIT>' ) ) : <EOL> if quotebuf is None : <EOL> if char == "<STR_LIT:'>" : <EOL> if fieldchar [ <NUM_LIT:0> ] : <EOL> append_field ( ) <EOL> elif charbuf : <EOL> append_chars ( ) <EOL> quotebuf = [ ] <EOL> elif char in PATTERN_CHARS : <EOL> if charbuf : <EOL> append_chars ( ) <EOL> if char == fieldchar [ <NUM_LIT:0> ] : <EOL> fieldnum [ <NUM_LIT:0> ] += <NUM_LIT:1> <EOL> else : <EOL> if fieldchar [ <NUM_LIT:0> ] : <EOL> append_field ( ) <EOL> fieldchar [ <NUM_LIT:0> ] = char <EOL> fieldnum [ <NUM_LIT:0> ] = <NUM_LIT:1> <EOL> else : <EOL> if fieldchar [ <NUM_LIT:0> ] : <EOL> append_field ( ) <EOL> charbuf . append ( char ) <EOL> elif quotebuf is not None : <EOL> if char == "<STR_LIT:'>" : <EOL> charbuf . extend ( quotebuf ) <EOL> quotebuf = None <EOL> else : <EOL> quotebuf . append ( char ) <EOL> if fieldchar [ <NUM_LIT:0> ] : <EOL> append_field ( ) <EOL> elif charbuf : <EOL> append_chars ( ) <EOL> return DateTimePattern ( pattern , u'<STR_LIT>' . join ( result ) . replace ( '<STR_LIT>' , "<STR_LIT:'>" ) ) </s>
