seconds.h
Go to the documentation of this file.
1 #ifndef __seconds_h__
2 #define __seconds_h__ 1
3 
4 #ifdef _WIN32
5 #include <Winsock2.h>
6 
7 #include <time.h>
8 #include <windows.h>
9 #include <iostream>
10 
11 #if defined(_MSC_VER) || defined(_MSC_EXTENSIONS)
12 #define DELTA_EPOCH_IN_MICROSECS 11644473600000000Ui64
13 #else
14 #define DELTA_EPOCH_IN_MICROSECS 11644473600000000ULL
15 #endif
16 
17 struct timezone
18 {
19  int tz_minuteswest; /* minutes W of Greenwich */
20  int tz_dsttime; /* type of dst correction */
21 };
22 
23 // Definition of a gettimeofday function
24 
25 inline int gettimeofday(struct timeval *tv, struct timezone *tz)
26 {
27 // Define a structure to receive the current Windows filetime
28  FILETIME ft;
29 
30 // Initialize the present time to 0 and the timezone to UTC
31  unsigned __int64 tmpres = 0;
32  static int tzflag = 0;
33 
34  if (NULL != tv)
35  {
36  GetSystemTimeAsFileTime(&ft);
37 
38 // The GetSystemTimeAsFileTime returns the number of 100 nanosecond
39 // intervals since Jan 1, 1601 in a structure. Copy the high bits to
40 // the 64 bit tmpres, shift it left by 32 then or in the low 32 bits.
41  tmpres |= ft.dwHighDateTime;
42  tmpres <<= 32;
43  tmpres |= ft.dwLowDateTime;
44 
45 // Convert to microseconds by dividing by 10
46  tmpres /= 10;
47 
48 // The Unix epoch starts on Jan 1 1970. Need to subtract the difference
49 // in seconds from Jan 1 1601.
50  tmpres -= DELTA_EPOCH_IN_MICROSECS;
51 
52 // Finally change microseconds to seconds and place in the seconds value.
53 // The modulus picks up the microseconds.
54  tv->tv_sec = (long)(tmpres / 1000000UL);
55  tv->tv_usec = (long)(tmpres % 1000000UL);
56  }
57 
58  if (NULL != tz)
59  {
60  if (!tzflag)
61  {
62  _tzset();
63  tzflag++;
64  }
65 
66 // Adjust for the timezone west of Greenwich
67  tz->tz_minuteswest = _timezone / 60;
68  tz->tz_dsttime = _daylight;
69  }
70 
71  return 0;
72 }
73 
74 #else
75 
76 #include <sys/time.h>
77 
78 #endif
79 
80 namespace utils {
81 
82 inline double totalseconds()
83 {
84  timeval tv;
85  gettimeofday(&tv,NULL);
86  return tv.tv_sec+tv.tv_usec/1000000.0;
87 }
88 
89 inline double seconds()
90 {
91  static double lastseconds=totalseconds();
92  double t=totalseconds();
93  double seconds=t-lastseconds;
94  lastseconds=t;
95  return seconds;
96 }
97 
98 }
99 
100 #endif
double totalseconds()
Definition: seconds.h:82
double seconds()
Definition: seconds.h:89

, generated on Tue Sep 26 2023.