1)获取当前时间
string CurrentTimeString()
{
using namespace std::chrono;
struct tm tstruct;
char buf[80];
auto tp = system_clock::now();
auto now = system_clock::to_time_t(tp);
int size = localtime_s(&tstruct,&now);
size_t written = strftime(buf, sizeof(buf), "%X", &tstruct);
if (ratio_less<system_clock::period, seconds::period>::value &&
written && (sizeof(buf) - written) > 5) {
auto tp_secs =
time_point_cast<seconds>(tp);
auto millis =
duration_cast<milliseconds>(tp - tp_secs).count();
sprintf_s(buf + written, sizeof(buf) - written, ".%03u",
static_cast<unsigned>(millis));
}
return buf;
}
2)获取当前日期
string CurrentDateTimeString()
{
time_t now = time(0);
struct tm tstruct;
char buf[80];
localtime_s(&tstruct,&now);
strftime(buf, sizeof(buf), "%Y-%m-%d, %X", &tstruct);
return buf;
}