我正在使用 VS2010 此代码编译为 ANSI C 项目
int C90Clockadapter_GetCW_ISO8601(const C90Clockadapter * clockadapter, int * calendarweek)
{
int e = 0;
struct tm tm;
char timebuf[64];
memset( &tm, 0, sizeof( tm ) );
tm.tm_mday = clockadapter->mDay;
tm.tm_mon = clockadapter->mMonth;
tm.tm_year = clockadapter->mYear;
mktime(&tm);
if (0 != strftime(timebuf,sizeof(timebuf),"%W", &tm) ) //i know %W is not ISO8601
{
*calendarweek = atoi(timebuf);
}
else
{
e |= 1;
}
return e;
}
不知何故 mktime(&tm)
不会改变 tm
的状态。这种行为完全不符合我的预期。
我写了一些具有 self 解释输出的单元测试:
4.1.1971 - expected CW 1 but was CW 0
31.12.1992 - expected CW 53 but was CW 0
我的代码有什么问题 - 或者我缺少 Windows 或 vs2010 和 ansi-c 中的某些特性?
最佳答案
引用资料说它不一定会修改 timeptr 参数。您应该使用返回值。参见 http://www.cplusplus.com/reference/ctime/mktime/
time_t mktime (struct tm * timeptr);
关于c - mktime 不更新 struct tm,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21587909/