c++ 获取时间

1 当前系统时间

  秒级别:

#include<ctime>
//1

time_t seconds;

  seconds = time(NULL);
  printf("自 1970-01-01 起的小时数 = %ld\n", seconds/3600);
// 2
	time_t tt;
	char * str= ctime(&tt);
	qDebug()<<str;
//Sat Dec 29 16:39:12 2018

毫秒级别:

#include <windows.h>

SYSTEMTIME sys;
	GetLocalTime(&sys);
qDebug() << sys.wYear << sys.wMonth << sys.wDay << sys.wHour << sys.wMinute << sys.wSecond << sys.wMilliseconds << sys.wDayOfWeek;

2

 #include <sys\timeb.h>
//linux 

struct timeval
{
     __time_t tv_sec;                /* Seconds. */
     __suseconds_t tv_usec;      /* Microseconds. */
};
其中,tv_sec为Epoch(1970-1-1零点零分)到创建struct timeval时的秒数,tv_usec为微秒数,即秒后面的零头。

struct timeval tv;  
   gettimeofday(&tv,NULL);  
   return tv.tv_sec * 1000 + tv.tv_usec / 1000; 

2时间戳

#include<ctime>

	clock_t start, ends;

	
	start = clock();
	
	ends = clock();
//相减就是时间差---ms
	qDebug() << ends - start;

猜你喜欢

转载自blog.csdn.net/U201414786/article/details/85340670
今日推荐