我定义了一个关于日期的类。
但是我如何能限制输入的日期的格式一定为YYYY-MM-DD ;
否则我无法对日期进行判断。
class TimeTime
{
public:
TimeTime() ;
void displaydate() ;
bool isTimeValid(char *) ;
private:
char tmpbuf1[9] ; //获取的系统日期 YY/DD/MM
char tmpbuf2[11] ; //日期的另一形式 YYYY-MM-DD
char year[5] ;
char month[3] ;
char date[3] ;
} ;
TimeTime::TimeTime()
{
_strdate(tmpbuf1) ;
year[0] = '2' ;
year[1] = '0' ;
year[2] = tmpbuf1[0] ;
year[3] = tmpbuf1[1] ;
year[4] = '\0' ;
month[0] = tmpbuf1[6] ;
month[1] = tmpbuf1[7] ;
month[2] = '\0' ;
date[0] = tmpbuf1[3] ;
date[1] = tmpbuf1[4] ;
date[2] = '\0' ;
tmpbuf2[0] = '2' ;
tmpbuf2[1] = '0' ;
tmpbuf2[2] = year[2] ;
tmpbuf2[3] = year[3] ;
tmpbuf2[4] = '-' ;
tmpbuf2[5] = month[0] ;
tmpbuf2[6] = month[1] ;
tmpbuf2[7] = '-' ;
tmpbuf2[8] = date[0] ;
tmpbuf2[9] = date[1] ;
tmpbuf2[10] = '\0' ;
}
void TimeTime::displaydate()
{
cout << "\t日期: "<<year << "年" << month << "月" << date << "日" << endl ;
}
bool TimeTime::isTimeValid(char da[])
{
if (strcmp(tmpbuf2 , da) >= 0)
return true ;
else
return false ;
}