From 373afad4234b4a07a08e0593f3f3c6048add14f4 Mon Sep 17 00:00:00 2001 From: Daniel Tsvetkov Date: Thu, 14 Mar 2019 18:25:05 -0700 Subject: [PATCH] tz aware as default return --- __init__.py | 0 tz.py | 7 +++++-- 2 files changed, 5 insertions(+), 2 deletions(-) create mode 100644 __init__.py diff --git a/__init__.py b/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tz.py b/tz.py index 1d71a9d..e302455 100644 --- a/tz.py +++ b/tz.py @@ -9,6 +9,7 @@ import dateparser from pytz.exceptions import UnknownTimeZoneError FUZZ_THRESHOLD = 70 +ISO_FORMAT = '%Y-%m-%dT%H:%M:%S%z' DEFAULT_FORMAT = '%Y-%m-%d %H:%M:%S%z' basepath = os.path.dirname(os.path.abspath(__file__)) @@ -20,6 +21,7 @@ def parse_args(): parser = argparse.ArgumentParser() parser.add_argument('query', nargs='*', help=" to ") parser.add_argument('--format', dest='format', default=DEFAULT_FORMAT) + parser.add_argument('--iso', dest='iso', action='store_true') parser.add_argument('--debug', dest='debug', action='store_true') args = parser.parse_args() return args @@ -200,7 +202,7 @@ def parse_query(query): def solve_query(human_dt, human_tz_loc): try: # first try parsing the timezone from user input - result = dateparser.parse(human_dt) + result = dateparser.parse(human_dt, settings={'RETURN_AS_TIMEZONE_AWARE': True}) logger.debug("human_dt result: {}".format(result)) if human_tz_loc: # if the human_tz_loc contains /, assume it's a timezone which could be @@ -252,7 +254,8 @@ def query_to_format_result(query, fmt=DEFAULT_FORMAT): def main(args): - formated_result = query_to_format_result(args.query, args.format) + fmt = ISO_FORMAT if args.iso else args.format + formated_result = query_to_format_result(args.query, fmt) print(formated_result)