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/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/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 7733f5d..5689258 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 @@ -75,3 +76,5 @@ def timefhuman_tokens(string, now): tokens = categorize(tokens, now) tokens = build_tree(tokens, now) return tokens +if __name__ == '__main__': + print(timefhuman('next month')) \ No newline at end of file