pferd/PFERD/ilias/date_demangler.py

31 lines
818 B
Python
Raw Normal View History

2020-04-22 12:44:41 +02:00
"""
Helper methods to demangle an ILIAS date.
"""
import datetime
import locale
2020-04-22 12:44:41 +02:00
import re
def demangle_date(date: str) -> datetime.datetime:
"""
Demangle a given date in one of the following formats:
"Gestern, HH:MM"
"Heute, HH:MM"
"dd. mon.yyyy, HH:MM
"""
saved = locale.setlocale(locale.LC_ALL)
try:
locale.setlocale(locale.LC_ALL, 'de_DE.UTF-8')
2020-04-22 12:44:41 +02:00
date = re.sub(r"\s+", " ", date)
date = date.replace("Gestern", _yesterday().strftime("%d. %b %Y"))
date = date.replace("Heute", datetime.date.today().strftime("%d. %b %Y"))
return datetime.datetime.strptime(date, "%d. %b %Y, %H:%M")
finally:
locale.setlocale(locale.LC_ALL, saved)
2020-04-22 12:44:41 +02:00
def _yesterday() -> datetime.date:
return datetime.date.today() - datetime.timedelta(days=1)