1. 程式人生 > >C++校驗日期串合法性

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("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;
}