time_tool.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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 time_tm = get_current_time_struct();
  31. char strTime[100] = "";
  32. sprintf(strTime, "%d-%02d-%02d %02d:%02d:%02d", time_tm.tm_year + 1900,
  33. time_tm.tm_mon + 1, time_tm.tm_mday, time_tm.tm_hour,
  34. time_tm.tm_min, time_tm.tm_sec);
  35. std::string str=strTime;
  36. return str;
  37. }
  38. std::string Time_tool::get_current_time_millisecond()
  39. {
  40. auto now = std::chrono::system_clock::now();
  41. //通过不同精度获取相差的毫秒数
  42. uint64_t dis_millseconds = std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch()).count()
  43. - std::chrono::duration_cast<std::chrono::seconds>(now.time_since_epoch()).count() * 1000;
  44. std::string strTime=get_current_time_seconds()+" "+std::to_string((int)dis_millseconds);
  45. return strTime;
  46. }
  47. std::string Time_tool::get_current_time_microsecond()
  48. {
  49. auto now = std::chrono::system_clock::now();
  50. //通过不同精度获取相差的微秒
  51. uint64_t dis_microseconds = std::chrono::duration_cast<std::chrono::microseconds>(now.time_since_epoch()).count()
  52. - std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch()).count() * 1000;
  53. std::string strTime=get_current_time_millisecond()+" "+std::to_string((int)dis_microseconds);
  54. return strTime;
  55. }
  56. void Time_tool::time_start(int key)
  57. {
  58. timetool_map[key].t_time_start=get_system_point();//保存开始的时间 //单位为微秒
  59. }
  60. void Time_tool::time_end(int key)
  61. {
  62. if ( timetool_map.find(key)!=timetool_map.end() )
  63. {
  64. timetool_map[key].t_time_end = get_system_point();//保存结束的时间
  65. timetool_map[key].t_time_difference = timetool_map[key].t_time_end - timetool_map[key].t_time_start;//保存时差
  66. }
  67. else
  68. {
  69. std::cout << "计时器:" << key<<"还未开始"<<std::endl;
  70. }
  71. }
  72. void Time_tool::cout_time_seconds(int key)
  73. {
  74. if ( timetool_map.find(key)!=timetool_map.end() )
  75. {
  76. double dieoutTime=(double)timetool_map[key].t_time_difference.count()/1000000000;
  77. std::cout << "计时器:"<<key<<" 计时的时间为:" <<dieoutTime<<" 秒" << std::endl;
  78. }
  79. else
  80. {
  81. std::cout<<"没有此计时器:"<<key<<std::endl;
  82. }
  83. }
  84. void Time_tool::cout_time_millisecond(int key)
  85. {
  86. if ( timetool_map.find(key)!=timetool_map.end() )
  87. {
  88. double dieoutTime=(double)timetool_map[key].t_time_difference.count()/1000000;
  89. std::cout << "计时器:"<<key<<" 计时的时间为:" <<dieoutTime<<" 毫秒" << std::endl;
  90. }
  91. else
  92. {
  93. std::cout<<"没有此计时器:"<<key<<std::endl;
  94. }
  95. }
  96. void Time_tool::cout_time_microsecond(int key)
  97. {
  98. if ( timetool_map.find(key)!=timetool_map.end() )
  99. {
  100. double dieoutTime=(double)timetool_map[key].t_time_difference.count()/1000;
  101. std::cout << "计时器:"<<key<<" 计时的时间为:" <<dieoutTime<<" 微秒" << std::endl;
  102. }
  103. else
  104. {
  105. std::cout<<"没有此计时器:"<<key<<std::endl;
  106. }
  107. }
  108. void Time_tool::cout_time_nanosecond(int key)
  109. {
  110. if ( timetool_map.find(key)!=timetool_map.end() )
  111. {
  112. std::cout << "计时器:"<<key<<" 计时的时间为:" <<timetool_map[key].t_time_difference.count()<<" 纳秒" << std::endl;
  113. }
  114. else
  115. {
  116. std::cout<<"没有此计时器:"<<key<<std::endl;
  117. }
  118. }
  119. void Time_tool::clear_timer()
  120. {
  121. Time_tool_uninit();
  122. }
  123. void Time_tool::Time_tool_uninit()
  124. {
  125. timetool_map.clear();
  126. }