一个判断时间是否为当日的函数:
BOOL Cmport::TimeToday(time_t time)
{
CTime t1 = CTime::GetCurrentTime();
CTime t2 = time; if ((t1.GetDay()==t1.GetDay())&&( t1.GetMonth()==t2.GetMonth())&&(t1.GetYear()==t2.GetYear()))
return TRUE;
else
return FALSE;
}可是我在debug下调试出现非法操作,在于t1.GetMonth()等的取值出现非法,后来发现原因大概在于我这个函数的参数time_t time取的就是一个非法的数据,例如参数为Buf[i].time ,而Buf[i]的内容为空时,作为我这个函数的参数Buf[i].time 就是一个空或者非法数据,结果函数执行的时候出现了非法操作,请问我如何在我这个TimeToday函数体内判断参数非法,然后return?
BOOL Cmport::TimeToday(time_t time)
{
CTime t1 = CTime::GetCurrentTime();
CTime t2 = time; if ((t1.GetDay()==t1.GetDay())&&( t1.GetMonth()==t2.GetMonth())&&(t1.GetYear()==t2.GetYear()))
return TRUE;
else
return FALSE;
}可是我在debug下调试出现非法操作,在于t1.GetMonth()等的取值出现非法,后来发现原因大概在于我这个函数的参数time_t time取的就是一个非法的数据,例如参数为Buf[i].time ,而Buf[i]的内容为空时,作为我这个函数的参数Buf[i].time 就是一个空或者非法数据,结果函数执行的时候出现了非法操作,请问我如何在我这个TimeToday函数体内判断参数非法,然后return?
{
CTime t1 = CTime::GetCurrentTime();
CTime t2 = time; if (t1 == t2)
return TRUE;
else
return FALSE;
}
BOOL Cmport::TimeToday(time_t time)
{
CTime t1 = CTime::GetCurrentTime();
CTime t2(time); if (t1 == t2)
return TRUE;
else
return FALSE;
}
if (t1 == t2)因为t1 t2中包含有小时,分钟,秒,我只是判断是否是同一天而非同一时刻!
这样:
BOOL Cmport::TimeToday(time_t time)
{
COleDateTime t1(COleDateTime::GetCurrentTime());
COleDateTime t2(time); if (t2.GetStatus() != COleDateTime::valid)
return FALSE; return ( (t1.GetDay() == t1.GetDay()) &&
(t1.GetMonth() == t2.GetMonth()) &&
(t1.GetYear()==t2.GetYear()));
}