test_clock.cpp 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. /*
  2. * Copyright (C) 2014 Pavel Kirienko <pavel.kirienko@gmail.com>
  3. */
  4. #include <iostream>
  5. #include <cerrno>
  6. #include <chrono>
  7. #include <uavcan_linux/uavcan_linux.hpp>
  8. static std::string systime2str(const std::chrono::system_clock::time_point& tp)
  9. {
  10. const auto tt = std::chrono::system_clock::to_time_t(tp);
  11. return std::ctime(&tt);
  12. }
  13. int main()
  14. {
  15. uavcan_linux::SystemClock clock;
  16. /*
  17. * Auto-detected clock adjustment mode
  18. */
  19. std::cout << "Clock adjustment mode: ";
  20. switch (clock.getAdjustmentMode())
  21. {
  22. case uavcan_linux::ClockAdjustmentMode::SystemWide:
  23. {
  24. std::cout << "SystemWide";
  25. break;
  26. }
  27. case uavcan_linux::ClockAdjustmentMode::PerDriverPrivate:
  28. {
  29. std::cout << "PerDriverPrivate";
  30. break;
  31. }
  32. default:
  33. {
  34. std::abort();
  35. break;
  36. }
  37. }
  38. std::cout << std::endl;
  39. /*
  40. * Test adjustment
  41. */
  42. double sec = 0;
  43. std::cout << "Enter system time adjustment in seconds (fractions allowed): " << std::endl;
  44. std::cin >> sec;
  45. const auto before = std::chrono::system_clock::now();
  46. try
  47. {
  48. clock.adjustUtc(uavcan::UtcDuration::fromUSec(sec * 1e6));
  49. }
  50. catch (const uavcan_linux::Exception& ex)
  51. {
  52. std::cout << ex.what() << std::endl;
  53. std::cout << strerror(ex.getErrno()) << std::endl;
  54. return 1;
  55. }
  56. const auto after = std::chrono::system_clock::now();
  57. std::cout << "Time before: " << systime2str(before) << "\n"
  58. << "Time after: " << systime2str(after) << "\n"
  59. << "Millisecond diff (after - before): "
  60. << std::chrono::duration_cast<std::chrono::milliseconds>(after - before).count() << std::endl;
  61. return 0;
  62. }