Skip to content

feat: (low-code cdk) datetime format with milliseconds #369

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

Merged
merged 2 commits into from
Feb 27, 2025
Merged
Show file tree
Hide file tree
Changes from all 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
8 changes: 7 additions & 1 deletion airbyte_cdk/sources/declarative/datetime/datetime_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ def parse(self, date: Union[str, int], format: str) -> datetime.datetime:
return datetime.datetime.fromtimestamp(float(date), tz=datetime.timezone.utc)
elif format == "%ms":
return self._UNIX_EPOCH + datetime.timedelta(milliseconds=int(date))

elif "%_ms" in format:
format = format.replace("%_ms", "%f")
parsed_datetime = datetime.datetime.strptime(str(date), format)
if self._is_naive(parsed_datetime):
return parsed_datetime.replace(tzinfo=datetime.timezone.utc)
Expand All @@ -48,6 +49,11 @@ def format(self, dt: datetime.datetime, format: str) -> str:
if format == "%ms":
# timstamp() returns a float representing the number of seconds since the unix epoch
return str(int(dt.timestamp() * 1000))
if "%_ms" in format:
_format = format.replace("%_ms", "%f")
milliseconds = int(dt.microsecond / 1000)
formatted_dt = dt.strftime(_format).replace(dt.strftime("%f"), "%03d" % milliseconds)
return formatted_dt
else:
return dt.strftime(format)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -844,6 +844,7 @@ definitions:
* **%M**: Minute (zero-padded) - `00`, `01`, ..., `59`
* **%S**: Second (zero-padded) - `00`, `01`, ..., `59`
* **%f**: Microsecond (zero-padded to 6 digits) - `000000`
* **%_ms**: Millisecond (zero-padded to 3 digits) - `000`
* **%z**: UTC offset - `(empty)`, `+0000`, `-04:00`
* **%Z**: Time zone name - `(empty)`, `UTC`, `GMT`
* **%j**: Day of the year (zero-padded) - `001`, `002`, ..., `366`
Expand Down Expand Up @@ -2401,6 +2402,7 @@ definitions:
* **%M**: Minute (zero-padded) - `00`, `01`, ..., `59`
* **%S**: Second (zero-padded) - `00`, `01`, ..., `59`
* **%f**: Microsecond (zero-padded to 6 digits) - `000000`, `000001`, ..., `999999`
* **%_ms**: Millisecond (zero-padded to 3 digits) - `000`, `001`, ..., `999`
* **%z**: UTC offset - `(empty)`, `+0000`, `-04:00`
* **%Z**: Time zone name - `(empty)`, `UTC`, `GMT`
* **%j**: Day of the year (zero-padded) - `001`, `002`, ..., `366`
Expand Down
12 changes: 12 additions & 0 deletions unit_tests/sources/declarative/datetime/test_datetime_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@
"%Y%m%d",
datetime.datetime(2021, 1, 1, 0, 0, tzinfo=datetime.timezone.utc),
),
(
"test_parse_format_datetime_with__ms",
"2021-11-22T08:41:55.640Z",
"%Y-%m-%dT%H:%M:%S.%_msZ",
datetime.datetime(2021, 11, 22, 8, 41, 55, 640000, tzinfo=datetime.timezone.utc),
),
],
)
def test_parse_date(test_name, input_date, date_format, expected_output_date):
Expand Down Expand Up @@ -91,6 +97,12 @@ def test_parse_date(test_name, input_date, date_format, expected_output_date):
"%Y%m%d",
"20210101",
),
(
"test_parse_format_datetime_with__ms",
datetime.datetime(2021, 11, 22, 8, 41, 55, 640000, tzinfo=datetime.timezone.utc),
"%Y-%m-%dT%H:%M:%S.%_msZ",
"2021-11-22T08:41:55.640Z",
),
],
)
def test_format_datetime(test_name, input_dt, datetimeformat, expected_output):
Expand Down
Loading