获取精确到毫秒的当前日期时间
学习备忘——获取当前日期时间(精确到毫秒)
#include <iostream>
#include <sys/timeb.h>
#include <time.h>
#include <stdio.h>
using namespace std;
typedef char * PCHAR;
void Now(PCHAR buff);
int main()
{
char buff[128];
Now(buff);
cout << buff << endl;
return 0;
}
void Now(PCHAR buff)
{
struct timeb tp;
struct tm *tm;
ftime(&tp);
tm = localtime(&tp.time);
sprintf(buff, "%.4d-%.2d-%.2d %.2d:%.2d:%.2d.%.3d",
1900+ tm->tm_year,
1 + tm->tm_mon,
tm->tm_yday,
tm->tm_hour,
tm->tm_min,
tm->tm_sec,
tp.millitm);
}