time_mosq.c 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. /*
  2. Copyright (c) 2013-2020 Roger Light <roger@atchoo.org>
  3. All rights reserved. This program and the accompanying materials
  4. are made available under the terms of the Eclipse Public License 2.0
  5. and Eclipse Distribution License v1.0 which accompany this distribution.
  6. The Eclipse Public License is available at
  7. https://www.eclipse.org/legal/epl-2.0/
  8. and the Eclipse Distribution License is available at
  9. http://www.eclipse.org/org/documents/edl-v10.php.
  10. SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause
  11. Contributors:
  12. Roger Light - initial implementation and documentation.
  13. */
  14. #include "config.h"
  15. #ifdef __APPLE__
  16. #include <mach/mach.h>
  17. #include <mach/mach_time.h>
  18. #endif
  19. #ifdef WIN32
  20. #if !(defined(_MSC_VER) && _MSC_VER <= 1500)
  21. # define _WIN32_WINNT _WIN32_WINNT_VISTA
  22. #endif
  23. # include <windows.h>
  24. #else
  25. # include <unistd.h>
  26. #endif
  27. #include <time.h>
  28. #include "mosquitto.h"
  29. #include "time_mosq.h"
  30. time_t mosquitto_time(void)
  31. {
  32. #ifdef WIN32
  33. return GetTickCount64()/1000;
  34. #elif _POSIX_TIMERS>0 && defined(_POSIX_MONOTONIC_CLOCK)
  35. struct timespec tp;
  36. clock_gettime(CLOCK_MONOTONIC, &tp);
  37. return tp.tv_sec;
  38. #elif defined(__APPLE__)
  39. static mach_timebase_info_data_t tb;
  40. uint64_t ticks;
  41. uint64_t sec;
  42. ticks = mach_absolute_time();
  43. if(tb.denom == 0){
  44. mach_timebase_info(&tb);
  45. }
  46. sec = ticks*tb.numer/tb.denom/1000000000;
  47. return (time_t)sec;
  48. #else
  49. return time(NULL);
  50. #endif
  51. }