C/C++ 毫秒时间戳

记录一下,方便取用

#include <thread>
#ifdef _WIN32
#include <Windows.h>
const char* timenow()
{
    static thread_local char str[32];
    SYSTEMTIME st;
    GetLocalTime(&st);
    snprintf(str,32,"%d-%d-%d %02d:%02d:%02d.%03d",st.wYear, st.wMonth, st.wDay, st.wHour, st.wMinute, st.wSecond, st.wMilliseconds);
    return str;
}
#else
#include <sys/time.h>
const char* timenow()
{
    static thread_local char str[32];
    struct timeval time;
    gettimeofday(&time, NULL);
    struct tm* p = localtime(&(time.tv_sec));
    snprintf(str, 32, "%d-%d-%d %02d:%02d:%02d.%03ld", 1900 + p->tm_year, 1 + p->tm_mon, p->tm_mday, p->tm_hour, p->tm_min, p->tm_sec, time.tv_usec / 1000);
    return str;
}
#endif // _WIN32
#define TIMENOW timenow()
posted @ 2022-10-19 00:22  SupperMary  阅读(371)  评论(0编辑  收藏  举报