C++ windows和linux获取当前时间(精确到毫秒)

windows环境下:

        struct SYSTEMTIME
        {
            boost::uint16_t wYear;
            boost::uint16_t wMonth;
            boost::uint16_t wDayOfWeek;
            boost::uint16_t wDay;
            boost::uint16_t wHour;
            boost::uint16_t wMinute;
            boost::uint16_t wSecond;
            boost::uint16_t wMilliseconds;
        };
#include 
#include 
 
using namespace std;
 
int main(int argc, char* argv[])
{
 
	SYSTEMTIME st = { 0 };  
	GetLocalTime(&st);  //获取当前时间 可精确到ms
	printf("%d-%02d-%02d %02d:%02d:%02d:%02d\n",
		st.wYear,
		st.wMonth,
		st.wDay,
		st.wHour,
		st.wMinute,
		st.wSecond,
        st.wMilliseconds);
}

 

Linux环境下:

C++ windows和linux获取当前时间(精确到毫秒)_第1张图片

#include 
#include 
#include 
#include 
 
int main(int argc, char *argv[])
{
    struct timeval time;
 
    /* 获取时间,理论到us */
    gettimeofday(&time, NULL);
    printf("s: %ld, ms: %ld\n", time.tv_sec, (time.tv_sec*1000 + time.tv_usec/1000));
 
    sleep(3);  //延时
 
    /* 重新获取 */
    gettimeofday(&time, NULL);
    printf("s: %ld, ms: %ld\n", time.tv_sec, (time.tv_sec*1000 + time.tv_usec/1000));
 
 
    exit(0);
}

 

你可能感兴趣的:(Linux,C++,linux,windows,c++)