blob: 1fd3181859f584e92168b71e10f63990fd6d1a3e [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 {
49 time_t tv_sec;
50 int tv_nsec;
51} 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
65/* Return the current time measured from the system's default epoch. */
66gpr_timespec gpr_now(void);
67
68/* Return -ve, 0, or +ve according to whether a < b, a == b, or a > b
69 respectively. */
70int gpr_time_cmp(gpr_timespec a, gpr_timespec b);
71
ctiller3bf466f2014-12-19 16:21:57 -080072gpr_timespec gpr_time_max(gpr_timespec a, gpr_timespec b);
73gpr_timespec gpr_time_min(gpr_timespec a, gpr_timespec b);
74
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080075/* Add and subtract times. Calculations saturate at infinities. */
76gpr_timespec gpr_time_add(gpr_timespec a, gpr_timespec b);
77gpr_timespec gpr_time_sub(gpr_timespec a, gpr_timespec b);
78
Nicolas Noble65b07592015-02-23 15:28:04 -080079/* Return a timespec representing a given number of time units. LONG_MIN is
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080080 interpreted as gpr_inf_past, and LONG_MAX as gpr_inf_future. */
81gpr_timespec gpr_time_from_micros(long x);
82gpr_timespec gpr_time_from_nanos(long x);
83gpr_timespec gpr_time_from_millis(long x);
84gpr_timespec gpr_time_from_seconds(long x);
85gpr_timespec gpr_time_from_minutes(long x);
86gpr_timespec gpr_time_from_hours(long x);
87
ctiller58393c22015-01-07 14:03:30 -080088gpr_int32 gpr_time_to_millis(gpr_timespec timespec);
89
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080090/* Return 1 if two times are equal or within threshold of each other,
91 0 otherwise */
92int gpr_time_similar(gpr_timespec a, gpr_timespec b, gpr_timespec threshold);
93
94/* Sleep until at least 'until' - an absolute timeout */
95void gpr_sleep_until(gpr_timespec until);
96
hongyu24200d32015-01-08 15:13:49 -080097double gpr_timespec_to_micros(gpr_timespec t);
98
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080099#ifdef __cplusplus
100}
101#endif
102
Nicolas "Pixel" Noble1ff52d52015-03-01 05:24:36 +0100103#endif /* GRPC_SUPPORT_TIME_H */