I'm converting python's very un user-friendly strftime date time directives to user-friendly formats so that users of an API will know how to format a date if they send an invalid format.
My thinking is that %Y-%m-%d
is platform specific to Python where as even non-programmers would know what to send if we told them dates must be in the format YYYY-MM-DD
. I guess my biggest issue with this code is I feel like this might already be inbuilt with strftime or python's datetime lib. If not, can this be improved or is this good enough?
import re
directives = {
"%d": "DD",
"%-d": "D",
"%m": "MM",
"%-m": "M",
"%y": "YY",
"%Y": "YYYY",
"%H": "HH",
"%-H": "H",
"%I": "hh",
"%-I": "h",
"%p": "A",
"%M": "mm",
"%-M": "m",
"%S": "ss",
"%-S": "s"
}
format = "%Y-%m-%d"
pattern = re.compile('|'.join(directives.keys()))
nice_format = pattern.sub(lambda x: directives[x.group()], format)
print nice_format