-1

I have a C++ application I'm developing where I just need to check if the current day's date is in a char array, specifically in the format "2015-05-10". I'm pretty new to C++ coming over from PHP where it is very easy to do, but I'm struggling trying to find a good method in C++. This needs to be automated as the script runs daily on a cron job. So the process is:

If (today's date is in char array) {
do this } 
else {
do nothing
}

Edit: I am obviously useless at expressing my problems, sorry!

My main issues are:

  1. How do I get the current day's date in a nice simple string in this format - 2015-05-10

  2. How do I then check if a char array I have stored (which I know contains a date amongst some other text) contains the current day's date (which I will, when I know how, have stored as a string).

Tim
  • 105
  • 1
  • 8
  • There's [`std::regex`](http://en.cppreference.com/w/cpp/regex/basic_regex) to achieve such format checks. – πάντα ῥεῖ May 10 '15 at 07:17
  • "check if the current day's date is in a char array" surely you would know that because of where you are getting the date from - and the return type wouldn't change. – cmannett85 May 10 '15 at 07:21
  • @cmannett85 the char array stores a response from another server (the response contains other text along with the date), and at same point across 24 hours will change from containing an old date, to the current days date. So when I fetch a new response, I need to check if it now contains today's date, and act accordingly. – Tim May 10 '15 at 07:29
  • @πάντα ῥεῖ yes but I'm nowhere near getting to that stage yet, I'm struggling to even have the necessary strings prepared to compare. – Tim May 10 '15 at 07:31
  • @Tim It's completely unclear what you're talking about. Converting the current time to a string? – πάντα ῥεῖ May 10 '15 at 07:34
  • @πάντα ῥεῖ sorry, I am obviously very bad at explaining what I need. I've added a couple of points to the original question. – Tim May 10 '15 at 07:43
  • @Tim , 1. you need to get current date. 2. you want to check what?? if you're getting current date then why are you checking again that is it today's date or not bc you have just got it? – Abhinav Gauniyal May 10 '15 at 07:46
  • btw you can use : http://en.cppreference.com/w/cpp/chrono – Abhinav Gauniyal May 10 '15 at 07:47

1 Answers1

0

If I understood correctly, your first want to convert the current date to the format yyyy-mm-dd and then search for the string in another string.

For the first question, you may refer to How to get current time and date in C++? where there are multiple solutions given. For the second part of the question, if you are using strings, you should use the find (http://www.cplusplus.com/reference/string/string/find/) method and if you are using char arrays, you could use the C strstr (http://www.cplusplus.com/reference/cstring/strstr/) method. Here's what I tried:

       #include <iostream>
       #include <string>
       #include <cstdio>
       #include <ctime>
       #include <cstring>

    time_t     now = time(0);
    struct tm  tstruct;
    char       buf[100];
    tstruct = *localtime(&now);
    strftime(buf, sizeof(buf), "%Y-%m-%d", &tstruct);

   //char arrays used
    char ch_array[] = "This is the received string 2015-05-10 from server";
    char * pch;
    pch = strstr(ch_array, buf);
    if (pch != nullptr)
        std::cout << "Found";

    //string used
    std::string str("This is the received string 2015-05-10 from server");
    std::size_t found = str.find(buf);
    if (found != std::string::npos)
        std::cout << "date found at: " << found << '\n';
Community
  • 1
  • 1