Getting the current hour in C using time.h

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

time_t now = time(NULL);
struct tm *tm_struct = localtime(&now);

int hour = tm_struct->tm_hour;

The call localtime(time(NULL)) will never work. The return value of time() is a time_t, and the first argument of localtime is a time_t*. Neither is the accepted answer, nor is the one with printf correct.

time_t now;
struct tm *now_tm;
int hour;

now = time(NULL);
now_tm = localtime(&now);
hour = now_tm->tm_hour;

printf("the hour is %d\n", localtime(time(NULL))->tm_hour);

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

Tags:

Time

C