-
Notifications
You must be signed in to change notification settings - Fork 0
/
time_mosq.c
82 lines (66 loc) · 1.64 KB
/
time_mosq.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/*
Copyright (c) 2013,2014 Roger Light <[email protected]>
All rights reserved. This program and the accompanying materials
are made available under the terms of the Eclipse Public License v1.0
and Eclipse Distribution License v1.0 which accompany this distribution.
The Eclipse Public License is available at
https://www.eclipse.org/legal/epl-v10.html
and the Eclipse Distribution License is available at
https://www.eclipse.org/org/documents/edl-v10.php.
Contributors:
Roger Light - initial implementation and documentation.
*/
#ifdef __APPLE__
#include <mach/mach.h>
#include <mach/mach_time.h>
#endif
#ifdef WIN32
# define _WIN32_WINNT _WIN32_WINNT_VISTA
# include <windows.h>
#else
# include <unistd.h>
#endif
#include <time.h>
#include "mosquitto.h"
#include "time_mosq.h"
#ifdef WIN32
static bool tick64 = false;
void _windows_time_version_check(void)
{
OSVERSIONINFO vi;
tick64 = false;
memset(&vi, 0, sizeof(OSVERSIONINFO));
vi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
if(GetVersionEx(&vi)){
if(vi.dwMajorVersion > 5){
tick64 = true;
}
}
}
#endif
time_t mosquitto_time(void)
{
#ifdef WIN32
if(tick64){
return GetTickCount64()/1000;
}else{
return GetTickCount()/1000; /* FIXME - need to deal with overflow. */
}
#elif _POSIX_TIMERS>0 && defined(_POSIX_MONOTONIC_CLOCK)
struct timespec tp;
clock_gettime(CLOCK_MONOTONIC, &tp);
return tp.tv_sec;
#elif defined(__APPLE__)
static mach_timebase_info_data_t tb;
uint64_t ticks;
uint64_t sec;
ticks = mach_absolute_time();
if(tb.denom == 0){
mach_timebase_info(&tb);
}
sec = ticks*tb.numer/tb.denom/1000000000;
return (time_t)sec;
#else
return time(NULL);
#endif
}