Skip to content
Merged
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
28 changes: 19 additions & 9 deletions pystreamapi/conditions/date.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,9 +165,11 @@ def next_week():
A condition that checks if a datetime is next week.
:return: A condition that checks if a datetime is next week.
"""
return lambda d: __datetime.now().date().isocalendar()[1] + 1 == d.date().isocalendar()[1] if \
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 __datetime.now().date().isocalendar()[1] + 1 == d.isocalendar()[1]
else week == d.isocalendar()[1]


def next_week_utc():
Expand All @@ -176,9 +178,10 @@ def next_week_utc():
parenthesis in your Stream).
:return: A condition that checks if a datetime is next 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]
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]


def this_month():
Expand Down Expand Up @@ -232,16 +235,18 @@ def next_month_utc():
return lambda d: __check_is_month(d, 1, tz=__timezone.utc)


def __check_is_month(d: __datetime, offset: int = 0, tz: __timezone = None):
def __check_is_month(d: Union[__datetime, __date], offset: int = 0, tz: __timezone = None):
"""
The actual function that checks if a datetime is a specific month also supporting
UTC timezone and offsets.
:param d: The datetime to check against.
:param offset: The offset to check against.
:param tz: The timezone to check against.
"""
return __datetime.now(tz).date().month + offset == d.astimezone(tz).date().month if \
isinstance(d, __datetime) else __datetime.now(tz).date().month + offset == d.month
month = __datetime.now(tz).date().month + offset
month = reduce_to_valid_range(month, 12)
return month == d.astimezone(tz).date().month if \
isinstance(d, __datetime) else month == d.month


def this_year():
Expand Down Expand Up @@ -294,7 +299,7 @@ def next_year_utc():
return lambda d: __check_is_year(d, 1, tz=__timezone.utc)


def __check_is_year(d: __datetime, offset: int = 0, tz: __timezone = None):
def __check_is_year(d: Union[__datetime, __date], offset: int = 0, tz: __timezone = None):
"""
A condition that checks if a datetime is a specific year also supporting
UTC timezone and offsets.
Expand All @@ -303,3 +308,8 @@ def __check_is_year(d: __datetime, offset: int = 0, tz: __timezone = None):
"""
return __datetime.now(tz).date().year + offset == d.astimezone(tz).date().year if \
isinstance(d, __datetime) else __datetime.now(tz).date().year + offset == d.year


def reduce_to_valid_range(value, range_val) -> int:
result = value - (range_val * (value // range_val))
return range_val if result == 0 else result
1 change: 1 addition & 0 deletions tests/date_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from datetime import datetime, timedelta, timezone
from unittest import TestCase


# skipcq: PTC-W0046
class DateTest(TestCase):

Expand Down