fork(1) download
  1. #include <iostream>
  2. #include <string.h>
  3. #include <cstdio>
  4. using namespace std;
  5.  
  6. time_t string_to_time_t(string s)
  7. {
  8. int yy, mm, dd, hour, min, sec;
  9. struct tm when;
  10. long tme;
  11.  
  12. memset(&when, 0, sizeof(struct tm));
  13. sscanf(s.c_str(), "%d/%d/%d:%d:%d:%d", &dd, &mm, &yy, &hour, &min, &sec);
  14.  
  15. time(&tme);
  16. when = *localtime(&tme);
  17. when.tm_year = yy-1900;
  18. when.tm_mon = mm-1;
  19. when.tm_mday = dd;
  20. when.tm_hour = hour;
  21. when.tm_min = min;
  22. when.tm_sec = sec;
  23.  
  24. return mktime(&when);
  25. }
  26.  
  27. string time_t_to_string(time_t t)
  28. {
  29. char buff[20];
  30. strftime(buff, 20, "%d/%m/%Y:%H:%M:%S", localtime(&t));
  31. string s(buff);
  32. return s;
  33. }
  34.  
  35. int main()
  36. {
  37. string s = "30/11/2012:13:49:55";
  38.  
  39. time_t t = string_to_time_t(s);
  40. string ss = time_t_to_string(t);
  41.  
  42. cout << ss << "\n";
  43.  
  44.  
  45. return 0;
  46. }
Success #stdin #stdout 0s 3460KB
stdin
Standard input is empty
stdout
30/11/2012:13:49:55