我想获得一周的一周。为此,我使用tm * __CRTDECL localtime(const time_t * _Time)
,但我没有得到期望的结果,这应该是与语言环境相关的。所以我一直在寻找解决方案和更多信息。我发现JAVA Calendar.getInstance(Locale.GERMAN);
中有API。所以我只是想知道c / c ++中是否有这样的API,因为我找不到(不确定)。如果没有,任何人都可以给我一些时间指针以获得周年级语言环境。
我正在使用的代码
int MyClass::getCalendarWeek(time_t time, int * p_year)
{
// Get tm structure of time parameter
tm* pCurrentTm = localtime(&time);
// determine Thursday in that week
LONG offSet = 4 - pCurrentTm->tm_wday;
if (offSet >= 4) {
offSet = -3; // Sunday
}
time += offSet * 86400L;
pCurrentTm = localtime(&time);
if (p_year) {
*p_year = pCurrentTm->tm_year + 1900;// year of current calendar week
}
return (pCurrentTm->tm_yday + 7) / 7; // current calendar week
}
输出:
7月11日是在第28周,当时运行大多数星期一作为一周的第一天的语言环境,但星期日是第29周作为第一周的第一天。