Initial commit
This commit is contained in:
commit
303c6eb0bb
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
venv
|
54
README.md
Normal file
54
README.md
Normal file
@ -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
|
||||||
|
```
|
||||||
|
```
|
11
requrements.txt
Normal file
11
requrements.txt
Normal file
@ -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
|
37
tz.py
Normal file
37
tz.py
Normal file
@ -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)
|
Loading…
Reference in New Issue
Block a user