#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)
{
// 这里限制了年份需要在2013-2020,可去掉
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)
{ // 4.6.9.11月天数不能大于30
return false;
}
break;
case 2:
{
bool bLeapYear = (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
if ((bLeapYear && day > 29) || (!bLeapYear && day > 28))
{
// 闰年2月不能大于29天;平年2月不能大于28天
return false;
}
}
break;
default:
break;
}
return true;
}
// 校验yyyy/mm/dd
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;
}
// TODO:这里最好再下判断字符转换是否成功
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);
}
// 校验HH:MM:SS
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;
}
// 日期格式为: yyyy/mm/dd || yyyy/mm/dd HH:MM:SS
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]);
}
C++校验日期串合法性
于 2022-10-18 14:33:09 首次发布