Skip to content

Commit 978be1b

Browse files
authored
feat: expose str_to_datetime jinja macro (#351)
1 parent e1a182c commit 978be1b

File tree

11 files changed

+84
-13
lines changed

11 files changed

+84
-13
lines changed

airbyte_cdk/sources/declarative/declarative_component_schema.yaml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3744,6 +3744,16 @@ interpolation:
37443744
- "{{ format_datetime(config['start_time'], '%Y-%m-%d') }}"
37453745
- "{{ format_datetime(config['start_date'], '%Y-%m-%dT%H:%M:%S.%fZ') }}"
37463746
- "{{ format_datetime(config['start_date'], '%Y-%m-%dT%H:%M:%S.%fZ', '%a, %d %b %Y %H:%M:%S %z') }}"
3747+
- title: str_to_datetime
3748+
description: Converts a string to a datetime object with UTC timezone.
3749+
arguments:
3750+
s: The string to convert.
3751+
return_type: datetime.datetime
3752+
examples:
3753+
- "{{ str_to_datetime('2022-01-14') }}"
3754+
- "{{ str_to_datetime('2022-01-01 13:45:30') }}"
3755+
- "{{ str_to_datetime('2022-01-01T13:45:30+00:00') }}"
3756+
- "{{ str_to_datetime('2022-01-01T13:45:30.123456Z') }}"
37473757
filters:
37483758
- title: hash
37493759
description: Convert the specified value to a hashed string.

airbyte_cdk/sources/declarative/interpolation/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#
2-
# Copyright (c) 2023 Airbyte, Inc., all rights reserved.
2+
# Copyright (c) 2025 Airbyte, Inc., all rights reserved.
33
#
44

55
from airbyte_cdk.sources.declarative.interpolation.interpolated_boolean import InterpolatedBoolean

airbyte_cdk/sources/declarative/interpolation/filters.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#
2-
# Copyright (c) 2023 Airbyte, Inc., all rights reserved.
2+
# Copyright (c) 2025 Airbyte, Inc., all rights reserved.
33
#
4+
45
import base64
56
import hashlib
67
import json

airbyte_cdk/sources/declarative/interpolation/interpolated_boolean.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#
2-
# Copyright (c) 2023 Airbyte, Inc., all rights reserved.
2+
# Copyright (c) 2025 Airbyte, Inc., all rights reserved.
33
#
44

55
from dataclasses import InitVar, dataclass

airbyte_cdk/sources/declarative/interpolation/interpolated_mapping.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#
2-
# Copyright (c) 2023 Airbyte, Inc., all rights reserved.
2+
# Copyright (c) 2025 Airbyte, Inc., all rights reserved.
33
#
44

55

airbyte_cdk/sources/declarative/interpolation/interpolated_nested_mapping.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#
2-
# Copyright (c) 2023 Airbyte, Inc., all rights reserved.
2+
# Copyright (c) 2025 Airbyte, Inc., all rights reserved.
33
#
44

55

airbyte_cdk/sources/declarative/interpolation/interpolated_string.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#
2-
# Copyright (c) 2023 Airbyte, Inc., all rights reserved.
2+
# Copyright (c) 2025 Airbyte, Inc., all rights reserved.
33
#
44

55
from dataclasses import InitVar, dataclass

airbyte_cdk/sources/declarative/interpolation/interpolation.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
#
2-
# Copyright (c) 2023 Airbyte, Inc., all rights reserved.
2+
# Copyright (c) 2025 Airbyte, Inc., all rights reserved.
33
#
44

5+
56
from abc import ABC, abstractmethod
67
from typing import Any, Optional
78

airbyte_cdk/sources/declarative/interpolation/jinja.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#
2-
# Copyright (c) 2023 Airbyte, Inc., all rights reserved.
2+
# Copyright (c) 2025 Airbyte, Inc., all rights reserved.
33
#
44

55
import ast

airbyte_cdk/sources/declarative/interpolation/macros.py

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#
2-
# Copyright (c) 2023 Airbyte, Inc., all rights reserved.
2+
# Copyright (c) 2025 Airbyte, Inc., all rights reserved.
33
#
44

55
import builtins
@@ -63,10 +63,24 @@ def timestamp(dt: Union[float, str]) -> Union[int, float]:
6363
if isinstance(dt, (int, float)):
6464
return int(dt)
6565
else:
66-
return _str_to_datetime(dt).astimezone(pytz.utc).timestamp()
66+
return str_to_datetime(dt).astimezone(pytz.utc).timestamp()
6767

6868

69-
def _str_to_datetime(s: str) -> datetime.datetime:
69+
def str_to_datetime(s: str) -> datetime.datetime:
70+
"""
71+
Converts a string to a datetime object with UTC timezone
72+
73+
If the input string does not contain timezone information, UTC is assumed.
74+
Supports both basic date strings like "2022-01-14" and datetime strings with optional timezone
75+
like "2022-01-01T13:45:30+00:00".
76+
77+
Usage:
78+
`"{{ str_to_datetime('2022-01-14') }}"`
79+
80+
:param s: string to parse as datetime
81+
:return: datetime object in UTC timezone
82+
"""
83+
7084
parsed_date = parser.isoparse(s)
7185
if not parsed_date.tzinfo:
7286
# Assume UTC if the input does not contain a timezone
@@ -155,7 +169,7 @@ def format_datetime(
155169
if isinstance(dt, datetime.datetime):
156170
return dt.strftime(format)
157171
dt_datetime = (
158-
datetime.datetime.strptime(dt, input_format) if input_format else _str_to_datetime(dt)
172+
datetime.datetime.strptime(dt, input_format) if input_format else str_to_datetime(dt)
159173
)
160174
if format == "%s":
161175
return str(int(dt_datetime.timestamp()))
@@ -172,5 +186,6 @@ def format_datetime(
172186
duration,
173187
format_datetime,
174188
today_with_timezone,
189+
str_to_datetime,
175190
]
176191
macros = {f.__name__: f for f in _macros_list}

0 commit comments

Comments
 (0)