clock_gettime.c 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /*
  2. * Copyright (c), MM Weiss
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without modification,
  6. * are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice,
  9. * this list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * 3. Neither the name of the MM Weiss nor the names of its contributors
  16. * may be used to endorse or promote products derived from this software without
  17. * specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
  20. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  21. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
  22. * SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
  24. * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  25. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
  26. * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  27. * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. */
  29. #ifdef HAVE_CONFIG_H
  30. # include "config.h"
  31. #endif
  32. #ifndef _WIN32
  33. #include <sys/time.h>
  34. #include <sys/resource.h>
  35. #include <mach/mach.h>
  36. #include <mach/clock.h>
  37. #include <mach/mach_time.h>
  38. #include <unistd.h>
  39. #include <sched.h>
  40. int clock_gettime(clockid_t clk_id, struct timespec *tp) {
  41. kern_return_t ret;
  42. clock_serv_t clk;
  43. clock_id_t clk_serv_id;
  44. mach_timespec_t tm;
  45. if (clk_id != CLOCK_REALTIME)
  46. return -1;
  47. clk_serv_id = clk_id == CLOCK_REALTIME ? CALENDAR_CLOCK : SYSTEM_CLOCK;
  48. if (KERN_SUCCESS == (ret = host_get_clock_service(mach_host_self(), clk_serv_id, &clk))) {
  49. if (KERN_SUCCESS == (ret = clock_get_time(clk, &tm))) {
  50. tp->tv_sec = tm.tv_sec;
  51. tp->tv_nsec = tm.tv_nsec;
  52. }
  53. }
  54. if (KERN_SUCCESS != ret)
  55. return -1;
  56. return 0;
  57. }
  58. #else
  59. #include <windows.h>
  60. #include <stdint.h>
  61. #include <time.h>
  62. int clock_gettime(clockid_t clk_id, struct timespec *spec) {
  63. (void)clk_id;
  64. FILETIME wintime;
  65. GetSystemTimeAsFileTime(&wintime);
  66. uint64_t t = (uint64_t)wintime.dwHighDateTime << 32 | wintime.dwLowDateTime;
  67. t -= 116444736000000000;
  68. spec->tv_sec = t / 10000000;
  69. spec->tv_nsec = t % 10000000 * 100;
  70. return 0;
  71. }
  72. #endif