简体   繁体   English

DateTime 计算的奇怪问题

[英]Weird Issue with DateTime calculation

I've probably been working too hard, but can someone explain to me the following, taken from the Immediate window?我可能工作太努力了,但有人可以向我解释以下内容,取自Immediate window?

(int)DateTime.Now.Date.DayOfWeek = 4 (int)DateTime.Now.Date.DayOfWeek = 4

and

(int)DayOfWeek.Sunday = 0 (int)DayOfWeek.Sunday = 0

and

(int)DateTime.Now.Date.DayOfWeek - (int)DayOfWeek.Sunday = 4 (int)DateTime.Now.Date.DayOfWeek - (int)DayOfWeek.Sunday = 4

but
(int)DayOfWeek.Sunday - (int)DateTime.Now.Date.DayOfWeek = Could not evaluate expression` (int)DayOfWeek.Sunday - (int)DateTime.Now.Date.DayOfWeek = 无法评估表达式`

Thanks for reading.谢谢阅读。

EDIT:编辑:

Its the Immediate window that's giving me this weird result, not regular code.它的即时 window 给了我这个奇怪的结果,而不是常规代码。

Screenshot: http://ploader.net/files/0c2556df475b3075634d7fd2b0575794.PNG截图: http://ploader.net/files/0c2556df475b3075634d7fd2b0575794.PNG

EDIT2:编辑2:

The community seem to think its a bug in VS2010.社区似乎认为它是 VS2010 中的一个错误。 I wonder if @EricLippert or @JonSkeet could spare a minute to confirm this or, if its not, offer an explanation about this behaviour?我想知道@EricLippert 或@JonSkeet 是否可以花一点时间来确认这一点,或者如果不是,请提供有关此行为的解释?

It looks specific to the constant 0 and a non-literal value.它看起来特定于常量 0 和非文字值。 The following works just fine:以下工作正常:

int zero = 0;
zero - (int)DateTime.Now.Date.DayOfWeek
-4

While the following fails:虽然以下失败:

int four = 4;
0 - four
Could not evaluate expression

Update: I couldn't find a similar bug report, so I created one: https://connect.microsoft.com/VisualStudio/feedback/details/679501/integer-literal-0-integer-variable-could-not-evaluate-expression-immediate-window更新:我找不到类似的错误报告,所以我创建了一个: https://connect.microsoft.com/VisualStudio/feedback/details/679501/integer-literal-0-integer-variable-could-not-evaluate -表达式立即窗口

Update #2: Microsoft is able to reproduce the issue and has resolved it as "Won't Fix", meaning there's hope for the next version of Visual Studio, but not for VS2010.更新 #2:Microsoft 能够重现该问题并将其解决为“无法修复”,这意味着 Visual Studio 的下一个版本有希望,但 VS2010 没有。

I have no idea, it looks like a bug to me.我不知道,这对我来说似乎是一个错误。

// This doesn't work
0 - (int)DateTime.Now.Date.DayOfWeek

// But loads of similar variations do:
1 - (int)DateTime.Now.Date.DayOfWeek
-1 - (int)DateTime.Now.Date.DayOfWeek
a - (int)DateTime.Now.Date.DayOfWeek
0 - (int)DayOfWeek.Thursday

In any case everything behaves as expected in the compiled code.在任何情况下,编译代码中的一切都按预期运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM