[code]
//日期格式为数字 如 20080901
//By zsp007@gmail.com
///验证是否是有效日期
bool date_validate(T)(T date)
{
static T daysOfMonth[] = [0,31,28,31,30,31,30,31,31,30,31,30,31];
T monthDay = date % 10000;
if(monthDay > 1231)
return false;
if(monthDay == 229)
{
int year = date / 10000;
if((year % 4) != 0)
return false;
return (!(year %100 == 0 && year % 400 != 0));
}
int month = monthDay / 100;
int day = monthDay % 100;
return (day > 0 && day <= daysOfMonth[month]);
}
///下一天
T tomorrow(T)(T i){
static T daysOfMonth[] = [0,31,28,31,30,31,30,31,31,30,31,30,31];
T day=i%100,month=(i/100)%100,year=i/10000;
if(day>=daysOfMonth[month]){
if(month==12){
++year;
month=1;
day=1;
}else if(month==2&&day==28){
if((year % 4)== 0){
if(year%100 == 0 && year % 400 != 0){
goto add_month;
}else ++day;
}else goto add_month;
}else{
add_month:
++month;
day=1;
}
}else ++day;
return day+100*month+10000*year;
}[/code]
//日期格式为数字 如 20080901
//By zsp007@gmail.com
///验证是否是有效日期
bool date_validate(T)(T date)
{
static T daysOfMonth[] = [0,31,28,31,30,31,30,31,31,30,31,30,31];
T monthDay = date % 10000;
if(monthDay > 1231)
return false;
if(monthDay == 229)
{
int year = date / 10000;
if((year % 4) != 0)
return false;
return (!(year %100 == 0 && year % 400 != 0));
}
int month = monthDay / 100;
int day = monthDay % 100;
return (day > 0 && day <= daysOfMonth[month]);
}
///下一天
T tomorrow(T)(T i){
static T daysOfMonth[] = [0,31,28,31,30,31,30,31,31,30,31,30,31];
T day=i%100,month=(i/100)%100,year=i/10000;
if(day>=daysOfMonth[month]){
if(month==12){
++year;
month=1;
day=1;
}else if(month==2&&day==28){
if((year % 4)== 0){
if(year%100 == 0 && year % 400 != 0){
goto add_month;
}else ++day;
}else goto add_month;
}else{
add_month:
++month;
day=1;
}
}else ++day;
return day+100*month+10000*year;
}[/code]