blob: d3696053bc5b54401b595ddf7ddcd6f9d6b8c5ab [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
Antonio Di Monaco796667b2016-01-04 23:05:19 +010024#if defined(__MINGW32__)
25 #include <sys/time.h>
26#endif
27
Antonio Di Monaco796667b2016-01-04 23:05:19 +010028#if !defined(__MINGW32__)
Konrad Grochowski16a23a62014-11-13 15:33:38 +010029struct timezone {
30 int tz_minuteswest; /* minutes W of Greenwich */
31 int tz_dsttime; /* type of dst correction */
Roger Meier84e4a3c2011-09-16 20:58:44 +000032};
Antonio Di Monaco796667b2016-01-04 23:05:19 +010033#endif
Roger Meier84e4a3c2011-09-16 20:58:44 +000034
Antonio Di Monaco796667b2016-01-04 23:05:19 +010035#if defined(__MINGW32__)
36int thrift_gettimeofday(struct timeval* tv, struct timezone* tz) {
37 return gettimeofday(tv,tz);
38}
39#else
James E. King III4eac57a2019-01-14 08:10:42 -050040#define WIN32_LEAN_AND_MEAN
Jeremiah50819ce2022-02-08 12:46:45 +010041#include <winsock2.h>
James E. King III4eac57a2019-01-14 08:10:42 -050042#include <cstdint>
43#include <sstream>
44#include <thrift/transport/TTransportException.h>
Roger Meier84e4a3c2011-09-16 20:58:44 +000045
James E. King III4eac57a2019-01-14 08:10:42 -050046// 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
51int thrift_gettimeofday(struct timeval * tp, struct timezone * tzp)
52{
53 // We don't fill it in so prove nobody is looking for the data
zeshuai00726681fb2020-06-03 17:24:38 +080054 assert(tzp == nullptr);
Roger Meier84e4a3c2011-09-16 20:58:44 +000055
James E. King III4eac57a2019-01-14 08:10:42 -050056 // 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 Meier84e4a3c2011-09-16 20:58:44 +000060
James E. King III4eac57a2019-01-14 08:10:42 -050061 SYSTEMTIME system_time;
62 FILETIME file_time;
63 uint64_t time;
Konrad Grochowski16a23a62014-11-13 15:33:38 +010064
James E. King III4eac57a2019-01-14 08:10:42 -050065 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 Meier84e4a3c2011-09-16 20:58:44 +000072 }
James E. King III4eac57a2019-01-14 08:10:42 -050073 time = static_cast<uint64_t>(file_time.dwLowDateTime ) ;
74 time += static_cast<uint64_t>(file_time.dwHighDateTime) << 32;
Roger Meier84e4a3c2011-09-16 20:58:44 +000075
James E. King III4eac57a2019-01-14 08:10:42 -050076 tp->tv_sec = static_cast<long>((time - EPOCH) / 10000000L);
77 tp->tv_usec = static_cast<long>(system_time.wMilliseconds * 1000);
78 return 0;
Roger Meier84e4a3c2011-09-16 20:58:44 +000079}
Antonio Di Monaco796667b2016-01-04 23:05:19 +010080#endif
Carl Yeksigian7cb7fc82013-06-07 07:33:01 -040081
Konrad Grochowski16a23a62014-11-13 15:33:38 +010082int thrift_sleep(unsigned int seconds) {
Carl Yeksigian7cb7fc82013-06-07 07:33:01 -040083 ::Sleep(seconds * 1000);
84 return 0;
85}
Konrad Grochowski16a23a62014-11-13 15:33:38 +010086int thrift_usleep(unsigned int microseconds) {
87 unsigned int milliseconds = (microseconds + 999) / 1000;
Carl Yeksigian7cb7fc82013-06-07 07:33:01 -040088 ::Sleep(milliseconds);
89 return 0;
90}
91
Konrad Grochowski16a23a62014-11-13 15:33:38 +010092char* thrift_ctime_r(const time_t* _clock, char* _buf) {
93 strcpy(_buf, ctime(_clock));
94 return _buf;
Carl Yeksigian7cb7fc82013-06-07 07:33:01 -040095}