time.cxx 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. // file : examples/performance/time.cxx
  2. // copyright : not copyrighted - public domain
  3. #include "time.hxx"
  4. #if defined (WIN32) || defined (__WIN32__)
  5. # define WIN32_LEAN_AND_MEAN
  6. # include <windows.h> // GetSystemTimeAsFileTime
  7. #else
  8. # include <time.h> // gettimeofday
  9. # include <sys/time.h> // timeval
  10. #endif
  11. #include <ostream> // std::ostream
  12. #include <iomanip> // std::setfill, std::setw
  13. namespace os
  14. {
  15. time::
  16. time ()
  17. {
  18. #if defined (WIN32) || defined (__WIN32__)
  19. FILETIME ft;
  20. GetSystemTimeAsFileTime (&ft);
  21. unsigned long long v (
  22. ((unsigned long long) (ft.dwHighDateTime) << 32) + ft.dwLowDateTime);
  23. sec_ = static_cast<unsigned long> (v / 10000000ULL);
  24. nsec_ = static_cast<unsigned long> ((v % 10000000ULL) * 100);
  25. #else
  26. timeval tv;
  27. if (gettimeofday(&tv, 0) != 0)
  28. throw failed ();
  29. sec_ = static_cast<unsigned long> (tv.tv_sec);
  30. nsec_ = static_cast<unsigned long> (tv.tv_usec * 1000);
  31. #endif
  32. }
  33. std::ostream&
  34. operator<< (std::ostream& o, time const& t)
  35. {
  36. return o << t.sec () << '.'
  37. << std::setfill ('0') << std::setw (9) << t.nsec ();
  38. }
  39. }