mirror of
https://github.com/IfcOpenShell/IfcOpenShell.git
synced 2026-09-22 16:31:10 +00:00
Fix #3791. Add isodate as an optional dependency to ifcopenshell-python.
This commit is contained in:
@@ -35,8 +35,7 @@ def timedelta2duration(timedelta):
|
|||||||
}
|
}
|
||||||
if components["seconds"]:
|
if components["seconds"]:
|
||||||
components["hours"], components["minutes"], components["seconds"] = [
|
components["hours"], components["minutes"], components["seconds"] = [
|
||||||
int(i)
|
int(i) for i in str(datetime.timedelta(seconds=components["seconds"])).split(":")
|
||||||
for i in str(datetime.timedelta(seconds=components["seconds"])).split(":")
|
|
||||||
]
|
]
|
||||||
return isodate.Duration(**components)
|
return isodate.Duration(**components)
|
||||||
|
|
||||||
@@ -122,9 +121,7 @@ def datetime2ifc(dt, ifc_type):
|
|||||||
if isinstance(dt, datetime.datetime):
|
if isinstance(dt, datetime.datetime):
|
||||||
return dt.isoformat()
|
return dt.isoformat()
|
||||||
elif isinstance(dt, datetime.date):
|
elif isinstance(dt, datetime.date):
|
||||||
return datetime.datetime.combine(
|
return datetime.datetime.combine(dt, datetime.datetime.min.time()).isoformat()
|
||||||
dt, datetime.datetime.min.time()
|
|
||||||
).isoformat()
|
|
||||||
elif ifc_type == "IfcDate":
|
elif ifc_type == "IfcDate":
|
||||||
if isinstance(dt, datetime.datetime):
|
if isinstance(dt, datetime.datetime):
|
||||||
return dt.date().isoformat()
|
return dt.date().isoformat()
|
||||||
@@ -180,9 +177,7 @@ def string_to_duration(duration_string):
|
|||||||
match = findall(r"(\d+\.?\d*)s", duration_string)
|
match = findall(r"(\d+\.?\d*)s", duration_string)
|
||||||
if match:
|
if match:
|
||||||
seconds = float(match[0])
|
seconds = float(match[0])
|
||||||
return isodate.duration_isoformat(
|
return isodate.duration_isoformat(datetime.timedelta(days=days, hours=hours, minutes=minutes, seconds=seconds))
|
||||||
datetime.timedelta(days=days, hours=hours, minutes=minutes, seconds=seconds)
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
def parse_duration(value):
|
def parse_duration(value):
|
||||||
@@ -192,9 +187,11 @@ def parse_duration(value):
|
|||||||
if "P" in value:
|
if "P" in value:
|
||||||
try:
|
try:
|
||||||
return isodate.parse_duration(value)
|
return isodate.parse_duration(value)
|
||||||
|
except ModuleNotFoundError:
|
||||||
|
print("Duration parsing not supported: isodate module not found")
|
||||||
except:
|
except:
|
||||||
print("error parsing ISO string duration")
|
print("Error parsing ISO string duration")
|
||||||
return None
|
return None
|
||||||
else:
|
else:
|
||||||
try:
|
try:
|
||||||
final_string = "P"
|
final_string = "P"
|
||||||
@@ -204,11 +201,7 @@ def parse_duration(value):
|
|||||||
final_string += char
|
final_string += char
|
||||||
elif char == "D":
|
elif char == "D":
|
||||||
final_string += "D"
|
final_string += "D"
|
||||||
if (
|
if "H" in value_upper or "S" in value_upper or "MIN" in value_upper:
|
||||||
"H" in value_upper
|
|
||||||
or "S" in value_upper
|
|
||||||
or "MIN" in value_upper
|
|
||||||
):
|
|
||||||
final_string += "T"
|
final_string += "T"
|
||||||
elif char == "W":
|
elif char == "W":
|
||||||
final_string += "W"
|
final_string += "W"
|
||||||
@@ -218,9 +211,7 @@ def parse_duration(value):
|
|||||||
final_string += "Y"
|
final_string += "Y"
|
||||||
elif char == "H":
|
elif char == "H":
|
||||||
final_string = (
|
final_string = (
|
||||||
final_string[:1] + "T" + final_string[1:]
|
final_string[:1] + "T" + final_string[1:] if "T" not in final_string else final_string
|
||||||
if "T" not in final_string
|
|
||||||
else final_string
|
|
||||||
)
|
)
|
||||||
final_string += "H"
|
final_string += "H"
|
||||||
elif char == "M":
|
elif char == "M":
|
||||||
@@ -229,9 +220,7 @@ def parse_duration(value):
|
|||||||
final_string += "M"
|
final_string += "M"
|
||||||
elif char == "S":
|
elif char == "S":
|
||||||
final_string = (
|
final_string = (
|
||||||
final_string[:1] + "T" + final_string[1:]
|
final_string[:1] + "T" + final_string[1:] if "T" not in final_string else final_string
|
||||||
if "T" not in final_string
|
|
||||||
else final_string
|
|
||||||
)
|
)
|
||||||
final_string += "S"
|
final_string += "S"
|
||||||
return isodate.parse_duration(final_string)
|
return isodate.parse_duration(final_string)
|
||||||
|
|||||||
@@ -15,6 +15,7 @@ classifiers = [
|
|||||||
]
|
]
|
||||||
[project.optional-dependencies]
|
[project.optional-dependencies]
|
||||||
geometry = ["mathutils"]
|
geometry = ["mathutils"]
|
||||||
|
date = ["isodate"]
|
||||||
[project.urls]
|
[project.urls]
|
||||||
"Homepage" = "http://ifcopenshell.org"
|
"Homepage" = "http://ifcopenshell.org"
|
||||||
"Bug Tracker" = "https://github.com/ifcopenshell/ifcopenshell/issues"
|
"Bug Tracker" = "https://github.com/ifcopenshell/ifcopenshell/issues"
|
||||||
|
|||||||
Reference in New Issue
Block a user