date.ifc2datetime - refactor for readability

This commit is contained in:
Andrej730
2025-07-25 13:08:31 +05:00
parent 3c338db364
commit 99cdc4da35
@@ -39,20 +39,24 @@ def timedelta2duration(timedelta):
def ifc2datetime(element: Union[str, int, ifcopenshell.entity_instance]): def ifc2datetime(element: Union[str, int, ifcopenshell.entity_instance]):
if isinstance(element, str) and "P" in element[0:2]: # IfcDuration if isinstance(element, str):
if "P" in element[0:2]: # IfcDuration
duration = parse_duration(element) duration = parse_duration(element)
if isinstance(duration, datetime.timedelta): if isinstance(duration, datetime.timedelta):
return timedelta2duration(duration) return timedelta2duration(duration)
return duration return duration
elif isinstance(element, str) and len(element) > 3 and element[2] == ":": # IfcTime elif len(element) > 3 and element[2] == ":": # IfcTime
return datetime.time.fromisoformat(element) return datetime.time.fromisoformat(element)
elif isinstance(element, str) and ":" in element: # IfcDateTime elif ":" in element: # IfcDateTime
return datetime.datetime.fromisoformat(element) return datetime.datetime.fromisoformat(element)
elif isinstance(element, str): # IfcDate else: # IfcDate
return datetime.date.fromisoformat(element) return datetime.date.fromisoformat(element)
elif isinstance(element, int): # IfcTimeStamp elif isinstance(element, int): # IfcTimeStamp
return datetime.datetime.fromtimestamp(element) return datetime.datetime.fromtimestamp(element)
elif element.is_a("IfcDateAndTime"):
elif isinstance(element, ifcopenshell.entity_instance):
if element.is_a("IfcDateAndTime"):
return datetime.datetime( return datetime.datetime(
element.DateComponent.YearComponent, element.DateComponent.YearComponent,
element.DateComponent.MonthComponent, element.DateComponent.MonthComponent,