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 |
Antonio Di Monaco | 796667b | 2016-01-04 23:05:19 +0100 | [diff] [blame] | 24 | #if defined(__MINGW32__) |
| 25 | #include <sys/time.h> |
| 26 | #endif |
| 27 | |
Antonio Di Monaco | 796667b | 2016-01-04 23:05:19 +0100 | [diff] [blame] | 28 | #if !defined(__MINGW32__) |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 29 | struct timezone { |
| 30 | int tz_minuteswest; /* minutes W of Greenwich */ |
| 31 | int tz_dsttime; /* type of dst correction */ |
Roger Meier | 84e4a3c | 2011-09-16 20:58:44 +0000 | [diff] [blame] | 32 | }; |
Antonio Di Monaco | 796667b | 2016-01-04 23:05:19 +0100 | [diff] [blame] | 33 | #endif |
Roger Meier | 84e4a3c | 2011-09-16 20:58:44 +0000 | [diff] [blame] | 34 | |
Antonio Di Monaco | 796667b | 2016-01-04 23:05:19 +0100 | [diff] [blame] | 35 | #if defined(__MINGW32__) |
| 36 | int thrift_gettimeofday(struct timeval* tv, struct timezone* tz) { |
| 37 | return gettimeofday(tv,tz); |
| 38 | } |
| 39 | #else |
James E. King III | 4eac57a | 2019-01-14 08:10:42 -0500 | [diff] [blame] | 40 | #define WIN32_LEAN_AND_MEAN |
Jeremiah | 50819ce | 2022-02-08 12:46:45 +0100 | [diff] [blame] | 41 | #include <winsock2.h> |
James E. King III | 4eac57a | 2019-01-14 08:10:42 -0500 | [diff] [blame] | 42 | #include <cstdint> |
| 43 | #include <sstream> |
| 44 | #include <thrift/transport/TTransportException.h> |
Roger Meier | 84e4a3c | 2011-09-16 20:58:44 +0000 | [diff] [blame] | 45 | |
James E. King III | 4eac57a | 2019-01-14 08:10:42 -0500 | [diff] [blame] | 46 | // This code started from a "FREE implementation" posted to Stack Overflow at: |
| 47 | // https://stackoverflow.com/questions/10905892/equivalent-of-gettimeday-for-windows |
| 48 | // added: assert |
| 49 | // added: error handling |
| 50 | // added: C++ style casts |
| 51 | int thrift_gettimeofday(struct timeval * tp, struct timezone * tzp) |
| 52 | { |
| 53 | // We don't fill it in so prove nobody is looking for the data |
zeshuai007 | 26681fb | 2020-06-03 17:24:38 +0800 | [diff] [blame] | 54 | assert(tzp == nullptr); |
Roger Meier | 84e4a3c | 2011-09-16 20:58:44 +0000 | [diff] [blame] | 55 | |
James E. King III | 4eac57a | 2019-01-14 08:10:42 -0500 | [diff] [blame] | 56 | // Note: some broken versions only have 8 trailing zero's, the correct epoch has 9 trailing zero's |
| 57 | // This magic number is the number of 100 nanosecond intervals since January 1, 1601 (UTC) |
| 58 | // until 00:00:00 January 1, 1970 |
| 59 | static const uint64_t EPOCH = static_cast<uint64_t>(116444736000000000ULL); |
Roger Meier | 84e4a3c | 2011-09-16 20:58:44 +0000 | [diff] [blame] | 60 | |
James E. King III | 4eac57a | 2019-01-14 08:10:42 -0500 | [diff] [blame] | 61 | SYSTEMTIME system_time; |
| 62 | FILETIME file_time; |
| 63 | uint64_t time; |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 64 | |
James E. King III | 4eac57a | 2019-01-14 08:10:42 -0500 | [diff] [blame] | 65 | GetSystemTime( &system_time ); |
| 66 | if (!SystemTimeToFileTime( &system_time, &file_time )) { |
| 67 | DWORD lastError = GetLastError(); |
| 68 | std::stringstream ss; |
| 69 | ss << "SystemTimeToFileTime failed: 0x" << std::hex << lastError; |
| 70 | using apache::thrift::transport::TTransportException; |
| 71 | throw TTransportException(TTransportException::INTERNAL_ERROR, ss.str()); |
Roger Meier | 84e4a3c | 2011-09-16 20:58:44 +0000 | [diff] [blame] | 72 | } |
James E. King III | 4eac57a | 2019-01-14 08:10:42 -0500 | [diff] [blame] | 73 | time = static_cast<uint64_t>(file_time.dwLowDateTime ) ; |
| 74 | time += static_cast<uint64_t>(file_time.dwHighDateTime) << 32; |
Roger Meier | 84e4a3c | 2011-09-16 20:58:44 +0000 | [diff] [blame] | 75 | |
James E. King III | 4eac57a | 2019-01-14 08:10:42 -0500 | [diff] [blame] | 76 | tp->tv_sec = static_cast<long>((time - EPOCH) / 10000000L); |
| 77 | tp->tv_usec = static_cast<long>(system_time.wMilliseconds * 1000); |
| 78 | return 0; |
Roger Meier | 84e4a3c | 2011-09-16 20:58:44 +0000 | [diff] [blame] | 79 | } |
Antonio Di Monaco | 796667b | 2016-01-04 23:05:19 +0100 | [diff] [blame] | 80 | #endif |
Carl Yeksigian | 7cb7fc8 | 2013-06-07 07:33:01 -0400 | [diff] [blame] | 81 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 82 | int thrift_sleep(unsigned int seconds) { |
Carl Yeksigian | 7cb7fc8 | 2013-06-07 07:33:01 -0400 | [diff] [blame] | 83 | ::Sleep(seconds * 1000); |
| 84 | return 0; |
| 85 | } |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 86 | int thrift_usleep(unsigned int microseconds) { |
| 87 | unsigned int milliseconds = (microseconds + 999) / 1000; |
Carl Yeksigian | 7cb7fc8 | 2013-06-07 07:33:01 -0400 | [diff] [blame] | 88 | ::Sleep(milliseconds); |
| 89 | return 0; |
| 90 | } |
| 91 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 92 | char* thrift_ctime_r(const time_t* _clock, char* _buf) { |
| 93 | strcpy(_buf, ctime(_clock)); |
| 94 | return _buf; |
Carl Yeksigian | 7cb7fc8 | 2013-06-07 07:33:01 -0400 | [diff] [blame] | 95 | } |