2 * Copyright (c) 2012, 2013, JANET(UK)
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of JANET(UK) nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
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
23 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
25 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
29 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
31 * OF THE POSSIBILITY OF SUCH DAMAGE.
42 void tr_bin_to_hex(const unsigned char * bin, size_t bin_len,
43 char * hex_out, size_t hex_len)
45 assert(hex_len >= 2*bin_len);
47 snprintf(hex_out, hex_len, "%.2x", bin[0]);
55 * Compare two timespecs
57 * Assumes tv_nsec <= 1e9
61 * @return 0 if ts1==ts2, -1 if ts1<ts2, 1 if ts1>ts2.
63 int tr_cmp_timespec(const struct timespec *ts1, const struct timespec *ts2)
65 if (ts1->tv_sec > ts2->tv_sec)
68 if (ts1->tv_sec < ts2->tv_sec)
71 /* ts1->tv_sec==ts2->tv_sec */
73 if (ts1->tv_nsec > ts2->tv_nsec)
76 if (ts1->tv_nsec < ts2->tv_nsec)
87 * @param sum ts1 + ts2
88 * @return 0 on success, nonzero on error
90 int tr_add_timespec(const struct timespec *ts1, const struct timespec *ts2, struct timespec *sum)
92 const time_t ONE_BILLION = 1000000000;
94 if (!ts1 || !ts2 || !sum)
97 /* would be nice to do range checking, but I don't know a portable way to get the
98 * max value of a time_t. Figure that nsec <= 1e9 and seconds are unlikely to go off
99 * too close to infinity, so overflow is unlikely */
100 sum->tv_nsec = ts1->tv_nsec + ts2->tv_nsec;
101 sum->tv_sec = ts1->tv_sec + ts2->tv_sec;
103 /* make sure that we have no more than a second worth of nsec */
104 while (sum->tv_nsec >= ONE_BILLION) {
105 sum->tv_nsec -= ONE_BILLION;
115 * Allows negative results, which will be represented as a negative tv_sec.
116 * The returned tv_nsec is always positive and less than 1e9.
118 * (The value represented is tv_sec + tv_nsec/1e9 seconds - this is a little
119 * counterintuitive when tv_sec is negative. E.g., -1.5 seconds is represented
120 * as {-2, 500000000). Negative time_t values are not guaranteed to be supported
121 * anyway, so it's probably best to stay away from them.)
125 * @param diff ts1 - ts2
126 * @return 0 on success, nonzero on error
128 int tr_sub_timespec(const struct timespec *ts1, const struct timespec *ts2, struct timespec *diff)
130 const time_t ONE_BILLION = 1000000000;
131 struct timespec ts1_copy = {0};
132 struct timespec ts2_copy = {0};
134 if (!ts1 || !ts2 || !diff)
140 while (ts2_copy.tv_nsec > ts1_copy.tv_nsec) {
141 /* Reduce ts2 by one second worth of nsec, balanced by removing a second
142 * from ts1. Repeat until ts2->tv_nsec <= ts1->tv_nsec. */
143 ts2_copy.tv_nsec -= ONE_BILLION;
144 ts1_copy.tv_sec -= 1;
147 diff->tv_nsec = ts1_copy.tv_nsec - ts2_copy.tv_nsec; /* >= 0 */
148 diff->tv_sec = ts1_copy.tv_sec - ts2_copy.tv_sec; /* sign indeterminate */
150 /* make sure we have no more than 1 sec worth of nsec */
151 while (diff->tv_nsec > ONE_BILLION) {
152 diff->tv_nsec -= ONE_BILLION;
160 * Convert a struct timespec to a string representation
164 char *timespec_to_str(const struct timespec *ts)
169 if (gmtime_r(&(ts->tv_sec), &tm)==NULL)
172 s=malloc(40); /* long enough to contain strftime result */
176 if (strftime(s, 40, "%F %T UTC", &tm)==0) {
184 * Convert a time from one clock to another
186 * Because this involves reading each clock, it is not exact.
188 * @param from clock to convert from
189 * @param when time to convert, measured on the 'from' clock
190 * @param to clock to convert to
191 * @param dst destination, measured on the 'to' clock
192 * @return dst or null on error
194 struct timespec *tr_clock_convert(clockid_t from, const struct timespec *when,
195 clockid_t to, struct timespec *dst)
197 struct timespec now_from = {0};
198 struct timespec diff = {0}; /* difference between when and now_from */
199 struct timespec now_to = {0};
201 if ((clock_gettime(from, &now_from) != 0)
202 || (clock_gettime(to, &now_to) != 0)) {
205 if (tr_sub_timespec(when, &now_from, &diff) != 0) {
208 if (tr_add_timespec(&now_to, &diff, dst) != 0) {