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