判断时间日期格式合法性,C和C++实现

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)
{
    // 这里限制了年份需要在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]);
}
 
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)                                                             //闰年2月+1天
        (((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;
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值