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.