快速的DateTime保证 [英] Quick DateTime reassurance

查看:118
本文介绍了快速的DateTime保证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究原始程序员留下的一些旧代码.我知道此方法总是返回false,但是由于我最近看到了一些奇怪的解决方法,因此必须确保.



I am working through some old sections of code that the original programmer left behind. I know this method always returns false, but with some the strange workarounds I have seen lately, I have to make sure.



protected static bool IsAS400Offline()
{
    return DateTime.Now.TimeOfDay >=
              new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 
                           23, 30, 0).TimeOfDay &&
           DateTime.Now.TimeOfDay <=
              new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 
                           2, 0, 0).TimeOfDay;
}



如果旧的熨斗每晚进行备份,则此代码用于将查询重定向到临时sql数据库.自从大约2 1/2年前更改为SVN以来,它尚未进行过修改.

他们的方法是否可以返回true?

[修改:更改了制表符和空格以使其更易于理解,也将其向下移动]



This code is meant to redirect queries to a temp sql database if the old iron is doing nightly backups. It hasn''t been modified since we changed to SVN about 2 1/2 years ago.

Is their anyway this method can return true?

[Modified: changed the tabbing and spacing to try to make it more understandable, also moved it down]

推荐答案

这将永远不会返回true.

代码询问是否在今晚晚上11:30至今天早上2点之间.如果我正确理解逻辑,则认为您需要使用OR而不是AND.

干杯.
This will never return true.

The code is asking if it''s ever between tonight at 11:30 pm and this morning at 2 am. If I''m understanding the logic correctly, I think you need an OR instead of an AND.

Cheers.


我怀疑表达式的后半部分应该增加一天的时间,因此表达式会测试一下时间是否在今晚23:30至02:00之间.明天早上.但是,程序员要么完全错过了,要么无法在月底或年月底搞清楚该做什么.就目前而言,任何时间点都不能晚于23:30和早于02:00.


正如William所指出的,上尉,这是不合逻辑的!
I suspect the second half of the expression was supposed to increment the day so the expression would test to see if the time is between 23:30 tonight and 02:00 tomorrow morning. However the programmer either missed that totally or couldn''t figure out what to do at the end of the month or year. As it stands no point in time can be both later than 23:30 and earlier than 02:00.


As William points out, that is illogical, Captain!


不……它总是返回false.

那句话是说,

当前时间是今天晚上11:30之后和今天凌晨2点之前."

说的是另一种方式

no...it will always return false.

That statement is saying,

"Is the current time after 11:30pm today and before 2am today."

Written a different way, it says

return (DateTime.Now.Hour = 23 && DateTime.Minute >= 30) &&
       (DateTime.Now.Hour <= 2)



这不可能.那应该告诉你为什么...你不能将小时= 23并小于2.

也许是说要用或"(||)代替和"吗?

虽然,说实话,为什么你只说一遍就写所有这些



That''s impossible. That should show you why...you can''t have the hour = 23 and be less than 2.

Maybe it was mean to be an "or" (||) instead of "and"?

Though, honestly, why you would write all of that when you could just say

return (DateTime.Now.Hour = 23 && DateTime.Now.Minute >= 30) ||
       (DateTime.Now.Hour <= 2)


这篇关于快速的DateTime保证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆