Kernel part of bluetooth stack ported by Dmitry Komissaroff. Very much work
[dragonfly.git] / usr.sbin / dntpd / convert.c
blobeb35820c8e98bc7572468596dcce427dcca4ee0e
1 /*
2 * Copyright (c) 2005 The DragonFly Project. All rights reserved.
3 *
4 * This code is derived from software contributed to The DragonFly Project
5 * by Matthew Dillon <dillon@backplane.com>
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
16 * distribution.
17 * 3. Neither the name of The DragonFly Project nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific, prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
34 * $DragonFly: src/usr.sbin/dntpd/convert.c,v 1.4 2005/04/25 17:42:49 dillon Exp $
37 #include "defs.h"
39 void
40 l_fixedpt_to_tv(struct l_fixedpt *fixed, struct timeval *tvp)
42 tvp->tv_sec = fixed->int_partl - JAN_1970;
43 tvp->tv_usec = (long)((double)fixed->fractionl * 1000000.0 / UINT_MAX);
47 * Subtract usec from tvp, storing the result in tvp.
49 void
50 tv_subtract_micro(struct timeval *tvp, long usec)
52 if (usec < 0) {
53 tv_add_micro(tvp, -usec);
54 } else {
55 tvp->tv_usec -= usec;
56 if (tvp->tv_usec < 0) {
57 tvp->tv_sec -= (-tvp->tv_usec + 999999) / 1000000;
58 tvp->tv_usec = 1000000 - (-tvp->tv_usec % 1000000);
64 * Add usec to tvp, storing the result in tvp.
66 void
67 tv_add_micro(struct timeval *tvp, long usec)
69 if (usec < 0) {
70 tv_subtract_micro(tvp, -usec);
71 } else {
72 tvp->tv_usec += usec;
73 if (tvp->tv_usec >= 1000000) {
74 tvp->tv_sec += tvp->tv_usec / 1000000;
75 tvp->tv_usec = tvp->tv_usec % 1000000;
81 * Add the fp offset to tvp.
83 void
84 tv_add_offset(struct timeval *tvp, double offset)
86 tvp->tv_sec += (long)offset;
87 offset -= (double)(long)offset; /* e.g. -1.3 - (-1) = -0.3 */
88 tv_add_micro(tvp, (int)(offset * 1000000.0));
92 * Return the time differential in microseconds.
94 double
95 tv_delta_double(struct timeval *tv1, struct timeval *tv2)
97 double usec;
99 usec = (double)(tv2->tv_sec - tv1->tv_sec) +
100 (double)(tv2->tv_usec - tv1->tv_usec) / 1000000.0;
101 return(usec);
104 void
105 tv_to_ts(struct timeval *tv, struct timespec *ts)
107 ts->tv_sec = tv->tv_sec;
108 ts->tv_nsec = tv->tv_usec * 1000;
111 void
112 ts_to_tv(struct timespec *ts, struct timeval *tv)
114 tv->tv_sec = ts->tv_sec;
115 tv->tv_usec = ts->tv_nsec / 1000;