tags:

views:

723

answers:

4

I have a service that user can configure to run during "off-peak" hours. They have the ability to set the time frame that the service can run.

For Example:

User A works 8am-5pm, so they want to schedule the app to run between 5:30pm and 7:30am.

User B works 9pm-6am, so they schedule the app to run between 6:30am and 8:30 pm.

The point is that the app uses their computer while they are not.

Given a DateTime of the current time, a DateTime of the start and a DateTime of the stop time, how can I check if current is between start and stop.

The tricky part for me is that the time can cross the midnight boundary.

+4  A: 

If startTime and endTime represent a single time interval (it will only happen once, and startTime and endTime represent the date and the time to start/stop), then it's as easy as saying

bool isTimeBetween = someTime >= startTime && someTime <= endTime;

If it's a recurring event (happens every day, during some interval), you can do comparisons using the TimeOfDay property. (The recurring case is the one where you have to consider a start/stop that crosses midnight)

static public bool IsTimeOfDayBetween(DateTime time, 
                                      TimeSpan startTime, TimeSpan endTime)
{
    if (endTime == startTime)
    {
        return true;   
    }
    else if (endTime < startTime)
    {
        return time.TimeOfDay <= endTime ||
            time.TimeOfDay >= startTime;
    }
    else
    {
        return time.TimeOfDay >= startTime &&
            time.TimeOfDay <= endTime;
    }

}

(Note: This code assumes that if start == end, then it covers all times. You made a comment to this effect on another post)

For example, to check if it's between 5 AM and 9:30 PM

IsTimeOfDayBetween(someTime, new TimeSpan(5, 0, 0), new TimeSpan(21, 30, 0))

If startTime and endTime are DateTimes, you could say

IsTimeOfDayBetween(someTime, startTime.TimeOfDay, endTime.TimeOfDay)
Daniel LeCheminant
starttime and endtime are datetime also, not timespan
scottm
In my application
scottm
@scotty - Right, I understand now :] I've provided an example to show how you could do that (the second one)
Daniel LeCheminant
@ daniel - doesn't work. Try with a start time of 11pm, stop of 9am and a current time of 11:01pm. 11:01pm is between 11pm and 9am
scottm
Daniel LeCheminant
That's working. Thanks
scottm
A: 

I'm assuming you are saving the start and end times in the applications configuration file, so all you basically have to do is have your application set a flag to "on" when the "start time" occurs and set it to "off" when the stop time occurs.

That way you don't have to be constantly checking if "now" is "between start and end" times.

Ron

Ron Savage
The user set's the on and off time in a configuration file. They enter something like "start:8:00pm;end:6:00am". The application is a service that runs all the time, on some users' computers, they will enter 8:00am-8:00am indicating is has all 24 hours a day available on that system.
scottm
Exactly - and since the service is running all the time, it just has to turn itself "on" at 8pm ... then process stuff if "on" ... then at 6am turn itself "off" (set a flag). Thus no need for the "between" math.
Ron Savage
A: 

if (current >= start && current <= stop)

(or without = )

I think that is all you need?

the midnight boundary is a red herring - all you need to know is the status of both of the comparisons.

If you do things like handle weekends differently then you have other logic, but the basic compare is simple.

Tim
A: 

So I assume from the question that you want to know if given a start time and end time for a day (not including the actual date, i.e., 1/1/1900 or something like that) to see if another time is with the time specified by start and end. E.g., if start is 9pm and end is 9am, accept 10pm but reject 10am.

You can do this either per time range types (times are equal, end is after start, end is before start) which is simple:

if (end==start) return true
else if (end>start) return start<=time && time<=end
else return !(time>end && time<start)

Or you can extend the range of start and end such that end is always after start as such:

if (end<=start) end += <24 hours>
if (time<start) time+= <24 hours>
return time<=end
MSN