blob: 190630243bab4cc0bc1a8c86636c2f8edb06c0aa [file] [log] [blame]
Roger Meier84e4a3c2011-09-16 20:58:44 +00001/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
Roger Meier4285ba22013-06-10 21:17:23 +020020#include <thrift/windows/GetTimeOfDay.h>
Roger Meierff77d072013-06-28 22:26:43 +020021#include <thrift/thrift-config.h>
Roger Meier84e4a3c2011-09-16 20:58:44 +000022
23// win32
24#include <time.h>
25
26#if defined(_MSC_VER) || defined(_MSC_EXTENSIONS)
27# define DELTA_EPOCH_IN_MICROSECS 11644473600000000Ui64
28#else
29# define DELTA_EPOCH_IN_MICROSECS 11644473600000000ULL
30#endif
31
32struct timezone
33{
34 int tz_minuteswest; /* minutes W of Greenwich */
35 int tz_dsttime; /* type of dst correction */
36};
37
Carl Yeksigian7cb7fc82013-06-07 07:33:01 -040038int thrift_gettimeofday(struct timeval * tv, struct timezone * tz)
Roger Meier84e4a3c2011-09-16 20:58:44 +000039{
40 FILETIME ft;
41 unsigned __int64 tmpres(0);
42 static int tzflag;
43
44 if (NULL != tv)
45 {
46 GetSystemTimeAsFileTime(&ft);
47
48 tmpres |= ft.dwHighDateTime;
49 tmpres <<= 32;
50 tmpres |= ft.dwLowDateTime;
51
52 /*converting file time to unix epoch*/
Carl Yeksigian7cb7fc82013-06-07 07:33:01 -040053 tmpres -= DELTA_EPOCH_IN_MICROSECS;
Roger Meier84e4a3c2011-09-16 20:58:44 +000054 tmpres /= 10; /*convert into microseconds*/
55 tv->tv_sec = (long)(tmpres / 1000000UL);
56 tv->tv_usec = (long)(tmpres % 1000000UL);
57 }
58
59 if (NULL != tz)
60 {
61 if (!tzflag)
62 {
63 _tzset();
64 tzflag++;
65 }
66
67 long time_zone(0);
68 errno_t err(_get_timezone(&time_zone));
69 if (err == NO_ERROR)
70 {
71 tz->tz_minuteswest = time_zone / 60;
72 }
73 else
74 {
75 return -1;
76 }
77
78 int day_light(0);
79 err = (_get_daylight(&day_light));
80 if (err == NO_ERROR)
81 {
82 tz->tz_dsttime = day_light;
83 return 0;
84 }
85 else
86 {
87 return -1;
88 }
89 }
90
91 return -1;
92}
Carl Yeksigian7cb7fc82013-06-07 07:33:01 -040093
94int thrift_sleep(unsigned int seconds)
95{
96 ::Sleep(seconds * 1000);
97 return 0;
98}
99int thrift_usleep(unsigned int microseconds)
100{
101 unsigned int milliseconds = (microseconds + 999)/ 1000;
102 ::Sleep(milliseconds);
103 return 0;
104}
105
106char *thrift_ctime_r(const time_t *_clock, char *_buf)
107{
108 strcpy(_buf, ctime(_clock));
109 return _buf;
110}
111
112