如何用win32 API转换时区?

我有日期字符串,如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时间库.

相关文章

Windows2012R2备用域控搭建 前置操作 域控主域控的主dns:自...
主域控角色迁移和夺取(转载) 转载自:http://yupeizhi.blo...
Windows2012R2 NTP时间同步 Windows2012R2里没有了internet时...
Windows注册表操作基础代码 Windows下对注册表进行操作使用的...
黑客常用WinAPI函数整理之前的博客写了很多关于Windows编程的...
一个简单的Windows Socket可复用框架说起网络编程,无非是建...