C ++中的错误时间

时间:2009-05-04 19:13:56

标签: 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

3 个答案:

答案 0 :(得分:17)

根据文档,时间以UTC为单位。链接 HERE

当地时间你需要 GetLocalTime()

答案 1 :(得分:3)

GetSystemTime()以UTC格式返回当前时间(请参阅documentation)。如果你在EST(当DST有效时是UTC-4),那么它将返回当前时间+4小时。

答案 2 :(得分:1)

您是否检查过您的时间是否在正确的时区?

Windows也有getlocaltime功能,应该在您的时区中返回正确的时间。