Linux下C/C++获得当前时间(秒/毫秒)

秒或者毫秒是根据当前时间到1970年1月1日计算,主要用于两个时间戳计算间隔时间

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

int main(int argc, char *argv[])
{
    
    
    struct timeval time;
 
    /* 获取时间,理论到us */
    gettimeofday(&time, NULL);
    std::cout << time.tv_sec << std::endl;  // 获取秒数
    std::cout << time.tv_sec*1000 + time.tv_usec/1000 << std::endl; //获取毫秒
 
    return 0;
}

猜你喜欢

转载自blog.csdn.net/gls_nuaa/article/details/129889500
今日推荐