Coverage Report

Created: 2025-11-07 06:54

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/mosquitto/libcommon/time_common.c
Line
Count
Source
1
/*
2
Copyright (c) 2013-2021 Roger Light <roger@atchoo.org>
3
4
All rights reserved. This program and the accompanying materials
5
are made available under the terms of the Eclipse Public License 2.0
6
and Eclipse Distribution License v1.0 which accompany this distribution.
7
8
The Eclipse Public License is available at
9
   https://www.eclipse.org/legal/epl-2.0/
10
and the Eclipse Distribution License is available at
11
  http://www.eclipse.org/org/documents/edl-v10.php.
12
13
SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause
14
15
Contributors:
16
   Roger Light - initial implementation and documentation.
17
*/
18
19
#include "config.h"
20
21
#ifdef __APPLE__
22
#include <mach/mach.h>
23
#include <mach/mach_time.h>
24
#include <sys/time.h>
25
#endif
26
27
#ifdef WIN32
28
#if !(defined(_MSC_VER) && _MSC_VER <= 1500)
29
#  define _WIN32_WINNT _WIN32_WINNT_VISTA
30
#endif
31
#  include <windows.h>
32
#else
33
#  include <unistd.h>
34
#endif
35
#include <time.h>
36
37
#include "mosquitto.h"
38
39
#if _POSIX_TIMERS>0 && defined(_POSIX_MONOTONIC_CLOCK)
40
static clockid_t time_clock = CLOCK_MONOTONIC;
41
#endif
42
43
44
void mosquitto_time_init(void)
45
8.33k
{
46
8.33k
#if _POSIX_TIMERS>0 && defined(_POSIX_MONOTONIC_CLOCK)
47
8.33k
  struct timespec tp;
48
49
8.33k
#ifdef CLOCK_BOOTTIME
50
8.33k
  if(clock_gettime(CLOCK_BOOTTIME, &tp) == 0){
51
8.33k
    time_clock = CLOCK_BOOTTIME;
52
8.33k
  }else{
53
0
    time_clock = CLOCK_MONOTONIC;
54
0
  }
55
#else
56
  time_clock = CLOCK_MONOTONIC;
57
#endif
58
8.33k
#endif
59
8.33k
}
60
61
62
time_t mosquitto_time(void)
63
8.33k
{
64
#ifdef WIN32
65
  return GetTickCount64()/1000;
66
#elif _POSIX_TIMERS>0 && defined(_POSIX_MONOTONIC_CLOCK)
67
  struct timespec tp;
68
69
8.33k
  if(clock_gettime(time_clock, &tp) == 0){
70
8.33k
    return tp.tv_sec;
71
8.33k
  }
72
73
0
  return (time_t)-1;
74
#elif defined(__APPLE__)
75
  static mach_timebase_info_data_t tb;
76
  uint64_t ticks;
77
  uint64_t sec;
78
79
  ticks = mach_absolute_time();
80
81
  if(tb.denom == 0){
82
    mach_timebase_info(&tb);
83
  }
84
  sec = ticks*tb.numer/tb.denom/1000000000;
85
86
  return (time_t)sec;
87
#else
88
  return time(NULL);
89
#endif
90
8.33k
}
91
92
93
void mosquitto_time_ns(time_t *s, long *ns)
94
0
{
95
#ifdef WIN32
96
  SYSTEMTIME st;
97
  GetLocalTime(&st);
98
  *s = time(NULL);
99
  *ns = st.wMilliseconds*1000000L;
100
#elif _POSIX_TIMERS>0 && defined(_POSIX_MONOTONIC_CLOCK)
101
  struct timespec tp;
102
103
0
  clock_gettime(CLOCK_REALTIME, &tp);
104
0
  *s = tp.tv_sec;
105
0
  *ns = tp.tv_nsec;
106
#else
107
  struct timeval tv;
108
109
  gettimeofday(&tv, NULL);
110
  *s = tv.tv_sec;
111
  *ns = tv.tv_usec * 1000;
112
#endif
113
0
}
114
115
116
long mosquitto_time_cmp(time_t t1_s, long t1_ns, time_t t2_s, long t2_ns)
117
0
{
118
0
  if(t1_s == t2_s){
119
0
    return (long)(t1_ns - t2_ns);
120
0
  }else{
121
0
    return (long)(t1_s - t2_s);
122
0
  }
123
0
}