Skip to content

Commit 499e7ce

Browse files
updated format_datetime macros to format timestamps
1 parent d7516ec commit 499e7ce

File tree

2 files changed

+15
-4
lines changed

2 files changed

+15
-4
lines changed

airbyte_cdk/sources/declarative/interpolation/macros.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ def duration(datestring: str) -> Union[datetime.timedelta, isodate.Duration]:
156156

157157

158158
def format_datetime(
159-
dt: Union[str, datetime.datetime], format: str, input_format: Optional[str] = None
159+
dt: Union[str, datetime.datetime, int], format: str, input_format: Optional[str] = None
160160
) -> str:
161161
"""
162162
Converts datetime to another format
@@ -170,9 +170,13 @@ def format_datetime(
170170
"""
171171
if isinstance(dt, datetime.datetime):
172172
return dt.strftime(format)
173-
dt_datetime = (
174-
datetime.datetime.strptime(dt, input_format) if input_format else str_to_datetime(dt)
175-
)
173+
174+
if isinstance(dt, int):
175+
dt_datetime = datetime.datetime.fromtimestamp(dt, tz=datetime.timezone.utc)
176+
else:
177+
dt_datetime = (
178+
datetime.datetime.strptime(dt, input_format) if input_format else str_to_datetime(dt)
179+
)
176180
return DatetimeParser().format(dt=dt_datetime, format=format)
177181

178182

unit_tests/sources/declarative/interpolation/test_macros.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,12 @@ def test_macros_export(test_name, fn_name, found_in_macros):
8383
"%Y-%m-%dT%H:%M:%SZ",
8484
"1640998861000000",
8585
),
86+
(
87+
1683729087,
88+
"%Y-%m-%dT%H:%M:%SZ",
89+
None,
90+
"2023-05-10T14:31:27Z",
91+
),
8692
],
8793
ids=[
8894
"test_datetime_string_to_date",
@@ -96,6 +102,7 @@ def test_macros_export(test_name, fn_name, found_in_macros):
96102
"test_datetime_string_to_rfc2822_date",
97103
"test_datetime_string_to_timestamp_in_seconds",
98104
"test_datetime_string_to_timestamp_in_microseconds",
105+
"test_timestamp_to_format_string",
99106
],
100107
)
101108
def test_format_datetime(input_value, format, input_format, expected_output):

0 commit comments

Comments
 (0)