posix_clock.cpp 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. #include<__vic/posix/time.h>
  2. #include<iostream>
  3. #include<exception>
  4. #include<cassert>
  5. #include<ctime>
  6. void run_tests()
  7. {
  8. __vic::posix::time_spec t = __vic::posix::nanoseconds_since_epoch();
  9. char st[64];
  10. std::strftime(st, sizeof st,
  11. "%H:%M:%S %d.%m.%Y", std::localtime(&t.tv_sec));
  12. std::cout << "Current time: " << st << '\n';
  13. #if _POSIX_TIMERS > 0
  14. t = __vic::posix::realtime_clock::get_time();
  15. std::strftime(st, sizeof st,
  16. "%H:%M:%S %d.%m.%Y", std::localtime(&t.tv_sec));
  17. std::cout << "Current (real) time: " << st << '\n';
  18. #ifdef _POSIX_MONOTONIC_CLOCK
  19. std::cout << "Monotonic elapsed: " <<
  20. __vic::posix::monotonic_clock::get_time().tv_sec << " seconds \n";
  21. #endif
  22. #ifdef __VIC_HAVE_STD_CHRONO
  23. std::cout << "Nanoseconds since 1970: " <<
  24. __vic::posix::realtime_clock::now().time_since_epoch().count() << '\n';
  25. #endif
  26. #endif
  27. }
  28. int main()
  29. {
  30. try
  31. {
  32. run_tests();
  33. return 0;
  34. }
  35. catch(const std::exception &ex)
  36. {
  37. std::cerr << ex.what() << '\n';
  38. }
  39. return 1;
  40. }