time_tool.cpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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. //转化 时间 精确到秒
  87. std::chrono::system_clock::time_point Time_tool::transform_time_string_seconds(std::string time_string)
  88. {
  89. std::chrono::system_clock::time_point t_time_point;
  90. time_t tt;
  91. tm time_tm;
  92. int t_year = 0;
  93. int t_mon = 0;
  94. sscanf(time_string.c_str(), "%d-%02d-%02d %02d:%02d:%02d", &t_year,
  95. &t_mon, &time_tm.tm_mday, &time_tm.tm_hour,
  96. &time_tm.tm_min, &time_tm.tm_sec);
  97. time_tm.tm_year = t_year -1900;
  98. time_tm.tm_mon = t_mon -1;
  99. time_tm.tm_isdst = 0;
  100. // std::cout << " huli test :::: " << " t_year = " << t_year << std::endl;
  101. // std::cout << " huli test :::: " << " t_mon = " << t_mon << std::endl;
  102. // std::cout << " huli test :::: " << " time_tm.tm_year = " << time_tm.tm_year << std::endl;
  103. // std::cout << " huli test :::: " << " time_tm.tm_mon = " << time_tm.tm_mon << std::endl;
  104. // std::cout << " huli test :::: " << " time_tm.tm_mday = " << time_tm.tm_mday << std::endl;
  105. // std::cout << " huli test :::: " << " time_tm.tm_hour = " << time_tm.tm_hour << std::endl;
  106. // std::cout << " huli test :::: " << " time_tm.tm_min = " << time_tm.tm_min << std::endl;
  107. // std::cout << " huli test :::: " << " time_tm.tm_sec = " << time_tm.tm_sec << std::endl;
  108. tt = mktime(&time_tm);
  109. t_time_point = std::chrono::system_clock::from_time_t(tt);
  110. // std::cout << " huli test :::: " << " time_tm.tt = " << tt << std::endl;
  111. // std::cout << " huli test :::: " << " t_time_point = " << Time_tool::get_instance_references().get_time_string_seconds(t_time_point) << std::endl;
  112. return t_time_point;
  113. }
  114. std::string Time_tool::get_time_string_millisecond(std::chrono::system_clock::time_point time_point)
  115. {
  116. time_t tt = std::chrono::system_clock::to_time_t(time_point);
  117. tm time_tm=*localtime(&tt);
  118. char strTime[100] = "";
  119. sprintf(strTime, "%d-%02d-%02d %02d:%02d:%02d", time_tm.tm_year + 1900,
  120. time_tm.tm_mon + 1, time_tm.tm_mday, time_tm.tm_hour,
  121. time_tm.tm_min, time_tm.tm_sec);
  122. std::string result=strTime;
  123. //通过不同精度获取相差的毫秒数
  124. uint64_t dis_millseconds = std::chrono::duration_cast<std::chrono::milliseconds>(time_point.time_since_epoch()).count()
  125. - std::chrono::duration_cast<std::chrono::seconds>(time_point.time_since_epoch()).count() * 1000;
  126. result = result+" "+std::to_string((int)dis_millseconds);
  127. return result;
  128. }
  129. std::string Time_tool::get_time_string_microsecond(std::chrono::system_clock::time_point time_point)
  130. {
  131. time_t tt = std::chrono::system_clock::to_time_t(time_point);
  132. tm time_tm=*localtime(&tt);
  133. char strTime[100] = "";
  134. sprintf(strTime, "%d-%02d-%02d %02d:%02d:%02d", time_tm.tm_year + 1900,
  135. time_tm.tm_mon + 1, time_tm.tm_mday, time_tm.tm_hour,
  136. time_tm.tm_min, time_tm.tm_sec);
  137. std::string result=strTime;
  138. //通过不同精度获取相差的毫秒数
  139. uint64_t dis_millseconds = std::chrono::duration_cast<std::chrono::milliseconds>(time_point.time_since_epoch()).count()
  140. - std::chrono::duration_cast<std::chrono::seconds>(time_point.time_since_epoch()).count() * 1000;
  141. //通过不同精度获取相差的微秒
  142. uint64_t dis_microseconds = std::chrono::duration_cast<std::chrono::microseconds>(time_point.time_since_epoch()).count()
  143. - std::chrono::duration_cast<std::chrono::milliseconds>(time_point.time_since_epoch()).count() * 1000;
  144. result = result+" "+std::to_string((int)dis_millseconds)+" "+std::to_string((int)dis_microseconds);
  145. return result;
  146. }
  147. void Time_tool::time_start(int key)
  148. {
  149. timetool_map[key].t_time_start=get_system_point();//保存开始的时间 //单位为微秒
  150. }
  151. void Time_tool::time_end(int key)
  152. {
  153. if ( timetool_map.find(key)!=timetool_map.end() )
  154. {
  155. timetool_map[key].t_time_end = get_system_point();//保存结束的时间
  156. timetool_map[key].t_time_difference = timetool_map[key].t_time_end - timetool_map[key].t_time_start;//保存时差
  157. }
  158. else
  159. {
  160. std::cout << "计时器:" << key<<"还未开始"<<std::endl;
  161. }
  162. }
  163. void Time_tool::cout_time_seconds(int key)
  164. {
  165. if ( timetool_map.find(key)!=timetool_map.end() )
  166. {
  167. double dieoutTime=(double)timetool_map[key].t_time_difference.count()/1000000000;
  168. std::cout << "计时器:"<<key<<" 计时的时间为:" <<dieoutTime<<" 秒" << std::endl;
  169. }
  170. else
  171. {
  172. std::cout<<"没有此计时器:"<<key<<std::endl;
  173. }
  174. }
  175. double Time_tool::get_time_seconds(int key)
  176. {
  177. if ( timetool_map.find(key)!=timetool_map.end() )
  178. {
  179. double dieoutTime=(double)timetool_map[key].t_time_difference.count()/1000000000;
  180. return dieoutTime;
  181. }
  182. else
  183. {
  184. return 0;
  185. }
  186. }
  187. void Time_tool::cout_time_millisecond(int key)
  188. {
  189. if ( timetool_map.find(key)!=timetool_map.end() )
  190. {
  191. double dieoutTime=(double)timetool_map[key].t_time_difference.count()/1000000;
  192. std::cout << "计时器:"<<key<<" 计时的时间为:" <<dieoutTime<<" 毫秒" << std::endl;
  193. }
  194. else
  195. {
  196. std::cout<<"没有此计时器:"<<key<<std::endl;
  197. }
  198. }
  199. double Time_tool::get_time_millisecond(int key)
  200. {
  201. if ( timetool_map.find(key)!=timetool_map.end() )
  202. {
  203. double dieoutTime=(double)timetool_map[key].t_time_difference.count()/1000000;
  204. return dieoutTime;
  205. }
  206. else
  207. {
  208. return 0;
  209. }
  210. }
  211. void Time_tool::cout_time_microsecond(int key)
  212. {
  213. if ( timetool_map.find(key)!=timetool_map.end() )
  214. {
  215. double dieoutTime=(double)timetool_map[key].t_time_difference.count()/1000;
  216. std::cout << "计时器:"<<key<<" 计时的时间为:" <<dieoutTime<<" 微秒" << std::endl;
  217. }
  218. else
  219. {
  220. std::cout<<"没有此计时器:"<<key<<std::endl;
  221. }
  222. }
  223. double Time_tool::get_time_microsecond(int key)
  224. {
  225. if ( timetool_map.find(key)!=timetool_map.end() )
  226. {
  227. double dieoutTime=(double)timetool_map[key].t_time_difference.count()/1000;
  228. return dieoutTime;
  229. }
  230. else
  231. {
  232. return 0;
  233. }
  234. }
  235. void Time_tool::cout_time_nanosecond(int key)
  236. {
  237. if ( timetool_map.find(key)!=timetool_map.end() )
  238. {
  239. std::cout << "计时器:"<<key<<" 计时的时间为:" <<timetool_map[key].t_time_difference.count()<<" 纳秒" << std::endl;
  240. }
  241. else
  242. {
  243. std::cout<<"没有此计时器:"<<key<<std::endl;
  244. }
  245. }
  246. double Time_tool::get_time_nanosecond(int key)
  247. {
  248. if ( timetool_map.find(key)!=timetool_map.end() )
  249. {
  250. double dieoutTime=(double)timetool_map[key].t_time_difference.count();
  251. return dieoutTime;
  252. }
  253. else
  254. {
  255. return 0;
  256. }
  257. }
  258. void Time_tool::clear_timer()
  259. {
  260. Time_tool_uninit();
  261. }
  262. void Time_tool::Time_tool_uninit()
  263. {
  264. timetool_map.clear();
  265. }