Advertisement
Guest User

Untitled

a guest
Jan 17th, 2017
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.17 KB | None | 0 0
  1. #include "Timer.h"
  2. #include "memoryMN.h"
  3.  
  4. std::map<std::string, int> study_log;
  5. std::map<std::string, int>::iterator study_iter;
  6.  
  7. Timer::Timer()
  8. {
  9.  
  10. }
  11.  
  12. Timer::~Timer()
  13. {
  14. }
  15.  
  16. //Function has an side effect of
  17. timeObj *Timer::timeCounting() { //timeObj- std::chrono::time_point<std::chrono::system_clock>
  18. timeObj *now = new timeObj(std::chrono::system_clock::now());
  19.  
  20. return now;
  21. }
  22.  
  23. //If true it would print the current date, else not.
  24. timeObj *Timer::timeCounting(bool print_or_not) {
  25. timeObj *now = new timeObj(std::chrono::system_clock::now());
  26. if (print_or_not) {
  27. printDate(now);
  28. }
  29.  
  30. return now;
  31. }
  32.  
  33. timeObj *Timer::compareDiff(timeObj *start, timeObj *end, bool print_or_not) {
  34. auto int_min = std::chrono::duration_cast<std::chrono::minutes>(*end - *start); //The diffrenece between start and end.
  35. timeObj *now = new timeObj(int_min);
  36. if (print_or_not) {
  37. std::stringstream ss;
  38. ss << int_min.count() << std::flush;
  39. std::cout << "You have studied for " << ss.str() << " minutes." << std::endl;
  40. }
  41.  
  42. return now;
  43. }
  44.  
  45. //Puts it all toghether.
  46. void Timer::countIt() {
  47. bool first_time = true;
  48. bool keep_sess = true;
  49. bool make_dec = false;
  50. while (keep_sess) {
  51. operateTimeCount(first_time);
  52. std::string ans;
  53. while (!make_dec) {
  54. std::cout << "Would you like to continue? if so press \"yes\", else press \"no\"" << std::endl;
  55. std::cin >> ans;
  56. if (ans == "yes") {
  57. keep_sess = true;
  58. make_dec = true;
  59. }
  60. else if (ans == "no") {
  61. keep_sess = false;
  62. make_dec = true;
  63. }
  64. }
  65. make_dec = false;
  66. first_time = false;
  67. }
  68. }
  69.  
  70. void Timer::operateTimeCount(bool first_time) {
  71. if (!first_time) {
  72. std::cout << "-----------------------------------------\n" << std::endl;
  73. }
  74.  
  75. std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
  76. std::cout << "Starting time is" << std::flush;
  77. timeObj *start = timeCounting(true);
  78. std::cout << "Press enter when you wish to take a break" << std::endl;
  79. std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
  80. timeObj *end = timeCounting(true);
  81. timeObj *diff = compareDiff(start, end, true);
  82. auto int_min = std::chrono::duration_cast<std::chrono::minutes>(*end - *start);
  83. int how_long = int_min.count();
  84. std::string date_sess_str(retDateOfSession(start, end));
  85. std::cout << date_sess_str << std::endl;
  86. study_log.insert(std::make_pair(std::string(date_sess_str), how_long));
  87.  
  88. //Delete all pointed objects from other functions...
  89. safeDelete(start);
  90. safeDelete(end);
  91. safeDelete(diff);
  92. }
  93.  
  94. void Timer::printDate(const timeObj *date) {
  95. auto in_time_t = std::chrono::system_clock::to_time_t(*date);
  96. std::stringstream ss;
  97. ss << std::put_time(std::localtime(&in_time_t), "%Y/%m/%d %X");
  98. std::cout << ss.str() << std::endl;
  99. }
  100.  
  101. std::string Timer::retDateOfSession(const timeObj *date1, const timeObj *date2) {
  102. auto in_time_t1 = std::chrono::system_clock::to_time_t(*date1);
  103. auto in_time_t2 = std::chrono::system_clock::to_time_t(*date2);
  104. std::stringstream ss;
  105. ss << std::put_time(std::localtime(&in_time_t1), "%Y/%m/%d %X") << " - "
  106. << std::put_time(std::localtime(&in_time_t1), "%Y/%m/%d %X") << std::endl;
  107.  
  108. return ss.str();
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement