Roger Meier | 84e4a3c | 2011-09-16 20:58:44 +0000 | [diff] [blame] | 1 | /* |
| 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 Meier | 4285ba2 | 2013-06-10 21:17:23 +0200 | [diff] [blame] | 20 | #include <thrift/windows/GetTimeOfDay.h> |
Roger Meier | ff77d07 | 2013-06-28 22:26:43 +0200 | [diff] [blame] | 21 | #include <thrift/thrift-config.h> |
Roger Meier | 84e4a3c | 2011-09-16 20:58:44 +0000 | [diff] [blame] | 22 | |
| 23 | // win32 |
| 24 | #include <time.h> |
| 25 | |
| 26 | #if defined(_MSC_VER) || defined(_MSC_EXTENSIONS) |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 27 | #define DELTA_EPOCH_IN_MICROSECS 11644473600000000Ui64 |
Roger Meier | 84e4a3c | 2011-09-16 20:58:44 +0000 | [diff] [blame] | 28 | #else |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 29 | #define DELTA_EPOCH_IN_MICROSECS 11644473600000000ULL |
Roger Meier | 84e4a3c | 2011-09-16 20:58:44 +0000 | [diff] [blame] | 30 | #endif |
| 31 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 32 | struct timezone { |
| 33 | int tz_minuteswest; /* minutes W of Greenwich */ |
| 34 | int tz_dsttime; /* type of dst correction */ |
Roger Meier | 84e4a3c | 2011-09-16 20:58:44 +0000 | [diff] [blame] | 35 | }; |
| 36 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 37 | int thrift_gettimeofday(struct timeval* tv, struct timezone* tz) { |
| 38 | FILETIME ft; |
| 39 | unsigned __int64 tmpres(0); |
| 40 | static int tzflag; |
Roger Meier | 84e4a3c | 2011-09-16 20:58:44 +0000 | [diff] [blame] | 41 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 42 | if (NULL != tv) { |
| 43 | GetSystemTimeAsFileTime(&ft); |
Roger Meier | 84e4a3c | 2011-09-16 20:58:44 +0000 | [diff] [blame] | 44 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 45 | tmpres |= ft.dwHighDateTime; |
| 46 | tmpres <<= 32; |
| 47 | tmpres |= ft.dwLowDateTime; |
Roger Meier | 84e4a3c | 2011-09-16 20:58:44 +0000 | [diff] [blame] | 48 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 49 | /*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 Meier | 84e4a3c | 2011-09-16 20:58:44 +0000 | [diff] [blame] | 60 | } |
| 61 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 62 | 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 Meier | 84e4a3c | 2011-09-16 20:58:44 +0000 | [diff] [blame] | 68 | } |
| 69 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 70 | 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 Meier | 84e4a3c | 2011-09-16 20:58:44 +0000 | [diff] [blame] | 81 | } |
Carl Yeksigian | 7cb7fc8 | 2013-06-07 07:33:01 -0400 | [diff] [blame] | 82 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 83 | int thrift_sleep(unsigned int seconds) { |
Carl Yeksigian | 7cb7fc8 | 2013-06-07 07:33:01 -0400 | [diff] [blame] | 84 | ::Sleep(seconds * 1000); |
| 85 | return 0; |
| 86 | } |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 87 | int thrift_usleep(unsigned int microseconds) { |
| 88 | unsigned int milliseconds = (microseconds + 999) / 1000; |
Carl Yeksigian | 7cb7fc8 | 2013-06-07 07:33:01 -0400 | [diff] [blame] | 89 | ::Sleep(milliseconds); |
| 90 | return 0; |
| 91 | } |
| 92 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 93 | char* thrift_ctime_r(const time_t* _clock, char* _buf) { |
| 94 | strcpy(_buf, ctime(_clock)); |
| 95 | return _buf; |
Carl Yeksigian | 7cb7fc8 | 2013-06-07 07:33:01 -0400 | [diff] [blame] | 96 | } |