Skip to content

Commit 4b75c98

Browse files
author
Ran Isenberg
committed
fixed all tests, added tests
1 parent c24118c commit 4b75c98

File tree

2 files changed

+21
-6
lines changed

2 files changed

+21
-6
lines changed

aws_lambda_powertools/utilities/feature_flags/feature_flags.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ def _evaluate_conditions(
9191
cond_value = condition.get(schema.CONDITION_VALUE)
9292

9393
# rule based actions have no user context. the context is the condition key
94-
if cond_action == schema.RuleAction.TIME_RANGE.value or schema.RuleAction.TIME_SELECTED_DAYS:
94+
if cond_action in [schema.RuleAction.TIME_RANGE.value, schema.RuleAction.TIME_SELECTED_DAYS.value]:
9595
context_value = condition.get(schema.CONDITION_KEY)
9696

9797
if not self._match_by_action(action=cond_action, condition_value=cond_value, context_value=context_value):

aws_lambda_powertools/utilities/feature_flags/time_conditions.py

+20-5
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,22 @@
66
HOUR_MIN_SEPARATOR = ":"
77

88

9+
DAY_MAPPING = {
10+
1: TimeValues.MONDAY.value,
11+
2: TimeValues.TUESDAY.value,
12+
3: TimeValues.WEDNESDAY.value,
13+
4: TimeValues.THURSDAY.value,
14+
5: TimeValues.FRIDAY.value,
15+
6: TimeValues.SATURDAY.value,
16+
7: TimeValues.SUNDAY.value,
17+
}
18+
19+
920
def time_range_compare(action: str, values: Dict) -> bool:
1021
if action == TimeKeys.CURRENT_TIME_UTC.value:
1122
return _time_range_compare_current_time_utc(values)
1223
elif action == TimeKeys.CURRENT_HOUR_UTC.value:
13-
return _time_range_compare_current_time_utc(values)
24+
return _time_range_compare_current_hour_utc(values)
1425
# we assume it passed validation right? so no need to raise an error
1526
return False
1627

@@ -22,20 +33,24 @@ def time_selected_days_compare(action: str, values: List[str]) -> bool:
2233
return False
2334

2435

36+
def _get_utc_time_now() -> datetime:
37+
return datetime.now(timezone.utc)
38+
39+
2540
def _time_selected_days_current_days_compare(values: List[str]) -> bool:
26-
current_day: datetime = datetime.now(timezone.utc).strftime('%A').lower()
27-
return current_day in values
41+
current_day_number = _get_utc_time_now().isoweekday()
42+
return DAY_MAPPING.get(current_day_number, "") in values
2843

2944

3045
def _time_range_compare_current_time_utc(values: Dict) -> bool:
31-
current_time_utc: datetime = datetime.now(timezone.utc)
46+
current_time_utc: datetime = _get_utc_time_now()
3247
start_date = datetime.strptime(values.get(TimeValues.START_TIME, ""), "%Y-%m-%dT%H:%M:%S%z")
3348
end_date = datetime.strptime(values.get(TimeValues.END_TIME, ""), "%Y-%m-%dT%H:%M:%S%z")
3449
return current_time_utc >= start_date and current_time_utc <= end_date
3550

3651

3752
def _time_range_compare_current_hour_utc(values: Dict) -> bool:
38-
current_time_utc: datetime = datetime.now(timezone.utc)
53+
current_time_utc: datetime = _get_utc_time_now()
3954
start_hour, start_min = values.get(TimeValues.START_TIME, HOUR_MIN_SEPARATOR).split(HOUR_MIN_SEPARATOR)
4055
end_hour, end_min = values.get(TimeValues.END_TIME, HOUR_MIN_SEPARATOR).split(HOUR_MIN_SEPARATOR)
4156
return (

0 commit comments

Comments
 (0)