artdaq_core  v3_06_00
TimeUtils.hh
1 #ifndef artdaq_core_Utilities_TimeUtils_h
2 #define artdaq_core_Utilities_TimeUtils_h
3 
4 #include <sys/time.h>
5 #include <chrono>
6 #include <string>
7 
8 namespace artdaq {
12 namespace TimeUtils {
22 typedef std::chrono::duration<double, std::ratio<1>> seconds;
23 
30 inline constexpr double GetElapsedTime(std::chrono::steady_clock::time_point then, std::chrono::steady_clock::time_point now = std::chrono::steady_clock::now())
31 {
32  return std::chrono::duration_cast<seconds>(now - then).count();
33 }
34 
41 inline constexpr size_t GetElapsedTimeMicroseconds(std::chrono::steady_clock::time_point then, std::chrono::steady_clock::time_point now = std::chrono::steady_clock::now())
42 {
43  return static_cast<size_t>(GetElapsedTime(then, now) * 1000000);
44 }
45 
52 inline constexpr size_t GetElapsedTimeMilliseconds(std::chrono::steady_clock::time_point then, std::chrono::steady_clock::time_point now = std::chrono::steady_clock::now())
53 {
54  return static_cast<size_t>(GetElapsedTime(then, now) * 1000);
55 }
56 
62 std::string convertUnixTimeToString(time_t inputUnixTime);
63 
69 std::string convertUnixTimeToString(struct timeval const& inputUnixTime);
70 
76 std::string convertUnixTimeToString(struct timespec const& inputUnixTime);
77 
82 uint64_t gettimeofday_us();
83 
88 struct timespec get_realtime_clock();
89 
95 double convertUnixTimeToSeconds(time_t inputUnixTime);
96 
102 double convertUnixTimeToSeconds(struct timeval const& inputUnixTime);
103 
109 double convertUnixTimeToSeconds(struct timespec const& inputUnixTime);
110 } // namespace TimeUtils
111 } // namespace artdaq
112 
113 #endif /* artdaq_core_Utilities_TimeUtils_h */
114 
115 // Local Variables:
116 // mode: c++
117 // End:
constexpr size_t GetElapsedTimeMilliseconds(std::chrono::steady_clock::time_point then, std::chrono::steady_clock::time_point now=std::chrono::steady_clock::now())
Gets the number of milliseconds in the given time interval
Definition: TimeUtils.hh:52
constexpr size_t GetElapsedTimeMicroseconds(std::chrono::steady_clock::time_point then, std::chrono::steady_clock::time_point now=std::chrono::steady_clock::now())
Gets the number of microseconds in the given time interval
Definition: TimeUtils.hh:41
struct timespec get_realtime_clock()
Get the current time of day as a pair of seconds and nanoseconds (from clock_gettime(CLOCK_REALTIME, ...) system call)
Definition: TimeUtils.cc:58
double convertUnixTimeToSeconds(time_t inputUnixTime)
Converts a Unix time to double.
Definition: TimeUtils.cc:66
constexpr double GetElapsedTime(std::chrono::steady_clock::time_point then, std::chrono::steady_clock::time_point now=std::chrono::steady_clock::now())
Get the number of seconds in the given interval
Definition: TimeUtils.hh:30
std::chrono::duration< double, std::ratio< 1 > > seconds
Definition: TimeUtils.hh:22
std::string convertUnixTimeToString(time_t inputUnixTime)
Converts a Unix time to its string representation, in UTC.
Definition: TimeUtils.cc:7
uint64_t gettimeofday_us()
Get the current time of day in microseconds (from gettimeofday system call)
Definition: TimeUtils.cc:51