a7e5cf2da932e1d30904b34d5c536bdc5d35727a
[folly.git] / folly / portability / Time.h
1 /*
2  * Copyright 2017 Facebook, Inc.
3  *
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
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #pragma once
18
19 #include <stdint.h>
20 #include <time.h>
21
22 #include <folly/portability/Config.h>
23 #include <folly/Portability.h>
24
25 // OSX is a pain. The XCode 8 SDK always declares clock_gettime
26 // even if the target OS version doesn't support it, so you get
27 // an error at runtime because it can't resolve the symbol. We
28 // solve that by pretending we have it here in the header and
29 // then enable our implementation on the source side so that
30 // gets linked in instead.
31 #if __MACH__ && FOLLY_HAVE_CLOCK_GETTIME &&                    \
32     (MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_12 || \
33      __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_10_0)
34
35 #define FOLLY_FORCE_CLOCK_GETTIME_DEFINITION 1
36
37 #endif
38
39 // These aren't generic implementations, so we can only declare them on
40 // platforms we support.
41 #if !FOLLY_HAVE_CLOCK_GETTIME && (defined(__MACH__) || defined(_WIN32))
42 #define CLOCK_REALTIME 0
43 #define CLOCK_MONOTONIC 1
44 #define CLOCK_PROCESS_CPUTIME_ID 2
45 #define CLOCK_THREAD_CPUTIME_ID 3
46 #endif
47
48 #if (!FOLLY_HAVE_CLOCK_GETTIME || FOLLY_FORCE_CLOCK_GETTIME_DEFINITION) && (defined(__MACH__) || defined(_WIN32))
49 namespace folly {
50 namespace portability {
51 namespace time {
52 typedef uint8_t clockid_t;
53 int clock_gettime(clockid_t clk_id, struct timespec* ts);
54 int clock_getres(clockid_t clk_id, struct timespec* ts);
55 }
56 }
57 }
58
59 FOLLY_PUSH_WARNING
60 #if __CLANG_PREREQ(3, 0)
61 FOLLY_GCC_DISABLE_WARNING("-Wheader-hygiene")
62 #endif
63 /* using override */ using namespace folly::portability::time;
64 FOLLY_POP_WARNING
65 #endif
66
67 #ifdef _WIN32
68 #define TM_YEAR_BASE (1900)
69
70 extern "C" {
71 char* asctime_r(const tm* tm, char* buf);
72 char* ctime_r(const time_t* t, char* buf);
73 tm* gmtime_r(const time_t* t, tm* res);
74 tm* localtime_r(const time_t* t, tm* o);
75 int nanosleep(const struct timespec* request, struct timespec* remain);
76 char* strptime(
77     const char* __restrict buf,
78     const char* __restrict fmt,
79     struct tm* __restrict tm);
80 }
81 #endif