[FIX] res_lang: sanitize time_format for python's strftime
authorOlivier Dony <odo@openerp.com>
Mon, 10 Jan 2011 15:19:12 +0000 (16:19 +0100)
committerOlivier Dony <odo@openerp.com>
Mon, 10 Jan 2011 15:19:12 +0000 (16:19 +0100)
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

bin/addons/base/res/res_lang.py

index abaa81e..d19e938 100644 (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'])),
         }