From f0d4a026f5a8e2f11c0dfccf04cee7baa872406b Mon Sep 17 00:00:00 2001 From: Daniel Tsvetkov Date: Mon, 23 Sep 2019 20:35:18 +0200 Subject: [PATCH] separate tz resolver --- tww.py | 56 ++++++++++++++++++++++++++++++++++---------------------- 1 file changed, 34 insertions(+), 22 deletions(-) diff --git a/tww.py b/tww.py index dc151f6..854dbe9 100644 --- a/tww.py +++ b/tww.py @@ -8,6 +8,7 @@ import logging import os import dateparser +from datetime import datetime from pytz.exceptions import UnknownTimeZoneError FUZZ_THRESHOLD = 70 @@ -202,39 +203,50 @@ def parse_query(query): return human_dt, human_tz_loc +def resolve_timezone(query): + result = "" + try: + dateparser.parse(str(datetime.now()), settings={'TO_TIMEZONE': query}) + # if the human_tz_loc contains /, assume it's a timezone which could be + # incorrectly written with small letters - need Continent/City + if "/" in query: + result = timezone_to_normal(query) + except UnknownTimeZoneError: + from timezonefinder import TimezoneFinder + logger.debug("No timezone: {}".format(query)) + # if the human_tz_loc contains /, assume it's a timezone + # the timezone could still be guessed badly, attempt to get the city + # e.g.america/dallas + if "/" in query: + logger.debug("Assuming wrongly guessed tz {}".format(query)) + query = query.split('/')[-1] + logger.debug("Try city {}".format(query)) + # we don't know this timezone one, assume location + # Try to get from local file first + location = resolve_location_local(query) + if not location: + # finally go to remote + location = resolve_location_remote(query) + tzf = TimezoneFinder() + loc_tz = tzf.timezone_at(lat=location.latitude, lng=location.longitude) + logger.debug("Timezone: {}".format(loc_tz)) + result = loc_tz + return result + + def solve_query(human_dt, human_tz_loc): try: # first try parsing the timezone from user input result = dateparser.parse(human_dt, settings={'RETURN_AS_TIMEZONE_AWARE': True}) logger.debug("human_dt result: {}".format(result)) if human_tz_loc: - # if the human_tz_loc contains /, assume it's a timezone which could be - # incorrectly written with small letters - need Continent/City - if "/" in human_tz_loc: - human_tz_loc = timezone_to_normal(human_tz_loc) + human_tz_loc = resolve_timezone(human_tz_loc) isofmt = result.isoformat() logger.debug("human_dt isofmt: {}".format(isofmt)) result = dateparser.parse(isofmt, settings={'TO_TIMEZONE': human_tz_loc}) logger.debug("human_dt to_timezone result: {}".format(result)) except UnknownTimeZoneError: - from timezonefinder import TimezoneFinder - logger.debug("No timezone: {}".format(human_tz_loc)) - # if the human_tz_loc contains /, assume it's a timezone - # the timezone could still be guessed badly, attempt to get the city - # e.g.america/dallas - if "/" in human_tz_loc: - logger.debug("Assuming wrongly guessed tz {}".format(human_tz_loc)) - human_tz_loc = human_tz_loc.split('/')[-1] - logger.debug("Try city {}".format(human_tz_loc)) - # we don't know this timezone one, assume location - # Try to get from local file first - location = resolve_location_local(human_tz_loc) - if not location: - # finally go to remote - location = resolve_location_remote(human_tz_loc) - tzf = TimezoneFinder() - loc_tz = tzf.timezone_at(lat=location.latitude, lng=location.longitude) - logger.debug("Timezone: {}".format(loc_tz)) + loc_tz = resolve_timezone(human_tz_loc) result = dateparser.parse(human_dt, settings={'TO_TIMEZONE': loc_tz}) return result