blob: a5c947dfc8bd3e4edd04cce99b40e8039b16dd5d [file] [log] [blame]
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001/*
2 *
Craig Tiller06059952015-02-18 08:34:56 -08003 * Copyright 2015, Google Inc.
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08004 * 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
Nicolas "Pixel" Noble1ff52d52015-03-01 05:24:36 +010034#ifndef GRPC_SUPPORT_TIME_H
35#define GRPC_SUPPORT_TIME_H
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080036/* Time support.
David Klempner9b60fa32015-02-04 09:48:53 -080037 We use gpr_timespec, which is analogous to struct timespec. On some
38 machines, absolute times may be in local time. */
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080039
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080040#include <grpc/support/port_platform.h>
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080041#include <stddef.h>
David Klempner0c61dc52015-02-04 13:24:04 -080042#include <time.h>
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080043
44#ifdef __cplusplus
45extern "C" {
46#endif
47
David Klempner0c61dc52015-02-04 13:24:04 -080048typedef struct gpr_timespec {
Craig Tillerf1bff012015-07-06 11:20:50 -070049 time_t tv_sec;
50 int tv_nsec;
David Klempner0c61dc52015-02-04 13:24:04 -080051} gpr_timespec;
52
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080053/* Time constants. */
54extern const gpr_timespec gpr_time_0; /* The zero time interval. */
55extern const gpr_timespec gpr_inf_future; /* The far future */
56extern const gpr_timespec gpr_inf_past; /* The far past. */
57
58#define GPR_MS_PER_SEC 1000
59#define GPR_US_PER_SEC 1000000
60#define GPR_NS_PER_SEC 1000000000
61#define GPR_NS_PER_MS 1000000
62#define GPR_NS_PER_US 1000
63#define GPR_US_PER_MS 1000
64
Craig Tillerf3756c12015-07-01 17:21:01 -070065/* The clocks we support. */
66typedef enum {
67 /* Monotonic clock. Epoch undefined. Always moves forwards. */
68 GPR_CLOCK_MONOTONIC = 0,
Craig Tillerf1bff012015-07-06 11:20:50 -070069 /* Realtime clock. May jump forwards or backwards. Settable by
70 the system administrator. Has its epoch at 0:00:00 UTC 1 Jan 1970. */
Craig Tillerf3756c12015-07-01 17:21:01 -070071 GPR_CLOCK_REALTIME
72} gpr_clock_type;
73
74/* initialize time subsystem */
75void gpr_time_init(void);
76
77/* Return the current time measured from the given clocks epoch. */
78gpr_timespec gpr_now(gpr_clock_type clock);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080079
80/* Return -ve, 0, or +ve according to whether a < b, a == b, or a > b
81 respectively. */
82int gpr_time_cmp(gpr_timespec a, gpr_timespec b);
83
ctiller3bf466f2014-12-19 16:21:57 -080084gpr_timespec gpr_time_max(gpr_timespec a, gpr_timespec b);
85gpr_timespec gpr_time_min(gpr_timespec a, gpr_timespec b);
86
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080087/* Add and subtract times. Calculations saturate at infinities. */
88gpr_timespec gpr_time_add(gpr_timespec a, gpr_timespec b);
89gpr_timespec gpr_time_sub(gpr_timespec a, gpr_timespec b);
90
Nicolas Noble65b07592015-02-23 15:28:04 -080091/* Return a timespec representing a given number of time units. LONG_MIN is
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080092 interpreted as gpr_inf_past, and LONG_MAX as gpr_inf_future. */
93gpr_timespec gpr_time_from_micros(long x);
94gpr_timespec gpr_time_from_nanos(long x);
95gpr_timespec gpr_time_from_millis(long x);
96gpr_timespec gpr_time_from_seconds(long x);
97gpr_timespec gpr_time_from_minutes(long x);
98gpr_timespec gpr_time_from_hours(long x);
99
ctiller58393c22015-01-07 14:03:30 -0800100gpr_int32 gpr_time_to_millis(gpr_timespec timespec);
101
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800102/* Return 1 if two times are equal or within threshold of each other,
103 0 otherwise */
104int gpr_time_similar(gpr_timespec a, gpr_timespec b, gpr_timespec threshold);
105
106/* Sleep until at least 'until' - an absolute timeout */
107void gpr_sleep_until(gpr_timespec until);
108
hongyu24200d32015-01-08 15:13:49 -0800109double gpr_timespec_to_micros(gpr_timespec t);
110
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800111#ifdef __cplusplus
112}
113#endif
114
Craig Tillerf1bff012015-07-06 11:20:50 -0700115#endif /* GRPC_SUPPORT_TIME_H */