Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Test #28

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open

Test #28

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion test.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
from timefhuman import timefhuman
print(timefhuman('october 27th'))
print(timefhuman('March 30th'))

9 changes: 8 additions & 1 deletion timefhuman/categorize.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
9 changes: 8 additions & 1 deletion timefhuman/data.py
Original file line number Diff line number Diff line change
@@ -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:

Expand Down Expand Up @@ -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):
"""
Expand Down
3 changes: 3 additions & 0 deletions timefhuman/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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'))