2 * Copyright 2016 Facebook, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #include <folly/portability/Time.h>
19 #if !FOLLY_HAVE_CLOCK_GETTIME
22 #include <mach/mach_time.h>
24 static const mach_timebase_info_data_t* tbInfo() {
25 static auto info = [] {
26 static mach_timebase_info_data_t info;
27 return (mach_timebase_info(&info) == KERN_SUCCESS) ? &info : nullptr;
32 int clock_gettime(clockid_t clk_id, struct timespec* ts) {
33 auto tb_info = tbInfo();
34 if (tb_info == nullptr) {
39 uint64_t now_ticks = mach_absolute_time();
40 uint64_t now_ns = (now_ticks * tb_info->numer) / tb_info->denom;
41 ts->tv_sec = now_ns / 1000000000;
42 ts->tv_nsec = now_ns % 1000000000;
47 int clock_getres(clockid_t clk_id, struct timespec* ts) {
48 auto tb_info = tbInfo();
49 if (tb_info == nullptr) {
55 ts->tv_nsec = tb_info->numer / tb_info->denom;
64 #include <folly/portability/Windows.h>
66 static constexpr size_t kNsPerSec = 1000000000;
68 extern "C" int clock_getres(clockid_t clock_id, struct timespec* res) {
75 case CLOCK_MONOTONIC: {
77 if (!QueryPerformanceFrequency(&freq)) {
83 res->tv_nsec = (long)((kNsPerSec + (freq.QuadPart >> 1)) / freq.QuadPart);
84 if (res->tv_nsec < 1) {
92 case CLOCK_PROCESS_CPUTIME_ID:
93 case CLOCK_THREAD_CPUTIME_ID: {
94 DWORD adj, timeIncrement;
96 if (!GetSystemTimeAdjustment(&adj, &timeIncrement, &adjDisabled)) {
102 res->tv_nsec = timeIncrement * 100;
112 extern "C" int clock_gettime(clockid_t clock_id, struct timespec* tp) {
118 const auto ftToUint = [](FILETIME ft) -> uint64_t {
120 i.HighPart = ft.dwHighDateTime;
121 i.LowPart = ft.dwLowDateTime;
124 const auto timeToTimespec = [](timespec* tp, uint64_t t) -> int {
125 constexpr size_t k100NsPerSec = kNsPerSec / 100;
127 // The filetimes t is based on are represented in
128 // 100ns's. (ie. a value of 4 is 400ns)
129 tp->tv_sec = t / k100NsPerSec;
130 tp->tv_nsec = ((long)(t % k100NsPerSec)) * 100;
134 FILETIME createTime, exitTime, kernalTime, userTime;
136 case CLOCK_REALTIME: {
137 constexpr size_t kDeltaEpochIn100NS = 116444736000000000ULL;
139 GetSystemTimeAsFileTime(&createTime);
140 return timeToTimespec(tp, ftToUint(createTime) - kDeltaEpochIn100NS);
142 case CLOCK_PROCESS_CPUTIME_ID: {
143 if (!GetProcessTimes(
153 return timeToTimespec(tp, ftToUint(kernalTime) + ftToUint(userTime));
155 case CLOCK_THREAD_CPUTIME_ID: {
166 return timeToTimespec(tp, ftToUint(kernalTime) + ftToUint(userTime));
168 case CLOCK_MONOTONIC: {
169 LARGE_INTEGER fl, cl;
170 if (!QueryPerformanceFrequency(&fl) || !QueryPerformanceCounter(&cl)) {
175 int64_t freq = fl.QuadPart;
176 int64_t counter = cl.QuadPart;
177 tp->tv_sec = counter / freq;
178 tp->tv_nsec = (long)(((counter % freq) * kNsPerSec + (freq >> 1)) / freq);
179 if (tp->tv_nsec >= kNsPerSec) {
181 tp->tv_nsec -= kNsPerSec;
193 #error No clock_gettime(3) compatibility wrapper available for this platform.
201 #include <folly/portability/Windows.h>
204 char* asctime_r(const tm* tm, char* buf) {
206 if (asctime_s(tmpBuf, tm)) {
209 // Nothing we can do if the buff is to small :(
210 return strcpy(buf, tmpBuf);
213 char* ctime_r(const time_t* t, char* buf) {
215 if (ctime_s(tmpBuf, t)) {
218 // Nothing we can do if the buff is to small :(
219 return strcpy(buf, tmpBuf);
222 tm* gmtime_r(const time_t* t, tm* res) {
223 if (!gmtime_s(res, t)) {
229 tm* localtime_r(const time_t* t, tm* o) {
230 if (!localtime_s(o, t)) {
236 int nanosleep(const struct timespec* request, struct timespec* remain) {
237 Sleep((DWORD)((request->tv_sec * 1000) + (request->tv_nsec / 1000000));
243 char* strptime(const char* __restrict s,
244 const char* __restrict f,
245 struct tm* __restrict tm) {
246 // Isn't the C++ standard lib nice? std::get_time is defined such that its
247 // format parameters are the exact same as strptime. Of course, we have to
248 // create a string stream first, and imbue it with the current C locale, and
249 // we also have to make sure we return the right things if it fails, or
250 // if it succeeds, but this is still far simpler an implementation than any
251 // of the versions in any of the C standard libraries.
252 std::istringstream input(s);
253 input.imbue(std::locale(setlocale(LC_ALL, nullptr)));
254 input >> std::get_time(tm, f);
258 return const_cast<char*>(s + input.tellg());