@@ -86,20 +86,21 @@ def _guess_datetime_format(dt_str, dayfirst=False,
8686 if not isinstance (dt_str , compat .string_types ):
8787 return None
8888
89- day_attribute_and_format = (('day' ,), '%d' )
89+ day_attribute_and_format = (('day' ,), '%d' , 2 )
9090
91+ # attr name, format, padding (if any)
9192 datetime_attrs_to_format = [
92- (('year' , 'month' , 'day' ), '%Y%m%d' ),
93- (('year' ,), '%Y' ),
94- (('month' ,), '%B' ),
95- (('month' ,), '%b' ),
96- (('month' ,), '%m' ),
93+ (('year' , 'month' , 'day' ), '%Y%m%d' , 0 ),
94+ (('year' ,), '%Y' , 0 ),
95+ (('month' ,), '%B' , 0 ),
96+ (('month' ,), '%b' , 0 ),
97+ (('month' ,), '%m' , 2 ),
9798 day_attribute_and_format ,
98- (('hour' ,), '%H' ),
99- (('minute' ,), '%M' ),
100- (('second' ,), '%S' ),
101- (('microsecond' ,), '%f' ),
102- (('second' , 'microsecond' ), '%S.%f' ),
99+ (('hour' ,), '%H' , 2 ),
100+ (('minute' ,), '%M' , 2 ),
101+ (('second' ,), '%S' , 2 ),
102+ (('microsecond' ,), '%f' , 6 ),
103+ (('second' , 'microsecond' ), '%S.%f' , 0 ),
103104 ]
104105
105106 if dayfirst :
@@ -125,7 +126,7 @@ def _guess_datetime_format(dt_str, dayfirst=False,
125126 format_guess = [None ] * len (tokens )
126127 found_attrs = set ()
127128
128- for attrs , attr_format in datetime_attrs_to_format :
129+ for attrs , attr_format , padding in datetime_attrs_to_format :
129130 # If a given attribute has been placed in the format string, skip
130131 # over other formats for that same underlying attribute (IE, month
131132 # can be represented in multiple different ways)
@@ -134,9 +135,11 @@ def _guess_datetime_format(dt_str, dayfirst=False,
134135
135136 if all (getattr (parsed_datetime , attr ) is not None for attr in attrs ):
136137 for i , token_format in enumerate (format_guess ):
138+ token_filled = tokens [i ].zfill (padding )
137139 if (token_format is None and
138- tokens [ i ] == parsed_datetime .strftime (attr_format )):
140+ token_filled == parsed_datetime .strftime (attr_format )):
139141 format_guess [i ] = attr_format
142+ tokens [i ] = token_filled
140143 found_attrs .update (attrs )
141144 break
142145
@@ -163,6 +166,8 @@ def _guess_datetime_format(dt_str, dayfirst=False,
163166
164167 guessed_format = '' .join (output_format )
165168
169+ # rebuild string, capturing any inferred padding
170+ dt_str = '' .join (tokens )
166171 if parsed_datetime .strftime (guessed_format ) == dt_str :
167172 return guessed_format
168173
0 commit comments