diff --git a/searx/plugins/time_zone.py b/searx/plugins/time_zone.py new file mode 100644 index 000000000..e6d3e9dac --- /dev/null +++ b/searx/plugins/time_zone.py @@ -0,0 +1,59 @@ +# SPDX-License-Identifier: AGPL-3.0-or-later +# pylint: disable=missing-module-docstring, missing-class-docstring +import zoneinfo +import datetime + +from flask_babel import gettext +from searx.result_types import EngineResults + +from . import Plugin, PluginInfo + + +datetime_format = "%H:%M - %A, %d/%m/%y" + + +class SXNGPlugin(Plugin): + + id = "time_zone" + keywords = ["time", "timezone", "now", "clock", "timezones"] + + def __init__(self, plg_cfg: "PluginCfg"): + super().__init__(plg_cfg) + + self.info = PluginInfo( + id=self.id, + name=gettext("Timezones plugin"), + description=gettext("Display the current time on different time zones."), + preference_section="query", + examples=["time Berlin", "clock Los Angeles"], + ) + + def post_search(self, request: "SXNG_Request", search: "SearchWithPlugins") -> EngineResults: + results = EngineResults() + + # remove keywords from the query + query = search.search_query.query + query_parts = filter(lambda part: part.lower() not in self.keywords, query.split(" ")) + location = "_".join(query_parts) + + if not location: + results.add(results.types.Answer(answer=f"{datetime.datetime.now().strftime(datetime_format)}")) + return results + + # location is too short for proper matching + if len(location) <= 3: + return results + + tz_names = zoneinfo.available_timezones() + for tz_name in tz_names: + if location in tz_name.lower(): + zone = zoneinfo.ZoneInfo(tz_name) + now = datetime.datetime.now(tz=zone) + + results.add( + results.types.Answer( + answer=f"{now.strftime(datetime_format)} at {tz_name.replace('_', ' ')} ({now.strftime('%Z')})" + ) + ) + + return results diff --git a/searx/settings.yml b/searx/settings.yml index 7e0455701..7ccfeb62e 100644 --- a/searx/settings.yml +++ b/searx/settings.yml @@ -238,6 +238,9 @@ plugins: searx.plugins.hostnames.SXNGPlugin: active: true + searx.plugins.time_zone.SXNGPlugin: + active: true + searx.plugins.oa_doi_rewrite.SXNGPlugin: active: false