mirror of
https://github.com/IfcOpenShell/IfcOpenShell.git
synced 2026-09-20 15:08:51 +00:00
ios utility to transform a human readable date or duration from string into objects
This commit is contained in:
@@ -18,6 +18,7 @@
|
|||||||
|
|
||||||
import datetime
|
import datetime
|
||||||
from re import findall
|
from re import findall
|
||||||
|
from dateutil import parser
|
||||||
|
|
||||||
try:
|
try:
|
||||||
import isodate
|
import isodate
|
||||||
@@ -104,3 +105,34 @@ def datetime2ifc(dt, ifc_type):
|
|||||||
elif ifc_type == "IfcLocalTime":
|
elif ifc_type == "IfcLocalTime":
|
||||||
# TODO implement timezones
|
# TODO implement timezones
|
||||||
return {"HourComponent": dt.hour, "MinuteComponent": dt.minute, "SecondComponent": dt.second}
|
return {"HourComponent": dt.hour, "MinuteComponent": dt.minute, "SecondComponent": dt.second}
|
||||||
|
|
||||||
|
def string_to_date(string):
|
||||||
|
if not string:
|
||||||
|
return None
|
||||||
|
try:
|
||||||
|
return parser.isoparse(string)
|
||||||
|
except:
|
||||||
|
try:
|
||||||
|
return parser.parse(string, dayfirst=True, fuzzy=True)
|
||||||
|
except:
|
||||||
|
return None
|
||||||
|
|
||||||
|
def string_to_duration(duration_string):
|
||||||
|
# TODO support years, months, weeks aswell
|
||||||
|
days = 0
|
||||||
|
hours = 0
|
||||||
|
minutes = 0
|
||||||
|
seconds = 0
|
||||||
|
match = findall(r"(\d+\.?\d*)d", duration_string)
|
||||||
|
if match:
|
||||||
|
days = float(match[0])
|
||||||
|
match = findall(r"(\d+\.?\d*)h", duration_string)
|
||||||
|
if match:
|
||||||
|
hours = float(match[0])
|
||||||
|
match = findall(r"(\d+\.?\d*)m", duration_string)
|
||||||
|
if match:
|
||||||
|
minutes = float(match[0])
|
||||||
|
match = findall(r"(\d+\.?\d*)s", duration_string)
|
||||||
|
if match:
|
||||||
|
seconds = float(match[0])
|
||||||
|
return isodate.duration_isoformat(datetime.timedelta(days=days, hours=hours, minutes=minutes, seconds=seconds))
|
||||||
Reference in New Issue
Block a user