For simplicity of the discussion lets say the time is stored in as some type time.
The problem is that the clock rolls over from 23:59 to 00:00 and we do not know if on or off comes first.
The answer is to take the time at wake up and count backwards to find if the last thing that should have been done is a ON or OFF.
I would do this by writing a function that computes the diff between a past time and the current time.
Code:
time timeDiff(time curTime, time eventTime)
{
if (cutTime >= eventTime)
{
return (curTime - eventTime);
}
else
{
return(24Hours - (eventTime-curTime));
}
}
Call timeDiff ON and OFF to find which has the lowest value. Then set the pin to that state.
This code can not be directly used as we have not defined the type time and the if statment will not work with defined types.
HTH