Fix l10n.formatTimestamp so it accepts strings

This commit is contained in:
bmng-dev 2014-08-08 04:38:23 +00:00
parent 0d2c94f060
commit 0a2fb54f78

View File

@ -11,6 +11,7 @@ logger = logging.getLogger('file_only')
DEFAULT_ENCODING = 'ISO8859-1' DEFAULT_ENCODING = 'ISO8859-1'
DEFAULT_LANGUAGE = 'en_US' DEFAULT_LANGUAGE = 'en_US'
DEFAULT_TIME_FORMAT = '%Y-%m-%d %H:%M:%S'
encoding = DEFAULT_ENCODING encoding = DEFAULT_ENCODING
language = DEFAULT_LANGUAGE language = DEFAULT_LANGUAGE
@ -23,13 +24,38 @@ except:
logger.exception('Could not determine language or encoding') logger.exception('Could not determine language or encoding')
if shared.config.has_option('bitmessagesettings', 'timeformat'):
time_format = shared.config.get('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): def formatTimestamp(timestamp = None, as_unicode = True):
if timestamp and isinstance(timestamp, (float, int)): #For some reason some timestamps are strings so we need to sanitize.
timestring = time.strftime(time_format, time.localtime(timestamp)) if timestamp is not None and not isinstance(timestamp, int):
else: 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) 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: if as_unicode:
return unicode(timestring, encoding) return unicode(timestring, encoding)
return timestring return timestring