If able, use sleep()
to pause.
Use time()
and localtime()
to determine nap time.
#include <unistd.h>
#include <time.h>
#include <stdio.h>
int main() {
while (1) {
time_t Now;
if (time(&Now) == -1) {
return -1;
}
struct tm tm;
tm = *localtime(&Now);
tm.tm_mday++;
tm.tm_hour = 0;
tm.tm_min = 0;
tm.tm_sec = 0;
tm.tm_isdst = -1;
time_t Then;
Then = mktime(&tm);
if (Then == -1) {
return -1;
}
if (Then <= Now) {
return -1; // Time moving backwards - Hmmmm?
}
unsigned int NapTime = (unsigned int) (Then - Now);
printf("Going to sleep %u\n", NapTime );
fflush(stdout); // Let's empty the buffers before nap time
if (0 != sleep(NapTime)) {
return -1; // trouble sleeping
}
// Done sleeping!
printf("Midnight\n");
fflush(stdout);
sleep(10);
}
}