Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

CDK: improve day_delta macro and MinMaxDatetime component #22400

Merged
merged 5 commits into from
Feb 8, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,15 @@ def get_datetime(self, config, **additional_options) -> dt.datetime:
time = self._parser.parse(str(self.datetime.eval(config, **additional_options)), datetime_format, self.timezone)

if self.min_datetime:
min_time = self._parser.parse(str(self.min_datetime.eval(config, **additional_options)), datetime_format, self.timezone)
time = max(time, min_time)
min_time = str(self.min_datetime.eval(config, **additional_options))
grubberr marked this conversation as resolved.
Show resolved Hide resolved
if min_time:
min_time = self._parser.parse(min_time, datetime_format, self.timezone)
time = max(time, min_time)
if self.max_datetime:
max_time = self._parser.parse(str(self.max_datetime.eval(config, **additional_options)), datetime_format, self.timezone)
time = min(time, max_time)
max_time = str(self.max_datetime.eval(config, **additional_options))
if max_time:
max_time = self._parser.parse(max_time, datetime_format, self.timezone)
time = min(time, max_time)
return time

@property
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ def max(*args):
return builtins.max(*args)


def day_delta(num_days: int) -> str:
def day_delta(num_days: int, format: str = "%Y-%m-%dT%H:%M:%S.%f%z") -> str:
"""
Returns datetime of now() + num_days

Expand All @@ -93,7 +93,7 @@ def day_delta(num_days: int) -> str:
:param num_days: number of days to add to current date time
:return: datetime formatted as RFC3339
"""
return (datetime.datetime.now(datetime.timezone.utc) + datetime.timedelta(days=num_days)).strftime("%Y-%m-%dT%H:%M:%S.%f%z")
return (datetime.datetime.now(datetime.timezone.utc) + datetime.timedelta(days=num_days)).strftime(format)


def format_datetime(dt: Union[str, datetime.datetime], format: str):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,3 +94,16 @@ def test_set_datetime_format():
actual_date = min_max_date.get_datetime(custom_fmt_config)

assert actual_date == datetime.datetime.strptime("2022-01-01T20:12:19", "%Y-%m-%dT%H:%M:%S").replace(tzinfo=datetime.timezone.utc)


def test_min_max_datetime_lazy_eval():
kwargs = {
"datetime": "2022-01-10T00:00:00",
"datetime_format": "%Y-%m-%dT%H:%M:%S",
"min_datetime": "{{ options.min_datetime }}",
"max_datetime": "{{ options.max_datetime }}",
}

assert datetime.datetime(2022, 1, 10, 0, 0, tzinfo=datetime.timezone.utc) == MinMaxDatetime(**kwargs, options={}).get_datetime({})
assert datetime.datetime(2022, 1, 20, 0, 0, tzinfo=datetime.timezone.utc) == MinMaxDatetime(**kwargs, options={"min_datetime": "2022-01-20T00:00:00"}).get_datetime({})
assert datetime.datetime(2021, 1, 1, 0, 0, tzinfo=datetime.timezone.utc) == MinMaxDatetime(**kwargs, options={"max_datetime": "2021-01-01T00:00:00"}).get_datetime({})