Skip to content

Commit fd0243d

Browse files
committed
Added check for 2 formats
1 parent 2ab9a5f commit fd0243d

File tree

1 file changed

+15
-10
lines changed

1 file changed

+15
-10
lines changed

src/databricks/sql/utils.py

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -628,6 +628,20 @@ def convert_decimals_in_arrow_table(table, description) -> "pyarrow.Table":
628628
return table
629629

630630

631+
def datetime_parser(datetime_string):
632+
formats = ["%Y-%m-%d %H:%M:%S", "%Y-%m-%d"]
633+
634+
for fmt in formats:
635+
try:
636+
return datetime.datetime.strptime(datetime_string, fmt)
637+
except ValueError:
638+
continue
639+
640+
raise ValueError(
641+
f"Datetime string '{datetime_string}' does not match any expected formats"
642+
)
643+
644+
631645
def convert_to_assigned_datatypes_in_column_table(column_table, description):
632646

633647
converted_column_table = []
@@ -642,16 +656,7 @@ def convert_to_assigned_datatypes_in_column_table(column_table, description):
642656
)
643657
elif description[i][1] == "timestamp":
644658
converted_column_table.append(
645-
tuple(
646-
(
647-
v
648-
if v is None
649-
else datetime.datetime.strptime(
650-
v, "%Y-%m-%d %H:%M:%S.%f"
651-
).replace(tzinfo=pytz.UTC)
652-
)
653-
for v in col
654-
)
659+
tuple((v if v is None else datetime_parser(v)) for v in col)
655660
)
656661
else:
657662
converted_column_table.append(col)

0 commit comments

Comments
 (0)