Added format and updated README
This commit is contained in:
parent
303c6eb0bb
commit
98a9a0465f
32
README.md
32
README.md
@ -3,11 +3,12 @@ Find time now, in the past or future in any timezone or location.
|
||||
|
||||
## 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_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`:
|
||||
|
||||
@ -34,21 +35,36 @@ pip install -r requirements.txt
|
||||
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:
|
||||
```
|
||||
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:
|
||||
```
|
||||
tww "3/14 15 9:26:53 PST" sofia
|
||||
Time in location (**slow!**):
|
||||
```
|
||||
$ tww "3/14 15 9:26:53 PST" sofia
|
||||
2015-03-14 19:26:53+02:00
|
||||
```
|
||||
|
12
tz.py
12
tz.py
@ -5,8 +5,9 @@ from pytz.exceptions import UnknownTimeZoneError
|
||||
|
||||
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument('human_dt')
|
||||
parser.add_argument('human_tz', nargs='?')
|
||||
parser.add_argument('human_dt', help="datetime-like string")
|
||||
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()
|
||||
|
||||
human_dt = args.human_dt
|
||||
@ -34,4 +35,9 @@ except UnknownTimeZoneError:
|
||||
loc_tz = tzf.timezone_at(lng=location.longitude, lat=location.latitude)
|
||||
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)
|
||||
|
Loading…
Reference in New Issue
Block a user