Source code pulled from OpenBSD for OpenNTPD. The place to contribute to this code is via the OpenBSD CVS tree.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

88 lines
1.9 KiB

20 years ago
20 years ago
20 years ago
20 years ago
  1. /* $OpenBSD: util.c,v 1.8 2004/07/10 22:24:20 alexander Exp $ */
  2. /*
  3. * Copyright (c) 2004 Alexander Guy <alexander.guy@andern.org>
  4. *
  5. * Permission to use, copy, modify, and distribute this software for any
  6. * purpose with or without fee is hereby granted, provided that the above
  7. * copyright notice and this permission notice appear in all copies.
  8. *
  9. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  10. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  11. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  12. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  13. * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
  14. * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
  15. * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  16. */
  17. #include <sys/time.h>
  18. #include "ntpd.h"
  19. double
  20. gettime(void)
  21. {
  22. struct timeval tv;
  23. if (gettimeofday(&tv, NULL) == -1)
  24. fatal("gettimeofday");
  25. return (tv.tv_sec + JAN_1970 + 1.0e-6 * tv.tv_usec);
  26. }
  27. void
  28. d_to_tv(double d, struct timeval *tv)
  29. {
  30. tv->tv_sec = (long)d;
  31. tv->tv_usec = (d - tv->tv_sec) * 1000000;
  32. }
  33. double
  34. lfp_to_d(struct l_fixedpt lfp)
  35. {
  36. double ret;
  37. lfp.int_part = ntohl(lfp.int_part);
  38. lfp.fraction = ntohl(lfp.fraction);
  39. ret = (double)(lfp.int_part) + ((double)lfp.fraction / UINT_MAX);
  40. return (ret);
  41. }
  42. struct l_fixedpt
  43. d_to_lfp(double d)
  44. {
  45. struct l_fixedpt lfp;
  46. lfp.int_part = htonl((u_int32_t)d);
  47. lfp.fraction = htonl((u_int32_t)((d - (u_int32_t)d) * UINT_MAX));
  48. return (lfp);
  49. }
  50. double
  51. sfp_to_d(struct s_fixedpt sfp)
  52. {
  53. double ret;
  54. sfp.int_part = ntohs(sfp.int_part);
  55. sfp.fraction = ntohs(sfp.fraction);
  56. ret = (double)(sfp.int_part) + ((double)sfp.fraction / USHRT_MAX);
  57. return (ret);
  58. }
  59. struct s_fixedpt
  60. d_to_sfp(double d)
  61. {
  62. struct s_fixedpt sfp;
  63. sfp.int_part = htons((u_int16_t)d);
  64. sfp.fraction = htons((u_int16_t)((d - (u_int16_t)d) * USHRT_MAX));
  65. return (sfp);
  66. }