fork(1) download
  1. #include <vector>
  2. #include <string>
  3. #include <iostream>
  4. #include <ctime>
  5. #include <iomanip>
  6. #include <sstream>
  7. #include <cstring>
  8.  
  9. int main()
  10. {
  11. std::vector<std::string> vec;
  12.  
  13. vec.push_back("2016:07:30 09:27:06");
  14. vec.push_back("2016:07:30 09:27:07");
  15. vec.push_back("2016:07:30 09:27:08");
  16.  
  17. for (auto & i : vec)
  18. {
  19. struct std::tm tm;
  20.  
  21. /* std::sscanf() method: */
  22. std::memset(&tm, 0, sizeof(tm));
  23. if (6 != std::sscanf(i.c_str(), "%d:%d:%d %d:%d:%d",
  24. &tm.tm_year, &tm.tm_mon, &tm.tm_mday,
  25. &tm.tm_hour, &tm.tm_min, &tm.tm_sec))
  26. {
  27. return -1;
  28. }
  29.  
  30. /* correct the numbers according to:
  31.   * see: http://e...content-available-to-author-only...e.com/w/cpp/chrono/c/tm */
  32. --tm.tm_mon;
  33. tm.tm_year -= 1900;
  34. /* mktime determines if Daylight Saving Time was in effect
  35.   * see: http://e...content-available-to-author-only...e.com/w/cpp/chrono/c/mktime */
  36. tm.tm_isdst = -1;
  37.  
  38. std::time_t time = std::mktime(&tm);
  39.  
  40. std::cout << i << " | sscanf() time = " << time << std::endl;
  41.  
  42. /************************************************************/
  43.  
  44. /* std::get_time() method: */
  45. std::istringstream iss;
  46. iss.str(i);
  47. iss >> std::get_time(&tm, "%Y:%m:%d %H:%M:%S");
  48.  
  49. time = std::mktime(&tm);
  50.  
  51. std::cout << i << " | std::get_time() time = " << time << std::endl;
  52. }
  53. }
Success #stdin #stdout 0s 15248KB
stdin
Standard input is empty
stdout
2016:07:30 09:27:06 | sscanf() time =        1469870826
2016:07:30 09:27:06 | std::get_time() time = 1469870826
2016:07:30 09:27:07 | sscanf() time =        1469870827
2016:07:30 09:27:07 | std::get_time() time = 1469870827
2016:07:30 09:27:08 | sscanf() time =        1469870828
2016:07:30 09:27:08 | std::get_time() time = 1469870828