c - 如何使用 win32 API 在时区之间进行转换?

标签 c windows winapi

我有诸如 2009-02-28 15:40:05 AEDST 之类的日期字符串,我想将其转换为 SYSTEMTIME 结构。到目前为止,我有:

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);

但是我本地的时区不是 AEDST。所以我需要能够在转换为 UTC 时指定时区。

最佳答案

看看这个:

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 );

问题:如何获取特定时区的 TIME_TIMEZONE_INFORMATION?

很遗憾,您不能使用 win32 API 做到这一点。引用MSDNHow do I get a specific TIME_ZONE_INFORMATION struct in Win32?

您需要创建一个空变量并手动填充它,或者使用标准 C 时间库。

关于c - 如何使用 win32 API 在时区之间进行转换?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/597554/

相关文章:

c - C 中的简单连接

java - 如何将 window 带到前面?

multithreading - 德尔福 11.2 : CreateWindowEx fails thread on x64

c++ - WinVerifyTrust 返回 CERT_E_CHAINING

c - RSA_private_decrypt 在 Windows 上导致内存泄漏

python - CCKeyDerivationPBKDF 线程安全吗?

c -/dev/urandom 为 mmap() 提供随机地址

windows - 将文本附加到 Azure 中的 Blob

MySQL 子查询死锁(某种)

c++ - BM_GETCHECK 不工作