translation

This commit is contained in:
Daniel Tsvetkov 2020-02-11 13:14:00 +01:00
parent 39527abc0a
commit f5e51bbdab
2 changed files with 8 additions and 12 deletions

View File

@ -68,11 +68,7 @@ def handler_timezone_translation(dt_s: str, timezone_like_s: str) -> dict:
else: else:
offset = tz.get('tz_offset') offset = tz.get('tz_offset')
dst_dt = dt_tz_translation(src_dt, offset) dst_dt = dt_tz_translation(src_dt, offset)
return dict( return src_dt, dst_dt, tz
src_dt=dt_pretty(src_dt),
dst_dt=dt_pretty(dst_dt),
tz=tz,
)
def handler_generic_parser(dt_s: str) -> datetime: def handler_generic_parser(dt_s: str) -> datetime:
@ -137,7 +133,7 @@ def tokenize(s):
except Exception as e: except Exception as e:
continue continue
if result is not None: if result is not None:
solutions.append((h.__name__, (result,), t)) solutions.append((h.__name__, result, t))
return solutions return solutions
@ -185,7 +181,6 @@ def dt_pretty(dt):
rv["locale_month_short"] = dt.strftime("%b") rv["locale_month_short"] = dt.strftime("%b")
rv["tz_name"] = dt.strftime("%Z") rv["tz_name"] = dt.strftime("%Z")
rv["tz_offset"] = dt.strftime("%z") rv["tz_offset"] = dt.strftime("%z")
rv["tz"] = resolve_timezone(dt.strftime("%z"))
rv["hh:mm"] = dt.strftime("%H:%M") rv["hh:mm"] = dt.strftime("%H:%M")
rv["locale_time"] = dt.strftime("%X") rv["locale_time"] = dt.strftime("%X")
rv["locale_date"] = dt.strftime("%x") rv["locale_date"] = dt.strftime("%x")
@ -283,20 +278,21 @@ def resolve_query(query):
element["handler"] = handler element["handler"] = handler
element["query_type"] = query_type element["query_type"] = query_type
if query_type == QUERY_TYPE_DT: if query_type == QUERY_TYPE_DT:
element["dt"] = dt_pretty(results[0]) element["dt"] = dt_pretty(results)
elif query_type == QUERY_TYPE_DT_TR: elif query_type == QUERY_TYPE_DT_TR:
element["src_dt"] = dt_pretty(results[0]) element["src_dt"] = dt_pretty(results[0])
element["dst_dt"] = dt_pretty(results[1]) element["dst_dt"] = dt_pretty(results[1])
element["tz"] = results[2]
elif query_type == QUERY_TYPE_TZ: elif query_type == QUERY_TYPE_TZ:
element["tz"] = results[0] element["tz"] = results
elif query_type == QUERY_TYPE_TD: elif query_type == QUERY_TYPE_TD:
element["timedelta"] = results[0] element["timedelta"] = results
rv["solutions"].append(element) rv["solutions"].append(element)
return rv return rv
if __name__ == "__main__": if __name__ == "__main__":
# query = "time in sofia" # query = "2020-02-11T13:01:05+0100 to sofia"
query = ' '.join(sys.argv[1:]) query = ' '.join(sys.argv[1:])
result = resolve_query(query) result = resolve_query(query)
pretty_print_dict(result) pretty_print_dict(result)

View File

@ -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(':') from_shh, from_mm = from_tz_offset.split(':')
else: else:
from_shh, from_mm = from_tz_offset[:-2], to_tz_offset[-2:] 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: if dt.tzinfo:
return dt.astimezone(tzinfo) return dt.astimezone(tzinfo)
r_dt = dt + timedelta(hours=int(to_shh), minutes=int(to_mm)) - timedelta( r_dt = dt + timedelta(hours=int(to_shh), minutes=int(to_mm)) - timedelta(