SLikeNet  0.1.3
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
gettimeofday.cpp
Go to the documentation of this file.
1 /*
2  * Original work: Copyright (c) 2014, Oculus VR, Inc.
3  * All rights reserved.
4  *
5  * This source code is licensed under the BSD-style license found in the
6  * RakNet License.txt file in the licenses directory of this source tree. An additional grant
7  * of patent rights can be found in the RakNet Patents.txt file in the same directory.
8  *
9  *
10  * Modified work: Copyright (c) 2016-2017, SLikeSoft UG (haftungsbeschränkt)
11  *
12  * This source code was modified by SLikeSoft. Modifications are licensed under the MIT-style
13  * license found in the license.txt file in the root directory of this source tree.
14  */
15 
16 #if defined(_WIN32) && !defined(__GNUC__) &&!defined(__GCCXML__)
17 
18 #include "slikenet/gettimeofday.h"
19 
20 // From http://www.openasthra.com/c-tidbits/gettimeofday-function-for-windows/
21 
23 
24 #if defined(_MSC_VER) || defined(_MSC_EXTENSIONS)
25  #define DELTA_EPOCH_IN_MICROSECS 11644473600000000Ui64
26 #else
27  #define DELTA_EPOCH_IN_MICROSECS 11644473600000000ULL
28 #endif
29 
30 int gettimeofday(struct timeval *tv, struct timezone *tz)
31 {
32 #if defined(WINDOWS_PHONE_8) || defined(WINDOWS_STORE_RT)
33  // _tzset not supported
34  (void) tv;
35  (void) tz;
36 #else
37 
38  FILETIME ft;
39  unsigned __int64 tmpres = 0;
40  static int tzflag;
41 
42  if (NULL != tv)
43  {
44  GetSystemTimeAsFileTime(&ft);
45 
46  tmpres |= ft.dwHighDateTime;
47  tmpres <<= 32;
48  tmpres |= ft.dwLowDateTime;
49 
50  /*converting file time to unix epoch*/
51  tmpres /= 10; /*convert into microseconds*/
52  tmpres -= DELTA_EPOCH_IN_MICROSECS;
53  tv->tv_sec = (long)(tmpres / 1000000UL);
54  tv->tv_usec = (long)(tmpres % 1000000UL);
55  }
56 
57  if (NULL != tz)
58  {
59  if (!tzflag)
60  {
61  _tzset();
62  tzflag++;
63  }
64  long seconds;
65  _get_timezone(&seconds);
66  tz->tz_minuteswest = seconds / 60;
67  _get_daylight(&(tz->tz_dsttime));
68  }
69 
70 #endif
71 
72  return 0;
73 }
74 
75 #endif
76