def datenum(d, format):
if isinstance(d, np.ndarray):
return list(d)
if isinstance(d, list):
res = np.zeros(len(d))
n = 0
for d1 in d:
if format == "%Y-%m-%d %H:%M:%S":
dt = datetime.datetime.strptime(d1, format)
res[n] = python_to_matlab_timestamp(dt.timestamp())
elif format == "%H:%M:%S":
dt = datetime.datetime.strptime(d1, "%Y-%m-%d %H:%M:%S")
now = datetime.datetime.now()
dt = dt.replace(year=now.year)
dt = dt.replace(month=now.month)
dt = dt.replace(day=now.day)
res[n] = python_to_matlab_timestamp(dt.timestamp())
elif format == "%Y-%m-%d":
dt = datetime.datetime.strptime(d1, "%Y-%m-%d %H:%M:%S")
res[n] = int(python_to_matlab_timestamp(dt.timestamp()))
n += 1
else:
if format == "%Y-%m-%d %H:%M:%S":
dt = datetime.datetime.strptime(d, format)
res = python_to_matlab_timestamp(dt.timestamp())
elif format == "%H:%M:%S":
dt = datetime.datetime.strptime(d, "%Y-%m-%d %H:%M:%S")
now = datetime.datetime.now()
dt = dt.replace(year=now.year)
dt = dt.replace(month=now.month)
dt = dt.replace(day=now.day)
res = python_to_matlab_timestamp(dt.timestamp())
elif format == "%Y-%m-%d":
dt = datetime.datetime.strptime(d, "%Y-%m-%d %H:%M:%S")
res = int(python_to_matlab_timestamp(dt.timestamp()))
return res
Matlab-Python datenum函数
最新推荐文章于 2024-08-14 10:35:52 发布