Improve time and date support (don't cast to datetime) and support all recurrence types and time periods in work schedules

This commit is contained in:
Dion Moult
2021-04-29 17:52:45 +10:00
parent 78e4478242
commit e5cf0cde2d
5 changed files with 179 additions and 52 deletions
@@ -1,5 +1,5 @@
import datetime
from re import findall
from datetime import datetime
def duration2dict(duration):
@@ -12,12 +12,16 @@ def duration2dict(duration):
def ifc2datetime(element):
if isinstance(element, str) and element[0] == "P": # IfcDuration
return duration2dict(element)
elif isinstance(element, str): # IfcDateTime, IfcDate
return datetime.fromisoformat(element)
elif isinstance(element, str) and element[2] == ":": # IfcTime
return datetime.time.fromisoformat(element)
elif isinstance(element, str) and ":" in element: # IfcDateTime
return datetime.datetime.fromisoformat(element)
elif isinstance(element, str): # IfcDate
return datetime.date.fromisoformat(element)
elif isinstance(element, int): # IfcTimeStamp
return datetime.fromtimestamp(element)
return datetime.datetime.fromtimestamp(element)
elif element.is_a("IfcDateAndTime"):
return datetime(
return datetime.datetime(
element.DateComponent.YearComponent,
element.DateComponent.MonthComponent,
element.DateComponent.DayComponent,
@@ -27,7 +31,7 @@ def ifc2datetime(element):
# TODO: implement TimeComponent timezone
)
elif element.is_a("IfcCalendarDate"):
return datetime(
return datetime.date(
element.YearComponent,
element.MonthComponent,
element.DayComponent,
@@ -36,15 +40,24 @@ def ifc2datetime(element):
def datetime2ifc(dt, ifc_type):
if isinstance(dt, str):
dt = datetime.fromisoformat(dt)
dt = datetime.datetime.fromisoformat(dt)
if ifc_type == "IfcTimeStamp":
return int(dt.timestamp())
elif ifc_type == "IfcDateTime":
return dt.isoformat()
if isinstance(dt, datetime.datetime):
return dt.isoformat()
elif isinstance(dt, datetime.date):
return datetime.datetime.combine(dt, datetime.datetime.min.time()).isoformat()
elif ifc_type == "IfcDate":
return dt.date().isoformat()
if isinstance(dt, datetime.datetime):
return dt.date().isoformat()
elif isinstance(dt, datetime.date):
return dt.isoformat()
elif ifc_type == "IfcTime":
return dt.time().isoformat()
if isinstance(dt, datetime.datetime):
return dt.time().isoformat()
elif isinstance(dt, datetime.time):
return dt.isoformat()
elif ifc_type == "IfcCalendarDate":
return {"DayComponent": dt.day, "MonthComponent": dt.month, "YearComponent": dt.year}
elif ifc_type == "IfcLocalTime":