tm t1 has incomplete type and cannot be defined

shane picture shane · Nov 29, 2011 · Viewed 8.9k times · Source

I have to write a program that calls sleep(60) in an infinite loop. Every five times through the loop I have to fetch the current time-of-day and print the tm_sec field.

This is what I have written:

#include <stdio.h>
#include <sys/time.h>
#include <unistd.h>

int main()
{
    struct tm t1;
    int i=0;
    for(;;)
    {
        sleep(60);
        if(i%5==0)
            {
                gettimeofday(&t1,NULL);
                printf("%d\n",t1.tm_sec);
            }
        i++;
    }
}

I'm getting an error saying aggregate tm t1 has incomplete type and cannot be defined.

I don't know what I'm doing wrong.

Answer

rob mayoff picture rob mayoff · Nov 29, 2011

You want struct timeval, not struct tm. Try this:

struct timeval t1;

Also, you want t1.tv_sec, not t1.tm_sec.