Skip to content

Commit 7a609ad

Browse files
fix: add special handling for %s format in ab_datetime_parse
Co-Authored-By: Aaron <AJ> Steers <[email protected]>
1 parent 64f9662 commit 7a609ad

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

airbyte_cdk/utils/datetime_helpers.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -407,10 +407,32 @@ def ab_datetime_parse(
407407
'2023-03-14T15:09:26+00:00'
408408
"""
409409
try:
410+
if formats:
411+
for format_str in formats:
412+
try:
413+
if format_str == "%s":
414+
if isinstance(dt_str, int) or (isinstance(dt_str, str) and dt_str.isdigit()):
415+
timestamp = int(dt_str)
416+
if timestamp < 0:
417+
raise ValueError("Timestamp cannot be negative")
418+
return AirbyteDateTime.from_datetime(datetime.fromtimestamp(timestamp, tz=timezone.utc))
419+
parsed = datetime.strptime(dt_str, format_str)
420+
if parsed.tzinfo is None:
421+
parsed = parsed.replace(tzinfo=timezone.utc)
422+
return AirbyteDateTime.from_datetime(parsed)
423+
except ValueError:
424+
continue
425+
426+
if disallow_other_formats:
427+
raise ValueError(
428+
f"Could not parse datetime string '{dt_str}' with any of the provided formats: {formats}"
429+
)
430+
410431
# Handle numeric values as Unix timestamps (UTC)
411432
if isinstance(dt_str, int) or (
412433
isinstance(dt_str, str)
413434
and (dt_str.isdigit() or (dt_str.startswith("-") and dt_str[1:].isdigit()))
435+
and not formats # Skip timestamp handling if formats were provided but failed
414436
):
415437
timestamp = int(dt_str)
416438
if timestamp < 0:
@@ -428,6 +450,12 @@ def ab_datetime_parse(
428450
if formats:
429451
for format_str in formats:
430452
try:
453+
if format_str == "%s":
454+
if isinstance(dt_str, int) or (isinstance(dt_str, str) and dt_str.isdigit()):
455+
timestamp = int(dt_str)
456+
if timestamp < 0:
457+
raise ValueError("Timestamp cannot be negative")
458+
return AirbyteDateTime.from_datetime(datetime.fromtimestamp(timestamp, tz=timezone.utc))
431459
parsed = datetime.strptime(dt_str, format_str)
432460
if parsed.tzinfo is None:
433461
parsed = parsed.replace(tzinfo=timezone.utc)

0 commit comments

Comments
 (0)