From f5e51bbdab0e0d2eba31b2f59b6e2d09ec59d8a7 Mon Sep 17 00:00:00 2001 From: Daniel Tsvetkov Date: Tue, 11 Feb 2020 13:14:00 +0100 Subject: [PATCH] translation --- src/tww/tokenizer.py | 18 +++++++----------- src/tww/tww.py | 2 +- 2 files changed, 8 insertions(+), 12 deletions(-) diff --git a/src/tww/tokenizer.py b/src/tww/tokenizer.py index e52ebc1..323c185 100644 --- a/src/tww/tokenizer.py +++ b/src/tww/tokenizer.py @@ -68,11 +68,7 @@ def handler_timezone_translation(dt_s: str, timezone_like_s: str) -> dict: else: offset = tz.get('tz_offset') dst_dt = dt_tz_translation(src_dt, offset) - return dict( - src_dt=dt_pretty(src_dt), - dst_dt=dt_pretty(dst_dt), - tz=tz, - ) + return src_dt, dst_dt, tz def handler_generic_parser(dt_s: str) -> datetime: @@ -137,7 +133,7 @@ def tokenize(s): except Exception as e: continue if result is not None: - solutions.append((h.__name__, (result,), t)) + solutions.append((h.__name__, result, t)) return solutions @@ -185,7 +181,6 @@ def dt_pretty(dt): rv["locale_month_short"] = dt.strftime("%b") rv["tz_name"] = dt.strftime("%Z") rv["tz_offset"] = dt.strftime("%z") - rv["tz"] = resolve_timezone(dt.strftime("%z")) rv["hh:mm"] = dt.strftime("%H:%M") rv["locale_time"] = dt.strftime("%X") rv["locale_date"] = dt.strftime("%x") @@ -283,20 +278,21 @@ def resolve_query(query): element["handler"] = handler element["query_type"] = query_type if query_type == QUERY_TYPE_DT: - element["dt"] = dt_pretty(results[0]) + element["dt"] = dt_pretty(results) elif query_type == QUERY_TYPE_DT_TR: element["src_dt"] = dt_pretty(results[0]) element["dst_dt"] = dt_pretty(results[1]) + element["tz"] = results[2] elif query_type == QUERY_TYPE_TZ: - element["tz"] = results[0] + element["tz"] = results elif query_type == QUERY_TYPE_TD: - element["timedelta"] = results[0] + element["timedelta"] = results rv["solutions"].append(element) return rv if __name__ == "__main__": - # query = "time in sofia" + # query = "2020-02-11T13:01:05+0100 to sofia" query = ' '.join(sys.argv[1:]) result = resolve_query(query) pretty_print_dict(result) diff --git a/src/tww/tww.py b/src/tww/tww.py index 08b2253..5aa191d 100644 --- a/src/tww/tww.py +++ b/src/tww/tww.py @@ -508,7 +508,7 @@ def dt_tz_translation(dt: datetime, to_tz_offset: str, from_tz_offset: str = "+0 from_shh, from_mm = from_tz_offset.split(':') else: from_shh, from_mm = from_tz_offset[:-2], to_tz_offset[-2:] - tzinfo = tzinfo_from_offset(to_tz_offset) + tzinfo = tzinfo_from_offset(to_tz_offset)[0] if dt.tzinfo: return dt.astimezone(tzinfo) r_dt = dt + timedelta(hours=int(to_shh), minutes=int(to_mm)) - timedelta(