Kernel part of bluetooth stack ported by Dmitry Komissaroff. Very much work
[dragonfly.git] / contrib / ntpd / util.c
blob0014487919a094dba61a3017f99609dba2c40ba7
1 /* $OpenBSD: src/usr.sbin/ntpd/util.c,v 1.10 2004/12/08 15:47:38 mickey Exp $ */
3 /*
4 * Copyright (c) 2004 Alexander Guy <alexander.guy@andern.org>
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
15 * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
16 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 #include <sys/time.h>
20 #include <limits.h>
22 #include "ntpd.h"
24 double
25 gettime(void)
27 struct timeval tv;
29 if (gettimeofday(&tv, NULL) == -1)
30 fatal("gettimeofday");
32 return (tv.tv_sec + JAN_1970 + 1.0e-6 * tv.tv_usec);
36 void
37 d_to_tv(double d, struct timeval *tv)
39 tv->tv_sec = (long)d;
40 tv->tv_usec = (d - tv->tv_sec) * 1000000;
43 double
44 lfp_to_d(struct l_fixedpt lfp)
46 double ret;
48 lfp.int_partl = ntohl(lfp.int_partl);
49 lfp.fractionl = ntohl(lfp.fractionl);
51 ret = (double)(lfp.int_partl) + ((double)lfp.fractionl / UINT_MAX);
53 return (ret);
56 struct l_fixedpt
57 d_to_lfp(double d)
59 struct l_fixedpt lfp;
61 lfp.int_partl = htonl((u_int32_t)d);
62 lfp.fractionl = htonl((u_int32_t)((d - (u_int32_t)d) * UINT_MAX));
64 return (lfp);
67 double
68 sfp_to_d(struct s_fixedpt sfp)
70 double ret;
72 sfp.int_parts = ntohs(sfp.int_parts);
73 sfp.fractions = ntohs(sfp.fractions);
75 ret = (double)(sfp.int_parts) + ((double)sfp.fractions / USHRT_MAX);
77 return (ret);
80 struct s_fixedpt
81 d_to_sfp(double d)
83 struct s_fixedpt sfp;
85 sfp.int_parts = htons((u_int16_t)d);
86 sfp.fractions = htons((u_int16_t)((d - (u_int16_t)d) * USHRT_MAX));
88 return (sfp);