C程序校验日期
#define is_leap_year(year) \
((((year) % 400 == 0) || ((year) % 4 == 0 && (year) % 100 != 0)) ? 1 : 0)
/**
* time_is_valid()
*
* test time is valid.
*
* remark:
*
* The number of seconds(sec) after the minute, normally in the range 0 to 59,
* but can be up to 60 to allow for leap.
* Deciding when to introduce a leap second is the responsibility of the
* international earth rotation and reference systems service.
*
* returns:
* 0: error
* 1: ok
*/
static int time_is_valid (int year, int mon, int day, int hour, int min, int sec)
{
if (year < 1900 || year > 9999 || mon <= 0 || mon > 12 || day <= 0 || day > 31 || hour < 0 || hour > 24 || min < 0 || min > 59 || sec < 0 || sec > 60) {
return 0;
}
if (mon == 1 || mon == 3 || mon == 5 || mon == 7 || mon == 8 || mon == 10 || mon == 12) {
// 31 days ok
return 1;
} else if (mon == 4 || mon == 6 || mon == 9 || mon == 11) {
if (day < 31) {
return 1;
}
} else {
// mon=2
return (day > 29? 0 : (day < 29? 1: (is_leap_year(year)? 1 : 0)));
}
return 0;
}
转载:https://blog.csdn.net/cheungmine/article/details/101532597
查看评论