DIY  3.0
data-parallel out-of-core C++ library
 All Classes Namespaces Functions Typedefs Groups Pages
time.hpp
1 #ifndef DIY_TIME_HPP
2 #define DIY_TIME_HPP
3 
4 #ifndef _WIN32
5 #include <sys/time.h>
6 #ifdef __MACH__
7 #include <mach/clock.h>
8 #include <mach/mach.h>
9 #endif // __MACH__
10 #endif // ifndef _WIN32
11 
12 namespace diy
13 {
14 
15 typedef unsigned long time_type;
16 
17 inline time_type get_time()
18 {
19 #ifdef __MACH__ // OS X does not have clock_gettime, use clock_get_time
20  clock_serv_t cclock;
21  mach_timespec_t ts;
22  host_get_clock_service(mach_host_self(), CALENDAR_CLOCK, &cclock);
23  clock_get_time(cclock, &ts);
24  mach_port_deallocate(mach_task_self(), cclock);
25  return ts.tv_sec*1000 + static_cast<unsigned int>(ts.tv_nsec/1000000);
26 #elif defined(_WIN32)
27  // SOURCE: http://stackoverflow.com/questions/5404277/porting-clock-gettime-to-windows
28  __int64 wintime;
29  GetSystemTimeAsFileTime((FILETIME*)&wintime);
30  wintime -=116444736000000000i64; //1jan1601 to 1jan1970
31  long tv_sec = static_cast<long>(wintime / 10000000i64); //seconds
32  long tv_nsec = static_cast<long>(wintime % 10000000i64 *100); //nano-seconds
33  return static_cast<time_type>(tv_sec*1000 + tv_nsec/1000000);
34 #else
35  timespec ts;
36  clock_gettime(CLOCK_REALTIME, &ts);
37  return ts.tv_sec*1000 + ts.tv_nsec/1000000;
38 #endif
39 }
40 
41 }
42 
43 #endif