From 0d2c94f0603f493479f7131bf8a7e62c7a22950e Mon Sep 17 00:00:00 2001 From: bmng-dev Date: Fri, 8 Aug 2014 01:34:57 +0000 Subject: [PATCH 1/2] Fix l10n so getTranslationLanguage always returns a string --- src/l10n.py | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/src/l10n.py b/src/l10n.py index c04b5cdb..cab012bd 100644 --- a/src/l10n.py +++ b/src/l10n.py @@ -8,15 +8,19 @@ import shared #logger = logging.getLogger(__name__) logger = logging.getLogger('file_only') + +DEFAULT_ENCODING = 'ISO8859-1' +DEFAULT_LANGUAGE = 'en_US' + +encoding = DEFAULT_ENCODING +language = DEFAULT_LANGUAGE + try: import locale - encoding = locale.getpreferredencoding(False) - language = locale.getlocale()[0] or locale.getdefaultlocale()[0] + encoding = locale.getpreferredencoding(False) or DEFAULT_ENCODING + language = locale.getlocale()[0] or locale.getdefaultlocale()[0] or DEFAULT_LANGUAGE except: logger.exception('Could not determine language or encoding') - if not encoding: - encoding = 'ISO8859-1' - language = 'en_US' time_format = shared.config.get('bitmessagesettings', 'timeformat') From 0a2fb54f783d1a894b280e02c7da64c66cca084d Mon Sep 17 00:00:00 2001 From: bmng-dev Date: Fri, 8 Aug 2014 04:38:23 +0000 Subject: [PATCH 2/2] Fix l10n.formatTimestamp so it accepts strings --- src/l10n.py | 34 ++++++++++++++++++++++++++++++---- 1 file changed, 30 insertions(+), 4 deletions(-) diff --git a/src/l10n.py b/src/l10n.py index cab012bd..d097f2f9 100644 --- a/src/l10n.py +++ b/src/l10n.py @@ -11,6 +11,7 @@ logger = logging.getLogger('file_only') DEFAULT_ENCODING = 'ISO8859-1' DEFAULT_LANGUAGE = 'en_US' +DEFAULT_TIME_FORMAT = '%Y-%m-%d %H:%M:%S' encoding = DEFAULT_ENCODING language = DEFAULT_LANGUAGE @@ -23,13 +24,38 @@ except: logger.exception('Could not determine language or encoding') -time_format = shared.config.get('bitmessagesettings', 'timeformat') +if shared.config.has_option('bitmessagesettings', 'timeformat'): + time_format = shared.config.get('bitmessagesettings', 'timeformat') + #Test the format string + try: + time.strftime(time_format) + except: + time_format = DEFAULT_TIME_FORMAT +else: + time_format = DEFAULT_TIME_FORMAT + def formatTimestamp(timestamp = None, as_unicode = True): - if timestamp and isinstance(timestamp, (float, int)): - timestring = time.strftime(time_format, time.localtime(timestamp)) - else: + #For some reason some timestamps are strings so we need to sanitize. + if timestamp is not None and not isinstance(timestamp, int): + try: + timestamp = int(timestamp) + except: + timestamp = None + + #timestamp can't be less than 0. + if timestamp is not None and timestamp < 0: + timestamp = None + + if timestamp is None: timestring = time.strftime(time_format) + else: + #In case timestamp is too far in the future + try: + timestring = time.strftime(time_format, time.localtime(timestamp)) + except ValueError: + timestring = time.strftime(time_format) + if as_unicode: return unicode(timestring, encoding) return timestring