2009-02-28 19 views
5

Tengo cadenas de fecha como 2009-02-28 15:40:05 AEDST y quiero convertirlo en estructura SYSTEMTIME. Hasta ahora tengo:Cómo convertir entre zonas horarias con API win32?

SYSTEMTIME st; 
FILETIME ft; 
SecureZeroMemory(&st, sizeof(st)); 
sscanf_s(contents, "%u-%u-%u %u:%u:%u", 
    &st.wYear, 
    &st.wMonth, 
    &st.wDay, 
    &st.wHour, 
    &st.wMinute, 
    &st.wSecond); 
// Timezone correction 
SystemTimeToFileTime(&st, &ft); 
LocalFileTimeToFileTime(&ft, &ft); 
FileTimeToSystemTime(&ft, &st); 

Sin embargo, mi zona horaria local no es AEDST. Por lo tanto, debo poder especificar la zona horaria al convertir a UTC.

Respuesta

7

Tome un vistazo a esto:

https://web.archive.org/web/20140205072348/http://weseetips.com:80/2008/05/28/how-to-convert-local-system-time-to-utc-or-gmt/

// Get the local system time. 
SYSTEMTIME LocalTime = { 0 }; 
GetSystemTime(&LocalTime); 

// Get the timezone info. 
TIME_ZONE_INFORMATION TimeZoneInfo; 
GetTimeZoneInformation(&TimeZoneInfo); 

// Convert local time to UTC. 
SYSTEMTIME GmtTime = { 0 }; 
TzSpecificLocalTimeToSystemTime(&TimeZoneInfo, 
            &LocalTime, 
            &GmtTime); 

// GMT = LocalTime + TimeZoneInfo.Bias 
// TimeZoneInfo.Bias is the difference between local time 
// and GMT in minutes. 

// Local time expressed in terms of GMT bias. 
float TimeZoneDifference = -(float(TimeZoneInfo.Bias)/60); 
CString csLocalTimeInGmt; 
csLocalTimeInGmt.Format(_T("%ld:%ld:%ld + %2.1f Hrs"), 
          GmtTime.wHour, 
          GmtTime.wMinute, 
          GmtTime.wSecond, 
          TimeZoneDifference); 

Pregunta: ¿Cómo se obtiene la TIME_TIMEZONE_INFORMATION para una zona horaria específica?

Desafortunadamente no se puede hacer eso con la API win32. Consulte MSDN y How do I get a specific TIME_ZONE_INFORMATION struct in Win32?

Deberá crear una variable vacía y completarla manualmente, o utilizar la biblioteca de tiempo C estándar.

+0

Bueno, lamentablemente no se puede hacer eso con la API win32 ... ver http://msdn.microsoft.com/en-us/library/ms725481(VS.85).aspx Deberá crear una variable vacía y llenarlo manualmente, o usar la biblioteca de tiempo C estándar. – uzbones

+0

También vea http://stackoverflow.com/questions/466071/how-do-i-get-a-specific-timezoneinformation-struct-in-win32 – uzbones

+0

, esto también requiere XP o superior. TzSpecificLocalTimeToSystemTime no funciona en Win2k – Tim

0

¿Has mirado la API TzSpecificLocalTimeToSystemTime Win32?

+2

Gracias por eso, pero ¿cómo puedo recuperar el TIME_ZONE_INFORMATION para una zona horaria diferente (es decir, AEDST)? – grom

Cuestiones relacionadas