Skip to content

Commit

Permalink
feat: add microseconds timestamp format (#373)
Browse files Browse the repository at this point in the history
Signed-off-by: Artem Inzhyyants <artem.inzhyyants@gmail.com>
  • Loading branch information
artem1205 authored Mar 3, 2025
1 parent 651f07f commit 61a9ee3
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 15 deletions.
2 changes: 2 additions & 0 deletions airbyte_cdk/sources/declarative/interpolation/macros.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,8 @@ def format_datetime(
)
if format == "%s":
return str(int(dt_datetime.timestamp()))
elif format == "%ms":
return str(int(dt_datetime.timestamp() * 1_000_000))
return dt_datetime.strftime(format)


Expand Down
53 changes: 38 additions & 15 deletions unit_tests/sources/declarative/interpolation/test_macros.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,68 +30,91 @@ def test_macros_export(test_name, fn_name, found_in_macros):


@pytest.mark.parametrize(
"test_name, input_value, format, input_format, expected_output",
"input_value, format, input_format, expected_output",
[
("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"),
("2022-01-01T01:01:01Z", "%Y-%m-%d", None, "2022-01-01"),
("2022-01-01", "%Y-%m-%d", None, "2022-01-01"),
("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",
),
(
"2022-01-01T01:01:01Z",
"%s",
"%Y-%m-%dT%H:%M:%SZ",
"1640998861",
),
(
"2022-01-01T01:01:01Z",
"%ms",
"%Y-%m-%dT%H:%M:%SZ",
"1640998861000000",
),
],
ids=[
"test_datetime_string_to_date",
"test_date_string_to_date",
"test_datetime_string_to_date",
"test_datetime_with_tz_string_to_date",
"test_datetime_string_to_datetime",
"test_datetime_string_with_tz_to_datetime",
"test_datetime_object_tz_to_date",
"test_datetime_object_tz_to_datetime",
"test_datetime_string_to_rfc2822_date",
"test_datetime_string_to_timestamp_in_seconds",
"test_datetime_string_to_timestamp_in_microseconds",
],
)
def test_format_datetime(test_name, input_value, format, input_format, expected_output):
def test_format_datetime(input_value, format, input_format, expected_output):
format_datetime = macros["format_datetime"]
assert format_datetime(input_value, format, input_format) == expected_output


@pytest.mark.parametrize(
"test_name, input_value, expected_output",
"input_value, expected_output",
[
("test_one_day", "P1D", datetime.timedelta(days=1)),
("test_6_days_23_hours", "P6DT23H", datetime.timedelta(days=6, hours=23)),
("P1D", datetime.timedelta(days=1)),
("P6DT23H", datetime.timedelta(days=6, hours=23)),
],
ids=[
"test_one_day",
"test_6_days_23_hours",
],
)
def test_duration(test_name, input_value, expected_output):
def test_duration(input_value: str, expected_output: datetime.timedelta):
duration_fn = macros["duration"]
assert duration_fn(input_value) == expected_output

Expand Down

0 comments on commit 61a9ee3

Please sign in to comment.