@@ -86,20 +86,21 @@ def _guess_datetime_format(dt_str, dayfirst=False,
86
86
if not isinstance (dt_str , compat .string_types ):
87
87
return None
88
88
89
- day_attribute_and_format = (('day' ,), '%d' )
89
+ day_attribute_and_format = (('day' ,), '%d' , 2 )
90
90
91
+ # attr name, format, padding (if any)
91
92
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 ),
97
98
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 ),
103
104
]
104
105
105
106
if dayfirst :
@@ -125,7 +126,7 @@ def _guess_datetime_format(dt_str, dayfirst=False,
125
126
format_guess = [None ] * len (tokens )
126
127
found_attrs = set ()
127
128
128
- for attrs , attr_format in datetime_attrs_to_format :
129
+ for attrs , attr_format , padding in datetime_attrs_to_format :
129
130
# If a given attribute has been placed in the format string, skip
130
131
# over other formats for that same underlying attribute (IE, month
131
132
# can be represented in multiple different ways)
@@ -134,9 +135,11 @@ def _guess_datetime_format(dt_str, dayfirst=False,
134
135
135
136
if all (getattr (parsed_datetime , attr ) is not None for attr in attrs ):
136
137
for i , token_format in enumerate (format_guess ):
138
+ token_filled = tokens [i ].zfill (padding )
137
139
if (token_format is None and
138
- tokens [ i ] == parsed_datetime .strftime (attr_format )):
140
+ token_filled == parsed_datetime .strftime (attr_format )):
139
141
format_guess [i ] = attr_format
142
+ tokens [i ] = token_filled
140
143
found_attrs .update (attrs )
141
144
break
142
145
@@ -163,6 +166,8 @@ def _guess_datetime_format(dt_str, dayfirst=False,
163
166
164
167
guessed_format = '' .join (output_format )
165
168
169
+ # rebuild string, capturing any inferred padding
170
+ dt_str = '' .join (tokens )
166
171
if parsed_datetime .strftime (guessed_format ) == dt_str :
167
172
return guessed_format
168
173
0 commit comments