fork(1) download
  1. #include <iostream>
  2. #include <ctime>
  3. using namespace std;
  4.  
  5. int main() {
  6. // your code goes here
  7.  
  8. time_t previous_time = 1470104004; // input previous time by hand
  9.  
  10. // convert time_t to structure tm. You can use instead gmtime() for UTC
  11. tm *tm_previous = localtime(&previous_time);
  12.  
  13. // print time using asctime() function.
  14. cout << asctime(tm_previous);
  15.  
  16. // for current time
  17. time_t current_time = time(0); // get current time
  18. tm *tm_current = localtime(&current_time);
  19. cout << asctime(tm_current);
  20.  
  21. // print by accessing contents of the structure
  22. cout << tm_current->tm_year + 1900 // year 0 corresponds to 1900
  23. << "/" << tm_current->tm_mon+1 // month starts from 0
  24. << "/" << tm_current->tm_mday // mday means day in a month
  25. << " " << tm_current->tm_hour
  26. << ":" << tm_current->tm_min
  27. << ":" << tm_current->tm_sec <<endl;
  28.  
  29.  
  30. // get unixtime from YY-mm-dd
  31.  
  32. tm *jan2014 = localtime(&current_time);
  33. jan2014->tm_year = 2014 - 1900;
  34. jan2014->tm_mon = 1 - 1;
  35. jan2014->tm_mday = 1;
  36. jan2014->tm_hour = 0;
  37. jan2014->tm_min = 0;
  38. jan2014->tm_sec = 0;
  39.  
  40. // print unix time
  41. cout << mktime(jan2014) << endl;
  42.  
  43. // print datime
  44. cout << asctime(jan2014) << endl;
  45.  
  46. return 0;
  47. }
Success #stdin #stdout 0s 3460KB
stdin
Standard input is empty
stdout
Tue Aug  2 02:13:24 2016
Tue Aug  2 03:55:19 2016
2016/8/2 3:55:19
1388534400
Wed Jan  1 00:00:00 2014