blob: 654d005923acf9a1f80f550cf8094d6c3e812471 [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)
Konrad Grochowski16a23a62014-11-13 15:33:38 +010027#define DELTA_EPOCH_IN_MICROSECS 11644473600000000Ui64
Roger Meier84e4a3c2011-09-16 20:58:44 +000028#else
Konrad Grochowski16a23a62014-11-13 15:33:38 +010029#define DELTA_EPOCH_IN_MICROSECS 11644473600000000ULL
Roger Meier84e4a3c2011-09-16 20:58:44 +000030#endif
31
Konrad Grochowski16a23a62014-11-13 15:33:38 +010032struct timezone {
33 int tz_minuteswest; /* minutes W of Greenwich */
34 int tz_dsttime; /* type of dst correction */
Roger Meier84e4a3c2011-09-16 20:58:44 +000035};
36
Konrad Grochowski16a23a62014-11-13 15:33:38 +010037int thrift_gettimeofday(struct timeval* tv, struct timezone* tz) {
38 FILETIME ft;
39 unsigned __int64 tmpres(0);
40 static int tzflag;
Roger Meier84e4a3c2011-09-16 20:58:44 +000041
Konrad Grochowski16a23a62014-11-13 15:33:38 +010042 if (NULL != tv) {
43 GetSystemTimeAsFileTime(&ft);
Roger Meier84e4a3c2011-09-16 20:58:44 +000044
Konrad Grochowski16a23a62014-11-13 15:33:38 +010045 tmpres |= ft.dwHighDateTime;
46 tmpres <<= 32;
47 tmpres |= ft.dwLowDateTime;
Roger Meier84e4a3c2011-09-16 20:58:44 +000048
Konrad Grochowski16a23a62014-11-13 15:33:38 +010049 /*converting file time to unix epoch*/
50 tmpres -= DELTA_EPOCH_IN_MICROSECS;
51 tmpres /= 10; /*convert into microseconds*/
52 tv->tv_sec = (long)(tmpres / 1000000UL);
53 tv->tv_usec = (long)(tmpres % 1000000UL);
54 }
55
56 if (NULL != tz) {
57 if (!tzflag) {
58 _tzset();
59 tzflag++;
Roger Meier84e4a3c2011-09-16 20:58:44 +000060 }
61
Konrad Grochowski16a23a62014-11-13 15:33:38 +010062 long time_zone(0);
63 errno_t err(_get_timezone(&time_zone));
64 if (err == NO_ERROR) {
65 tz->tz_minuteswest = time_zone / 60;
66 } else {
67 return -1;
Roger Meier84e4a3c2011-09-16 20:58:44 +000068 }
69
Konrad Grochowski16a23a62014-11-13 15:33:38 +010070 int day_light(0);
71 err = (_get_daylight(&day_light));
72 if (err == NO_ERROR) {
73 tz->tz_dsttime = day_light;
74 return 0;
75 } else {
76 return -1;
77 }
78 }
79
80 return 0;
Roger Meier84e4a3c2011-09-16 20:58:44 +000081}
Carl Yeksigian7cb7fc82013-06-07 07:33:01 -040082
Konrad Grochowski16a23a62014-11-13 15:33:38 +010083int thrift_sleep(unsigned int seconds) {
Carl Yeksigian7cb7fc82013-06-07 07:33:01 -040084 ::Sleep(seconds * 1000);
85 return 0;
86}
Konrad Grochowski16a23a62014-11-13 15:33:38 +010087int thrift_usleep(unsigned int microseconds) {
88 unsigned int milliseconds = (microseconds + 999) / 1000;
Carl Yeksigian7cb7fc82013-06-07 07:33:01 -040089 ::Sleep(milliseconds);
90 return 0;
91}
92
Konrad Grochowski16a23a62014-11-13 15:33:38 +010093char* thrift_ctime_r(const time_t* _clock, char* _buf) {
94 strcpy(_buf, ctime(_clock));
95 return _buf;
Carl Yeksigian7cb7fc82013-06-07 07:33:01 -040096}