4141 _Instant : TypeAlias = datetime .date | datetime .time | float | None
4242 _PredefinedTimeFormat : TypeAlias = Literal ['full' , 'long' , 'medium' , 'short' ]
4343 _Context : TypeAlias = Literal ['format' , 'stand-alone' ]
44- _DtOrTzinfo : TypeAlias = datetime .datetime | datetime .tzinfo | str | int | datetime .time | None
44+ _DtOrTzinfo : TypeAlias = datetime .datetime | datetime .tzinfo | str | int | datetime .time | None # fmt: skip
4545
4646# "If a given short metazone form is known NOT to be understood in a given
4747# locale and the parent locale has this value such that it would normally
@@ -1018,9 +1018,17 @@ def _format_fallback_interval(
10181018) -> str :
10191019 if skeleton in locale .datetime_skeletons : # Use the given skeleton
10201020 format = lambda dt : format_skeleton (skeleton , dt , tzinfo , locale = locale )
1021- elif all ((isinstance (d , datetime .date ) and not isinstance (d , datetime .datetime )) for d in (start , end )): # Both are just dates
1021+ elif all (
1022+ # Both are just dates
1023+ (isinstance (d , datetime .date ) and not isinstance (d , datetime .datetime ))
1024+ for d in (start , end )
1025+ ):
10221026 format = lambda dt : format_date (dt , locale = locale )
1023- elif all ((isinstance (d , datetime .time ) and not isinstance (d , datetime .date )) for d in (start , end )): # Both are times
1027+ elif all (
1028+ # Both are times
1029+ (isinstance (d , datetime .time ) and not isinstance (d , datetime .date ))
1030+ for d in (start , end )
1031+ ):
10241032 format = lambda dt : format_time (dt , tzinfo = tzinfo , locale = locale )
10251033 else :
10261034 format = lambda dt : format_datetime (dt , tzinfo = tzinfo , locale = locale )
@@ -1266,8 +1274,11 @@ def parse_date(
12661274 use_predefined_format = format in ('full' , 'long' , 'medium' , 'short' )
12671275 # we try ISO-8601 format first, meaning similar to formats
12681276 # extended YYYY-MM-DD or basic YYYYMMDD
1269- iso_alike = re .match (r'^(\d{4})-?([01]\d)-?([0-3]\d)$' ,
1270- string , flags = re .ASCII ) # allow only ASCII digits
1277+ iso_alike = re .match (
1278+ r'^(\d{4})-?([01]\d)-?([0-3]\d)$' ,
1279+ string ,
1280+ flags = re .ASCII , # allow only ASCII digits
1281+ )
12711282 if iso_alike and use_predefined_format :
12721283 try :
12731284 return datetime .date (* map (int , iso_alike .groups ()))
@@ -1637,35 +1648,24 @@ def format_timezone(self, char: str, num: int) -> str:
16371648 return get_timezone_gmt (value , width , locale = self .locale )
16381649 # TODO: To add support for O:1
16391650 elif char == 'v' :
1640- return get_timezone_name (value .tzinfo , width ,
1641- locale = self .locale )
1651+ return get_timezone_name (value .tzinfo , width , locale = self .locale )
16421652 elif char == 'V' :
16431653 if num == 1 :
1644- return get_timezone_name (value .tzinfo , width ,
1645- uncommon = True , locale = self .locale )
1654+ return get_timezone_name (value .tzinfo , width , locale = self .locale )
16461655 elif num == 2 :
16471656 return get_timezone_name (value .tzinfo , locale = self .locale , return_zone = True )
16481657 elif num == 3 :
1649- return get_timezone_location (value .tzinfo , locale = self .locale , return_city = True )
1658+ return get_timezone_location (value .tzinfo , locale = self .locale , return_city = True ) # fmt: skip
16501659 return get_timezone_location (value .tzinfo , locale = self .locale )
1651- # Included additional elif condition to add support for 'Xx' in timezone format
1652- elif char == 'X' :
1653- if num == 1 :
1654- return get_timezone_gmt (value , width = 'iso8601_short' , locale = self .locale ,
1655- return_z = True )
1656- elif num in (2 , 4 ):
1657- return get_timezone_gmt (value , width = 'short' , locale = self .locale ,
1658- return_z = True )
1659- elif num in (3 , 5 ):
1660- return get_timezone_gmt (value , width = 'iso8601' , locale = self .locale ,
1661- return_z = True )
1662- elif char == 'x' :
1660+ elif char in 'Xx' :
1661+ return_z = char == 'X'
16631662 if num == 1 :
1664- return get_timezone_gmt ( value , width = 'iso8601_short' , locale = self . locale )
1663+ width = 'iso8601_short'
16651664 elif num in (2 , 4 ):
1666- return get_timezone_gmt ( value , width = 'short' , locale = self . locale )
1665+ width = 'short'
16671666 elif num in (3 , 5 ):
1668- return get_timezone_gmt (value , width = 'iso8601' , locale = self .locale )
1667+ width = 'iso8601'
1668+ return get_timezone_gmt (value , width = width , locale = self .locale , return_z = return_z ) # fmt: skip
16691669
16701670 def format (self , value : SupportsInt , length : int ) -> str :
16711671 return '%0*d' % (length , value )
@@ -1739,7 +1739,7 @@ def get_week_number(self, day_of_period: int, day_of_week: int | None = None) ->
17391739 's' : [1 , 2 ], 'S' : None , 'A' : None , # second
17401740 'z' : [1 , 2 , 3 , 4 ], 'Z' : [1 , 2 , 3 , 4 , 5 ], 'O' : [1 , 4 ], 'v' : [1 , 4 ], # zone
17411741 'V' : [1 , 2 , 3 , 4 ], 'x' : [1 , 2 , 3 , 4 , 5 ], 'X' : [1 , 2 , 3 , 4 , 5 ], # zone
1742- }
1742+ } # fmt: skip
17431743
17441744#: The pattern characters declared in the Date Field Symbol Table
17451745#: (https://www.unicode.org/reports/tr35/tr35-dates.html#Date_Field_Symbol_Table)
@@ -1967,11 +1967,11 @@ def match_skeleton(skeleton: str, options: Iterable[str], allow_different_fields
19671967 if 'b' in skeleton and not any ('b' in option for option in options ):
19681968 skeleton = skeleton .replace ('b' , '' )
19691969
1970- get_input_field_width = dict (t [1 ] for t in tokenize_pattern (skeleton ) if t [0 ] == "field" ).get
1970+ get_input_field_width = dict (t [1 ] for t in tokenize_pattern (skeleton ) if t [0 ] == "field" ).get # fmt: skip
19711971 best_skeleton = None
19721972 best_distance = None
19731973 for option in options :
1974- get_opt_field_width = dict (t [1 ] for t in tokenize_pattern (option ) if t [0 ] == "field" ).get
1974+ get_opt_field_width = dict (t [1 ] for t in tokenize_pattern (option ) if t [0 ] == "field" ).get # fmt: skip
19751975 distance = 0
19761976 for field in PATTERN_CHARS :
19771977 input_width = get_input_field_width (field , 0 )
@@ -1982,13 +1982,18 @@ def match_skeleton(skeleton: str, options: Iterable[str], allow_different_fields
19821982 if not allow_different_fields : # This one is not okay
19831983 option = None
19841984 break
1985- distance += 0x1000 # Magic weight constant for "entirely different fields"
1986- elif field == 'M' and ((input_width > 2 and opt_width <= 2 ) or (input_width <= 2 and opt_width > 2 )):
1987- distance += 0x100 # Magic weight for "text turns into a number"
1985+ # Magic weight constant for "entirely different fields"
1986+ distance += 0x1000
1987+ elif field == 'M' and (
1988+ (input_width > 2 and opt_width <= 2 ) or (input_width <= 2 and opt_width > 2 )
1989+ ):
1990+ # Magic weight constant for "text turns into a number"
1991+ distance += 0x100
19881992 else :
19891993 distance += abs (input_width - opt_width )
19901994
1991- if not option : # We lost the option along the way (probably due to "allow_different_fields")
1995+ if not option :
1996+ # We lost the option along the way (probably due to "allow_different_fields")
19921997 continue
19931998
19941999 if not best_skeleton or distance < best_distance :
0 commit comments