Determining when the hour has rolled over using Time.h library

I'm using the Time.h library in a sketch, and I want to be able to determine if the hour has changed from one run through the sketch to the next, and either do something or don't do something based on this.

Right now, essentially what I'm doing is just setting a variable called "currenthour" to the value of the hour, then putting in a 2000ms delay. Then at the top of the sketch, I check to see if the hour still matches what's stored in "currenthour." Kind of like this.

void loop(){

if (currenthour != hour() )
{
**Do some stuff that is meant to be done only when the hour rolls over
}

**Do some other stuff that's meant to be done with every run through the sketch

currenthour = hour()
delay(2000)
}

The problem is, every now and then, it doesn't seem like the sketch does what it should do when the hour rolls over to the next. I'm guessing that in these instances, the hour is rolling over somewhere in the middle of the sketch, not during the delay, so when it sets "currenthour" to the hour right before the delay, the hour has already rolled over, so after the delay, "currenthour" still matches the current hour.

There must be a better way to check when the hour changes and do something based on this. I know the way I'm doing it now is fairly clumsy.

Why is the delay() there in the first place ?

The delay is there because it really only has to run through the program every one or two seconds. This program does something very slowly and gradually, so it doesn't really matter if it does it over and over again as fast as it can, or if it waits a second or two between each run. I guess mainly it's purpose is to try to catch the hour flipping over.

Actually what I might do is, instead of trying to figure out if the hour changed since last run, I'll just say that whenever the minute is 00 (ie, the hour just rolled over) I'll tell it to perform the stuff that needs to happen when the hour rolls over.

I'll also just have it set some kind of variable to 1 to acknowledge that it already did the stuff so it doesn't continually do it again and again for the first minute of the program. Then, then the minute flips over to 01, I'll set the "Ok, we already did the special stuff for each hour" variable back to 0, so then the next time the minute is 00, it will do the special hour change stuff again.

Does that make sense?

Ess_El_Emm:
The problem is, every now and then, it doesn't seem like the sketch does what it should do when the hour rolls over to the next. I'm guessing that in these instances, the hour is rolling over somewhere in the middle of the sketch, not during the delay, so when it sets "currenthour" to the hour right before the delay, the hour has already rolled over, so after the delay, "currenthour" still matches the current hour.

That's a reasonable guess - that code is vulnerable to that sort of problem. The way to avoid it is to only evaluate the current value of hour() once per iteration.

currentHour = hour();
if(currentHour != previousHour)
{
    // do any other stuff needed when the hour changes
    previousHour = currentHour;
}