tags:

views:

81

answers:

2

I have a frequency and a time range with which the frequency has to be executed. For eg, if the current time is 2AM and frequency is 360 mins, it has to assume that since it just passed midnight , the task should execute at 360 mins after 12 means 6 AM

the current time is server start time from midnight. so if server starts at 5AM and frequncy is 360 the task should be run at 6AM

but if the task is 5am and frequency is 240, since it has already passed 240 mins from midnight the next run should be at 8AM

If the server starts at 10AM and frequency is 300 then starting from midnight one run is elapsed at 5AM ans next as calculated at 10AM so will start immediately at 10AM

The time ranges are divded into 4 quarters, 12AM to 6AM to 12PM ,12PM to 6PM and 6PM to 12AM next day This is the code below which is working fine for 240 and 360 frequency but going wrong when its 60 frequency. Some values which are provided below:

 stNow----serverTime
sElapsed ---time elapsed from midnight
currFreq ----frequecy 
    protected int _getInitWorkerTime()
    {
         int vRun=0;
         STime stNow= new STime(PWMSystem.newDate());
         int currFreq = _findAlertFrequency()/60;
         int sElapsed =Constants.MINUTES_PER_DAY-stNow.elapsedMinutes(STime.midnight);
         _log.error("now time as 24------"+stNow.getHourNo24());
         _log.error("now currFreq------"+currFreq);
         _log.error("now sElapsed-----"+sElapsed);
         if(sElapsed == 1440)
             sElapsed=0;
         if(stNow.getHourNo24()>=0  && stNow.getHourNo24()<=6)
         {
             _log.error("now time as 24-inside cond-1-----"+stNow.getHourNo24());
                 if(currFreq>sElapsed)
                     vRun= currFreq-sElapsed;
                 else
                     vRun= -(currFreq-sElapsed);
         }
         if(stNow.getHourNo24()>6 && stNow.getHourNo24()<=12)
         {
             _log.error("now time as 24-inside cond-2-----"+stNow.getHourNo24());
             if(currFreq>sElapsed)
                  vRun=360-(currFreq-sElapsed);
              else
                  vRun=360-(-(currFreq-sElapsed));
         }
         if(stNow.getHourNo24()>12 && stNow.getHourNo24()<=18)
         {
             _log.error("now time as 24-inside cond-3-----"+stNow.getHourNo24());
             if(currFreq>sElapsed)
                  vRun=720-(currFreq-sElapsed);
              else
                  vRun=720-(-(currFreq-sElapsed));
         }
         if(stNow.getHourNo24()>18 && (stNow.getHourNo24()<=24 ||stNow.getHourNo24()<=0))
         {
             _log.error("now time as 24-inside cond-4-----"+stNow.getHourNo24());
             if(currFreq>sElapsed)
                  vRun=1080-(currFreq-sElapsed);
              else
                  vRun=1080-(-(currFreq-sElapsed));
         }
            // vRun=_MAX_FREQUENCY_DELAY_IN_SEC+ sElapsed*60+_findAlertFrequency()+_BOOT_DELAY_SECONDS_START;*/
        //vRun=stNow.elapsedMinutes(STime.midnight)*60+_findAlertFrequency()+_BOOT_DELAY_SECONDS_START;
        return (vRun*60 + _BOOT_DELAY_SECONDS_START);       
    }
+2  A: 

Isn't it just a case of saying:

int nextTime = ((timeSinceMidnight / (period-1)) + 1) * period;

(Where the division is done with integer arithmetic. The "period-1" bit is to catch the situation where it's exactly on time, such as the third test case below.)

Sample situations:

Period      Minutes since midnight        Result
360         120 (2am)                     360 (6am)
240         300 (5am)                     480 (8am)
300         600 (10am)                    600 (10am)    

All looks correct to me. You mention dividing time ranges into quarters, but I don't see how that's desirable or relevant... aren't you really just talking about "minutes since midnight" in every case? Note that the way I've expressed it, you could change it to "seconds since midnight" or "milliseconds since midnight" - or even "minutes since the start of the month"; so long as you have a consistent origin and time unit, it should work out fine.

(Note that I've referred to period rather than frequency - normally a higher frequency means something happens more often, not less.)

Jon Skeet
Taking clue from your response i made some modifications and it workedThanks
GustlyWind
A: 

I found out to be..

protected int _getInitWorkerTime()
{   
        int vRun = 0;
        STime stNow = new STime(PWMSystem.newDate());
        int currFreq = _findAlertFrequency() / Constants.SECONDS_PER_MINUTE;
        int sElapsed = Constants.MINUTES_PER_DAY- stNow.elapsedMinutes(STime.midnight);
        int runsCompleted = sElapsed / currFreq;
        int remainLeft = (runsCompleted + 1) * currFreq;


                if (remainLeft > sElapsed)
                    vRun = remainLeft - sElapsed;
                else
                    vRun = sElapsed - remainLeft;


return (vRun * 60 + _BOOT_DELAY_SECONDS_START);
GustlyWind
This still looks like an awful lot of code... are you really *sure* it needs to be this complex?
Jon Skeet
I think this is a more refined one.Thanks for suggestion!!!
GustlyWind