views:

199

answers:

4
+2  Q: 

Wrong hour in C++

// 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
+1  A: 

did you check to see if your time you get is in the correct timezone?

Windows has a getlocaltime function as well, that should return the proper time in your timezone.

yx
+14  A: 

The time is in UTC according to the docs. Link HERE

For local time you want GetLocalTime()

mjmarsh
+2  A: 

GetSystemTime() returns the current time in UTC (see the documentation). If you're in EST (which is UTC-4 when DST is in affect), then it would return the current time + 4 hours.

Andy
A: 

Thanks much guys, sorry about the formatting.