From 649b223ed3c90f2f6955014516311cd28bf44cb6 Mon Sep 17 00:00:00 2001 From: YevheniiProkopalo Date: Mon, 17 Jan 2022 10:33:46 +0100 Subject: [PATCH 1/4] next week, next month --- timefhuman/categorize.py | 9 ++++++++- timefhuman/main.py | 4 ++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/timefhuman/categorize.py b/timefhuman/categorize.py index e63349e..ae9f8e7 100644 --- a/timefhuman/categorize.py +++ b/timefhuman/categorize.py @@ -111,12 +111,19 @@ def convert_relative_days_ranges(tokens, now=datetime.datetime.now()): # TODO: add support for 'next weekday' (not daterange, conditioinal lookahead) (saturday,) = convert_day_of_week(['upcoming', 'Saturday'], now) (monday,) = convert_day_of_week(['upcoming', 'Monday'], now) + month_= (now.replace(day=28)+datetime.timedelta(days=4)).replace(day=1) + month_end_= (((now.replace(day=28)+datetime.timedelta(days=4)).replace(day=28)+datetime.timedelta(days=4)).replace(day=1)-datetime.timedelta(days=1)) + month=DayToken(month_.month,month_.day,month_.year) + month_end=DayToken(month_end_.month,month_end_.day,month_end_.year) + for keywords, replacement in ( (("today",), DayToken.from_datetime(now)), (("tomorrow", "tmw"), DayToken.from_datetime(now + datetime.timedelta(1))), (("yesterday",), DayToken.from_datetime(now - datetime.timedelta(1))), (("weekend",), DayRange(saturday, saturday + 1)), - (("weekdays",), DayRange(monday, monday + 4))): + (("weekdays",), DayRange(monday, monday + 4)), + (("week",), DayRange(monday, monday + 6)), + (("month",), DayRange(month, month_end))): for keyword in keywords: tokens = [replacement if token == keyword else token \ for token in tokens] diff --git a/timefhuman/main.py b/timefhuman/main.py index 7733f5d..166a6a8 100644 --- a/timefhuman/main.py +++ b/timefhuman/main.py @@ -56,6 +56,7 @@ def timefhuman(string, now=None, raw=None): now = datetime.datetime.now() tokens = timefhuman_tokens(string, now) + #print(isinstance(tokens[1], Token)) if raw: return tokens @@ -72,6 +73,9 @@ def timefhuman(string, now=None, raw=None): def timefhuman_tokens(string, now): """Convert string into timefhuman parsed, imputed, combined tokens""" tokens = tokenize(string) + print((tokens)) tokens = categorize(tokens, now) tokens = build_tree(tokens, now) return tokens +if __name__ == '__main__': + print(timefhuman('next month')) \ No newline at end of file From 993bd9fb6730cb6a8c56b09c6986fd5c1d1aa358 Mon Sep 17 00:00:00 2001 From: YevheniiProkopalo Date: Mon, 17 Jan 2022 10:35:51 +0100 Subject: [PATCH 2/4] next week, next month --- timefhuman/main.py | 1 - 1 file changed, 1 deletion(-) diff --git a/timefhuman/main.py b/timefhuman/main.py index 166a6a8..5689258 100644 --- a/timefhuman/main.py +++ b/timefhuman/main.py @@ -73,7 +73,6 @@ def timefhuman(string, now=None, raw=None): def timefhuman_tokens(string, now): """Convert string into timefhuman parsed, imputed, combined tokens""" tokens = tokenize(string) - print((tokens)) tokens = categorize(tokens, now) tokens = build_tree(tokens, now) return tokens From 407f2b1843f569b49a18fb4b67558d8594647de2 Mon Sep 17 00:00:00 2001 From: YevheniiProkopalo Date: Thu, 24 Mar 2022 10:46:30 +0100 Subject: [PATCH 3/4] token __add__ fix --- test.py | 2 +- timefhuman/data.py | 9 ++++++++- timefhuman/main.py | 1 + 3 files changed, 10 insertions(+), 2 deletions(-) diff --git a/test.py b/test.py index 4935d82..2c1541b 100644 --- a/test.py +++ b/test.py @@ -1,3 +1,3 @@ from timefhuman import timefhuman -print(timefhuman('october 27th')) +print(timefhuman('March 30th')) diff --git a/timefhuman/data.py b/timefhuman/data.py index 065cfd7..7e8749c 100644 --- a/timefhuman/data.py +++ b/timefhuman/data.py @@ -1,5 +1,6 @@ import datetime +monthlengths={1:31,2:28,3:31,4:30,5:31,6:30,7:31,8:31,9:30,10:31,11:30,12:31} class Token: @@ -178,7 +179,13 @@ def __add__(self, other): 3/5 """ assert isinstance(other, int) - return DayToken(self.month, self.day + other, self.year) + if self.day+other<=monthlengths[self.month]: + return DayToken(self.month, self.day + other, self.year) + elif self.month!=12: + return DayToken(self.month + 1, self.day + other - monthlengths[self.month], self.year) + else: + return DayToken(1, self.day + other - monthlengths[self.month], self.year+1) + def __radd__(self, other): """ diff --git a/timefhuman/main.py b/timefhuman/main.py index 5689258..c78c7da 100644 --- a/timefhuman/main.py +++ b/timefhuman/main.py @@ -73,6 +73,7 @@ def timefhuman(string, now=None, raw=None): def timefhuman_tokens(string, now): """Convert string into timefhuman parsed, imputed, combined tokens""" tokens = tokenize(string) + print(tokens) tokens = categorize(tokens, now) tokens = build_tree(tokens, now) return tokens From a7052c090a31b9fb2cec95f63df15f9c33885b04 Mon Sep 17 00:00:00 2001 From: YevheniiProkopalo Date: Thu, 24 Mar 2022 10:47:06 +0100 Subject: [PATCH 4/4] token __add__ fix --- timefhuman/main.py | 1 - 1 file changed, 1 deletion(-) diff --git a/timefhuman/main.py b/timefhuman/main.py index c78c7da..5689258 100644 --- a/timefhuman/main.py +++ b/timefhuman/main.py @@ -73,7 +73,6 @@ def timefhuman(string, now=None, raw=None): def timefhuman_tokens(string, now): """Convert string into timefhuman parsed, imputed, combined tokens""" tokens = tokenize(string) - print(tokens) tokens = categorize(tokens, now) tokens = build_tree(tokens, now) return tokens