Skip to content

Commit

Permalink
fix: allow to import time field (#17677) (#17690)
Browse files Browse the repository at this point in the history
(cherry picked from commit c478673)

Co-authored-by: Shariq Ansari <30859809+shariquerik@users.noreply.github.com>
  • Loading branch information
mergify[bot] and shariquerik committed Aug 1, 2022
1 parent 1409f48 commit 59f58c7
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
20 changes: 14 additions & 6 deletions frappe/core/doctype/data_import/importer.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import json
import os
import timeit
from datetime import date, datetime
from datetime import date, datetime, time

import frappe
from frappe import _
Expand Down Expand Up @@ -854,11 +854,13 @@ def guess_date_format_for_column(self):
"""

def guess_date_format(d):
if isinstance(d, (datetime, date)):
if isinstance(d, (datetime, date, time)):
if self.df.fieldtype == "Date":
return "%Y-%m-%d"
if self.df.fieldtype == "Datetime":
return "%Y-%m-%d %H:%M:%S"
if self.df.fieldtype == "Time":
return "%H:%M:%S"
if isinstance(d, str):
return frappe.utils.guess_date_format(d)

Expand Down Expand Up @@ -913,16 +915,22 @@ def validate_values(self):
}
)
elif self.df.fieldtype in ("Date", "Time", "Datetime"):
# guess date format
# guess date/time format
self.date_format = self.guess_date_format_for_column()
if not self.date_format:
self.date_format = "%Y-%m-%d"
if self.df.fieldtype == "Time":
self.date_format = "%H:%M:%S"
format = "HH:mm:ss"
else:
self.date_format = "%Y-%m-%d"
format = "yyyy-mm-dd"

self.warnings.append(
{
"col": self.column_number,
"message": _(
"Date format could not be determined from the values in this column. Defaulting to yyyy-mm-dd."
),
"{0} format could not be determined from the values in this column. Defaulting to {1}."
).format(self.df.fieldtype, format),
"type": "info",
}
)
Expand Down
5 changes: 5 additions & 0 deletions frappe/utils/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -1898,6 +1898,11 @@ def _get_time_format(time_str):
if date_format:
return date_format

# check if time format can be guessed
time_format = _get_time_format(date_string)
if time_format:
return time_format

# date_string doesnt look like date, it can have a time part too
# split the date string into date and time parts
if " " in date_string:
Expand Down

0 comments on commit 59f58c7

Please sign in to comment.