time_tool.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. //
  2. // Created by wk on 2020/9/25.
  3. //
  4. #include "time_tool.h"
  5. Time_tool::Time_tool()
  6. {
  7. }
  8. Time_tool::~Time_tool()
  9. {
  10. Time_tool_uninit();
  11. }
  12. void Time_tool::set_points_digits(int num)
  13. {
  14. std::cout.precision(num);
  15. std::cout.setf(std::ios::fixed);
  16. }
  17. std::chrono::system_clock::time_point Time_tool::get_system_point()
  18. {
  19. return std::chrono::system_clock::now();
  20. }
  21. tm Time_tool::get_current_time_struct()
  22. {
  23. auto now = std::chrono::system_clock::now();
  24. time_t tt = std::chrono::system_clock::to_time_t(now);
  25. tm time_tm=*localtime(&tt);
  26. return time_tm;
  27. }
  28. std::string Time_tool::get_current_time_seconds()
  29. {
  30. auto now = std::chrono::system_clock::now();
  31. time_t tt = std::chrono::system_clock::to_time_t(now);
  32. tm time_tm=*localtime(&tt);
  33. char strTime[100] = "";
  34. sprintf(strTime, "%d-%02d-%02d %02d:%02d:%02d", time_tm.tm_year + 1900,
  35. time_tm.tm_mon + 1, time_tm.tm_mday, time_tm.tm_hour,
  36. time_tm.tm_min, time_tm.tm_sec);
  37. std::string result=strTime;
  38. return result;
  39. }
  40. std::string Time_tool::get_current_time_millisecond()
  41. {
  42. auto now = std::chrono::system_clock::now();
  43. time_t tt = std::chrono::system_clock::to_time_t(now);
  44. tm time_tm=*localtime(&tt);
  45. char strTime[100] = "";
  46. sprintf(strTime, "%d-%02d-%02d %02d:%02d:%02d", time_tm.tm_year + 1900,
  47. time_tm.tm_mon + 1, time_tm.tm_mday, time_tm.tm_hour,
  48. time_tm.tm_min, time_tm.tm_sec);
  49. std::string result=strTime;
  50. //通过不同精度获取相差的毫秒数
  51. uint64_t dis_millseconds = std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch()).count()
  52. - std::chrono::duration_cast<std::chrono::seconds>(now.time_since_epoch()).count() * 1000;
  53. result = result+" "+std::to_string((int)dis_millseconds);
  54. return result;
  55. }
  56. std::string Time_tool::get_current_time_microsecond()
  57. {
  58. auto now = std::chrono::system_clock::now();
  59. time_t tt = std::chrono::system_clock::to_time_t(now);
  60. tm time_tm=*localtime(&tt);
  61. char strTime[100] = "";
  62. sprintf(strTime, "%d-%02d-%02d %02d:%02d:%02d", time_tm.tm_year + 1900,
  63. time_tm.tm_mon + 1, time_tm.tm_mday, time_tm.tm_hour,
  64. time_tm.tm_min, time_tm.tm_sec);
  65. std::string result=strTime;
  66. //通过不同精度获取相差的毫秒数
  67. uint64_t dis_millseconds = std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch()).count()
  68. - std::chrono::duration_cast<std::chrono::seconds>(now.time_since_epoch()).count() * 1000;
  69. //通过不同精度获取相差的微秒
  70. uint64_t dis_microseconds = std::chrono::duration_cast<std::chrono::microseconds>(now.time_since_epoch()).count()
  71. - std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch()).count() * 1000;
  72. result = result+" "+std::to_string((int)dis_millseconds)+" "+std::to_string((int)dis_microseconds);
  73. return result;
  74. }
  75. std::string Time_tool::get_time_string_seconds(std::chrono::system_clock::time_point time_point)
  76. {
  77. time_t tt = std::chrono::system_clock::to_time_t(time_point);
  78. tm time_tm=*localtime(&tt);
  79. char strTime[100] = "";
  80. sprintf(strTime, "%d-%02d-%02d %02d:%02d:%02d", time_tm.tm_year + 1900,
  81. time_tm.tm_mon + 1, time_tm.tm_mday, time_tm.tm_hour,
  82. time_tm.tm_min, time_tm.tm_sec);
  83. std::string result=strTime;
  84. return result;
  85. }
  86. std::string Time_tool::get_time_string_millisecond(std::chrono::system_clock::time_point time_point)
  87. {
  88. time_t tt = std::chrono::system_clock::to_time_t(time_point);
  89. tm time_tm=*localtime(&tt);
  90. char strTime[100] = "";
  91. sprintf(strTime, "%d-%02d-%02d %02d:%02d:%02d", time_tm.tm_year + 1900,
  92. time_tm.tm_mon + 1, time_tm.tm_mday, time_tm.tm_hour,
  93. time_tm.tm_min, time_tm.tm_sec);
  94. std::string result=strTime;
  95. //通过不同精度获取相差的毫秒数
  96. uint64_t dis_millseconds = std::chrono::duration_cast<std::chrono::milliseconds>(time_point.time_since_epoch()).count()
  97. - std::chrono::duration_cast<std::chrono::seconds>(time_point.time_since_epoch()).count() * 1000;
  98. result = result+" "+std::to_string((int)dis_millseconds);
  99. return result;
  100. }
  101. std::string Time_tool::get_time_string_microsecond(std::chrono::system_clock::time_point time_point)
  102. {
  103. time_t tt = std::chrono::system_clock::to_time_t(time_point);
  104. tm time_tm=*localtime(&tt);
  105. char strTime[100] = "";
  106. sprintf(strTime, "%d-%02d-%02d %02d:%02d:%02d", time_tm.tm_year + 1900,
  107. time_tm.tm_mon + 1, time_tm.tm_mday, time_tm.tm_hour,
  108. time_tm.tm_min, time_tm.tm_sec);
  109. std::string result=strTime;
  110. //通过不同精度获取相差的毫秒数
  111. uint64_t dis_millseconds = std::chrono::duration_cast<std::chrono::milliseconds>(time_point.time_since_epoch()).count()
  112. - std::chrono::duration_cast<std::chrono::seconds>(time_point.time_since_epoch()).count() * 1000;
  113. //通过不同精度获取相差的微秒
  114. uint64_t dis_microseconds = std::chrono::duration_cast<std::chrono::microseconds>(time_point.time_since_epoch()).count()
  115. - std::chrono::duration_cast<std::chrono::milliseconds>(time_point.time_since_epoch()).count() * 1000;
  116. result = result+" "+std::to_string((int)dis_millseconds)+" "+std::to_string((int)dis_microseconds);
  117. return result;
  118. }
  119. void Time_tool::time_start(int key)
  120. {
  121. timetool_map[key].t_time_start=get_system_point();//保存开始的时间 //单位为微秒
  122. }
  123. void Time_tool::time_end(int key)
  124. {
  125. if ( timetool_map.find(key)!=timetool_map.end() )
  126. {
  127. timetool_map[key].t_time_end = get_system_point();//保存结束的时间
  128. timetool_map[key].t_time_difference = timetool_map[key].t_time_end - timetool_map[key].t_time_start;//保存时差
  129. }
  130. else
  131. {
  132. std::cout << "计时器:" << key<<"还未开始"<<std::endl;
  133. }
  134. }
  135. void Time_tool::cout_time_seconds(int key)
  136. {
  137. if ( timetool_map.find(key)!=timetool_map.end() )
  138. {
  139. double dieoutTime=(double)timetool_map[key].t_time_difference.count()/1000000000;
  140. std::cout << "计时器:"<<key<<" 计时的时间为:" <<dieoutTime<<" 秒" << std::endl;
  141. }
  142. else
  143. {
  144. std::cout<<"没有此计时器:"<<key<<std::endl;
  145. }
  146. }
  147. void Time_tool::cout_time_millisecond(int key)
  148. {
  149. if ( timetool_map.find(key)!=timetool_map.end() )
  150. {
  151. double dieoutTime=(double)timetool_map[key].t_time_difference.count()/1000000;
  152. std::cout << "计时器:"<<key<<" 计时的时间为:" <<dieoutTime<<" 毫秒" << std::endl;
  153. }
  154. else
  155. {
  156. std::cout<<"没有此计时器:"<<key<<std::endl;
  157. }
  158. }
  159. void Time_tool::cout_time_microsecond(int key)
  160. {
  161. if ( timetool_map.find(key)!=timetool_map.end() )
  162. {
  163. double dieoutTime=(double)timetool_map[key].t_time_difference.count()/1000;
  164. std::cout << "计时器:"<<key<<" 计时的时间为:" <<dieoutTime<<" 微秒" << std::endl;
  165. }
  166. else
  167. {
  168. std::cout<<"没有此计时器:"<<key<<std::endl;
  169. }
  170. }
  171. void Time_tool::cout_time_nanosecond(int key)
  172. {
  173. if ( timetool_map.find(key)!=timetool_map.end() )
  174. {
  175. std::cout << "计时器:"<<key<<" 计时的时间为:" <<timetool_map[key].t_time_difference.count()<<" 纳秒" << std::endl;
  176. }
  177. else
  178. {
  179. std::cout<<"没有此计时器:"<<key<<std::endl;
  180. }
  181. }
  182. void Time_tool::clear_timer()
  183. {
  184. Time_tool_uninit();
  185. }
  186. void Time_tool::Time_tool_uninit()
  187. {
  188. timetool_map.clear();
  189. }