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;
65 #include <folly/portability/Windows.h>
67 static constexpr size_t kNsPerSec = 1000000000;
69 extern "C" int clock_getres(clockid_t clock_id, struct timespec* res) {
76 case CLOCK_MONOTONIC: {
78 if (!QueryPerformanceFrequency(&freq)) {
84 res->tv_nsec = (long)((kNsPerSec + (freq.QuadPart >> 1)) / freq.QuadPart);
85 if (res->tv_nsec < 1) {
93 case CLOCK_PROCESS_CPUTIME_ID:
94 case CLOCK_THREAD_CPUTIME_ID: {
95 DWORD adj, timeIncrement;
97 if (!GetSystemTimeAdjustment(&adj, &timeIncrement, &adjDisabled)) {
103 res->tv_nsec = timeIncrement * 100;
113 extern "C" int clock_gettime(clockid_t clock_id, struct timespec* tp) {
119 const auto ftToUint = [](FILETIME ft) -> uint64_t {
121 i.HighPart = ft.dwHighDateTime;
122 i.LowPart = ft.dwLowDateTime;
125 const auto timeToTimespec = [](timespec* tp, uint64_t t) -> int {
126 constexpr size_t k100NsPerSec = kNsPerSec / 100;
128 // The filetimes t is based on are represented in
129 // 100ns's. (ie. a value of 4 is 400ns)
130 tp->tv_sec = t / k100NsPerSec;
131 tp->tv_nsec = ((long)(t % k100NsPerSec)) * 100;
135 FILETIME createTime, exitTime, kernalTime, userTime;
137 case CLOCK_REALTIME: {
138 constexpr size_t kDeltaEpochIn100NS = 116444736000000000ULL;
140 GetSystemTimeAsFileTime(&createTime);
141 return timeToTimespec(tp, ftToUint(createTime) - kDeltaEpochIn100NS);
143 case CLOCK_PROCESS_CPUTIME_ID: {
144 if (!GetProcessTimes(
154 return timeToTimespec(tp, ftToUint(kernalTime) + ftToUint(userTime));
156 case CLOCK_THREAD_CPUTIME_ID: {
167 return timeToTimespec(tp, ftToUint(kernalTime) + ftToUint(userTime));
169 case CLOCK_MONOTONIC: {
170 LARGE_INTEGER fl, cl;
171 if (!QueryPerformanceFrequency(&fl) || !QueryPerformanceCounter(&cl)) {
176 int64_t freq = fl.QuadPart;
177 int64_t counter = cl.QuadPart;
178 tp->tv_sec = counter / freq;
179 tp->tv_nsec = (long)(((counter % freq) * kNsPerSec + (freq >> 1)) / freq);
180 if (tp->tv_nsec >= kNsPerSec) {
182 tp->tv_nsec -= kNsPerSec;
194 #error No clock_gettime(3) compatibility wrapper available for this platform.
202 #include <folly/portability/Windows.h>
205 char* asctime_r(const tm* tm, char* buf) {
207 if (asctime_s(tmpBuf, tm)) {
210 // Nothing we can do if the buff is to small :(
211 return strcpy(buf, tmpBuf);
214 char* ctime_r(const time_t* t, char* buf) {
216 if (ctime_s(tmpBuf, 64, t)) {
219 // Nothing we can do if the buff is to small :(
220 return strcpy(buf, tmpBuf);
223 tm* gmtime_r(const time_t* t, tm* res) {
224 if (!gmtime_s(res, t)) {
230 tm* localtime_r(const time_t* t, tm* o) {
231 if (!localtime_s(o, t)) {
237 int nanosleep(const struct timespec* request, struct timespec* remain) {
238 Sleep((DWORD)((request->tv_sec * 1000) + (request->tv_nsec / 1000000)));
244 char* strptime(const char* __restrict s,
245 const char* __restrict f,
246 struct tm* __restrict tm) {
247 // Isn't the C++ standard lib nice? std::get_time is defined such that its
248 // format parameters are the exact same as strptime. Of course, we have to
249 // create a string stream first, and imbue it with the current C locale, and
250 // we also have to make sure we return the right things if it fails, or
251 // if it succeeds, but this is still far simpler an implementation than any
252 // of the versions in any of the C standard libraries.
253 std::istringstream input(s);
254 input.imbue(std::locale(setlocale(LC_ALL, nullptr)));
255 input >> std::get_time(tm, f);
259 return const_cast<char*>(s + input.tellg());