windows 如何使用 win32 API 在时区之间进行转换?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/597554/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to convert between timezones with win32 API?
提问by grom
I have date strings such as 2009-02-28 15:40:05 AEDSTand want to convert it into SYSTEMTIME structure. So far I have:
我有日期字符串,例如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);
However my local timezone is not AEDST. So I need to be able to specify the timezone when converting to UTC.
但是我当地的时区不是 AEDST。所以我需要能够在转换为 UTC 时指定时区。
回答by uzbones
Take a look at this:
看看这个:
// 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 );
Question: How do you get the TIME_TIMEZONE_INFORMATION for a specific timezone?
问题:如何获取特定时区的 TIME_TIMEZONE_INFORMATION?
Well unfortunately you cannot do that with the win32 API. Refer to MSDNand How do I get a specific TIME_ZONE_INFORMATION struct in Win32?
不幸的是,你不能用 win32 API 做到这一点。请参阅MSDN和如何在 Win32 中获取特定的 TIME_ZONE_INFORMATION 结构?
You will either need to create an empty variable and fill it in manually, or use the standard C time library.
您将需要创建一个空变量并手动填充它,或者使用标准 C 时间库。
回答by Franci Penov
Have you looked at the TzSpecificLocalTimeToSystemTime
Win32 API?
你看过TzSpecificLocalTimeToSystemTime
Win32 API 吗?