Skip to content

Commit

Permalink
CDK: improve day_delta macro and MinMaxDatetime component (#22400)
Browse files Browse the repository at this point in the history
Signed-off-by: Sergey Chvalyuk <[email protected]>
  • Loading branch information
grubberr authored Feb 8, 2023
1 parent 4db6229 commit 730225f
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 6 deletions.
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))
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({})

0 comments on commit 730225f

Please sign in to comment.