Blame view

src/helpers/time_helper.hpp 1.9 KB
09c2d08c   Hu Chunming   arm交付版
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
  /*
   * @Author: yangzilong
   * @Date: 2021-12-17 17:45:43
   * @Last Modified by: yangzilong
   * @Email: yangzilong@objecteye.com
   * @Description:
   */
  #ifndef __OLFIELD_UTILS_TIMER_HPP__
  #define __OLFIELD_UTILS_TIMER_HPP__
  
  
  #include <chrono>
  #include <string>
  
  #include <mutex>
  #include <atomic>
  #include <thread>
  #include <unordered_map>
  #include <spdlog/spdlog.h>
  
  using namespace std;
  
  namespace helpers
  {
      namespace timer
      {
  
          template <typename T = std::chrono::seconds>
          static inline long long get_timestamp()
          {
              auto now = std::chrono::system_clock::now();
              return std::chrono::duration_cast<T>(now.time_since_epoch()).count();
          }
  
          static long long get_cur_time_ms(){
              chrono::time_point<chrono::system_clock, chrono::milliseconds> tpMicro
                  = chrono::time_point_cast<chrono::milliseconds>(chrono::system_clock::now());
  
              return tpMicro.time_since_epoch().count();
          }
  
          static std::string get_date(bool with_time = false)
          {
              std::chrono::system_clock::time_point t = std::chrono::system_clock::now();
              auto as_time_t = std::chrono::system_clock::to_time_t(t);
              struct tm tm;
      #if defined(WIN32) || defined(_WINDLL)
              localtime_s(&tm, &as_time_t);  // win api thread safe, but std::localtime can't thread safe.
      #else
              localtime_r(&as_time_t, &tm);  // linux api thread safe.
      #endif
              char buf[256]{0};
              if (with_time)
                  snprintf(buf, sizeof(buf), "%04d%02d%02d_%02d%02d%02d", tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec);
              else
                  snprintf(buf, sizeof(buf), "%04d%02d%02d", tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday);
              return std::string(buf);
          }
  
      }  // namespace timer
  }  // namespace helpers
  
  
  
  #endif // __OLFIELD_UTILS_TIMER_HPP__