libMesh
win_gettimeofday.h
Go to the documentation of this file.
1 /*
2  * This code is in public domain
3  */
4 
5 #ifndef _WIN_GETTIMEOFDAY_H_
6 #define _WIN_GETTIMEOFDAY_H_
7 
8 #include "libmesh/libmesh_config.h"
9 
10 #ifndef LIBMESH_HAVE_GETTIMEOFDAY
11 #ifdef _MSC_VER
12 #define NOMINMAX
13 #define WIN32_LEAN_AND_MEAN
14 #include <windows.h>
15 #include <stdint.h> // portable: uint64_t MSVC: __int64
16 
17 // MSVC defines this in winsock2.h!?
18 typedef struct timeval {
19  long tv_sec;
20  long tv_usec;
21 } timeval;
22 
23 inline int gettimeofday(struct timeval * tp, struct timezone * tzp)
24 {
25  // This magic number is the number of 100 nanosecond intervals since January 1, 1601 (UTC)
26  // until 00:00:00 January 1, 1970
27  static const uint64_t EPOCH = ((uint64_t) 116444736000000000ULL);
28 
29  SYSTEMTIME system_time;
30  FILETIME file_time;
31  uint64_t time;
32 
33  GetSystemTime(&system_time);
34  SystemTimeToFileTime(&system_time, &file_time);
35  time = ((uint64_t)file_time.dwLowDateTime );
36  time += ((uint64_t)file_time.dwHighDateTime) << 32;
37 
38  tp->tv_sec = (long) ((time - EPOCH) / 10000000L);
39  tp->tv_usec = (long) (system_time.wMilliseconds * 1000);
40  return 0;
41 }
42 #else // _MSC_VER
43 #error "gettimeofday() is not implemented"
44 #endif // _MSC_VER
45 #endif // !LIBMESH_HAVE_GETTIMEOFDAY
46 
47 #endif // _WIN_GETTIMEOFDAY_H_
int gettimeofday(struct timeval *tp, struct timezone *tzp)
struct timeval timeval