1、C++校验日期串合法性(兼容性比较好)
#include <assert.h>
#include <stdio.h>
#include <string.h>
#include <string>
#include <vector>
std::string trim(const std::string& str)
{
std::string::size_type pos = str.find_first_not_of(' ');
if (pos == std::string::npos)
{
return str;
}
std::string::size_type pos2 = str.find_last_not_of(' ');
if (pos2 != std::string::npos)
{
return str.substr(pos, pos2 - pos + 1);
}
return str.substr(pos);
}
void split(const std::string& str, std::vector<std::string>* ret_, std::string sep = ",")
{
if (str.empty())
{
return;
}
std::string tmp;
std::string::size_type pos_begin = str.find_first_not_of(sep);
std::string::size_type comma_pos = 0;
while (pos_begin != std::string::npos)
{
comma_pos = str.find(sep, pos_begin);
if (comma_pos != std::string::npos)
{
tmp = str.substr(pos_begin, comma_pos - pos_begin);
pos_begin = comma_pos + sep.length();
}
else
{
tmp = str.substr(pos_begin);
pos_begin = comma_pos;
}
if (!tmp.empty())
{
ret_->push_back(tmp);
tmp.clear();
}
}
}
bool DateVerify(int year, int month, int day)
{
if(year < 2013 || year > 2020 || month < 1 || month > 12 || day < 1 || day > 31)
{
return false;
}
switch (month)
{
case 4:
case 6:
case 9:
case 11:
if (day > 30)
{
return false;
}
break;
case 2:
{
bool bLeapYear = (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
if ((bLeapYear && day > 29) || (!bLeapYear && day > 28))
{
return false;
}
}
break;
default:
break;
}
return true;
}
bool CheckDateValid(const std::string& strDate)
{
std::string strPureDate = trim(strDate);
if(strPureDate.length() < 8 || strPureDate.length() > 10)
{
return false;
}
std::vector<std::string> vecFields;
split(strPureDate, &vecFields, "/");
if(vecFields.size() != 3)
{
return false;
}
int nYear = atoi(vecFields[0].c_str());
int nMonth = atoi(vecFields[1].c_str());
int nDay = atoi(vecFields[2].c_str());
return DateVerify(nYear, nMonth, nDay);
}
bool CheckTimeValid(const std::string& strTime)
{
std::string strPureTime = trim(strTime);
if(strPureTime.length() < 5 || strPureTime.length() > 8)
{
return false;
}
std::vector<std::string> vecFields;
split(strPureTime, &vecFields, ":");
if(vecFields.size() != 3)
{
return false;
}
int nHour = atoi(vecFields[0].c_str());
int nMinute = atoi(vecFields[1].c_str());
int nSecond = atoi(vecFields[2].c_str());
bool bValid = (nHour >= 0 && nHour <= 23);
bValid = bValid && (nMinute >=0 && nMinute <= 59);
bValid = bValid && (nSecond >= 0 && nSecond <= 59);
return bValid;
}
bool CheckDateTimeValid(const std::string& strDateTime)
{
std::string strPureDateTime = trim(strDateTime);
std::vector<std::string> vecFields;
split(strPureDateTime, &vecFields, " ");
if(vecFields.size() != 1 && vecFields.size() != 2)
{
return false;
}
if(vecFields.size() == 1)
{
return CheckDateValid(vecFields[0]);
}
return CheckDateValid(vecFields[0]) && CheckTimeValid(vecFields[1]);
}
int main()
{
assert(CheckDateTimeValid("2021-8-1 12:12:12"));
assert(CheckDateTimeValid("2013/8/1"));
assert(CheckDateTimeValid(" 2013/8/1 "));
assert(CheckDateTimeValid("2013/8/01"));
assert(CheckDateTimeValid("2013/08/1"));
assert(CheckDateTimeValid("2013/08/01"));
assert(!CheckDateTimeValid("2013/ / "));
assert(!CheckDateTimeValid("2013/ / "));
assert(!CheckDateTimeValid("2013/ / "));
assert(!CheckDateTimeValid("2013/13/01"));
assert(!CheckDateTimeValid("2013/-1/31"));
assert(CheckDateTimeValid("2013/01/31"));
assert(CheckDateTimeValid("2020/02/29"));
assert(!CheckDateTimeValid("2021/02/29"));
assert(CheckDateTimeValid("2013/8/1 8:8:8"));
assert(CheckDateTimeValid(" 2013/8/1 8:8:8 "));
assert(!CheckDateTimeValid("2013/8/1 18:8"));
assert(!CheckDateTimeValid("2013/8/1 18"));
assert(!CheckDateTimeValid("2013/8/1 18:8:60"));
assert(CheckDateTimeValid("2013/8/1 00:8:00"));
assert(CheckDateTimeValid("2013/8/1 00:00:00"));
assert(CheckDateTimeValid("2013/8/1 0:0:0"));
assert(!CheckDateTimeValid("2013/8/1 24:00:00"));
assert(CheckDateTimeValid("2013/8/1 23:59:59"));
assert(CheckDateTimeValid("2013/8/1 23:00:59"));
assert(!CheckDateTimeValid("2013/8/1 23: 00: 59"));
assert(CheckDateTimeValid(" 2013/8/1 23:59:59"));
assert(CheckDateTimeValid(" 2013/8/1 23:00:59"));
assert(!CheckDateTimeValid(" 2013/8/1 23: 00: 59"));
assert(!CheckDateTimeValid("2013-8/1 8:8:8"));
return 0;
}
2、C简单校验日期串合法性
bool Check_date(short w_year,char w_month,char w_date)
{
char Month_buf[12] = { 31,28,31,30,31,30,31,31,30,31,30,31 };
if (w_month==2)
(((w_year%4==0)&&(w_year%100!=0))||(w_year%400==0))?Month_buf[1]+=1:Month_buf[1];
if (w_month>12||w_month<1||w_date>Month_buf[w_month-1]||w_date<1)
return false;
return true;
}
typedef struct
{
short year;
char month;
char date;
}_Date;
_Date _date[8] =
{
2000,2,29,
1900,2,29,
2019,6,15,
2019,13,15,
2019,6,0,
2019,0,5,
2019,6,31,
2019,7,31,
};
int main()
{
for (int i = 0; i < 8; i++)
{
printf("%d-%d-%d\r\n", _date[i].year, _date[i].month, _date[i].date);
if (Check_date(_date[i].year, _date[i].month, _date[i].date) == true)
printf("该日期正确!\r\n");
else
printf("该日期错误!\r\n");
}
return 0;
}