Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
| 3 | * Copyright 2014, Google Inc. |
| 4 | * All rights reserved. |
| 5 | * |
| 6 | * Redistribution and use in source and binary forms, with or without |
| 7 | * modification, are permitted provided that the following conditions are |
| 8 | * met: |
| 9 | * |
| 10 | * * Redistributions of source code must retain the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer. |
| 12 | * * Redistributions in binary form must reproduce the above |
| 13 | * copyright notice, this list of conditions and the following disclaimer |
| 14 | * in the documentation and/or other materials provided with the |
| 15 | * distribution. |
| 16 | * * Neither the name of Google Inc. nor the names of its |
| 17 | * contributors may be used to endorse or promote products derived from |
| 18 | * this software without specific prior written permission. |
| 19 | * |
| 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 21 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 22 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 23 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 24 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 25 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 26 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 27 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 28 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 29 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 30 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 | * |
| 32 | */ |
| 33 | |
| 34 | #ifndef __GRPC_SUPPORT_TIME_H__ |
| 35 | #define __GRPC_SUPPORT_TIME_H__ |
| 36 | /* Time support. |
| 37 | We use gpr_timespec, which is typedefed to struct timespec on platforms which |
| 38 | have it. On some machines, absolute times may be in local time. */ |
| 39 | |
| 40 | /* Platform specific header declares gpr_timespec. |
| 41 | gpr_timespec contains: |
| 42 | time_t tv_sec; // seconds since start of 1970 |
| 43 | int tv_nsec; // nanoseconds; always in 0..999999999; never negative. |
| 44 | */ |
| 45 | |
| 46 | #include <grpc/support/port_platform.h> |
| 47 | |
| 48 | #if defined(GPR_POSIX_TIME) |
| 49 | #include <grpc/support/time_posix.h> |
| 50 | #elif defined(GPR_WIN32) |
| 51 | #include <grpc/support/time_win32.h> |
| 52 | #else |
| 53 | #error could not determine platform for time |
| 54 | #endif |
| 55 | |
| 56 | #include <stddef.h> |
| 57 | |
| 58 | #ifdef __cplusplus |
| 59 | extern "C" { |
| 60 | #endif |
| 61 | |
| 62 | /* Time constants. */ |
| 63 | extern const gpr_timespec gpr_time_0; /* The zero time interval. */ |
| 64 | extern const gpr_timespec gpr_inf_future; /* The far future */ |
| 65 | extern const gpr_timespec gpr_inf_past; /* The far past. */ |
| 66 | |
| 67 | #define GPR_MS_PER_SEC 1000 |
| 68 | #define GPR_US_PER_SEC 1000000 |
| 69 | #define GPR_NS_PER_SEC 1000000000 |
| 70 | #define GPR_NS_PER_MS 1000000 |
| 71 | #define GPR_NS_PER_US 1000 |
| 72 | #define GPR_US_PER_MS 1000 |
| 73 | |
| 74 | /* Return the current time measured from the system's default epoch. */ |
| 75 | gpr_timespec gpr_now(void); |
| 76 | |
| 77 | /* Return -ve, 0, or +ve according to whether a < b, a == b, or a > b |
| 78 | respectively. */ |
| 79 | int gpr_time_cmp(gpr_timespec a, gpr_timespec b); |
| 80 | |
ctiller | 3bf466f | 2014-12-19 16:21:57 -0800 | [diff] [blame] | 81 | gpr_timespec gpr_time_max(gpr_timespec a, gpr_timespec b); |
| 82 | gpr_timespec gpr_time_min(gpr_timespec a, gpr_timespec b); |
| 83 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 84 | /* Add and subtract times. Calculations saturate at infinities. */ |
| 85 | gpr_timespec gpr_time_add(gpr_timespec a, gpr_timespec b); |
| 86 | gpr_timespec gpr_time_sub(gpr_timespec a, gpr_timespec b); |
| 87 | |
| 88 | /* Return a timespec representing a given number of microseconds. LONG_MIN is |
| 89 | interpreted as gpr_inf_past, and LONG_MAX as gpr_inf_future. */ |
| 90 | gpr_timespec gpr_time_from_micros(long x); |
| 91 | gpr_timespec gpr_time_from_nanos(long x); |
| 92 | gpr_timespec gpr_time_from_millis(long x); |
| 93 | gpr_timespec gpr_time_from_seconds(long x); |
| 94 | gpr_timespec gpr_time_from_minutes(long x); |
| 95 | gpr_timespec gpr_time_from_hours(long x); |
| 96 | |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 97 | gpr_int32 gpr_time_to_millis(gpr_timespec timespec); |
| 98 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 99 | /* Return 1 if two times are equal or within threshold of each other, |
| 100 | 0 otherwise */ |
| 101 | int gpr_time_similar(gpr_timespec a, gpr_timespec b, gpr_timespec threshold); |
| 102 | |
| 103 | /* Sleep until at least 'until' - an absolute timeout */ |
| 104 | void gpr_sleep_until(gpr_timespec until); |
| 105 | |
| 106 | struct timeval gpr_timeval_from_timespec(gpr_timespec t); |
| 107 | |
| 108 | gpr_timespec gpr_timespec_from_timeval(struct timeval t); |
| 109 | |
| 110 | #ifdef __cplusplus |
| 111 | } |
| 112 | #endif |
| 113 | |
| 114 | #endif /* __GRPC_SUPPORT_TIME_H__ */ |