C++ 获取当前时间毫秒数(兼容windows和linux)

本博客转自:https://blog.csdn.net/sunxianliang1/article/details/52150365

#ifdef _WIN32
	#include 
	#include 
#else
	#include 
#endif

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
    }
--------------------- 
作者:先亮 
来源:CSDN 
原文:https://blog.csdn.net/sunxianliang1/article/details/52150365 
版权声明:本文为博主原创文章,转载请附上博文链接!

你可能感兴趣的:(c++)