3

I am working on a program that takes two command line arguments. Both arguments should be dates of the form yyyy-mm-dd. Since other folks will be using this program and it will be requesting from mysql, I want to make sure that the command line arguments are valid. My original thought was to loop over each element of the incoming string and perform some kind of test on it. The '-' would be easy to check but I'm not so sure how to handle the digits, and to distinguish them between ints and chars. Also, I need the first date to be "less than or equal to" the second but I'm pretty sure I can handle that.

nonremovable
  • 798
  • 1
  • 6
  • 19
  • try using std::stringstream – tejas Jun 22 '15 at 16:14
  • 3
    If you're using C++11 there's the [`regex`](http://en.cppreference.com/w/cpp/regex) library: `\d{4}\-\d{2}\-d{2}` will test for formatting. To verify it's a valid date and not `9999-99-99` or `2015-02-29` you will need to employ some kind of date library. – tadman Jun 22 '15 at 16:16
  • 1
    Use regex from standard library. Regarding "*I need the first date to be "less than or equal to" the second but I'm pretty sure I can handle that.*"... you might want to analyse the top answer from the top person on StackOverflow: http://stackoverflow.com/questions/6841333/why-is-subtracting-these-two-times-in-1927-giving-a-strange-result/6841479#6841479 Properly comparing dates is not a trivial task. – luk32 Jun 22 '15 at 16:20
  • Regex is not just a C++11 feature. It's available for many languages and in all versions of C and C++ as an add on library. – Jay Jun 22 '15 at 16:25
  • Have you tried a brute force technique? Please update your question with the code you have tried. – Thomas Matthews Jun 22 '15 at 16:42
  • Once you have split the string into day, month and year, you could then use [the C Time library](http://www.cplusplus.com/reference/ctime/), specifically the [`difftime` function](http://www.cplusplus.com/reference/ctime/difftime/), to compare the dates. – user3841621 Jun 22 '15 at 16:48
  • I fear, C/C++ date-time parsing is poor (still). I agree with @tadman –  Jun 22 '15 at 18:26

1 Answers1

5

If you can use boost library you could simple do it like this:

string date("2015-11-12");
string format("%Y-%m-%d");
date parsedDate = parser.parse_date(date, format, svp);

You can read more about this here.

If you want a pure C++ solution you can try using

struct tm tm;   
std::string s("2015-11-123");
if (strptime(s.c_str(), "%Y-%m-%d", &tm))
    std::cout << "Validate date" << std::endl;
else
    std::cout << "Invalid date" << std::endl;

Additionally you can do a simple check to see if the date is valid, and is not for example 2351-20-35. A simple solution would be:

bool isleapyear(unsigned short year){
    return (!(year%4) && (year%100) || !(year%400));
}

//1 valid, 0 invalid
bool valid_date(unsigned short year,unsigned short month,unsigned short day){
    unsigned short monthlen[]={31,28,31,30,31,30,31,31,30,31,30,31};
    if (!year || !month || !day || month>12)
        return 0;
    if (isleapyear(year) && month==2)
        monthlen[1]++;
    if (day>monthlen[month-1])
        return 0;
    return 1;
}

Source: http://www.cplusplus.com/forum/general/3094/

A B
  • 497
  • 2
  • 9