2

I'm setting up a scheduling system for one of my projects and one thing in particular that I need to do is allow for multiple windows to be present within each day. A window would represent two points in time, the start and the end.

I am not sure just how I should approach this issue. I can do this in a very hacky way but I would rather know how to do it right, so that I can be satisfied that my code is as it should be.

What I'm currently attempting to do is as seen here:

public class ScheduleWindow
{
    public string Name;
    public DateTime EndTime;
    public DateTime StartTime;
}

I have a name id for my schedule, but for this that is irrelevant. I have a date in time at which the window will start. I have a date in time at which the window will end.

The intent for the following method is to add a window to a schedule. I want the schedule to represent my day, so I'm using the current year, month and day and then setting the hours and minutes to the points in time that I would like this window to be active.

public void AddWindow(string name, int startHour, int endHour, int startMinute, int endMinute)
    {
        var year = DateTime.Now.Year;
        var month = DateTime.Now.Month;
        var day = DateTime.Now.Day;

        var startTime = new DateTime(year: year, month: month, day: day, hour: startHour, minute: startMinute, second: 0, millisecond: 0);
        var endTime = new DateTime(year: year, month: month, day: day, hour: endHour, minute: endMinute, second: 0, millisecond: 0);


        var window = new ScheduleWindow()
        {
            EndTime = endTime,
            StartTime = startTime,
            Name = name
        };

        _scheduleWindows.Add(window);
    }

So now we're to the root of my issue. I am actually completely unsure of how to check if we are within that time window.

`public bool WindowIsActive()
{
   foreach (var window in _scheduleWindows)
   {
      ...
      //if any window is currently active, return true
   }
}`

I've been fiddling here with this code for some time now, and any help would be super appreciated. If anyone can give me some pointers to perhaps a solution that would work better, that would be awesome!

The goal is to check and see if any window is currently active. Currently, I have no clue how.

Velcer
  • 43
  • 5
  • 1
    Minor suggestion: in your actual code - change your `ScheduleWindow` structure to use `TimeSpan` instead of `DateTime` - this constructor is more appropriate: `new TimeSpan(int hr, int min, int sec)` – Ambrose Leung Jan 30 '19 at 18:43
  • 1
    Possible duplicate of [Find if current time falls in a time range](https://stackoverflow.com/questions/1504494/find-if-current-time-falls-in-a-time-range) – devNull Jan 30 '19 at 18:46
  • And I'll rename the method to "AnyWindowIsActive". After year I will forget what simple method "WindowIsActive" do. – Ondřej Kubíček Jan 30 '19 at 20:33

2 Answers2

2

I imagine it's look something like this

public bool WindowIsActive()
{
   foreach (var window in _scheduleWindows)
   {
      if (DateTime.Now >= window.StartTime && DateTime.Now <= window.EndTime)
      {
          return true;
      }
   }
}

This works because DateTime implements the GreaterThanOrEqual and LessThanOrEqual operators.

Two things to consider about this answer:

  1. This code assumes EndTime is later than StartTime.
  2. If you care about timezones, you should use DateTimeOffset instead.
Powerlord
  • 87,612
  • 17
  • 125
  • 175
1

You can use < and > operators to compare DateTimes.

[edit: I realized that you just wanted to compare the time of day - i.e. disregarding the month and year - you'd use the TimeOfDay property of DateTime]

var timeOfDay = DateTime.Now.TimeOfDay; //this is a TimeSpan type
if(timeOfDay > window.StartTime.TimeOfDay && timeOfDay < window.EndTime.TimeOfDay)
{
    //time is within the time window.
}
Ambrose Leung
  • 3,704
  • 2
  • 25
  • 36