If you are not setting the locale with the locale
module, then Python uses the C locale and you can predict the maximum length produced. All strings will be in English and the maximum length per format character is known.
Parse the string yourself, count the non-format characters and map format characters to the maximum length for that field.
If you were to use locale
, you'll need to calculate the max length per language. You can automate the locale-dependent fields by looping over the months, weekdays, and AM/PM and measuring the max length for the %a
, %A
, %b
, %B
, %c
, %p
, %x
and %X
formats. I'd do that on the fly as needed.
The rest of the formats do not vary by locale and have a documented maximum length (the examples in the strptime
table are typical, you can rely on those documenting the field length).