Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
12 changes: 8 additions & 4 deletions airbyte_cdk/sources/declarative/interpolation/macros.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ def duration(datestring: str) -> Union[datetime.timedelta, isodate.Duration]:


def format_datetime(
dt: Union[str, datetime.datetime], format: str, input_format: Optional[str] = None
dt: Union[str, datetime.datetime, int], format: str, input_format: Optional[str] = None
) -> str:
"""
Converts datetime to another format
Expand All @@ -170,9 +170,13 @@ def format_datetime(
"""
if isinstance(dt, datetime.datetime):
return dt.strftime(format)
dt_datetime = (
datetime.datetime.strptime(dt, input_format) if input_format else str_to_datetime(dt)
)

if isinstance(dt, int):
dt_datetime = datetime.datetime.fromtimestamp(dt, tz=datetime.timezone.utc)
Comment thread
darynaishchenko marked this conversation as resolved.
Outdated
else:
dt_datetime = (
datetime.datetime.strptime(dt, input_format) if input_format else str_to_datetime(dt)
)
return DatetimeParser().format(dt=dt_datetime, format=format)


Expand Down
7 changes: 7 additions & 0 deletions unit_tests/sources/declarative/interpolation/test_macros.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,12 @@ def test_macros_export(test_name, fn_name, found_in_macros):
"%Y-%m-%dT%H:%M:%SZ",
"1640998861000000",
),
(
1683729087,
"%Y-%m-%dT%H:%M:%SZ",
None,
"2023-05-10T14:31:27Z",
),
],
ids=[
"test_datetime_string_to_date",
Expand All @@ -96,6 +102,7 @@ def test_macros_export(test_name, fn_name, found_in_macros):
"test_datetime_string_to_rfc2822_date",
"test_datetime_string_to_timestamp_in_seconds",
"test_datetime_string_to_timestamp_in_microseconds",
"test_timestamp_to_format_string",
],
)
def test_format_datetime(input_value, format, input_format, expected_output):
Expand Down