fork download
  1. #include <regex>
  2. #include <array>
  3. #include <vector>
  4. #include <chrono>
  5. #include <cassert>
  6. #include <utility>
  7. #include <iostream>
  8.  
  9. enum Month
  10. {
  11. Jan = 1, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec
  12. };
  13.  
  14. // structured binding, Datastructure for string to num conversion in month(.i.e."Mar" Month to 3)
  15. std::array<std::pair<std::string, Month>, 12> monthinfo =
  16. {
  17. std::make_pair("Jan", Month::Jan),
  18. std::make_pair("Feb", Month::Feb),
  19. std::make_pair("Mar", Month::Mar),
  20. std::make_pair("Apr", Month::Apr),
  21. std::make_pair("May", Month::May),
  22. std::make_pair("Jun", Month::Jun),
  23. std::make_pair("Jul", Month::Jul),
  24. std::make_pair("Aug", Month::Aug),
  25. std::make_pair("Sep", Month::Sep),
  26. std::make_pair("Oct", Month::Oct),
  27. std::make_pair("Nov", Month::Nov),
  28. std::make_pair("Dec", Month::Dec)
  29. };
  30.  
  31. // concrete daytime structure to store the data
  32. //template<typename T1, typename T2 = std::string>
  33. template<class T1, class T2 = std::string>
  34. struct DayTime
  35. {
  36. T1 day = T1();
  37. T1 month = T1();
  38. T1 year = T1();
  39. T1 hour = T1();
  40. T1 min = T1();
  41. T1 second = T1();
  42. T2 daystr = T2();
  43. T2 dtstring = T2();
  44. };
  45.  
  46.  
  47. // main class which would fetch/parse the current time and provide to the client
  48. class CurrentDateTime
  49. {
  50. DayTime<std::string> ParseDateTime(const std::string&);
  51. void StrToNumber(const DayTime<std::string>&);
  52. Month GetMonth(const std::string&) const;
  53. DayTime<int> dt;
  54.  
  55. public:
  56.  
  57. CurrentDateTime();
  58. virtual ~CurrentDateTime(){};
  59.  
  60. int GetDay() const { return dt.day; }
  61. Month GetMonth() const { return static_cast<Month>(dt.month); }
  62. int GetYear() const { return dt.year; }
  63. int GetHour() const { return dt.hour; }
  64. int GetMin() const { return dt.min; }
  65. int GetSecond() const { return dt.second; }
  66. std::string GetDayStr() const { return dt.daystr; }
  67. };
  68.  
  69. CurrentDateTime::CurrentDateTime()
  70. {
  71. //fetch/store current local-daytime information
  72. auto tp = std::chrono::system_clock::now();
  73. time_t cstyle_t = std::chrono::system_clock::to_time_t(tp);
  74. char* cstyleinfo = std::ctime(&cstyle_t);
  75. // copy(deep) the data into the std::string as ::ctime() provides static data
  76. // which might be overwritten in case someone call it again.
  77. std::string currentinfo(cstyleinfo);
  78.  
  79. //parse/store the information
  80. auto dtstr = ParseDateTime(currentinfo);
  81. StrToNumber(dtstr);
  82. }
  83.  
  84.  
  85. DayTime<std::string> CurrentDateTime::ParseDateTime(const std::string& information)
  86. {
  87. DayTime<std::string> info;
  88. std::regex dtimeregex(R"(^(\w{3}) (\w{3}) (\d{2}) (\d{2}):(\d{2}):(\d{2}) (\d{4}))");
  89. std::smatch match;
  90.  
  91. if (std::regex_search(information, match, dtimeregex))
  92. {
  93. // Match the group and subgroups by regex parser.
  94. auto index = 0;
  95. info.dtstring = match[index++];
  96. info.daystr = match[index++];
  97. info.month = match[index++];
  98. info.day = match[index++];
  99. info.hour = match[index++];
  100. info.min = match[index++];
  101. info.second = match[index++];
  102. info.year = match[index++];
  103. }
  104.  
  105. return info;
  106. }
  107.  
  108. Month CurrentDateTime::GetMonth(const std::string& input) const
  109. {
  110. for (const auto& itr : monthinfo)
  111. {
  112. if (itr.first == input) {
  113. return itr.second;
  114. }
  115. //return static_cast<Month>(itr.second);
  116. }
  117. assert(false && "Invalid month name");
  118. // Or return a default month if it makes sense.
  119. // Another possibility is extending the enum Month to
  120. // add an invalid dummy value, throwing an exception, etc.
  121. }
  122.  
  123. void CurrentDateTime::StrToNumber(const DayTime<std::string>& information)
  124. {
  125. dt.dtstring = information.dtstring;
  126. dt.daystr = information.daystr;
  127. dt.month = GetMonth(information.month);
  128.  
  129. dt.day = std::stoi(information.day.c_str());
  130. dt.hour = std::stoi(information.hour.c_str());
  131. dt.min = std::stoi(information.min.c_str());
  132. dt.second = std::stoi(information.second.c_str());
  133. dt.year = std::stoi(information.year.c_str());
  134. }
  135.  
  136.  
  137. int main()
  138. {
  139. CurrentDateTime current = *new CurrentDateTime();
  140.  
  141. std::cout << "\n\tCurrent Day....: " << current.GetDayStr()
  142. << "\n\tCurrent Date...: " << current.GetDay()
  143. << "\n\tCurrent Month..: " << current.GetMonth()
  144. << "\n\tCurrent Year...: " << current.GetYear()
  145. << "\n\tCurrent Hour...: " << current.GetHour()
  146. << "\n\tCurrent Min....: " << current.GetMin()
  147. << "\n\tCurrent Second.: " << current.GetSecond()
  148. << "\n\n";
  149. return 0;
  150. }
Success #stdin #stdout 0s 4536KB
stdin
Standard input is empty
stdout
	Current Day....: Wed
	Current Date...: 24
	Current Month..: 1
	Current Year...: 2018
	Current Hour...: 15
	Current Min....: 35
	Current Second.: 37