struct timeval结构体

本文详细介绍了Linux中的struct timeval时间结构,该结构包含秒数(tv_sec)和微秒数(tv_usec),并通过示例代码展示了如何使用gettimeofday函数获取当前时间。

struct timeval

struct timeval是在linux中的一个关于时间的定义,在time.h中进行定义,原型是:

struct timeval  
{  
__time_t tv_sec;        /* Seconds. */  
__suseconds_t tv_usec;  /* Microseconds. */  
}; 

其中,tv_sec为Epoch到创建struct timeval的秒数,tv_usec为微秒数。使用demo如下:

#include<iostream>
#include <unistd.h>
#include <sys/time.h>
using namespace std;

int main(int argc,char *argv[]){
    int i;
    struct timeval tv;

    for(i = 0;i < 10; i++) {
        gettimeofday(&tv, NULL);
        cout << tv.tv_sec << " " << tv.tv_usec << endl;
        sleep(1);
    }
    return 0;
}

/* vim: set ts=4 sw=4 sts=4 tw=100 */

运行结果如下:

1510467679 792342
1510467680 792449
1510467681 792503
1510467682 792577
1510467683 792646
1510467684 792691
1510467685 792764
1510467686 792829
1510467687 792896
1510467688 792959
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值