前一段時間工作需要用到毫秒級的時間戳,就百度了一下,網上都有,現在我把它封裝一下給大家方便使用。
先上代碼:(我使用的IDE:VS2017)
#include <windows.h>
#include <stdio.h>
#include <iostream>
#include <sys/timeb.h>
#include <string>
using namespace std;
long long getTimeStamp()
{
timeb t;
ftime(&t);
return t.time * 1000 + t.millitm;
}
void func()
{
//獲取毫秒級時間操作
SYSTEMTIME sys;
GetLocalTime(&sys);
char shijianchuo[50];
char showtime[50];
sprintf_s(shijianchuo, "%04d%02d%02d%02d%02d%02d%02d"
, sys.wYear, sys.wMonth, sys.wDay
, sys.wHour, sys.wMinute, sys.wSecond, sys.wMilliseconds);
sprintf_s(showtime, "%04d-%02d-%02d %02d:%02d:%02d:%02d"
, sys.wYear, sys.wMonth, sys.wDay
, sys.wHour, sys.wMinute, sys.wSecond, sys.wMilliseconds);
cout << shijianchuo << endl;
cout << showtime << endl;
}
int main(void)
{
func();
string unix;
unix= to_string(getTimeStamp());
cout << unix << endl;
return 0;
}
上面是時間戳形式的和平時時鐘顯示的時間還有unix時間。