forked from Icycoide/searxng
[fix][mod] wikidata date handling refactor - fixes #387
This commit is contained in:
parent
7580852bda
commit
362c849797
2 changed files with 35 additions and 25 deletions
|
@ -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):
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue