program does not take the system date

2

I have a program that I'm doing a test to see how it works out but I could not find the error that causes it not to return the system date and time, I thought it was regex but it is not already mexi practically in the constructor whole and nothing can anyone give me a light?

CurrentDateTime::CurrentDateTime()
{
    //fetch/store current local-daytime information
    auto tp = std::chrono::system_clock::now();
    time_t cstyle_t = std::chrono::system_clock::to_time_t(tp);
    char* cstyleinfo = std::ctime(&cstyle_t);
    // copy(deep) the data into the std::string as ::ctime() provides static data 
    // which might be overwritten in case someone call it again.
    std::string currentinfo(cstyleinfo);

    //parse/store  the information
    auto dtstr = ParseDateTime(currentinfo);
    StrToNumber(dtstr);
}

The complete program:

#include <regex>
#include <array>
#include <vector>
#include <chrono>
#include <utility>
#include <iostream>


enum class Month
{
  Jan = 1, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec
};

// Datastructure for string to num conversion in month(.i.e."Mar" Month to 3)
std::array<std::pair<std::string, Month>, 12> monthinfo = 
{
    std::make_pair(std::string("Jan"), Month::Jan),
    std::make_pair(std::string("Feb"), Month::Feb),
    std::make_pair(std::string("Mar"), Month::Mar),
    std::make_pair(std::string("Apr"), Month::Apr),
    std::make_pair(std::string("May"), Month::May),
    std::make_pair(std::string("Jun"), Month::Jun),
    std::make_pair(std::string("Jul"), Month::Jul),
    std::make_pair(std::string("Aug"), Month::Aug),
    std::make_pair(std::string("Sep"), Month::Sep),
    std::make_pair(std::string("Oct"), Month::Oct),
    std::make_pair(std::string("Nov"), Month::Nov),
    std::make_pair(std::string("Dec"), Month::Dec)
};

// concrete daytime structure to store the data
template<typename T1, typename T2 = std::string>
struct DayTime
{
    T1 day      = T1();
    T1 month    = T1();
    T1 year     = T1();
    T1 hour     = T1();
    T1 min      = T1();
    T1 second   = T1();
    T2 daystr   = T2();
    T2 dtstring = T2();
};


// main class which would fetech/parse the current time and provide to the client
class CurrentDateTime
{
 public:

   CurrentDateTime();
   virtual ~CurrentDateTime(){};

            int    GetDay() const { return dt.day;    }
            int  GetMonth() const { return dt.month;  }
            int   GetYear() const { return dt.year;   }
            int   GetHour() const { return dt.hour;   }
            int    GetMin() const { return dt.min;    }
            int GetSecond() const { return dt.second; }
    std::string GetDayStr() const { return dt.daystr; }

  private:

     DayTime<std::string> ParseDateTime(const std::string&);
     void StrToNumber(const DayTime<std::string>&);
     int GetMonth(const std::string&);
     DayTime<int> dt;
};


CurrentDateTime::CurrentDateTime()
{
    //fetch/store current local-daytime information
    auto tp = std::chrono::system_clock::now();
    time_t cstyle_t = std::chrono::system_clock::to_time_t(tp);
    char* cstyleinfo = std::ctime(&cstyle_t);
    // copy(deep) the data into the std::string as ::ctime() provides static data 
    // which might be overwritten in case someone call it again.
    std::string currentinfo(cstyleinfo);

    //parse/store  the information
    auto dtstr = ParseDateTime(currentinfo);
    StrToNumber(dtstr);
}


DayTime<std::string> CurrentDateTime::ParseDateTime(const std::string& information)
{
    DayTime<std::string> info;
    std::regex dtimeregex{ R"(^(\w{3}) (\w{3}) (\d{2}) (\d{2}):(\d{2}):(\d{2}) (\d{4})$)" };
    //std::regex dtimeregex("\b\w{3} \w{3} \d{2} \d{2}:\d{2}:\d{2} \d{4}\b");
    std::smatch match;

    if (std::regex_search(information, match, dtimeregex)) 
    {
        // Match the group and subgroups by regex parser.
        auto index = 0;
        info.dtstring = match[index++];
        info.daystr   = match[index++];
        info.month    = match[index++];
        info.day      = match[index++];
        info.hour     = match[index++];
        info.min      = match[index++];
        info.second   = match[index++];
        info.year     = match[index++];
    }
  return info;
}


void CurrentDateTime::StrToNumber(const DayTime<std::string>& information)
{
    dt.dtstring = information.dtstring;
    dt.daystr   = information.daystr;
    dt.month    = GetMonth(information.month);
    dt.day      = ::atoi(information.day.c_str());
    dt.hour     = ::atoi(information.hour.c_str());
    dt.min      = ::atoi(information.min.c_str());
    dt.second   = ::atoi(information.second.c_str());
    dt.year     = ::atoi(information.year.c_str());
}

int CurrentDateTime::GetMonth(const std::string& input)
{
    for(const auto& itr : monthinfo) 
    {
      if(itr.first == input)
      return static_cast<int>(itr.second);
    }
}

int main()
{
  CurrentDateTime current;

    std::cout << "\n\tCurrent Day....: " << current.GetDayStr()
              << "\n\tCurrent Date...: " << current.GetDay()
              << "\n\tCurrent Month..: " << current.GetMonth()
              << "\n\tCurrent Year...: " << current.GetYear()
              << "\n\tCurrent Hour...: " << current.GetHour()
              << "\n\tCurrent Min....: " << current.GetMin()
              << "\n\tCurrent Second.: " << current.GetSecond() 
              << "\n\n";
    return 0;
} 

The type of output you should generate:

Output:

Current Day: Tue
Current Date: 4
Current Month: 11
Current Year: 2014
Current Hour: 10
Current Min: 43
Current Second: 35

The output you are generating:

bash-4.4$ ./date

    Current Day....: 
    Current Date...: 0
    Current Month..: 6556000
    Current Year...: 0
    Current Hour...: 0
    Current Min....: 0
    Current Second.: qual o erro neste programa?
    
asked by anonymous 14.09.2017 / 06:15

1 answer

2

The problem with your program is in REGEX. To make the date parser, the correct regex, instead of:

std::regex dtimeregex{ R"(^(\w{3}) (\w{3}) (\d{2}) (\d{2}):(\d{2}):(\d{2}) (\d{4})$)" };

is:

std::regex dtimeregex{ R"(^(\w{3}) (\w{3}) (\d{2}) (\d{2}):(\d{2}):(\d{2}) (\d{4}))" };

that is, without $ at the end

    
14.09.2017 / 13:48