[FIX] res_lang: sanitize time_format for python's strftime

POSIX's strftime has a number of 'compound' formats for quick-formatting of time spans (e.g. %T for %H:%M:%S) which Python's own strftime does not support => error when trying to use them


manually expand those compound formats to their atoms

refs:
http://pubs.opengroup.org/onlinepubs/009695399/functions/strftime.html
http://docs.python.org/library/time.html#time.strftime

lp bug: https://launchpad.net/bugs/698134 fixed
lp bug: https://launchpad.net/bugs/697833 fixed

bzr revid: odo@openerp.com-20110110151912-ysu06271z40hxxho
This commit is contained in:
Olivier Dony 2011-01-10 16:19:12 +01:00
parent 5813e0ee20
commit 438232f6be
1 changed files with 13 additions and 1 deletions

View File

@ -66,17 +66,29 @@ class lang(osv.osv):
def fix_xa0(s):
"""Fix badly-encoded non-breaking space Unicode character from locale.localeconv(),
coercing to utf-8, as some platform seem to output localeconv() in their system
encoding, e.g. Windows-1252"""
if s == '\xa0':
return '\xc2\xa0'
return s
def fix_time_format(format):
"""Python's strftime does not support all the compound formats
from C's strftime, as returned by locale.nl_langinfo().
Under Ubuntu at least, we encounter %T or %r for some locales."""
format = format.replace('%T', '%H:%M:%S')\
.replace('%r', '%I:%M:%S %p')\
.replace('%R', '%H:%M')
return str(format)
lang_info = {
'code': lang,
'iso_code': iso_lang,
'name': lang_name,
'translatable': 1,
'date_format' : str(locale.nl_langinfo(locale.D_FMT).replace('%y', '%Y')),
'time_format' : str(locale.nl_langinfo(locale.T_FMT)),
'time_format' : fix_time_format(locale.nl_langinfo(locale.T_FMT)),
'decimal_point' : fix_xa0(str(locale.localeconv()['decimal_point'])),
'thousands_sep' : fix_xa0(str(locale.localeconv()['thousands_sep'])),
}