tags:

views:

489

answers:

2

Total newbie question here; I apologize in advance.

Suppose I have a daemon written in C that wakes itself up every five minutes or so, does some processing if there's anything in its input queue, and then goes back to sleep. Now suppose there is some processing that it only has to do after a certain (configurable) time--say, 2 pm (and before midnight).

In C, what is the quickest, best way to get the current time's hour into an int variable, so that it can easily be checked against--to determine if, in fact, it is after 2pm on today?

+8  A: 

localtime. See http://linux.die.net/man/3/localtime

struct tm *tm_struct = localtime(time(NULL));

int hour = tm_struct->tm_hour;
Paul Tomblin
+5  A: 
printf("the hour is %d\n", localtime(time(NULL))->tm_hour);

This relies on the fact that localtime() returns a pointer to static storage.

unwind