本文介绍了如何使用Win32 API的时区之间进行转换?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有日期字符串,如 2009-02-28 15点40分05秒AEDST 的,并希望将其转换为SYSTEMTIME结构。到目前为止,我有:

I have date strings such as 2009-02-28 15:40:05 AEDST and want to convert it into SYSTEMTIME structure. So far I have:

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时指定的时区。

However my local timezone is not AEDST. So I need to be able to specify the timezone when converting to UTC.

推荐答案

在这个看看:

 // 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。请参阅和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.

这篇关于如何使用Win32 API的时区之间进行转换?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 00:18