diff options
| author | Alexandre Flament <alex@al-f.net> | 2022-09-23 23:09:27 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-09-23 23:09:27 +0200 |
| commit | fc389f009d6da7f9d897c0b1ff50ef906866046f (patch) | |
| tree | 1b911cba1b96970455b115d1cc706db52d5613da /searx/webutils.py | |
| parent | bef3984d0320b2010c50097e186b07b6673a9647 (diff) | |
| parent | d6446be38f3f858c09887a89c8fc490a3c300b95 (diff) | |
Merge pull request #1705 from dalf/template_paper
Theme: add a paper.html template and update of the science engines
Diffstat (limited to 'searx/webutils.py')
| -rw-r--r-- | searx/webutils.py | 25 |
1 files changed, 25 insertions, 0 deletions
diff --git a/searx/webutils.py b/searx/webutils.py index b18fd5c6a..a5ed27c2c 100644 --- a/searx/webutils.py +++ b/searx/webutils.py @@ -7,11 +7,14 @@ import hmac import re import inspect import itertools +from datetime import datetime, timedelta from typing import Iterable, List, Tuple, Dict from io import StringIO from codecs import getincrementalencoder +from flask_babel import gettext, format_date + from searx import logger, settings from searx.engines import Engine, OTHER_CATEGORY @@ -138,6 +141,28 @@ def highlight_content(content, query): return content +def searxng_l10n_timespan(dt: datetime) -> str: # pylint: disable=invalid-name + """Returns a human-readable and translated string indicating how long ago + a date was in the past / the time span of the date to the present. + + On January 1st, midnight, the returned string only indicates how many years + ago the date was. + """ + # TODO, check if timezone is calculated right # pylint: disable=fixme + d = dt.date() + t = dt.time() + if d.month == 1 and d.day == 1 and t.hour == 0 and t.minute == 0 and t.second == 0: + return str(d.year) + if dt.replace(tzinfo=None) >= datetime.now() - timedelta(days=1): + timedifference = datetime.now() - dt.replace(tzinfo=None) + minutes = int((timedifference.seconds / 60) % 60) + hours = int(timedifference.seconds / 60 / 60) + if hours == 0: + return gettext('{minutes} minute(s) ago').format(minutes=minutes) + return gettext('{hours} hour(s), {minutes} minute(s) ago').format(hours=hours, minutes=minutes) + return format_date(dt) + + def is_flask_run_cmdline(): """Check if the application was started using "flask run" command line |