1
0
mirror of synced 2025-12-25 02:09:19 -05:00

CDK: Add support for input format parsing at jinja macro format_datetime (#40759)

Co-authored-by: cristina.mariscal <cristina.mariscal@cristina.mariscal--MacBook-Pro---DFJ27FJFXX>
This commit is contained in:
Cristina Mariscal
2024-07-08 02:42:09 -06:00
committed by GitHub
parent 3bc5b512af
commit c4b8212ba7
3 changed files with 18 additions and 15 deletions

View File

@@ -2565,11 +2565,13 @@ interpolation:
description: Converts a datetime or a datetime-string to the specified format.
arguments:
datetime: The datetime object or a string to convert. If datetime is a string, it must be formatted as ISO8601.
format: The datetime format
format: The datetime format.
input_format: (optional) The datetime format in the case it is an string.
return_type: str
examples:
- "{{ format_datetime(config['start_time'], '%Y-%m-%d') }}"
- "{{ format_datetime(config['start_date'], '%Y-%m-%dT%H:%M:%S.%fZ') }}"
- "{{ format_datetime(config['start_date'], '%Y-%m-%dT%H:%M:%S.%fZ', '%a, %d %b %Y %H:%M:%S %z') }}"
filters:
- title: Hash
description: Convert the specified value to a hashed string.

View File

@@ -5,7 +5,7 @@
import builtins
import datetime
import typing
from typing import Union
from typing import Optional, Union
import isodate
import pytz
@@ -107,7 +107,7 @@ def duration(datestring: str) -> Union[datetime.timedelta, isodate.Duration]:
return parse_duration(datestring) # type: ignore # mypy thinks this returns Any for some reason
def format_datetime(dt: Union[str, datetime.datetime], format: str) -> str:
def format_datetime(dt: Union[str, datetime.datetime], format: str, input_format: Optional[str] = None) -> str:
"""
Converts datetime to another format
@@ -120,7 +120,7 @@ def format_datetime(dt: Union[str, datetime.datetime], format: str) -> str:
"""
if isinstance(dt, datetime.datetime):
return dt.strftime(format)
dt_datetime = _str_to_datetime(dt)
dt_datetime = datetime.datetime.strptime(dt, input_format) if input_format else _str_to_datetime(dt)
if format == "%s":
return str(int(dt_datetime.timestamp()))
return dt_datetime.strftime(format)

View File

@@ -28,21 +28,22 @@ def test_macros_export(test_name, fn_name, found_in_macros):
@pytest.mark.parametrize(
"test_name, input_value, format, expected_output",
"test_name, input_value, format, input_format, expected_output",
[
("test_datetime_string_to_date", "2022-01-01T01:01:01Z", "%Y-%m-%d", "2022-01-01"),
("test_date_string_to_date", "2022-01-01", "%Y-%m-%d", "2022-01-01"),
("test_datetime_string_to_date", "2022-01-01T00:00:00Z", "%Y-%m-%d", "2022-01-01"),
("test_datetime_with_tz_string_to_date", "2022-01-01T00:00:00Z", "%Y-%m-%d", "2022-01-01"),
("test_datetime_string_to_datetime", "2022-01-01T01:01:01Z", "%Y-%m-%dT%H:%M:%SZ", "2022-01-01T01:01:01Z"),
("test_datetime_string_with_tz_to_datetime", "2022-01-01T01:01:01-0800", "%Y-%m-%dT%H:%M:%SZ", "2022-01-01T09:01:01Z"),
("test_datetime_object_tz_to_date", datetime.datetime(2022, 1, 1, 1, 1, 1), "%Y-%m-%d", "2022-01-01"),
("test_datetime_object_tz_to_datetime", datetime.datetime(2022, 1, 1, 1, 1, 1), "%Y-%m-%dT%H:%M:%SZ", "2022-01-01T01:01:01Z"),
("test_datetime_string_to_date", "2022-01-01T01:01:01Z", "%Y-%m-%d", None, "2022-01-01"),
("test_date_string_to_date", "2022-01-01", "%Y-%m-%d", None, "2022-01-01"),
("test_datetime_string_to_date", "2022-01-01T00:00:00Z", "%Y-%m-%d", None, "2022-01-01"),
("test_datetime_with_tz_string_to_date", "2022-01-01T00:00:00Z", "%Y-%m-%d", None, "2022-01-01"),
("test_datetime_string_to_datetime", "2022-01-01T01:01:01Z", "%Y-%m-%dT%H:%M:%SZ", None, "2022-01-01T01:01:01Z"),
("test_datetime_string_with_tz_to_datetime", "2022-01-01T01:01:01-0800", "%Y-%m-%dT%H:%M:%SZ", None, "2022-01-01T09:01:01Z"),
("test_datetime_object_tz_to_date", datetime.datetime(2022, 1, 1, 1, 1, 1), "%Y-%m-%d", None, "2022-01-01"),
("test_datetime_object_tz_to_datetime", datetime.datetime(2022, 1, 1, 1, 1, 1), "%Y-%m-%dT%H:%M:%SZ", None, "2022-01-01T01:01:01Z"),
("test_datetime_string_to_rfc2822_date", "Sat, 01 Jan 2022 01:01:01 +0000", "%Y-%m-%d", "%a, %d %b %Y %H:%M:%S %z", "2022-01-01"),
],
)
def test_format_datetime(test_name, input_value, format, expected_output):
def test_format_datetime(test_name, input_value, format, input_format, expected_output):
format_datetime = macros["format_datetime"]
assert format_datetime(input_value, format) == expected_output
assert format_datetime(input_value, format, input_format) == expected_output
@pytest.mark.parametrize(