Arduino (RTC)time scheduled relay on and off(TIMER)


Am having problem with my project ,I use the RTC timer to trigger the relay on and off But problem is when i set the ON time lesser than OFF time (i,e for example ON time 2:15 hrs OFF time 4:15 hrs) below codes work perfectly but when i set the ON time greater than OFF time (i,e or example ON time 22:15 hrs OFF time 2:15 hrs) below code not working and my relay not working ,I think my relay function is not correct please kindly suggest and resolve this problem

Your code is not exclusive, meaning it doesnt take care of the current state of the device and keeps checking the statements one after another.
So the last true statement gets executed.

You should use states or a flag to check in what state the device is and take action.
Something like,

        case ON:
            if(now > OFFTime) DeviceState = OFF;
        case OFF:
            if(now > ONTime) DeviceState = ON;

This way you will avoid checking for a condition which is redundant. You only check the event that is necessary to move to the next state.
P.S : This is just a pseudo code.

Sir can you send me complete code for project arduino rtc time scheduled relay on and off timer