Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add microseconds timestamp format #373

Merged
merged 3 commits into from
Mar 3, 2025
Merged
Show file tree
Hide file tree
Changes from 2 commits
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
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",
"1640995261",
),
(
"2022-01-01T01:01:01Z",
"%ms",
"%Y-%m-%dT%H:%M:%SZ",
"1640995261000000",
),
],
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
Loading