Given a start time and an interval in minutes, determine if current time is an interval

1.1k Views Asked by At

I'm working in VB.NET, but just looking for a formula that will give me the following:

Say I have a process that needs to be launched every x minutes between start time and end time. Every minute I need to determine if the process needs to be launched.

So if I have the following:

StartTime = 8:00:00 AM
EndTime = 11:00:00 PM
IntervalMinutes = 7

I have a timer set to fire every 1 minute. I need to determine if the current time is time to launch the process.

Currently I just use a loop that adds IntervalMinutes to StartTime and compares it to the current time and EndTime. If StartTime = CurrentTime then launch. If StartTime > End Time then exit loop. I know it's clunky but it works. However as it gets later in the day, it has to iterate through a lot more minutes. I know there has to be a formula for this but my brain is dead from searching and thinking.

2

There are 2 best solutions below

2
On BEST ANSWER

My pseudo modulus operation:

float tolerance = 0.0001f;

if((CurTime - StartTime) % IntervalMinutes <= tolerance)
{
    // Do something
}
0
On

You could pre-calcualte all of the launch times between StartTime and EndTime on app start, put them in a List then each time the timer fires you only need to check if the current time == launchList[0]. If it does launch and remove that entry from the list.