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);
}