tool_time.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. #ifndef _tool_time_h_
  2. #define _tool_time_h_
  3. #include <chrono>
  4. class tool_time
  5. {
  6. public:
  7. static uint32_t elapse_seconds(std::chrono::system_clock::time_point &start)
  8. {
  9. return static_cast<uint32_t>( std::chrono::duration_cast<std::chrono::seconds>
  10. (std::chrono::system_clock::now() - start).count() );
  11. }
  12. static uint64_t elapse_ms(std::chrono::system_clock::time_point &start)
  13. {
  14. return static_cast<uint64_t>( std::chrono::duration_cast<std::chrono::milliseconds>
  15. (std::chrono::system_clock::now() - start).count() );
  16. }
  17. static uint32_t now_to_seconds()
  18. {
  19. return static_cast<uint32_t>( std::chrono::duration_cast<std::chrono::seconds>
  20. (std::chrono::system_clock::now().time_since_epoch()).count() );
  21. }
  22. static uint64_t now_to_ms()
  23. {
  24. return static_cast<uint64_t>( std::chrono::duration_cast<std::chrono::milliseconds>
  25. (std::chrono::system_clock::now().time_since_epoch()).count() );
  26. }
  27. static uint64_t now_to_us()
  28. {
  29. return static_cast<uint64_t>( std::chrono::duration_cast<std::chrono::microseconds>
  30. (std::chrono::system_clock::now().time_since_epoch()).count() );
  31. }
  32. static uint64_t to_ms(const std::chrono::system_clock::time_point &time)
  33. {
  34. return static_cast<uint64_t>( std::chrono::duration_cast<std::chrono::milliseconds>
  35. (time.time_since_epoch()).count() );
  36. }
  37. static std::string to_str(const std::chrono::system_clock::time_point &time)
  38. {
  39. char _time[25] = {0};
  40. time_t tt = std::chrono::system_clock::to_time_t(time);
  41. struct tm local_time={0};
  42. localtime_r(&tt, &local_time);
  43. strftime(_time, 22, "%Y-%m-%d %H:%M:%S", &local_time);
  44. return std::string(_time);
  45. }
  46. static uint64_t morning_of_today_ms()
  47. {
  48. time_t now = time(0);
  49. return ((now+8*3600)/86400*86400-8*3600)*1000;
  50. //time_t now = time(0);
  51. //struct tm * loc_t = localtime(&now);
  52. //loc_t->tm_hour=0;loc_t->tm_min=0;loc_t->tm_sec=0;
  53. //time_t x = mktime(loc_t);
  54. //return x*1000;
  55. }
  56. //"%Y-%m-%d %H:%M:%S"
  57. static time_t to_time(const std::string&str)
  58. {
  59. time_t t_;
  60. tm tm_={0};
  61. strptime(str.c_str(), "%Y-%m-%d %H:%M:%S", &tm_); //将字符串转换为tm时间
  62. t_ = mktime(&tm_); //将tm时间转换为秒时间
  63. return t_;
  64. }
  65. ////"%d-%02d-%02d %02d:%02d:%02d.%03d"
  66. static std::chrono::system_clock::time_point to_time_ex(const std::string&str)
  67. {
  68. uint64_t pos = str.length()-3;
  69. time_t t_;
  70. tm tm_={0};
  71. strptime(str.substr(0,pos).c_str(), "%Y-%m-%d %H:%M:%S", &tm_); //将字符串转换为tm时间
  72. t_ = mktime(&tm_); //将tm时间转换为秒时间
  73. int milli = std::stoi(str.substr(pos));
  74. return std::chrono::system_clock::time_point(std::chrono::milliseconds(t_*1000 + milli));
  75. }
  76. //"%d-%02d-%02d %02d:%02d:%02d.%03d"
  77. static std::string to_str_ex(uint64_t ms)
  78. {
  79. int32_t mill = ms%1000;
  80. char _time[25] = {0};
  81. time_t tt = ms/1000;
  82. struct tm *local_time=localtime(&tt);
  83. //strftime(_time, 22, "%Y-%m-%d %H:%M:%S", local_time);
  84. sprintf(_time, "%d-%02d-%02d %02d:%02d:%02d.%03d", local_time->tm_year+1900,
  85. local_time->tm_mon+1, local_time->tm_mday, local_time->tm_hour,
  86. local_time->tm_min, local_time->tm_sec, mill);
  87. return std::string(_time);
  88. }
  89. static std::string to_str_ex(struct tm* t)
  90. {
  91. if(t == nullptr){
  92. return "";
  93. }
  94. char _time[25] = {0};
  95. sprintf(_time, "%d-%02d-%02d %02d:%02d:%02d", t->tm_year+1900,
  96. t->tm_mon+1, t->tm_mday, t->tm_hour,
  97. t->tm_min, t->tm_sec);
  98. return std::string(_time);
  99. }
  100. //"%d-%02d-%02d %02d:%02d:%02d.%03d"
  101. static std::string to_str_ex(std::chrono::system_clock::time_point time)
  102. {
  103. return to_str_ex(to_ms(time));
  104. }
  105. static int elapse_seconds(time_t &start)
  106. {
  107. time_t now;
  108. time(&now);
  109. return static_cast<int>(std::difftime(now, start));
  110. }
  111. static std::string now_to_str()
  112. {
  113. std::time_t t=time(0);
  114. return to_str(t);
  115. }
  116. //"%Y-%m-%d %H:%M:%S"
  117. static std::string to_str(const std::time_t &time)
  118. {
  119. char _time[25] = {0};
  120. struct tm local_time;
  121. localtime_r(&time, &local_time);
  122. strftime(_time, 22, "%Y-%m-%d %H:%M:%S", &local_time);
  123. return std::string(_time);
  124. }
  125. //"%H:%M:%S"
  126. static std::string to_str_time(const std::time_t &time)
  127. {
  128. char _time[25] = {0};
  129. struct tm local_time;
  130. localtime_r(&time, &local_time);
  131. strftime(_time, 22, "%H:%M:%S", &local_time);
  132. return std::string(_time);
  133. }
  134. static int time_to_day_seconds(uint64_t t)
  135. {
  136. std::time_t _time = t;
  137. if (_time == 0)
  138. {
  139. _time = tool_time::now_to_seconds();
  140. }
  141. struct tm local_time;
  142. localtime_r(&_time, &local_time);
  143. return local_time.tm_hour * 3600 + local_time.tm_min * 60 + local_time.tm_sec;
  144. }
  145. //t 格式:05:02:02
  146. static int time_to_day_seconds(const std::string &t)
  147. {
  148. int hour = 0,min = 0,sec = 0;
  149. sscanf(t.c_str(), "%d:%d:%d", &hour,&min,&sec);
  150. return hour * 3600 + min * 60 + sec;
  151. }
  152. static int get_hour()
  153. {
  154. time_t tt = time(0);
  155. struct tm *local_time=localtime(&tt);
  156. return local_time->tm_hour;
  157. }
  158. };
  159. #endif