|
楼主 |
发表于 2009-6-27 23:55:22
|
显示全部楼层
一个通用类CTime,可以跨平台的。注释掉的为在PC上测试用的
#ifndef classtime_kkk_h
#define classtime_kkk_h 1
class CTime
{
public:
CTime()
{
year=0,month=0,day=0,week=0,hour=0,minute=0,second=0;
validate();
}
CTime(char *s)
{
year=*s++;
month=*s++;
day=*s++;
week=*s++;
hour=*s++;
minute=*s++;
second=*s++;
validate();
}
~CTime()
{}
CTime& operator<<(CTime& ntime);
CTime& operator>>(CTime& ntime);
CTime& operator<<(char *d);
char getsecond(void)
{
return second;
}
char getminute(void)
{
return minute;
}
char gethour(void)
{
return hour;
}
char getday(void)
{
return day;
}
char getmonth(void)
{
return month;
}
char getyear(void)
{
return year;
}
char getweek(void)
{
return week;
}
void setsecond(char d)
{
second=d;
}
void setminute(char d)
{
minute=d;
}
void sethour(char d)
{
hour=d;
}
void setday(char d)
{
day=d;
}
void setmonth(char d)
{
month=d;
}
void setyear(char d)
{
year=d;
}
void setweek(char d)
{
week=d;
}
// void display(void)
//{
//cout<<"Year:"<<(int)year<<endl;
// cout<<"Month:"<<(int)month<<endl;
// cout<<"Day:"<<(int)day<<endl;
// cout<<"Week:"<<(int)week<<endl;
// cout<<"Hour:"<<(int)hour<<endl;
// cout<<"Minute:"<<(int)minute<<endl;
// cout<<"Second:"<<(int)second<<endl;
// }
private:
void validate(void);
char year,month,day,week,hour,minute,second;
};
void CTime::validate(void)
{
if(second>=60 || second<0)
{
second=0;
}
if(minute>=60 || minute<0)
{
minute=0;
}
if(hour>=24||hour<0)
{
hour=0;
}
if(day>=31 || day<=0)
{
day=1;
}
if(month>12 || month<=0)
{
month=1;
}
if(year<0)
{
year=0;
}
if(week>7 || week<0)
{
week=0;
}
}
CTime& CTime::operator<<(CTime& ntime)
{
second=ntime.getsecond();
minute=ntime.getminute();
hour=ntime.gethour();
day=ntime.getday();
month=ntime.getmonth();
year=ntime.getyear();
week=ntime.getweek();
validate();
return *this;
}
CTime& CTime::operator>>(CTime& ntime)
{
ntime.setsecond(second);
ntime.setminute(minute);
ntime.sethour(hour);
ntime.setday(day);
ntime.setmonth(month);
ntime.setyear(year);
ntime.setweek(week);
ntime.validate();
return *this;
}
CTime& CTime::operator<<(char *d)
{
second=*d++;
minute=*d++;
hour=*d++;
day=*d++;
month=*d++;
year=*d++;
week=*d++;
validate();
return *this;
}
#endif |
|