separate tz resolver

This commit is contained in:
Daniel Tsvetkov 2019-09-23 20:35:18 +02:00
parent 5ecc97983d
commit f0d4a026f5

56
tww.py
View File

@ -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