dst is crap

This commit is contained in:
Daniel Tsvetkov 2021-05-13 12:04:06 +02:00
parent e4c1b7991e
commit b897831827
3 changed files with 29 additions and 10 deletions

View File

@ -3,10 +3,16 @@ Find time now, in the past or future in any timezone or location.
## Usage ## Usage
``` ```
python tww QUERY [--debug] [--full] python tww QUERY [--debug] [--full] [--show=<param>]
``` ```
* Supported `QUERY` types: * The param `--full` shows the full solutions that parsers came up with.
* You can use `--show=<param>` 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: `<datetime-like>`: - Just time: `<datetime-like>`:
- `now` - `now`
- `in 5 hours` - `in 5 hours`
@ -40,6 +46,7 @@ python tww QUERY [--debug] [--full]
- `timezone in Brazil` - `timezone in Brazil`
Few more notes: Few more notes:
- `<datetime-like>` is any time or time-like string - or example: - `<datetime-like>` is any time or time-like string - or example:
- `2019-04-26 3:14`, `06:42` `27 January 1992`, - `2019-04-26 3:14`, `06:42` `27 January 1992`,

View File

@ -287,7 +287,7 @@ def find_from_offset(query):
return None, [] return None, []
def resolve_timezone(query): def resolve_timezone(query, dt=None):
if not query: if not query:
query = "utc" query = "utc"
# if the human_tz_loc contains /, assume it's a timezone which could be # if the human_tz_loc contains /, assume it's a timezone which could be
@ -343,8 +343,10 @@ def resolve_timezone(query):
except UnknownTimeZoneError: except UnknownTimeZoneError:
pytz_result = type('pytz', (), {"zone": ""}) pytz_result = type('pytz', (), {"zone": ""})
tz_name = pytz_result.zone tz_name = pytz_result.zone
tz_abbr = 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
tz_offset = pytz_result.localize(datetime.now()).strftime('%z') if tz_name else "" 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 { return {
"query": query, "query": query,
"normal_query": normal_query, "normal_query": normal_query,
@ -359,6 +361,8 @@ def resolve_timezone(query):
"tz_name": tz_name, "tz_name": tz_name,
"tz_abbr": tz_abbr, "tz_abbr": tz_abbr,
"tz_offset": tz_offset, "tz_offset": tz_offset,
"tz_dst_seconds": tz_dst_seconds,
"tz_is_dst": tz_dst_seconds != 0,
} }

View File

@ -110,7 +110,7 @@ def timezone_mangle(dt_s: str, timezone_like_s: str):
dt_s = "now" dt_s = "now"
src_dt = dateparser_parse_dt(dt_s) src_dt = dateparser_parse_dt(dt_s)
logger.debug("Source time: {}".format(src_dt)) 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)) logger.debug("Destination timezone: {}".format(tz))
if not tz: if not tz:
tz, dst_dt, offset = {}, src_dt, {} tz, dst_dt, offset = {}, src_dt, {}
@ -270,14 +270,20 @@ def show_magic_results(obj, args, results=1):
rv = [] rv = []
for solution in obj['solutions']: for solution in obj['solutions']:
entry_proxy = Cut(solution, sep='->') entry_proxy = Cut(solution, sep='->')
highlight_entry = solution["highlight"] if args.show:
highlight_entry = args.show
else:
highlight_entry = solution["highlight"]
try: try:
highlight_result = entry_proxy[highlight_entry] highlight_result = entry_proxy[highlight_entry]
except Exception as e: except Exception as e:
logger.debug("Exception from magic result: {} -> {}".format(highlight_entry, e)) logger.debug("Exception from magic result: {} -> {}".format(highlight_entry, e))
continue continue
if args.handlers: 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: else:
to_print = highlight_result to_print = highlight_result
rv.append(to_print) rv.append(to_print)
@ -306,6 +312,7 @@ def dt_pretty(dt):
rv["locale_time"] = dt.strftime("%X") rv["locale_time"] = dt.strftime("%X")
rv["locale_dt"] = dt.strftime("%c") rv["locale_dt"] = dt.strftime("%c")
rv["tz_offset"] = dt.strftime("%z") rv["tz_offset"] = dt.strftime("%z")
rv["tz_name"] = dt.strftime("%Z")
rv["hh:mm"] = dt.strftime("%H:%M") rv["hh:mm"] = dt.strftime("%H:%M")
rv["emoji_time"] = time_to_emoji(dt) rv["emoji_time"] = time_to_emoji(dt)
rv["unix_s"] = get_s_since_epoch(dt) rv["unix_s"] = get_s_since_epoch(dt)
@ -402,7 +409,8 @@ def parse_args():
parser = argparse.ArgumentParser() parser = argparse.ArgumentParser()
parser.add_argument('query', nargs='*', default="", help="freeform") parser.add_argument('query', nargs='*', default="", help="freeform")
parser.add_argument('--locale', dest='locale') 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('--full', dest='full', action='store_true')
parser.add_argument('--debug', dest='debug', action='store_true') parser.add_argument('--debug', dest='debug', action='store_true')
args = parser.parse_args() args = parser.parse_args()