@@ -402,7 +402,25 @@ def ab_datetime_parse(
402402 """
403403 try :
404404 # Remove None values from formats list, and coalesce to None if empty
405- formats = [f for f in formats if f ] or None
405+ formats = [f for f in formats or [] if f ] or None
406+
407+ # Handle numeric values as Unix timestamps (UTC)
408+ if isinstance (dt_str , int ) or (
409+ isinstance (dt_str , str )
410+ and (dt_str .isdigit () or (dt_str .startswith ("-" ) and dt_str [1 :].isdigit ()))
411+ and (
412+ not formats
413+ or ("%s" in formats ) # Custom case for Unix timestamp in declarative sources
414+ )
415+ ):
416+ timestamp = int (dt_str )
417+ if timestamp < 0 :
418+ raise ValueError ("Timestamp cannot be negative" )
419+ if len (str (abs (timestamp ))) > 10 :
420+ raise ValueError ("Timestamp value too large" )
421+
422+ instant = Instant .from_timestamp (timestamp )
423+ return AirbyteDateTime .from_datetime (instant .py_datetime ())
406424
407425 if formats :
408426 for format_str in formats :
@@ -429,20 +447,6 @@ def ab_datetime_parse(
429447 f"Could not parse datetime string '{ dt_str } ' with any of the provided formats: { formats } "
430448 )
431449
432- # Handle numeric values as Unix timestamps (UTC)
433- if isinstance (dt_str , int ) or (
434- isinstance (dt_str , str )
435- and (dt_str .isdigit () or (dt_str .startswith ("-" ) and dt_str [1 :].isdigit ()))
436- and not formats # Skip timestamp handling if formats were provided but failed
437- ):
438- timestamp = int (dt_str )
439- if timestamp < 0 :
440- raise ValueError ("Timestamp cannot be negative" )
441- if len (str (abs (timestamp ))) > 10 :
442- raise ValueError ("Timestamp value too large" )
443- instant = Instant .from_timestamp (timestamp )
444- return AirbyteDateTime .from_datetime (instant .py_datetime ())
445-
446450 if not isinstance (dt_str , str ):
447451 raise ValueError (
448452 f"Could not parse datetime string: expected string or integer, got { type (dt_str )} "
@@ -497,6 +501,7 @@ def ab_datetime_parse(
497501 return AirbyteDateTime .from_datetime (parsed )
498502 except (ValueError , TypeError ):
499503 raise ValueError (f"Could not parse datetime string: { dt_str } " )
504+
500505 except ValueError as e :
501506 if "Invalid date format:" in str (e ):
502507 raise
@@ -529,11 +534,17 @@ def ab_datetime_try_parse(
529534 >>> ab_datetime_try_parse("2023-03-14T15:09:26Z") # Returns AirbyteDateTime
530535 >>> ab_datetime_try_parse("2023-03-14 15:09:26Z") # Missing 'T' delimiter still parsable
531536 >>> ab_datetime_try_parse("2023-03-14") # Returns midnight UTC time
532- >>> ab_datetime_try_parse("2023-03-14 15:09:26", formats=["%Y-%m-%d %H:%M:%S"]) # Using specific format
537+ >>> ab_datetime_try_parse(
538+ >>> "2023-03-14 15:09:26",
539+ >>> formats=["%Y-%m-%d %H:%M:%S"], # Using specific format
540+ >>> disallow_other_formats=True, # Disallow other formats
541+ >>> )
533542 """
534543 try :
535544 return ab_datetime_parse (
536- dt_str , formats = formats , disallow_other_formats = disallow_other_formats
545+ dt_str ,
546+ formats = formats ,
547+ disallow_other_formats = disallow_other_formats ,
537548 )
538549 except (ValueError , TypeError ):
539550 return None
0 commit comments