使用从数据库中检索到的 DateTime 进行减法给出了奇怪的结果

Subtraction using DateTime retrieved from database giving strange results

我在对从 MySQL 数据库收到的时间进行 DateTime 减法时遇到了一些问题。所有非 UTC 时间均采用美国格式。

这是我收到的时间(我使用 Rails 应用程序上的 Ruby 从数据库中检索它),它以字符串的形式出现:

2017-04-10T15:00:00.000Z

然后我解析字符串以将其转换为 DateTime 并对常规 DateTime 进行调试(不带偏移量):

start_time = DateTimeOffset.Parse(event_time[2]);
Debug.Log(start_time.DateTime);

然后显示以下内容:

4/10/2017 3:00:00 PM

这似乎与我通常使用的格式相似(即 M/dd/yyy h/mm/ss tt),因此我与当前时间进行了快速比较,以检查我离它到底有多远(作为参考,DateTime.Now = 4/10/2017 12:46:26 AM 当我 运行 这个代码最后时):

TimeSpan tmp;
tmp = (start_time.DateTime).Subtract(DateTime.Now);

Debug.Log(tmp)

但是,这给了我以下结果:

-736428.00:46:22.2744445

用下面的方法进行这样的操作得到了正常的结果:

DateTime tmp2;
TimeSpan tmp3;

tmp2 = DateTime.Now.Add(new TimeSpan(0, 0, 360));

tmp3 = tmp2.Subtract(DateTime.Now)

Debug.Log(tmp2);
Debug.Log(tmp3);

正常结果:

4/10/2017 12:52:26 AM
00:06:00

我处理从数据库中获取的时间的方式有问题吗?无论我尝试什么,我似乎都无法得到正常的结果。

我在 fiddler 中尝试了相同的输入和语法,我得到了预期的结果。检查 Debug.Log 如何在内部处理 TimeSpan 对象。在将其传递给 Debug.Log 以检查它是否报告正确结果之前,也尝试打印值(hour/min/seconds 中的差异)。

我找到了我的问题的解决方案(有点?)。

我也必须道歉,因为我没有完全描述我是如何使用我最初发布的代码的。

事实证明问题是我试图在另一个脚本中执行 DateTime 减法(我在 Unity 中执行所有操作,就像我之前提到的那样)。

例如,这是在脚本 Buttonscript.cs:

中给我错误输出的代码
TimeSpan tmpButtonController;
tmpButtonController = HighscoreController.GetComponent<HSController>().start_time.DateTime.Subtract(DateTime.Now);
Debug.Log(tmpButtonController);

但是如果我做同样的事情,但是在我解析字符串的同一个脚本中 (HSController.cs),一切都很好:

  TimeSpan tmpHSController;

  tmpHSController = start_time.DateTime.Subtract(DateTime.Now);

  Debug.Log(tmpHSController);

同样,不确定为什么会发生这种情况,但我现在有 resolved/have 解决此问题的方法。如果有人首先知道为什么会出现此错误,请告诉我!

感谢大家的参与!