struct timeval tv 存储大小未知

void main()
{
    struct timeval tv;
   

    gettimeofday( &tv,NULL );
     
    
    printf( "sec is %d,usec is %d\n",tv.tv_sec,tv.tv_usec );
    
}



利用gcc gettimeofday.c -o gettimeofday  编译  提示错误tv的存储大小未知

解决办法:

       没加头文件。加上这句话再编译试试。
        #include <sys/time.h>

    其实提示,存储大小未知多半是没有头文件,再或者就是结构体的名字写错了

     顺便写一下timeval 的定义,

一、struct timespec 定义:

typedef long time_t;
#ifndef _TIMESPEC
#define _TIMESPEC
struct timespec {
time_t tv_sec; // seconds 
long tv_nsec; // and nanoseconds 
};
#endif
struct timespec有两个成员,一个是秒,一个是纳秒, 所以最高精确度是纳秒。
一般由函数int clock_gettime(clockid_t, struct timespec *)获取特定时钟的时间,常用如下4种时钟:
CLOCK_REALTIME 统当前时间,从1970年1.1日算起
CLOCK_MONOTONIC 系统的启动时间,不能被设置
CLOCK_PROCESS_CPUTIME_ID 本进程运行时间
CLOCK_THREAD_CPUTIME_ID 本线程运行时间
struct tm *localtime(const time_t *clock);  //线程不安全
struct tm* localtime_r( const time_t* timer, struct tm* result );//线程安全
size_t strftime (char* ptr, size_t maxsize, const char* format,const struct tm* timeptr );

二、struct timeval 定义:

struct timeval {
time_t tv_sec; // seconds 
long tv_usec; // microseconds 
};
struct timezone{ 
int tz_minuteswest; //miniutes west of Greenwich 
int tz_dsttime; //type of DST correction 
};

struct timeval有两个成员,一个是秒,一个是微秒, 所以最高精确度是微秒。
一般由函数int gettimeofday(struct timeval *tv, struct timezone *tz)获取系统的时间 

猜你喜欢

转载自blog.csdn.net/weixin_37569048/article/details/81705349