commit 303c6eb0bb6968ec1d9d3b77f4c14dfcaae6edbc Author: Daniel Tsvetkov Date: Wed Mar 13 15:02:54 2019 -0700 Initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..5ceb386 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +venv diff --git a/README.md b/README.md new file mode 100644 index 0000000..7eb7820 --- /dev/null +++ b/README.md @@ -0,0 +1,54 @@ +# Time when and where +Find time now, in the past or future in any timezone or location. + +## Usage +``` +python tz.py HUMAN_TIME [HUMAN_TZ_LOC] +``` + +* `HUMAN_TIME` - is any time or time-like string. See the [dateparser](https://pypi.org/project/dateparser/) for example `10:15`, `now`, `in 3 hours` and many others. +* `HUMAN_TZ_LOC` - (optional) is either timezone to which the date should be translated (fast) or a location (slow - and requires internet connection to resolve the location). Uses [geopy](https://geopy.readthedocs.io/en/stable/) for location resolution and [timezonefinder](https://pypi.org/project/timezonefinder/) for timezone resolution. + +You could alias the whole command to `tww` for faster typing, e.g. in your `.bashrc`: + +``` +alias tww="~/workspace/tz/venv/bin/python ~/workspace/tz/tz.py" +``` + +If `HUMAN_TIME` and/or `HUMAN_TZ_LOC` have spaces, they need to be quoted, e.g.: + +``` +tww "in 2 hours" "los angeles" +``` + +## Install + +``` +virtualenv -p python3 venv +source venv/bin/activate +pip install -r requirements.txt +``` + +## Examples + +Without timezone/location: + +``` +tww now +``` + +With timezone: +``` +tww now cet +``` + +Time in timezone: +``` +tww 15:10 cet +``` + +Time in location: +``` +tww "3/14 15 9:26:53 PST" sofia +``` +``` diff --git a/requrements.txt b/requrements.txt new file mode 100644 index 0000000..ae50f4e --- /dev/null +++ b/requrements.txt @@ -0,0 +1,11 @@ +dateparser==0.7.1 +geographiclib==1.49 +geopy==1.18.1 +importlib-resources==1.0.2 +numpy==1.16.2 +python-dateutil==2.8.0 +pytz==2018.9 +regex==2019.3.12 +six==1.12.0 +timezonefinder==4.0.1 +tzlocal==1.5.1 diff --git a/tz.py b/tz.py new file mode 100644 index 0000000..9631765 --- /dev/null +++ b/tz.py @@ -0,0 +1,37 @@ +import argparse + +import dateparser +from pytz.exceptions import UnknownTimeZoneError + + +parser = argparse.ArgumentParser() +parser.add_argument('human_dt') +parser.add_argument('human_tz', nargs='?') +args = parser.parse_args() + +human_dt = args.human_dt +human_tz = args.human_tz + +try: + # first try parsing the timezone from user input + if human_tz: + result = dateparser.parse(human_dt, settings={'TO_TIMEZONE': human_tz}) + else: + result = dateparser.parse(human_dt) +except UnknownTimeZoneError: + # we don't know this timezone one, assume location + import random + import string + + from geopy.geocoders import Nominatim + from timezonefinder import TimezoneFinder + + user_agent = ''.join(random.choices(string.ascii_uppercase + string.digits, k=20)) + geolocator = Nominatim(user_agent=user_agent) + location = geolocator.geocode(human_tz) + + tzf = TimezoneFinder() + loc_tz = tzf.timezone_at(lng=location.longitude, lat=location.latitude) + result = dateparser.parse(human_dt, settings={'TO_TIMEZONE': loc_tz}) + +print(result)