linux c获取系统时间戳

#include
#include 
#include 
#include 
#include 

int main(){
    struct timeval tv;
    gettimeofday(&tv,NULL);
    printf("second:%ld\n",tv.tv_sec);  //
    printf("millisecond:%ld\n",tv.tv_sec*1000 + tv.tv_usec/1000);  //毫秒
    printf("microsecond:%ld\n",tv.tv_sec*1000000 + tv.tv_usec);  //微秒

    sleep(3); // 为方便观看,让程序睡三秒后对比
    std::cout << "3s later:" << std::endl;

    gettimeofday(&tv,NULL);
    printf("second:%ld\n",tv.tv_sec);  //
    printf("millisecond:%ld\n",tv.tv_sec*1000 + tv.tv_usec/1000);  //毫秒
    printf("microsecond:%ld\n",tv.tv_sec*1000000 + tv.tv_usec);  //微秒
    return 0;
}

上面的结构体系统已经定义好了,如下:

struct timeval{ 
 long tv_sec; //
long tv_usec; //微秒 
}; 

 

转载于:https://www.cnblogs.com/airduce/p/10136806.html

你可能感兴趣的:(linux c获取系统时间戳)