From b89783182725ca024720982eb6ff9d2dfafdc1f6 Mon Sep 17 00:00:00 2001 From: Daniel Tsvetkov Date: Thu, 13 May 2021 12:04:06 +0200 Subject: [PATCH] dst is crap --- README.md | 13 ++++++++++--- tww/lib.py | 10 +++++++--- tww/tokenizer.py | 16 ++++++++++++---- 3 files changed, 29 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 6dc27ec..9046c43 100644 --- a/README.md +++ b/README.md @@ -3,10 +3,16 @@ Find time now, in the past or future in any timezone or location. ## Usage ``` -python tww QUERY [--debug] [--full] +python tww QUERY [--debug] [--full] [--show=] ``` -* Supported `QUERY` types: +* The param `--full` shows the full solutions that parsers came up with. +* You can use `--show=` to show a particular json entry from the default solution, separated by `->`. For example: + - `tww now --show="dt->locale_dt"`: Thu 13 May 2021 11:26:02 AM CET + - `tww 17:00 --show="dt->emoji_time"`: 🕔 + - `tww time until Christmas --show="timedelta->diff->duration_machine"`: 0 years, 225 days, 12:30:33 + +### Supported `QUERY` types: - Just time: ``: - `now` - `in 5 hours` @@ -38,7 +44,8 @@ python tww QUERY [--debug] [--full] - `2021-05-10 day of week` - Find timezones - `timezone in Brazil` - + + Few more notes: - `` is any time or time-like string - or example: diff --git a/tww/lib.py b/tww/lib.py index 6b0d621..230c9da 100644 --- a/tww/lib.py +++ b/tww/lib.py @@ -287,7 +287,7 @@ def find_from_offset(query): return None, [] -def resolve_timezone(query): +def resolve_timezone(query, dt=None): if not query: query = "utc" # if the human_tz_loc contains /, assume it's a timezone which could be @@ -343,8 +343,10 @@ def resolve_timezone(query): except UnknownTimeZoneError: pytz_result = type('pytz', (), {"zone": ""}) tz_name = pytz_result.zone - tz_abbr = pytz_result.localize(datetime.now()).strftime('%Z') if tz_name else "" - tz_offset = pytz_result.localize(datetime.now()).strftime('%z') if tz_name else "" + tz_dst_seconds = pytz_result.dst(dt.replace(tzinfo=None) or datetime.now()).seconds + pytz_localized = pytz_result.localize(dt.replace(tzinfo=None) or datetime.now()) + tz_abbr = pytz_localized.strftime('%Z') if tz_name else "" + tz_offset = pytz_localized.strftime('%z') if tz_name else "" return { "query": query, "normal_query": normal_query, @@ -359,6 +361,8 @@ def resolve_timezone(query): "tz_name": tz_name, "tz_abbr": tz_abbr, "tz_offset": tz_offset, + "tz_dst_seconds": tz_dst_seconds, + "tz_is_dst": tz_dst_seconds != 0, } diff --git a/tww/tokenizer.py b/tww/tokenizer.py index 4efb871..00e0efd 100644 --- a/tww/tokenizer.py +++ b/tww/tokenizer.py @@ -110,7 +110,7 @@ def timezone_mangle(dt_s: str, timezone_like_s: str): dt_s = "now" src_dt = dateparser_parse_dt(dt_s) logger.debug("Source time: {}".format(src_dt)) - tz = resolve_timezone(timezone_like_s) + tz = resolve_timezone(timezone_like_s, src_dt) logger.debug("Destination timezone: {}".format(tz)) if not tz: tz, dst_dt, offset = {}, src_dt, {} @@ -270,14 +270,20 @@ def show_magic_results(obj, args, results=1): rv = [] for solution in obj['solutions']: entry_proxy = Cut(solution, sep='->') - highlight_entry = solution["highlight"] + if args.show: + highlight_entry = args.show + else: + highlight_entry = solution["highlight"] try: highlight_result = entry_proxy[highlight_entry] except Exception as e: logger.debug("Exception from magic result: {} -> {}".format(highlight_entry, e)) continue if args.handlers: - to_print = "{} -> {}".format(solution['handler'], highlight_result) + # TODO: not working yet: + user_handlers = args.handlers.split(',') + for user_handler in user_handlers: + to_print = "{} -> {}".format(user_handler, highlight_result) else: to_print = highlight_result rv.append(to_print) @@ -306,6 +312,7 @@ def dt_pretty(dt): rv["locale_time"] = dt.strftime("%X") rv["locale_dt"] = dt.strftime("%c") rv["tz_offset"] = dt.strftime("%z") + rv["tz_name"] = dt.strftime("%Z") rv["hh:mm"] = dt.strftime("%H:%M") rv["emoji_time"] = time_to_emoji(dt) rv["unix_s"] = get_s_since_epoch(dt) @@ -402,7 +409,8 @@ def parse_args(): parser = argparse.ArgumentParser() parser.add_argument('query', nargs='*', default="", help="freeform") parser.add_argument('--locale', dest='locale') - parser.add_argument('--handlers', dest='handlers', action='store_true') + parser.add_argument('--handlers', dest='handlers') + parser.add_argument('--show', dest='show') parser.add_argument('--full', dest='full', action='store_true') parser.add_argument('--debug', dest='debug', action='store_true') args = parser.parse_args()