642a95de59e06365b01dbde8c9fe18987865014d
[folly.git] / folly / portability / Time.h
1 /*
2  * Copyright 2016 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
24 // These aren't generic implementations, so we can only declare them on
25 // platforms we support.
26 #if !FOLLY_HAVE_CLOCK_GETTIME && (defined(__MACH__) || defined(_WIN32))
27 #define CLOCK_REALTIME 0
28 // The Windows implementation supports a few other
29 // clock types as well.
30 #ifdef _WIN32
31 # define CLOCK_MONOTONIC 1
32 # define CLOCK_PROCESS_CPUTIME_ID 2
33 # define CLOCK_THREAD_CPUTIME_ID 3
34 #endif
35
36 typedef uint8_t clockid_t;
37 extern "C" int clock_gettime(clockid_t clk_id, struct timespec* ts);
38 extern "C" int clock_getres(clockid_t clk_id, struct timespec* ts);
39 #endif
40
41 #ifdef _WIN32
42 #define TM_YEAR_BASE (1900)
43
44 extern "C" {
45 char* asctime_r(const tm* tm, char* buf);
46 char* ctime_r(const time_t* t, char* buf);
47 tm* gmtime_r(const time_t* t, tm* res);
48 tm* localtime_r(const time_t* t, tm* o);
49 int nanosleep(const struct timespec* request, struct timespec* remain);
50 char* strptime(const char* __restrict buf,
51                const char* __restrict fmt,
52                struct tm* __restrict tm);
53 }
54 #endif