Calculating offsets

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Airslash
    New Member
    • Nov 2007
    • 221

    Calculating offsets

    Hello,

    I'm currently stuggling with an algorithm for determing the delay between timer calls. Allow me to describe the problem first:

    A user is able to setup a batch command that will either run at a specified interval or only once at the specific point in time. Due the required flexibility, the user should only be required to set the interval. If for example the user sets an interval of 10 minutes (600sec), the command should be executed at 12.00, 12.10, 12.20 and so on.

    The problem currently is that if the user created the command at 12.27, how I can calculate the next offset of the command and calculate the delay in second between Now and the next "interval".

    Calculating the offset between the two times is not the problem, I'm just looking for an algorithm that will give me the next interval based upon the current datetime.

    Extra issue: The interval can be increased up to 24hours...
  • jkmyoung
    Recognized Expert Top Contributor
    • Mar 2006
    • 2057

    #2
    I'm not understanding why you can't just go
    eventTime:=now( ) + interval

    Comment

    • Airslash
      New Member
      • Nov 2007
      • 221

      #3
      Because, let's say the interval is 10 minutes.
      From the assignment, that would mean each interval is at the following points:
      10.00
      10.10
      10.20
      10.30
      10.40
      10.50
      11.00

      Assuming we take the logic you provided, and the current time is 10.45, I would end up on 10.55, missing the first point and missing all subsequent points in time.

      Comment

      • jkmyoung
        Recognized Expert Top Contributor
        • Mar 2006
        • 2057

        #4
        Where do you get the base time (10.00 or 12.00) from? Do you simply truncate the current time to the nearest earlier hour?
        What if the interval is strange like 324032 ms?

        Code:
        time:= now() + interval;
        while(time < MaxTimeValue)
        {
          addEventTime(time);
          time+= interval;
        }

        Comment

        • Oralloy
          Recognized Expert Contributor
          • Jun 2010
          • 988

          #5
          say it's 12:03:00 (hh:mm:ss, three minutes after noon), and my interval is (1:7:00) - what should be the start time?

          Still, your basic problem is not all that difficult.

          Assume that 0:0:1 is your minimum interval.

          Code:
          Set startInterval = # of seconds into epoch
          
          Convert now() into integer seconds since startInterval, call it nowSeconds
          
          Convert interval into integer seconds.  (Assumed > 0)  call it intervalSeconds
          
          Set worstCase = nowSeconds + intervalSeconds - 1
          
          Set intervalsUntilStart = (worstCase / intervalSeconds)
          
          Set secondsUntilStart = intervalsUntilStart * intervalSeconds
          
          Set startTime = startInterval + secondsUntilStart
          Simple integer division gets you where you need to be.

          I think there's a fancy floating point rounding method in Double or Float that does pretty much the same thing. Look it up, it's a sexier solution, if it exists.

          Code:
          start = Double.RoundUp(now(), interval)

          Comment

          • Airslash
            New Member
            • Nov 2007
            • 221

            #6
            cheers for the replies, will definitly check em out when I have a bit more time.
            To get the start time, it's always the hour at which the "question" is asked. So basicly yes, just trunc the current hour.

            Comment

            Working...