Localized month names?

Fredrik Lundh fredrik at pythonware.com
Mon Mar 13 17:20:29 EST 2006


Jarek Zgoda wrote:

> How do I get a list of localized month names for current locale? The
> first thing that came to my mind was an ugly hack:
>
> import datetime
> for i in range(12):
>     # as I remember, all months in 2005 had 1st in days
>     datetime.date(2005, i + 1, 1).strftime('%B')

doesn't look very ugly to me...

the strftime tables are hidden deep inside the C library, but I guess you
could use the _strptime implementation module to dig out the information
you're after:

>>> import locale
>>> locale.setlocale(locale.LC_ALL, "sv_SE")
'sv_SE'
>>> import _strptime
>>> vars(_strptime.LocaleTime())
{'lang': ('sv_SE', 'ISO8859-1'), 'am_pm': ['', ''], 'f_month': ['', 'januari',
'februari', 'mars', 'april', 'maj', 'juni', 'juli', 'augusti', 'september', 'oktober',
'november', 'december'], 'LC_date': '%Y-%m-%d', 'a_weekday': ['m\xe5n',
'tis', 'ons', 'tor', 'fre', 'l\xf6r', 's\xf6n'], 'f_weekday': ['m\xe5ndag', 'tisdag',
'onsdag', 'torsdag', 'fredag', 'l\xf6rdag', 's\xf6ndag'], 'LC_date_time':
'%a %d %b %Y %H.%M.%S', 'timezone': (frozenset(['utc', 'cet', 'gmt']),
frozenset(['cest'])), 'a_month': ['', 'jan', 'feb', 'mar', 'apr', 'maj', 'jun',
'jul', 'aug', 'sep', 'okt', 'nov', 'dec'], 'LC_time': '%H.%M.%S'}

(I'm pretty sure _strptime uses your ugly hack-approach to extract this
information from the C library...)

hope this helps!

</F>






More information about the Python-list mailing list