diff --git a/tww/tww.py b/tww/lib.py similarity index 96% rename from tww/tww.py rename to tww/lib.py index 5bd9e03..5befb41 100644 --- a/tww/tww.py +++ b/tww/lib.py @@ -17,6 +17,7 @@ from heapq import heappush, heappop import dateparser import pytz from dateparser import parse as parse_dt +from dateparser.search import search_dates from dateparser.timezone_parser import StaticTzInfo from datetimerange import DateTimeRange from dateutil.parser import parse as dutil_parse @@ -37,6 +38,8 @@ DEFAULT_FORMAT = '%Y-%m-%d %H:%M:%S%z' basepath = os.path.dirname(os.path.abspath(__file__)) +SKIP_DATE_TOKENS = ['on', 'to', 'set', 'be'] + def parse_to_iso(dt): parsed_dt = dateparser.parse(dt) @@ -44,6 +47,27 @@ def parse_to_iso(dt): return parsed_dt.strftime(ISO_FORMAT) +def find_dates_in(text): + rv = [] + dateparser_dates = search_dates(text, settings={"SKIP_TOKENS": SKIP_DATE_TOKENS}) + if dateparser_dates: + rv += list(filter(_dateparser_search_filter, dateparser_dates)) + try: + dateutil_dates = dutil_parse(text, fuzzy_with_tokens=True) + ignored_tokens = dateutil_dates[1] + text_analysed = "" + for ignored_token in ignored_tokens: + text_analysed += text.split(ignored_token)[0] + rv.append([text_analysed, dateutil_dates[0]]) + except: + ... + return rv + + +def _dateparser_search_filter(dt_raw): + return not (dt_raw[0].isdigit() or dt_raw[0] in SKIP_DATE_TOKENS) + + class Location(object): """ Represents a location with name, latitude and longitude @@ -337,7 +361,7 @@ def resolve_timezone(query): def solve_query(human_dt, human_tz_loc=None): try: # first try parsing the timezone from user input - result = dateparser.parse(human_dt, settings={'RETURN_AS_TIMEZONE_AWARE': True}) + result = dateparser.parse(human_dt, settings={'RETURN_AS_TIMEZONE_AWARE': True, "TIMEZONE": "utc"}) logger.debug("human_dt result: {}".format(result)) if human_tz_loc: human_tz_loc = resolve_timezone(human_tz_loc)["tz_name"] @@ -374,20 +398,8 @@ def main(args): print(formated_result) -if __name__ == "__main__": - args = parse_args() - setup_logging_level(args.debug) - main(args) - - -def time_ago(date=None, diff=None): - """ - Get a datetime object, timedelta object or a int() Epoch timestamp and - return a - pretty string like 'an hour ago', 'Yesterday', '3 months ago', - 'just now', etc - Modified from: http://stackoverflow.com/a/1551394/141084 - """ +def time_ago(date=None, diff=None, min_resolution=None, max_resolution=None): + # TODO : create a min/max resolution now = get_utcnow() if not date: if diff: @@ -397,8 +409,8 @@ def time_ago(date=None, diff=None): else: if type(date) is str: parsed_dt = parse_dt(date) - if parsed_dt.tzinfo is not None: - now = get_utcnow(tzaware=True) + if parsed_dt.tzinfo is None: + now = get_utcnow(tzaware=False) diff = now - parsed_dt elif type(date) is timedelta: diff = date @@ -787,3 +799,9 @@ def td_iso8601(td): if rem[timeframe]: fmt += "{}{}".format(rem[timeframe], short) return fmt + + +if __name__ == "__main__": + args = parse_args() + setup_logging_level(args.debug) + main(args) diff --git a/tww/localization.py b/tww/localization.py index 12c0563..f318d71 100644 --- a/tww/localization.py +++ b/tww/localization.py @@ -5,7 +5,7 @@ import os from fuzzywuzzy import fuzz -from tww.tww import basepath +from tww.lib import basepath @contextlib.contextmanager diff --git a/tww/tokenizer.py b/tww/tokenizer.py index f690c4e..6cb2041 100644 --- a/tww/tokenizer.py +++ b/tww/tokenizer.py @@ -9,8 +9,8 @@ from pygments import highlight, lexers, formatters from scalpl import Cut from tww.localization import setlocale, resolve_locale -from tww.tww import ISO_FORMAT, time_to_emoji, time_ago, workday_diff, workhours_diff, td_remainders, td_totals, td_iso8601 -from tww.tww import resolve_timezone, dateparser_parse_dt, get_utcnow, get_s_since_epoch, get_ms_since_epoch, \ +from tww.lib import ISO_FORMAT, time_to_emoji, time_ago, workday_diff, workhours_diff, td_remainders, td_totals, td_iso8601 +from tww.lib import resolve_timezone, dateparser_parse_dt, get_utcnow, get_s_since_epoch, get_ms_since_epoch, \ dt_tz_translation, get_local_now, query_to_format_result from tww.common import logger