linux判断日期是否合法

 1 #include <time.h>
 2 #include <stdio.h>
 3  
 4  
 5 typedef enum
 6 {
 7     false = 0,
 8     true = 1
 9 } bool;
10  
11 bool check_date(int year, int month, int day)
12 {
13     if (year < 1900 || month <= 0 || month > 12
14         || day <= 0 || day > 31)
15           return false;
16  
17     struct tm tm_new;
18     tm_new.tm_year = year - 1900;
19     tm_new.tm_mon = month - 1;
20     tm_new.tm_mday = day;
21     tm_new.tm_hour = 0;
22     tm_new.tm_min = 0;
23     tm_new.tm_sec = 0;
24  
25     time_t time_new = mktime(&tm_new);
26     localtime_r(&time_new, &tm_new);
27  
28     if (tm_new.tm_year != year - 1900 || tm_new.tm_mon != month - 1 || tm_new.tm_mday != day)
29           return false;
30     return true;
31 }

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。