1 /* 2 * 3 * Copyright 2015 gRPC authors. 4 * 5 * Licensed under the Apache License, Version 2.0 (the "License"); 6 * you may not use this file except in compliance with the License. 7 * You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 * 17 */ 18 19 #ifndef GRPC_SUPPORT_TIME_H 20 #define GRPC_SUPPORT_TIME_H 21 22 #include <grpc/support/port_platform.h> 23 24 #include <grpc/impl/codegen/gpr_types.h> 25 26 #include <stddef.h> 27 #include <time.h> 28 29 #ifdef __cplusplus 30 extern "C" { 31 #endif 32 33 /** Time constants. */ 34 GPRAPI gpr_timespec 35 gpr_time_0(gpr_clock_type type); /** The zero time interval. */ 36 GPRAPI gpr_timespec gpr_inf_future(gpr_clock_type type); /** The far future */ 37 GPRAPI gpr_timespec gpr_inf_past(gpr_clock_type type); /** The far past. */ 38 39 #define GPR_MS_PER_SEC 1000 40 #define GPR_US_PER_SEC 1000000 41 #define GPR_NS_PER_SEC 1000000000 42 #define GPR_NS_PER_MS 1000000 43 #define GPR_NS_PER_US 1000 44 #define GPR_US_PER_MS 1000 45 46 /** initialize time subsystem */ 47 GPRAPI void gpr_time_init(void); 48 49 /** Return the current time measured from the given clocks epoch. */ 50 GPRAPI gpr_timespec gpr_now(gpr_clock_type clock); 51 52 /** Convert a timespec from one clock to another */ 53 GPRAPI gpr_timespec gpr_convert_clock_type(gpr_timespec t, 54 gpr_clock_type clock_type); 55 56 /** Return -ve, 0, or +ve according to whether a < b, a == b, or a > b 57 respectively. */ 58 GPRAPI int gpr_time_cmp(gpr_timespec a, gpr_timespec b); 59 60 GPRAPI gpr_timespec gpr_time_max(gpr_timespec a, gpr_timespec b); 61 GPRAPI gpr_timespec gpr_time_min(gpr_timespec a, gpr_timespec b); 62 63 /** Add and subtract times. Calculations saturate at infinities. */ 64 GPRAPI gpr_timespec gpr_time_add(gpr_timespec a, gpr_timespec b); 65 GPRAPI gpr_timespec gpr_time_sub(gpr_timespec a, gpr_timespec b); 66 67 /** Return a timespec representing a given number of time units. INT64_MIN is 68 interpreted as gpr_inf_past, and INT64_MAX as gpr_inf_future. */ 69 GPRAPI gpr_timespec gpr_time_from_micros(int64_t us, gpr_clock_type clock_type); 70 GPRAPI gpr_timespec gpr_time_from_nanos(int64_t ns, gpr_clock_type clock_type); 71 GPRAPI gpr_timespec gpr_time_from_millis(int64_t ms, gpr_clock_type clock_type); 72 GPRAPI gpr_timespec gpr_time_from_seconds(int64_t s, gpr_clock_type clock_type); 73 GPRAPI gpr_timespec gpr_time_from_minutes(int64_t m, gpr_clock_type clock_type); 74 GPRAPI gpr_timespec gpr_time_from_hours(int64_t h, gpr_clock_type clock_type); 75 76 GPRAPI int32_t gpr_time_to_millis(gpr_timespec timespec); 77 78 /** Return 1 if two times are equal or within threshold of each other, 79 0 otherwise */ 80 GPRAPI int gpr_time_similar(gpr_timespec a, gpr_timespec b, 81 gpr_timespec threshold); 82 83 /** Sleep until at least 'until' - an absolute timeout */ 84 GPRAPI void gpr_sleep_until(gpr_timespec until); 85 86 GPRAPI double gpr_timespec_to_micros(gpr_timespec t); 87 88 #ifdef __cplusplus 89 } 90 #endif 91 92 #endif /* GRPC_SUPPORT_TIME_H */ 93