fix tokenizer
This commit is contained in:
parent
ff03e93840
commit
559bd39e9a
@ -1,6 +1,7 @@
|
||||
import re
|
||||
import sys
|
||||
from datetime import timedelta, datetime
|
||||
from pprint import pprint, pformat
|
||||
|
||||
from tww import resolve_timezone, dateparser_parse_dt, get_utcnow, get_s_since_epoch, get_ms_since_epoch, \
|
||||
dt_tz_translation, DEFAULT_FORMAT, get_local_now
|
||||
@ -42,24 +43,34 @@ def handler_time_ms_now_utc() -> int:
|
||||
return get_ms_since_epoch(get_utcnow())
|
||||
|
||||
|
||||
def handler_time_diff(start_dt: str, end_dt: str) -> timedelta:
|
||||
return dateparser_parse_dt(end_dt) - dateparser_parse_dt(start_dt)
|
||||
def handler_time_diff(start_dt: str, end_dt: str) -> dict:
|
||||
start, end = dateparser_parse_dt(start_dt), dateparser_parse_dt(end_dt)
|
||||
return dict(start=start.strftime(DEFAULT_FORMAT),
|
||||
end=end.strftime(DEFAULT_FORMAT),
|
||||
diff=end - start)
|
||||
|
||||
|
||||
def handler_time_since(start_dt_s: str) -> timedelta:
|
||||
def handler_time_since(start_dt_s: str) -> dict:
|
||||
return handler_time_diff(start_dt_s, str(get_utcnow()))
|
||||
|
||||
|
||||
def handler_time_until(end_dt_s: str) -> timedelta:
|
||||
def handler_time_until(end_dt_s: str) -> dict:
|
||||
return handler_time_diff(str(get_utcnow()), end_dt_s)
|
||||
|
||||
|
||||
def handler_timezone_translation(dt_s: str, timezone_like_s: str) -> datetime:
|
||||
def handler_timezone_translation(dt_s: str, timezone_like_s: str) -> dict:
|
||||
dt = dateparser_parse_dt(dt_s)
|
||||
tz = resolve_timezone(timezone_like_s)
|
||||
if tz:
|
||||
if not tz:
|
||||
tz, offset, the_time = '', '', dt
|
||||
else:
|
||||
offset = tz.get('tz_offset')
|
||||
return dt_tz_translation(dt, offset)
|
||||
translation = dt_tz_translation(dt, offset)
|
||||
the_time = translation.strftime(DEFAULT_FORMAT)
|
||||
return dict(dt=the_time,
|
||||
offset=offset,
|
||||
tz=tz,
|
||||
)
|
||||
|
||||
|
||||
def handler_generic_parser(dt_s: str) -> datetime:
|
||||
@ -132,13 +143,15 @@ def test():
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
query = ' '.join(sys.argv[1:])
|
||||
query = "2020-02-07 11:25:58+0000 in seconds" #' '.join(sys.argv[1:])
|
||||
results = parse(query)
|
||||
for handler, result in results:
|
||||
if type(result) is datetime:
|
||||
print(" {} -> {}".format(handler, result.strftime(DEFAULT_FORMAT)))
|
||||
elif type(result) is timedelta:
|
||||
print(" {} -> {}".format(handler, result))
|
||||
elif type(result) is dict:
|
||||
print(" {} -> {}".format(handler, pformat(result)))
|
||||
elif type(result) is None:
|
||||
print(" {} -> Couldn't solve query".format(handler))
|
||||
else:
|
||||
|
@ -439,7 +439,7 @@ def query_to_dt(query):
|
||||
def tzinfo_from_offset(offset: str) -> pytz.timezone:
|
||||
if ':' in offset:
|
||||
offset = ''.join(offset.split(':'))
|
||||
tznames = TZ_OFFSETS.get(offset, ["utc"])
|
||||
tznames = TZ_OFFSETS.get(offset, [])
|
||||
for tzname in tznames:
|
||||
if tzname.startswith('Etc/GMT'):
|
||||
return pytz.timezone(tzname)
|
||||
@ -515,7 +515,10 @@ def get_dt_tz_offset(dt: datetime) -> timedelta:
|
||||
if type(dt.tzinfo) is StaticTzInfo:
|
||||
tzoffset = dt.tzinfo._StaticTzInfo__offset
|
||||
else:
|
||||
tzoffset = dt.tzinfo._utcoffset
|
||||
try:
|
||||
tzoffset = dt.tzinfo._utcoffset
|
||||
except Exception as e:
|
||||
return dt.tzinfo.utcoffset(dt)
|
||||
return tzoffset
|
||||
return timedelta(0)
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user