• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #include <time.h>
2 
3 #include <windows.h>
4 #if defined(_MSC_VER) || defined(_MSC_EXTENSIONS)
5   #define DELTA_EPOCH_IN_MICROSECS  11644473600000000Ui64
6 #else
7   #define DELTA_EPOCH_IN_MICROSECS  11644473600000000ULL
8 #endif
9 
10 #include "config.h"
11 
12 struct timezone
13 {
14   int tz_minuteswest; /* minutes W of Greenwich */
15   int tz_dsttime;     /* type of dst correction */
16 };
17 
gettimeofday(struct timeval * tv,struct timezone * tz)18 int gettimeofday(struct timeval *tv, struct timezone *tz)
19 {
20   FILETIME ft;
21   unsigned __int64 tmpres = 0;
22   static int tzflag;
23 
24   if (NULL != tv) {
25     GetSystemTimeAsFileTime(&ft);
26 
27     tmpres |= ft.dwHighDateTime;
28     tmpres <<= 32;
29     tmpres |= ft.dwLowDateTime;
30 
31     /*converting file time to unix epoch*/
32     tmpres -= DELTA_EPOCH_IN_MICROSECS;
33     tmpres /= 10;  /*convert into microseconds*/
34     tv->tv_sec = (long)(tmpres / 1000000UL);
35     tv->tv_usec = (long)(tmpres % 1000000UL);
36   }
37 
38   if (NULL != tz) {
39     if (!tzflag) {
40       _tzset();
41       tzflag++;
42     }
43     tz->tz_minuteswest = _timezone / 60;
44     tz->tz_dsttime = _daylight;
45   }
46 
47   return 0;
48 }
49 
50 struct tm *
localtime_r(const time_t * timer,struct tm * result)51 localtime_r(const time_t *timer, struct tm *result)
52 {
53   struct tm *local_result;
54 
55   if (result == NULL) {
56     return NULL;
57   }
58 
59   local_result = localtime (timer);
60   if (local_result == NULL) {
61     return NULL;
62   }
63 
64   memcpy(result, local_result, sizeof(*result));
65   return result;
66 }
67