C++中的错误时间

5 c++ windows timezone

// Simple program to get the date and time on Windows
//  It compiles and works fine but displays the wrong hour!


// Using Visual C++ 2008 Express on XP SP2
#include <Windows.h>
#include <iostream>
using namespace std;


void main()
{
     SYSTEMTIME st;
     GetSystemTime(&st);

     cout << "Year : " << st.wYear << "\n";
     cout << "Month : " << st.wMonth << "\n";
     cout << "Day : " << st.wDay << "\n";

     // The following line displays the wrong hour, off by 4 hours. 
         // What gives?
     cout << "Hour : " << st.wHour << "\n";
     cout << "Minute : " << st.wMinute << "\n";
     cout << "Second : " << st.wSecond << "\n";
}

// TIA guys!
// -- Bert
Run Code Online (Sandbox Code Playgroud)

Mik*_*all 17

根据文档,时间以UTC为单位.链接在这里

当地时间你想要GetLocalTime()


z *_* - 1

您是否检查过您获得的时间是否位于正确的时区?

Windows 也有一个getlocaltime函数,它应该返回您所在时区的正确时间。