The simplest solution is possible because your start time (9:00) and end time (17:00) are whole hours. For that particular example you just need this:
if ((hour >= 9) && (hour < 17)
// turn on lamp
// turn off lamp
That will keep the light on any time from 9:00:00 through 16:59:59. It will turn on right at 9:00:00, and turn off just as the time switches to 17:00:00 (assuming you are reading the time and making the comparison often enough.)
But I’m guessing you want a more general solution, one that can switch at other times and not just exactly on the hour.
JosAH has a good idea, but I’m not sure why he is multiplying the hour term by 24? Drop the 24 part of it, and you have a solution that works down to one second precision (lets you set a threshold time of 9:14:27, for example.)
But I’m going to guess that you don’t need that much precision. If you just want to switch at a certain hour and minute (with seconds always 00) you can just do this:
int makeTime(int hour, int minute)
return min + (hour * 60);
An integer is good enough here, as the maximum value is 59 + (23 * 60) or 1439. There is no need for a long (which takes more storage and processing time.)
To use this function, convert the current time and threshold times, and then do a simple range check:
int start = makeTime(9, 0);
int end = makeTime(17, 0);
int current = makeTime(hour, min);
if ((current >= start) && (current < end))
// light on
// light off
This code will still turn on at 9:00:00 and turn off at 17:00:00. But it could just as easily turn on at 8:30:00 and off at 17:05:00. It will always switch as the seconds change to/from zero.
Of course, it’s not necessary to calculate the start and end times every time, those values only need to be updated when they are changed. So it’s OK to have them as globals. Only the current time value needs to be recomputed every time you read the time from the Linux side.