logo

searx

My custom branche(s) on searx, a meta-search engine
commit: 23fa74b5f9c7eae4d1686ec5fd3d7e79c2a7ebdf
parent: e1856426e21a0ef4839365c5e49dd6a673cc7a02
Author: Adam Tauber <asciimoo@gmail.com>
Date:   Sun, 11 Jan 2015 13:26:40 +0100

[enh] date formatting by locale

Diffstat:

Msearx/utils.py33+++++++++++++++++++++++++++++----
1 file changed, 29 insertions(+), 4 deletions(-)

diff --git a/searx/utils.py b/searx/utils.py @@ -1,15 +1,21 @@ # import htmlentitydefs +import locale +import dateutil.parser +import cStringIO +import csv +import os +import re + from codecs import getincrementalencoder from HTMLParser import HTMLParser from random import choice from searx.version import VERSION_STRING from searx import settings +from searx import logger -import cStringIO -import csv -import os -import re + +logger = logger.getChild('utils') ua_versions = ('29.0', '30.0', @@ -181,3 +187,22 @@ def get_result_templates(base_path): f = os.path.join(directory[base_path_length:], filename) result_templates.add(f) return result_templates + + +def format_date_by_locale(date_string, 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