diff options
author | Adam Tauber <asciimoo@gmail.com> | 2015-09-07 22:39:33 +0200 |
---|---|---|
committer | Adam Tauber <asciimoo@gmail.com> | 2015-09-07 22:39:33 +0200 |
commit | 362c849797e2e6f0e232642c23744c47a75cdfd4 (patch) | |
tree | 3d10182cf80474eba21c2a6ce5bf05ac06ad3391 /searx/utils.py | |
parent | 7580852bda660471f8968b4f14cdf44dad73249f (diff) | |
download | searxng-362c849797e2e6f0e232642c23744c47a75cdfd4.tar.gz searxng-362c849797e2e6f0e232642c23744c47a75cdfd4.zip |
[fix][mod] wikidata date handling refactor - fixes #387
Diffstat (limited to 'searx/utils.py')
-rw-r--r-- | searx/utils.py | 28 |
1 files changed, 10 insertions, 18 deletions
diff --git a/searx/utils.py b/searx/utils.py index 3651cc389..b8561b84b 100644 --- a/searx/utils.py +++ b/searx/utils.py @@ -1,11 +1,10 @@ # import htmlentitydefs -import locale -import dateutil.parser import cStringIO import csv import os import re +from babel.dates import format_date from codecs import getincrementalencoder from HTMLParser import HTMLParser from random import choice @@ -195,23 +194,16 @@ def get_result_templates(base_path): return result_templates -def format_date_by_locale(date_string, locale_string): +def format_date_by_locale(date, locale_string): # strftime works only on dates after 1900 - parsed_date = dateutil.parser.parse(date_string) - if parsed_date.year <= 1900: - return parsed_date.isoformat().split('T')[0] - - orig_locale = locale.getlocale()[0] - try: - locale.setlocale(locale.LC_ALL, locale_string) - except: - logger.warning('cannot set locale: {0}'.format(locale_string)) - formatted_date = parsed_date.strftime(locale.nl_langinfo(locale.D_FMT)) - try: - locale.setlocale(locale.LC_ALL, orig_locale) - except: - logger.warning('cannot set original locale: {0}'.format(orig_locale)) - return formatted_date + + if date.year <= 1900: + return date.isoformat().split('T')[0] + + if locale_string == 'all': + locale_string = settings['ui']['default_locale'] or 'en_US' + + return format_date(date, locale=locale_string) def dict_subset(d, properties): |