Portable build framework for OpenNTPD
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.

44 lines
1.4 KiB

  1. /*
  2. * Copyright (c) 1999-2004 Damien Miller <djm@mindrot.org>
  3. * Copyright (c) 2014 Brent Cook <bcook@openbsd.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 USE, DATA OR PROFITS, WHETHER IN AN
  14. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  15. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  16. */
  17. #include <mach/mach_time.h>
  18. #include <sys/time.h>
  19. #include <stdint.h>
  20. #include <time.h>
  21. int
  22. clock_gettime(clockid_t clk_id, struct timespec *ts)
  23. {
  24. static uint64_t timebase_factor = 0;
  25. mach_timebase_info_data_t timebase_info;
  26. uint64_t nsec;
  27. if (clk_id != CLOCK_REALTIME)
  28. return -1; /* not implemented */
  29. if (timebase_factor == 0) {
  30. mach_timebase_info(&timebase_info);
  31. timebase_factor = timebase_info.numer / timebase_info.denom;
  32. }
  33. nsec = mach_absolute_time() * timebase_factor;
  34. ts->tv_sec = nsec / 1000000000UL;
  35. ts->tv_nsec = nsec % 1000000000UL;
  36. return 0;
  37. }