August 2018
Intermediate to advanced
366 pages
10h 14m
English
This recipe requires the following steps:
import locale
import contextlib
@contextlib.contextmanager
def switchlocale(name):
prev = locale.getlocale()
locale.setlocale(locale.LC_ALL, name)
yield
locale.setlocale(locale.LC_ALL, prev)
def format_date(loc, d):
with switchlocale(loc):
fmt = locale.nl_langinfo(locale.D_T_FMT)
return d.strftime(fmt)
>>> format_date('de_DE', datetime.datetime.utcnow()) 'Mi 21 ...