0

I wanna keep these time conditions in some sort of list or array in C#. I have these time conditions:

  • Saturday 12:00 – Sunday 08:00

    public bool CheckConditions(DateTime now)
    {
           //Pseudo
           if (now >= "Saturday 12:00" && now <= "Sunday 08:00")
           {
                return true;
           }
    }
    

I'm having a hard time to see how i should solve this, i guess i will need some if statements for checking all of these time conditions. The input parameter will be the date and time right now, then i wanna check if the time right now is in between these conditions and if they are i just wanna return true basically otherwise false.

I hope you understand what I'm looking for here, and need help with.

Thanks

  • Does this answer your question? [Find if current time falls in a time range](https://stackoverflow.com/questions/1504494/find-if-current-time-falls-in-a-time-range) – Sinatr Jul 08 '21 at 11:48
  • `var hours = new Dictionary { [DayOfWeek.Sunday] = new[] { 18, 19, 20, 21, 22, 23 }, [DayOfWeek.Monday] = new[] { 00, 01, 02, 03, 04, 05, 06, 18, 19, 20, 21, 22, 23 }, .. }`. `hours[now.DayOfWeek].Contains(now.Hour)`. More efficient ways of initializing/checking are possible if you are guaranteed the ranges are contiguous, of course. – Jeroen Mostert Jul 08 '21 at 11:53

1 Answers1

0

Here is the way I attempted this.

    public class WeekTimeSpan
    {
    public WeekDayTime StartDayTime
        {
        get; set;
        }
    public WeekDayTime EndDayTime
        {
        get; set;
        }

    public bool DoesFallInTimeSpan (DateTime dateTime)
        {
        return StartDayTime.IsLessThan (dateTime) && EndDayTime.IsGreaterThan (dateTime);
        }
    }

public class WeekDayTime
    {
    public DayOfWeek DayOfWeek
        {
        get; set;
        }
    public int HourOfTheDay
        {
        get; set;
        }

    public int MinuteOfHour
        {
        get; set;
        }

    public bool IsGreaterThan (DateTime dateTime)
        {
        if (DayOfWeek > dateTime.DayOfWeek)
            return true;
        if (DayOfWeek < dateTime.DayOfWeek)
            return false;
        if (HourOfTheDay > dateTime.Hour)
            return true;
        if (HourOfTheDay < dateTime.Hour)
            return false;
        return MinuteOfHour > dateTime.Minute;
        }

    public bool IsLessThan (DateTime dateTime)
        {
        if (DayOfWeek < dateTime.DayOfWeek)
            return true;
        if (DayOfWeek > dateTime.DayOfWeek)
            return false;
        if (HourOfTheDay < dateTime.Hour)
            return true;
        if (HourOfTheDay > dateTime.Hour)
            return false;
        return MinuteOfHour < dateTime.Minute;
        }
    }

Here is the usage.

    public bool CheckConditions (DateTime now)
        {
        List<WeekTimeSpan> timeSpans=new List<WeekTimeSpan>()
        {
            new WeekTimeSpan()
            {
                StartDayTime = new WeekDayTime() { DayOfWeek = DayOfWeek.Friday,HourOfTheDay = 18,MinuteOfHour = 0},
                EndDayTime = new WeekDayTime()   {DayOfWeek = DayOfWeek.Saturday,HourOfTheDay = 6,MinuteOfHour = 0}
            }
        };

        return timeSpans.Exists (span => span.DoesFallInTimeSpan (now));
        }

We got couple of data structures. WeekDayTime representing time in a week and WeekTimeSpan representing TimeSpan in a week. You have to extend WeekDayTime class if you are looking for more granular time like seconds and milliseconds.

Keep in my mind, this would fail for the very edge cases like Saturday to Sunday time span but would work for your scenario. I suggest to cover the edge cases before using it for production.

Please accept the response if it answers your question. Cheers!!

Prasad Bhokare
  • 349
  • 1
  • 2
  • 12