Click here to Skip to main content
15,904,503 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.



C#
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;
}


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]
Posted
Updated 21-Jun-10 7:34am
v3

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.
 
Share this answer
 
v2
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!
 
Share this answer
 
v2
Comments
William Winner 21-Jun-10 13:44pm    
except, how could the time ever return true if it was checking to see if it was tomorrow morning? It can never be tomorrow...
William Winner 21-Jun-10 15:23pm    
actually, I started to type exactly the same thing...and then realized what I wrote...so you're not alone.
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

C#
return (DateTime.Now.Hour = 23 && DateTime.Minute >= 30) &&
       (DateTime.Now.Hour <= 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

C#
return (DateTime.Now.Hour = 23 && DateTime.Now.Minute >= 30) ||
       (DateTime.Now.Hour <= 2)
 
Share this answer
 
Comments
Richard MacCutchan 21-Jun-10 14:06pm    
Well spotted, I knew there was something wrong in my logic, I guess I made a worse mistake than the original programmer.
I think maybe it can be true as DateTime.Now returns the current clock and that value is constantly changing.

Consider what might happen if the clock reads 23h59m59.99s for the first comparison and for the second it has incremented to 00h00m00s. To be truly reliable the method should read DateTime.Now once and use that unchanging value for all comparisons.

Alan.
 
Share this answer
 
Thanks both of you. Some of these mistakes have been in production long enough I doubt myself everytime I say it can't actually be working right.

Good thing not a lot of our sales reps are submitting orders that late.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900