mirror of
https://github.com/IfcOpenShell/IfcOpenShell.git
synced 2026-09-26 02:07:36 +00:00
New IfcOpenShell date utility module, because we shouldn't need to think too hard about dates
This commit is contained in:
@@ -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()
|
||||||
Reference in New Issue
Block a user