New IfcOpenShell date utility module, because we shouldn't need to think too hard about dates

This commit is contained in:
Dion Moult
2021-01-22 16:21:17 +11:00
parent 7522f8fb7a
commit c67a99fb5d
@@ -0,0 +1,45 @@
from re import findall
from datetime import datetime
def duration2dict(duration):
results = {}
for number, unit in findall("(?P<number>\d+)(?P<period>S|M|H|D|W|Y)", duration):
results[unit] = number
return results
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, int): # IfcTimeStamp
return datetime.fromtimestamp(element)
elif element.is_a("IfcDateAndTime"):
return datetime(
element.DateComponent.YearComponent,
element.DateComponent.MonthComponent,
element.DateComponent.DayComponent,
element.TimeComponent.HourComponent,
element.TimeComponent.MinuteComponent,
element.TimeComponent.SecondComponent,
# TODO: implement TimeComponent timezone
)
elif element.is_a("IfcCalendarDate"):
return datetime(
element.YearComponent,
element.MonthComponent,
element.DayComponent,
)
def datetime2ifc(dt, ifc_type):
if ifc_type == "IfcTimeStamp":
return int(dt.timestamp())
elif ifc_type == "IfcDateTime":
return dt.isoformat()
elif ifc_type == "IfcDate":
return dt.date().isoformat()
elif ifc_type == "IfcTime":
return dt.time().isoformat()