I need to convert dates represented as strings into datetime objects in python. Dates look like this:
"04. Mrz. 2021"
I have already set the correct locale:
import time
from datetime import datetime
import locale
# Let's set a german locale
locale.setlocale(locale.LC_ALL, 'de_DE.utf8')
print(locale.getlocale())
auction_date = datetime.strptime("03. Mrz. 2021", "%d. %b. %Y")
Traceback (most recent call last):
File "/home/tony/Documents/DEV/Whisky_Data/sandbox.py", line 8, in <module>
auction_date = datetime.strptime("03. Mrz. 2021", "%d. %b. %Y")
File "/usr/lib/python3.8/_strptime.py", line 568, in _strptime_datetime
tt, fraction, gmtoff_fraction = _strptime(data_string, format)
File "/usr/lib/python3.8/_strptime.py", line 349, in _strptime
raise ValueError("time data %r does not match format %r" %
ValueError: time data '03. Mrz. 2021' does not match format '%d. %b. %Y'
I have done some research and it seems like the "Mrz" is causing the issue. It works well with "Mär" and initially I thought the dates I get are just not correctly formatted. However according to Wikipedia DIN ISO 8601 specified that "Mär" should be abbreviated "Mrz" in Germany to avoid issues with coding due to the umlaut "ä".
So the dates I get are technically correctly formatted, however it seems like this has not been considered in either the locale "de_DE.utf8" or in the implementation of datetime.strptime
UPDATE: As @MrFuppers pointed out, the code does run just fine using Windows 10 Python 3.6. It seems this problem is specific to Linux/Ubuntu.
Does anyone know a solution to this?