简体   繁体   English

给定开始时间和以分钟为单位的间隔,请确定当前时间是否为间隔

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

I'm working in VB.NET, but just looking for a formula that will give me the following: 我正在VB.NET中工作,但只是在寻找一个可以给我以下内容的公式:

Say I have a process that needs to be launched every x minutes between start time and end time. 假设我有一个流程,需要在开始时间和结束时间之间每隔x分钟启动一次。 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. 我将计时器设置为每1分钟触发一次。 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. 当前,我仅使用一个将IntervalMinutes添加到StartTime并将其与当前时间和EndTime进行比较的循环。 If StartTime = CurrentTime then launch. 如果StartTime = CurrentTime,则启动。 If StartTime > End Time then exit loop. 如果StartTime> End Time,则退出循环。 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. 我知道必须有一个公式,但是我的大脑因搜索和思考而死了。

My pseudo modulus operation: 我的伪模运算:

float tolerance = 0.0001f;

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

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]. 您可以在应用启动时预先计算StartTime和EndTime之间的所有启动时间,将它们放置在List中,然后每次触发计时器时,您只需要检查当前时间是否== launchList [0]。 If it does launch and remove that entry from the list. 如果确实启动,则从列表中删除该条目。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM