哇,我以为我知道我的 C++,但这很奇怪
这个函数返回一个无符号整数,所以我认为这意味着我永远不会得到负数,对吧?
该函数确定您比 UTC 早或晚多少小时。所以对我来说,我在澳大利亚悉尼,所以我是格林威治标准时间 +10,这意味着我是 UTC = LocalTime + (-10)。因此 GetTimeZoneInformation 正确地确定我是 -10。
但是我的函数返回一个无符号整数,所以它不应该返回 10 而不是 -10 吗?
unsigned int getTimeZoneBias()
{
TIME_ZONE_INFORMATION tzInfo;
DWORD res = GetTimeZoneInformation( &tzInfo );
if ( res == TIME_ZONE_ID_INVALID )
{
return (INT_MAX/2);
}
return (unsigned int(tzInfo.Bias / 60)); // convert from minutes to hours
}
TCHAR ch[200];
_stprintf( ch, _T("A: %d\n"), getTimeZoneBias()); // this prints out A: -10
debugLog += _T("Bias: ") + tstring(ch) + _T("\r\n");
最佳答案
您正在尝试将 unsigned int
打印为 signed int
。将 %d
更改为 %u
_stprintf( ch, _T("A: %u\n"), getTimeZoneBias());
^
问题是整数 aren't positive or negative by themselves for most computers .这取决于它们的解释方式。
因此,一个大整数可能无法与一个小的(绝对值)负数区分开来。
关于c++ - unsigned int 类型的函数返回负数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7618088/