- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
-我希望转换 Matlab 串行时间(datenum,如 t_matlab=now
)
到 c# 日期时间(如 var t_cs = DateTime.Now.Ticks
)。
知道怎么做吗?
[编辑]我找到了一种方法,但仍然不确定这是否是最好的方法。
[Edit2] 修复了错误的 DateTimes,感谢 Jonas!
function cstime = datenum2datetime( matlabSerialTime )
%Convert matlab serial time (datenum) to .net datenum.
%
% Example:
% ntTime = datenum2datetime(now)
% cstime = datenum2datetime([734539.4717013890 734539.5051388888]);
%
% See also datenum.
% using System.DateTime.Parse(string).Ticks to convert to DateTime format.
% t1 = now; t2 = now+1; matlab_times = [t1 t2];
% cs_times = [System.DateTime.Parse(datestr(t1)).Ticks ...
% System.DateTime.Parse(datestr(t2)).Ticks]
% aver = diff(cs_times)/diff(matlab_times);
% offver = cs_times(1) - matlab_times(1)*aver;
a = 10^7*60*60*24;
offset = -367*10^7*60*60*24;
cstime = a*matlabSerialTime + offset;
最佳答案
编辑:对 jarr 的回答感到惊讶,我进一步调查了。事实证明,问题中 sharhar_m 给出的示例时间点是错误的(很抱歉没有早点检查)。
综上所述,题中给出的函数有367-281=86天错误,应该更正为
function cstime = datenum2datetime( matlabSerialTime )
cstime = 10^7*60*60*24*(matlabSerialTime - 367);
现在详细说说,以防有人感兴趣:你声称
% {05-Feb-2011 11:19:15} in System.DateTime is 634399319550000000
但是在 MATLAB R2010b 中
sdt =System.DateTime(634399319550000000);
[sdt.Day sdt.Month]
返回 [2 5]
,因此您的 DateTime 值实际上是 5 月 2 日,而不是 2 月 5 日!!计算出正确的值集
cs_times = [System.DateTime.Parse('05-Feb-2011 11:19:15').Ticks ...
System.DateTime.Parse('05-Feb-2011 12:07:24').Ticks]
给出 [634325015550000000 634325044440000000]
。
时间单位缩放
您的因子 a
是 10^7*60*60*24 即 MATLAB 以“天”为单位存储日期/时间(时间为小数天),C# 将时间存储为“滴答” ,即“10^-7 秒”的数量。您可以通过为 a
输入精确值来避免一些舍入错误。
引用时间点的差异
以天数表示的偏移量b
(b/a
) 告诉您它们的“时间原点”相隔 367 天;使用 b
的旧值,结果为 281 天。 datestr
状态的 MATLAB 文档
"A serial date number represents the whole and fractional number of days from 1-Jan-0000 to a specific date. The year 0000 is merely a reference point and is not intended to be interpreted as a real year in time."
(即使运行 datestr(0,'dd-mm-yyyy HH-MM-SS')
显示引用点实际上是 0-Jan-0000)。 C# ticks是
"number of 100-nanosecond intervals that have elapsed since 12:00:00 midnight, January 1, 0001, which represents DateTime.MinValue. It does not include the number of ticks that are attributable to leap seconds."
因此,总而言之,两个系统之间的“时间起源”相差一个闰年和一天,因此相差 367 天。如果你真的想处理很久以前的真实日期,你将不得不使用 Gregorian calendar改革和奥古斯都对闰年错误应用的更正Julian calendar考虑到 8 年级之前...但我怀疑这在这里是否有意义 ;-)。
关于c# - 将 Matlab Datenum 转换为 Datetime,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5855208/
我正在尝试使用以下方法对 datetime.datetime 对象列表求和: from datetime import datetime, timedelta d= [datetime.datetim
我正在尝试这个 (datetime.datetime.today()-datetime.datetime.today()).days 给出 -1 并期待值 0 而不是我得到 -1。在这种情况下,我将结
如果我列一个时间增量的列表,平均值比我对这些增量的微秒值求平均时要大。为什么会这样呢?。赠送。这是Linux上的Python3.8.10。
考虑以下片段: import datetime print(datetime.datetime.now() - datetime.datetime.now()) 在 x86_64 Linux 下的 P
如何在 SQLAlchemy 查询中比较 DateTime 字段和 datetime.datetime 对象? 例如,如果我这样做 candidates = session.query(User).f
我收到以下错误: type object 'datetime.datetime' has no attribute 'datetime' 在下面一行: date = datetime.datetime
尝试找出如何将当前日期锁定为变量,以从输入的 self.birthday 中减去。我已经查看了各种示例和链接,但无济于事......建议? from datetime import datetime
您好,我有一些 datetime.datetime 格式的日期,我用它们来过滤带有 Pandas 时间戳的 Pandas 数据框。我刚刚尝试了以下方法并获得了 2 小时的偏移量: from datet
如果您调用 datetime.datetime.now(datetime.timezone.utc) 您会得到类似 datetime.datetime(2021, 9, 8, 1, 33, 19, 6
我正在使用 pywin32 读取/写入 Excel 文件。我在 Excel 中有一些日期,以 yyyy-mm-dd hh:mm:ss 格式存储。我想将它们作为 datetime.datetime 对象
据我所知,自 Unix 纪元(1970-01-01 00:00:00 UTC)以来的秒数在全局各地应该是相同的,因为它固定为 UTC。 现在,如果您所在的时区有几个小时 +/- UTC,为什么这样做会
我正在尝试添加 datetime.datetime 和 datetime.time 以获得一列。我正在尝试结合: import datetime as dt dt.datetime.combine(m
我有一个脚本需要在脚本的不同行执行以下操作: today_date = datetime.date.today() date_time = datetime.strp(date_time_string
我在 AppEngine 上收到 type object 'datetime.datetime' has no attribute 'datetime' 错误,提示日期时间类型,但我的导入是 impo
所以我一直在使用 python 语言制作东西。我遇到了一些不太容易理解的错误: TypeError: 'datetime.datetime' object is not subscriptable (
当我运行时 from datetime import date, time, timedelta date(2012, 11, 1) + timedelta(0, 3600) 结果是 datetime
我的目标是转换 utc进入loc : use chrono::{Local, UTC, TimeZone}; let utc = chrono::UTC::now(); let loc = chron
假设您有一个 datetime.date 对象,例如 datetime.date.today() 返回的对象。 稍后您还会得到一个表示时间的字符串,它补充了日期对象。 在 datetime.datet
我试过了 In [16]: import datetime In [17]: now = datetime.datetime.utcnow() In [18]: isinstance(now, dat
我有以下代码并且收到上述错误。由于我是 python 新手,因此无法理解此处的语法以及如何修复错误: if not start or date < start: start = date 最佳答案 有
我是一名优秀的程序员,十分优秀!