C++获取时间函数

https://github.com/yangzijianGit/Time

#include <Windows.h>
#include <time.h>
#include <winbase.h>

unsigned long long GetCurrentTimeMsec()
{
#ifdef _WIN32
    struct timeval tv;
    time_t clock;
    struct tm tm;
    SYSTEMTIME wtm;
    GetLocalTime(&wtm);
    tm.tm_year = wtm.wYear - 1900;
    tm.tm_mon = wtm.wMonth - 1;
    tm.tm_mday = wtm.wDay;
    tm.tm_hour = wtm.wHour;
    tm.tm_min = wtm.wMinute;
    tm.tm_sec = wtm.wSecond;
    tm.tm_isdst = -1;
    clock = mktime(&tm);
    tv.tv_sec = clock;
    tv.tv_usec = wtm.wMilliseconds * 1000;
    return ((unsigned long long)tv.tv_sec * 1000 + (unsigned long long)tv.tv_usec / 1000);
#else
    struct timeval tv;
    gettimeofday(&tv, NULL);
    return ((unsigned long long)tv.tv_sec * 1000 + (unsigned long long)tv.tv_usec / 1000);
#endif
}

unsigned long long GetCurrentTimeMsec2()
{
    return (unsigned long long) clock();
}

  clock()函数是获取当前程序启动到现在的时间, 而GetLocalTime()获取本地时间,根据需求自行选择。

 

posted @ 2021-01-25 17:51  BackSword  阅读(418)  评论(0编辑  收藏  举报