微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何用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时间库.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐