erroring out on stderr

This commit is contained in:
Daniel Tsvetkov 2019-03-13 15:28:05 -07:00
parent 98a9a0465f
commit 0f3285062e

11
tz.py
View File

@ -1,3 +1,4 @@
import sys
import argparse import argparse
import dateparser import dateparser
@ -25,18 +26,24 @@ except UnknownTimeZoneError:
import string import string
from geopy.geocoders import Nominatim from geopy.geocoders import Nominatim
from geopy.exc import GeocoderTimedOut
from timezonefinder import TimezoneFinder from timezonefinder import TimezoneFinder
user_agent = ''.join(random.choices(string.ascii_uppercase + string.digits, k=20)) user_agent = ''.join(random.choices(string.ascii_uppercase + string.digits, k=20))
geolocator = Nominatim(user_agent=user_agent) geolocator = Nominatim(user_agent=user_agent)
location = geolocator.geocode(human_tz) try:
location = geolocator.geocode(human_tz)
except GeocoderTimedOut:
print("Timed out resolving location. Try specifying a timezone directly", file=sys.stderr)
exit(1)
tzf = TimezoneFinder() tzf = TimezoneFinder()
loc_tz = tzf.timezone_at(lng=location.longitude, lat=location.latitude) loc_tz = tzf.timezone_at(lng=location.longitude, lat=location.latitude)
result = dateparser.parse(human_dt, settings={'TO_TIMEZONE': loc_tz}) result = dateparser.parse(human_dt, settings={'TO_TIMEZONE': loc_tz})
if result is None: if result is None:
print("Could not parse '{human_dt}' or '{human_tz}'".format(human_dt, human_tz)) print("Could not parse '{human_dt}' or '{human_tz}'".format(human_dt, human_tz), file=sys.stderr)
exit(1) exit(1)
formatted_result = result.strftime(args.format) formatted_result = result.strftime(args.format)