Skip to content

Commit

Permalink
Merge pull request #88 from PickwickSoft/bugfix/fix-date-edge-case
Browse files Browse the repository at this point in the history
🐛 Fix edge case scenarios in date conditions
  • Loading branch information
garlontas authored Dec 30, 2023
2 parents ddc3566 + cad76c5 commit 30dc953
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 25 deletions.
40 changes: 16 additions & 24 deletions pystreamapi/conditions/date.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,7 @@ def this_week():
A condition that checks if a datetime is this week.
:return: A condition that checks if a datetime is this week.
"""
return lambda d: __datetime.now().date().isocalendar()[1] == d.date().isocalendar()[1] \
if isinstance(d, __datetime) \
else __datetime.now().date().isocalendar()[1] == d.isocalendar()[1]
return __check_is_week


def this_week_utc():
Expand All @@ -134,19 +132,15 @@ def this_week_utc():
parenthesis in your Stream).
:return: A condition that checks if a datetime is this week.
"""
return lambda d: __datetime.now(__timezone.utc).date().isocalendar()[1] == \
d.astimezone(__timezone.utc).date().isocalendar()[1] if isinstance(d, __datetime) else \
__datetime.now(__timezone.utc).date().isocalendar()[1] == d.isocalendar()[1]
return lambda d: __check_is_week(d, tz=__timezone.utc)


def last_week():
"""
A condition that checks if a datetime is last week.
:return: A condition that checks if a datetime is last week.
"""
return lambda d: __datetime.now().date().isocalendar()[1] - 1 == d.date().isocalendar()[1] if \
isinstance(d, __datetime) \
else __datetime.now().date().isocalendar()[1] - 1 == d.isocalendar()[1]
return lambda d: __check_is_week(d, -1)


def last_week_utc():
Expand All @@ -155,21 +149,15 @@ def last_week_utc():
parenthesis in your Stream).
:return: A condition that checks if a datetime is last week.
"""
return lambda d: __datetime.now(__timezone.utc).date().isocalendar()[1] - 1 == \
d.astimezone(__timezone.utc).date().isocalendar()[1] if isinstance(d, __datetime) else \
__datetime.now(__timezone.utc).date().isocalendar()[1] - 1 == d.isocalendar()[1]
return lambda d: __check_is_week(d, -1, tz=__timezone.utc)


def next_week():
"""
A condition that checks if a datetime is next week.
:return: A condition that checks if a datetime is next week.
"""
week = __datetime.now().date().isocalendar()[1] + 1
week = reduce_to_valid_range(week, 52)
return lambda d: week == d.date().isocalendar()[1] if \
isinstance(d, __datetime) \
else week == d.isocalendar()[1]
return lambda d: __check_is_week(d, 1)


def next_week_utc():
Expand All @@ -178,10 +166,14 @@ def next_week_utc():
parenthesis in your Stream).
:return: A condition that checks if a datetime is next week.
"""
week = __datetime.now(__timezone.utc).date().isocalendar()[1] + 1
week = reduce_to_valid_range(week, 52)
return lambda d: week == d.astimezone(__timezone.utc).date().isocalendar()[1] \
if isinstance(d, __datetime) else week == d.isocalendar()[1]
return lambda d: __check_is_week(d, 1, tz=__timezone.utc)


def __check_is_week(d: Union[__datetime, __date], offset: int = 0, tz: __timezone = None):
target_week = __datetime.now(tz=tz) + __timedelta(weeks=offset)
return target_week.isocalendar()[1] == d.date().isocalendar()[1] if \
isinstance(d, __datetime) \
else target_week.isocalendar()[1] == d.isocalendar()[1]


def this_month():
Expand Down Expand Up @@ -267,8 +259,8 @@ def this_year_utc():

def last_year():
"""
A condition that checks if a datetime is last year.
:return: A condition that checks if a datetime is last year.
A condition that checks if a datetime is from last year.
:return: A condition that checks if a datetime is from last year.
"""
return lambda d: __check_is_year(d, -1)

Expand All @@ -277,7 +269,7 @@ def last_year_utc():
"""
A condition that checks if a datetime is last year calculating in UTC (use without
parenthesis in your Stream).
:return: A condition that checks if a datetime is last year.
:return: A condition that checks if a datetime is from last year.
"""
return lambda d: __check_is_year(d, -1, tz=__timezone.utc)

Expand Down
1 change: 0 additions & 1 deletion tests/_conditions/test_date_conditions.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
# pylint: disable=wildcard-import,too-many-instance-attributes,unused-wildcard-import

from _conditions.date_test import DateTest
from pystreamapi.conditions import equal_to, not_equal_to, between, not_between
from pystreamapi.conditions.date import *
Expand Down

0 comments on commit 30dc953

Please sign in to comment.