Shorten DateTime Status Check

if ((DateTime.Now.DayOfWeek != DayOfWeek.Friday && DateTime.Now.DayOfWeek != DayOfWeek.Saturday) && ((DateTime.Now.Hour >= 10 && DateTime.Now.Hour < 13) || (DateTime.Now.Hour >= 20 && DateTime.Now.Hour < 23))) 

I need to reduce this condition, any suggestions?

+4
source share
5 answers

You can change the working time.

 (DateTime.Now.Hour % 12) +1 >= 10 && (DateTime.Now.Hour % 12) +1 < 13 

Perhaps even without a second check.

I don’t think you can improve much more than looking for other methods such as other answers

Update I tested the above and incorrectly, but it is more sadistic and works

 var check = (DateTime.Now.Hours - 10 % 12) % 10; var checkV = (DateTime.Now.Hours >= 10 && check < 3); 

Test code

 for (int i = 0; i < 24; i++) { var check = (i - 10 % 12) % 10; bool checkV = (i >= 10 && check < 3); Console.WriteLine(i.ToString() + ": " + checkV.ToString()); } Console.ReadKey(); 

Update 2 Full Abbreviated Code

 if( (int)DateTime.Now.DayOfWeek < 5 && DateTime.Now.Hours >= 10 && ((DateTime.Now.Hours - 10 % 12) % 10) < 3) 
+4
source

Well, you can create an extension method:

 public static bool BoundsCheck(this DateTime d, int min, int max, int min2, int max2) { return (d.DayOfWeek != DayOfWeek.Friday && d.DayOfWeek != DayOfWeek.Saturday && d.Hour >= min && d.Hour < max) || (d.Hour >= min2 && d.Hour < max2); } 

and then call it like this:

 if (DateTime.Now.BoundsCheck(10, 13, 20, 23))... 
+4
source

What is shorter? Maybe, but more important, in my opinion, more readable and supported:

 var now = DateTime.Now; var notAllowedDays = new[] { DayOfWeek.Friday, DayOfWeek.Saturday }; var allowedHours = Enumerable.Range(10, 3).Concat(Enumerable.Range(20, 3)); if(!notAllowedDays.Contains(now.DayOfWeek) && allowedHours.Contains(now.Hour)) { } 
+4
source
 if (!this.ItsPartyDay() && (this.ItsLunchTime() || this.ItsDinnerTime())) { ... } private bool ItsPartyDay() { return (Int32)DateTime.Now.DayOfWeek >= 5; } private bool ItsLunchTime() { return (DateTime.Now.Hour >= 10 && DateTime.Now.Hour < 13); } private bool ItsDinnerTime() { return (DateTime.Now.Hour >= 20 && DateTime.Now.Hour < 23); } 
+2
source

I don’t think there is a reasonable solution, but here is a couple that comes to mind. Use aliases for DateTime and DayOfWeek . Another option would be to assign all these values ​​to variables prior to the conditional.

So you can do things like:

  string fri = DayOfWeek.Friday; string sat = DayOfWeek.Saturday; 

then use the ones indicated in the conditional expression. Or

  using dt = DateTime; 

Then you could do dt.Now.DayOfWeek

I personally would not recommend doing any of these things. You are not actually reducing the conditional, you are simply refactoring. If you have a lot of them in the same class, this can be a compromise, otherwise it may not be so.

EDIT: suggesting Michael Perrenoud's extension method is a smart solution that really works very well.

0
source

Source: https://habr.com/ru/post/1487130/


All Articles