test_clock.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2014 Pavel Kirienko <pavel.kirienko@gmail.com>
3  */
4 
5 #include <iostream>
6 #include <cerrno>
7 #include <chrono>
9 
10 static std::string systime2str(const std::chrono::system_clock::time_point& tp)
11 {
12  const auto tt = std::chrono::system_clock::to_time_t(tp);
13  return std::ctime(&tt);
14 }
15 
16 int main()
17 {
19 
20  /*
21  * Auto-detected clock adjustment mode
22  */
23  std::cout << "Clock adjustment mode: ";
24  switch (clock.getAdjustmentMode())
25  {
27  {
28  std::cout << "SystemWide";
29  break;
30  }
32  {
33  std::cout << "PerDriverPrivate";
34  break;
35  }
36  default:
37  {
38  std::abort();
39  break;
40  }
41  }
42  std::cout << std::endl;
43 
44  /*
45  * Test adjustment
46  */
47  double sec = 0;
48  std::cout << "Enter system time adjustment in seconds (fractions allowed): " << std::endl;
49  std::cin >> sec;
50 
51  const auto before = std::chrono::system_clock::now();
52  try
53  {
54  clock.adjustUtc(uavcan::UtcDuration::fromUSec(sec * 1e6));
55  }
56  catch (const uavcan_linux::Exception& ex)
57  {
58  std::cout << ex.what() << std::endl;
59  std::cout << strerror(ex.getErrno()) << std::endl;
60  return 1;
61  }
62  const auto after = std::chrono::system_clock::now();
63 
64  std::cout << "Time before: " << systime2str(before) << "\n"
65  << "Time after: " << systime2str(after) << "\n"
66  << "Millisecond diff (after - before): "
67  << std::chrono::duration_cast<std::chrono::milliseconds>(after - before).count() << std::endl;
68 
69  return 0;
70 }
uavcan_linux::Exception
Definition: exception.hpp:16
uavcan_linux.hpp
uavcan_linux::ClockAdjustmentMode::PerDriverPrivate
@ PerDriverPrivate
Adjust the clock only for the current driver instance.
uavcan_linux::SystemClock::getAdjustmentMode
ClockAdjustmentMode getAdjustmentMode() const
Definition: platform_specific_components/linux/libuavcan/include/uavcan_linux/clock.hpp:166
uavcan_linux::ClockAdjustmentMode::SystemWide
@ SystemWide
Adjust the clock globally for the whole system; requires root privileges.
systime2str
static std::string systime2str(const std::chrono::system_clock::time_point &tp)
Definition: test_clock.cpp:10
uavcan_linux::SystemClock
Definition: platform_specific_components/linux/libuavcan/include/uavcan_linux/clock.hpp:33
uavcan_linux::Exception::getErrno
int getErrno() const
Definition: exception.hpp:35
main
int main()
Definition: test_clock.cpp:16
uavcan_linux::SystemClock::adjustUtc
void adjustUtc(const uavcan::UtcDuration adjustment) override
Definition: platform_specific_components/linux/libuavcan/include/uavcan_linux/clock.hpp:124
uavcan::DurationBase< UtcDuration >::fromUSec
static UtcDuration fromUSec(int64_t us)
Definition: time.hpp:35


uavcan_communicator
Author(s):
autogenerated on Fri Dec 13 2024 03:10:03