From 02fd78df1433cb2dc465838fd5a0f29907c7158d Mon Sep 17 00:00:00 2001 From: Andrej730 Date: Mon, 27 May 2024 17:18:44 +0500 Subject: [PATCH] datetime2ifc to support None values as it was before ebd03e9 #4734 --- .../ifcopenshell/util/date.py | 26 ++++++++++++++++--- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/src/ifcopenshell-python/ifcopenshell/util/date.py b/src/ifcopenshell-python/ifcopenshell/util/date.py index e547e5dbbf..50352544f7 100644 --- a/src/ifcopenshell-python/ifcopenshell/util/date.py +++ b/src/ifcopenshell-python/ifcopenshell/util/date.py @@ -19,7 +19,7 @@ import datetime from re import findall from dateutil import parser -from typing import Literal, Union, Any +from typing import Literal, Union, Any, overload try: import isodate @@ -105,8 +105,11 @@ def readable_ifc_duration(string): return final_string +@overload +def datetime2ifc(dt: None, ifc_type: Any) -> None: ... +@overload def datetime2ifc( - dt: Union[datetime.date, str], + dt: Union[datetime.date, str, None], ifc_type: Literal[ "IfcDuration", "IfcTimeStamp", @@ -116,7 +119,19 @@ def datetime2ifc( "IfcCalendarDate", "IfcLocalTime", ], -) -> Union[int, str, dict[str, Any]]: +) -> Union[int, str, dict[str, Any], None]: ... +def datetime2ifc( + dt: Union[datetime.date, str, None], + ifc_type: Literal[ + "IfcDuration", + "IfcTimeStamp", + "IfcDateTime", + "IfcDate", + "IfcTime", + "IfcCalendarDate", + "IfcLocalTime", + ], +) -> Union[int, str, dict[str, Any], None]: if isinstance(dt, str): if ifc_type == "IfcDuration": return dt @@ -124,6 +139,8 @@ def datetime2ifc( dt = datetime.datetime.fromisoformat(dt) except: dt = datetime.time.fromisoformat(dt) + elif dt is None: + return if ifc_type == "IfcDuration": return isodate.duration_isoformat(dt) @@ -157,7 +174,8 @@ def datetime2ifc( "MinuteComponent": dt.minute, "SecondComponent": dt.second, } - raise TypeError(f"Unsupported ifc_type for conversion from datetime.datetime = {ifc_type}.") + + raise TypeError(f"Unsupported ifc_type for conversion from datetime.datetime = {ifc_type}, value = {dt}") def string_to_date(string):