artdaq_core  v3_06_09
TimeUtils_t.cc
1 #include "artdaq-core/Utilities/TimeUtils.hh"
2 
3 #define BOOST_TEST_MODULE TimeUtils_t
4 #include <cmath>
5 #include "cetlib/quiet_unit_test.hpp"
6 #include "cetlib_except/exception.h"
7 
8 #define TRACE_NAME "TimeUtils_t"
9 #include "tracemf.h"
10 
11 BOOST_AUTO_TEST_SUITE(TimeUtils_test)
12 
13 BOOST_AUTO_TEST_CASE(GetElapsedTime)
14 {
15  auto then = std::chrono::steady_clock::now();
16  auto now = then + std::chrono::seconds(1);
17 
18  BOOST_REQUIRE_EQUAL(artdaq::TimeUtils::GetElapsedTime(then, now), 1);
19  BOOST_REQUIRE_EQUAL(artdaq::TimeUtils::GetElapsedTimeMilliseconds(then, now), 1000);
20  BOOST_REQUIRE_EQUAL(artdaq::TimeUtils::GetElapsedTimeMicroseconds(then, now), 1000000);
21 }
22 
23 BOOST_AUTO_TEST_CASE(UnixTime)
24 {
25  time_t t = time(0);
26  struct timeval tv;
27  gettimeofday(&tv, nullptr);
28  struct timespec ts = artdaq::TimeUtils::get_realtime_clock();
29 
30  auto timeString = artdaq::TimeUtils::convertUnixTimeToString(t);
31  TLOG(TLVL_INFO) << "time_t to string: " << timeString;
33  TLOG(TLVL_INFO) << "time_t to seconds: " << timeDouble;
34 
35  auto valString = artdaq::TimeUtils::convertUnixTimeToString(tv);
36  TLOG(TLVL_INFO) << "timeval to string: " << valString;
38  TLOG(TLVL_INFO) << "timeval to seconds: " << valDouble;
39 
40  auto specString = artdaq::TimeUtils::convertUnixTimeToString(ts);
41  TLOG(TLVL_INFO) << "timespec to string: " << specString;
42  auto specDouble = artdaq::TimeUtils::convertUnixTimeToSeconds(ts);
43  TLOG(TLVL_INFO) << "timespec to seconds: " << specDouble;
44 
45  BOOST_REQUIRE_EQUAL(timeDouble, std::floor(valDouble));
46  BOOST_REQUIRE_EQUAL(timeDouble, std::floor(specDouble));
47 }
48 
49 BOOST_AUTO_TEST_CASE(GetTimeOfDayUS)
50 {
52  struct timespec ts = artdaq::TimeUtils::get_realtime_clock();
53  BOOST_REQUIRE_EQUAL(now / 1000000, ts.tv_sec);
54 }
55 
56 BOOST_AUTO_TEST_SUITE_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::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