C++ 获取时间

#define _CRT_SECURE_NO_WARNINGS
#include <stdint.h>
#include <stdio.h>
#ifdef _WIN32
#include <time.h>
#include<windows.h>
#else
#include <sys/time.h>
#include <unistd.h>
#endif

uint64_t GetCurrentTimerMS()
{
    uint64_t nTimer = 0;
#ifdef _WIN32
    SYSTEMTIME currentTime;
    GetLocalTime(&currentTime);
    tm temptm = { currentTime.wSecond,
        currentTime.wMinute,
        currentTime.wHour,
        currentTime.wDay,
        currentTime.wMonth - 1,
        currentTime.wYear - 1900
    };
    nTimer = mktime(&temptm) * 1000 + currentTime.wMilliseconds;
#else
    struct timeval tv;
    gettimeofday(&tv, NULL);
    // printf("second:%ld\n",tv.tv_sec);  //
    nTimer = tv.tv_sec * 1000 + tv.tv_usec / 1000;
#endif
    return nTimer;
}

int main()
{
    char szTimer[64];
    uint64_t nTimer = -1;
    nTimer = GetCurrentTimerMS(); //不带参数
    printf("millisecond:\t%llu\n\n", nTimer);  //毫秒
    return 0;
}

 

posted @ 2021-10-25 14:36  冰糖葫芦很乖  阅读(67)  评论(0编辑  收藏  举报