@@ -123,9 +123,7 @@ def this_week():
123123 A condition that checks if a datetime is this week.
124124 :return: A condition that checks if a datetime is this week.
125125 """
126- return lambda d : __datetime .now ().date ().isocalendar ()[1 ] == d .date ().isocalendar ()[1 ] \
127- if isinstance (d , __datetime ) \
128- else __datetime .now ().date ().isocalendar ()[1 ] == d .isocalendar ()[1 ]
126+ return __check_is_week
129127
130128
131129def this_week_utc ():
@@ -134,19 +132,15 @@ def this_week_utc():
134132 parenthesis in your Stream).
135133 :return: A condition that checks if a datetime is this week.
136134 """
137- return lambda d : __datetime .now (__timezone .utc ).date ().isocalendar ()[1 ] == \
138- d .astimezone (__timezone .utc ).date ().isocalendar ()[1 ] if isinstance (d , __datetime ) else \
139- __datetime .now (__timezone .utc ).date ().isocalendar ()[1 ] == d .isocalendar ()[1 ]
135+ return lambda d : __check_is_week (d , tz = __timezone .utc )
140136
141137
142138def last_week ():
143139 """
144140 A condition that checks if a datetime is last week.
145141 :return: A condition that checks if a datetime is last week.
146142 """
147- return lambda d : __datetime .now ().date ().isocalendar ()[1 ] - 1 == d .date ().isocalendar ()[1 ] if \
148- isinstance (d , __datetime ) \
149- else __datetime .now ().date ().isocalendar ()[1 ] - 1 == d .isocalendar ()[1 ]
143+ return lambda d : __check_is_week (d , - 1 )
150144
151145
152146def last_week_utc ():
@@ -155,21 +149,15 @@ def last_week_utc():
155149 parenthesis in your Stream).
156150 :return: A condition that checks if a datetime is last week.
157151 """
158- return lambda d : __datetime .now (__timezone .utc ).date ().isocalendar ()[1 ] - 1 == \
159- d .astimezone (__timezone .utc ).date ().isocalendar ()[1 ] if isinstance (d , __datetime ) else \
160- __datetime .now (__timezone .utc ).date ().isocalendar ()[1 ] - 1 == d .isocalendar ()[1 ]
152+ return lambda d : __check_is_week (d , - 1 , tz = __timezone .utc )
161153
162154
163155def next_week ():
164156 """
165157 A condition that checks if a datetime is next week.
166158 :return: A condition that checks if a datetime is next week.
167159 """
168- week = __datetime .now ().date ().isocalendar ()[1 ] + 1
169- week = reduce_to_valid_range (week , 52 )
170- return lambda d : week == d .date ().isocalendar ()[1 ] if \
171- isinstance (d , __datetime ) \
172- else week == d .isocalendar ()[1 ]
160+ return lambda d : __check_is_week (d , 1 )
173161
174162
175163def next_week_utc ():
@@ -178,10 +166,14 @@ def next_week_utc():
178166 parenthesis in your Stream).
179167 :return: A condition that checks if a datetime is next week.
180168 """
181- week = __datetime .now (__timezone .utc ).date ().isocalendar ()[1 ] + 1
182- week = reduce_to_valid_range (week , 52 )
183- return lambda d : week == d .astimezone (__timezone .utc ).date ().isocalendar ()[1 ] \
184- if isinstance (d , __datetime ) else week == d .isocalendar ()[1 ]
169+ return lambda d : __check_is_week (d , 1 , tz = __timezone .utc )
170+
171+
172+ def __check_is_week (d : Union [__datetime , __date ], offset : int = 0 , tz : __timezone = None ):
173+ target_week = __datetime .now (tz = tz ) + __timedelta (weeks = offset )
174+ return target_week .isocalendar ()[1 ] == d .date ().isocalendar ()[1 ] if \
175+ isinstance (d , __datetime ) \
176+ else target_week .isocalendar ()[1 ] == d .isocalendar ()[1 ]
185177
186178
187179def this_month ():
@@ -267,8 +259,8 @@ def this_year_utc():
267259
268260def last_year ():
269261 """
270- A condition that checks if a datetime is last year.
271- :return: A condition that checks if a datetime is last year.
262+ A condition that checks if a datetime is from last year.
263+ :return: A condition that checks if a datetime is from last year.
272264 """
273265 return lambda d : __check_is_year (d , - 1 )
274266
@@ -277,7 +269,7 @@ def last_year_utc():
277269 """
278270 A condition that checks if a datetime is last year calculating in UTC (use without
279271 parenthesis in your Stream).
280- :return: A condition that checks if a datetime is last year.
272+ :return: A condition that checks if a datetime is from last year.
281273 """
282274 return lambda d : __check_is_year (d , - 1 , tz = __timezone .utc )
283275
0 commit comments