且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何使用Win32 API的时区之间进行转换?

更新时间:2023-11-09 13:23:16

在这个看看:

http://weseetips.com/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。请参阅 MSDN 和How我在Win32中获取特定的TIME_ZONE_INFORMATION结构?

Well unfortunately you cannot do that with the win32 API. Refer to MSDN and How do I get a specific TIME_ZONE_INFORMATION struct in Win32?

您要么需要创建一个空的变量,并在手工填写,或者使用标准C库时间

You will either need to create an empty variable and fill it in manually, or use the standard C time library.