1. 获取当前时间,可精确到秒(Windows)
#include <iostream>
#include <ctime>
int main(){
time_t tv;
tv = time(NULL);//time(&tv); get current time;
std::cout << tv << std::endl;//距离1970-01-01 00:00:00经历的秒数
std::cout << ctime(&tv) << std::endl;//显示当前时间
tm *local;
local = localtime(&tv);
std::cout << asctime(local) << std::endl;//显示当前时
return 0;
}
2.精确到毫秒
(1). <windows.h>
#include <iostream>
#include <windows.h>
int main(){
DWORD t_start, t_end;
t_start = GetTickCount();//从操作系统启动所经过(elapsed)的毫秒数,它的返回值是DWORD。
Sleep(3000);
t_end = GetTickCount();
std::cout << t_end - t_start << std::endl;
return 0;
}
(2). <timeb.h>
#include <iostream>
#include <sys\timeb.h>
long long getSystemTime(){
timeb t;
ftime(&t);
return t.time * 1000 + t.millitm;
}
int main(){
long long t_start = getSystemTime();
Sleep(3000);
long long t_end = getSystemTime();
std::cout << t_end - t_start << std::endl;
return 0;
}