1

I am working on a system that starts a process based of a set time value (startHour). The user sets the start hour and can only select values from 00-23. I have been asked to start the same process at 6 hour intervals so that it occurs 4 times during the day.

My initial though is to add three more variable, secondStartHour, thirdStartHour and fourthStartHour. secondStartHour would be assigned a value 6 hours ahead of the startHour, thirdstartHour a value 12 hours ahead of startHour and fourthStartHour would be assigned a value 18 hours ahead of start hour. I could then use any of the startHour variables to trigger the process.

I wrote some code (below) that will work but I'm wondering if there is a better, more efficient way of doing it.

//set up secondStartHour 6 hours ahead of startHour
if (startHour <= 5)
{
    secondStartHour = startHour - 18;
}
else
{
    secondStartHour = startHour + 6;
}

//set up thirdStartHour 12 hours ahead of startHour
if (startHour <= 11)
{
    thirdStartHour = startHour - 12;
}
else
{
    thirdStartHour = startHour + 12;
}

//set up fourthStartHour 18 hours ahead of startHour
if (startHour <= 17)
{
    fourthStartHour = startHour - 6;
}
else
{
    fourthStartHour = startHour + 18;
}

I had to write the code in the way I did above because I am working with a 24 hour clock so in some cases adding time to a startHour will actually reduce the value. By this I mean if startHour is 23 (23:00) and I need to add 6 hours to it to get secondStartHour the result will be 5 (05:00) so I can't simply write code to say secondStartHour = startHour + 6. The code just seems too long winded and messy but I cant think of a better way to write it. I could use a switch case statement but then I would have 24 cases with assignments for each of the new variables in each case so that seems worse, any ideas would be appreciated.

  • If you think about it - if it happens 4 times during the day at regular intervals, you actually can only have 6 start times... – Majenko Dec 18 '18 at 14:34
  • Hi Majenko, thank you for the reply but this is not the case as the user selects the time period at which they want the first process to occur – user1649972 Dec 19 '18 at 10:46
  • But that's the opposite of what you say in your question: "I have been asked to start the same process at 6 hour intervals so that it occurs 4 times during the day." – Majenko Dec 19 '18 at 10:50
  • Yes it is every 6 hours but it is based on the fact that "The user sets the start hour and can only select values from 00-23" So it is every 6 hours but changes based on the selected time – user1649972 Dec 19 '18 at 14:24
  • Yes, but what is the difference between them entering 4 and entering 10? – Majenko Dec 19 '18 at 14:33
1

Instead of comparing, you can use the modulo operator:

 secondStartHour = (startHour +  6) % 24;

And similar for the others:

 thirdStartHour  = (startHour + 12) % 24;
 fourthStartHour = (startHour + 18) % 24;

While xxxStartHour >= 24, 24 will be removed until it is within range [0, 24).

If you need an offset, you also can add some hours of course after the module (I don't fully understand your example which can have values >= 24.

Also, if you need to do the same task every 6 hours, you can use with one time and 'restart' it every 6 hours (and maybe taking summer/winter time into account).

  • thank you so much Michel, the mod operator is exactly what I am after, it will make the code much shorted and cleaner. Thanks again – user1649972 Dec 19 '18 at 10:48
  • You are welcome. You can use it in many cases which involves something periodical. – Michel Keijzers Dec 19 '18 at 11:54
1

If you think about it logically you actually only have 6 start hours. Things can only start at a time between 0 and 5 am inclusive. Everything else is a multiple of 6 hours on from that start. Any time provided that's past 5am is actually a future time of an earlier start time.

So you can just "modulus" the given time so it is one of the first 6 hours of the day:

startHour = startHour % 6;

And then add 6 each time for the other hours:

secondStartHour = startHour + 6;
thirdStartHour = startHour + 12;
fourthStartHour = startHour + 18;

So your hour arrangements can only be:

0  6  12  18
1  7  13  19
2  8  14  20
3  9  15  21
4 10  16  22
5 11  17  23

If you provide the hour 15 as a start time, 15 % 6 is 3 (divide 15 by 6, you get 2 with 3 remaining), so you are in the time pattern "3 9 15 21".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.