Added format and updated README

This commit is contained in:
Daniel Tsvetkov 2019-03-13 15:21:33 -07:00
parent 303c6eb0bb
commit 98a9a0465f
2 changed files with 33 additions and 11 deletions

View File

@ -3,11 +3,12 @@ Find time now, in the past or future in any timezone or location.
## Usage ## Usage
``` ```
python tz.py HUMAN_TIME [HUMAN_TZ_LOC] python tz.py HUMAN_TIME [HUMAN_TZ_LOC] [--format="%Y-%m-%d %H:%M:%S%z"]
``` ```
* `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_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. * `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.
* `--format` is the format of the time to be displayed. See supported [datetime formats](https://docs.python.org/3/library/datetime.html#strftime-strptime-behavior)
You could alias the whole command to `tww` for faster typing, e.g. in your `.bashrc`: You could alias the whole command to `tww` for faster typing, e.g. in your `.bashrc`:
@ -34,21 +35,36 @@ pip install -r requirements.txt
Without timezone/location: Without timezone/location:
``` ```
tww now $ tww now
2019-03-13 15:04:36.607080
```
Time now in another timezone
```
$ tww now CET
2019-03-13 23:07:54.957743
```
One hour from now in UTC, showing only the time:
```
$ tww "in 1 hour" utc --format="%T"
23:17:49
``` ```
With timezone: With timezone:
``` ```
tww now cet $ tww now Asia/Tokyo
2019-03-14 07:06:35.273192
``` ```
Time in timezone: Another time in timezone:
``` ```
tww 15:10 cet $ tww 15:10 cet
2019-03-13 23:10:00
``` ```
Time in location: Time in location (**slow!**):
```
tww "3/14 15 9:26:53 PST" sofia
``` ```
$ tww "3/14 15 9:26:53 PST" sofia
2015-03-14 19:26:53+02:00
``` ```

12
tz.py
View File

@ -5,8 +5,9 @@ from pytz.exceptions import UnknownTimeZoneError
parser = argparse.ArgumentParser() parser = argparse.ArgumentParser()
parser.add_argument('human_dt') parser.add_argument('human_dt', help="datetime-like string")
parser.add_argument('human_tz', nargs='?') parser.add_argument('human_tz', nargs='?', help="timezone-like or location string")
parser.add_argument('--format', dest='format', default='%Y-%m-%d %H:%M:%S%z')
args = parser.parse_args() args = parser.parse_args()
human_dt = args.human_dt human_dt = args.human_dt
@ -34,4 +35,9 @@ except UnknownTimeZoneError:
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})
print(result) if result is None:
print("Could not parse '{human_dt}' or '{human_tz}'".format(human_dt, human_tz))
exit(1)
formatted_result = result.strftime(args.format)
print(formatted_result)