UNPKG

2.65 kBtext/x-cView Raw
1/*
2Copyright (c) 2013 Roger Light <roger@atchoo.org>
3All rights reserved.
4
5Redistribution and use in source and binary forms, with or without
6modification, are permitted provided that the following conditions are met:
7
81. Redistributions of source code must retain the above copyright notice,
9 this list of conditions and the following disclaimer.
102. Redistributions in binary form must reproduce the above copyright
11 notice, this list of conditions and the following disclaimer in the
12 documentation and/or other materials provided with the distribution.
133. Neither the name of mosquitto nor the names of its
14 contributors may be used to endorse or promote products derived from
15 this software without specific prior written permission.
16
17THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27POSSIBILITY OF SUCH DAMAGE.
28*/
29
30#ifdef __APPLE__
31#include <mach/mach.h>
32#include <mach/mach_time.h>
33#endif
34
35#ifdef WIN32
36# define _WIN32_WINNT _WIN32_WINNT_VISTA
37# include <windows.h>
38#else
39# include <unistd.h>
40#endif
41#include <time.h>
42
43#include "mosquitto.h"
44#include "time_mosq.h"
45
46#ifdef WIN32
47static bool tick64 = false;
48
49void _windows_time_version_check(void)
50{
51 OSVERSIONINFO vi;
52
53 tick64 = false;
54
55 memset(&vi, 0, sizeof(OSVERSIONINFO));
56 vi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
57 if(GetVersionEx(&vi)){
58 if(vi.dwMajorVersion > 5){
59 tick64 = true;
60 }
61 }
62}
63#endif
64
65time_t mosquitto_time(void)
66{
67#ifdef WIN32
68 if(tick64){
69 return GetTickCount64()/1000;
70 }else{
71 return GetTickCount()/1000; /* FIXME - need to deal with overflow. */
72 }
73#elif _POSIX_TIMERS>0 && defined(_POSIX_MONOTONIC_CLOCK)
74 struct timespec tp;
75
76 clock_gettime(CLOCK_MONOTONIC, &tp);
77 return tp.tv_sec;
78#elif defined(__APPLE__)
79 static mach_timebase_info_data_t tb;
80 uint64_t ticks;
81 uint64_t sec;
82
83 ticks = mach_absolute_time();
84
85 if(tb.denom == 0){
86 mach_timebase_info(&tb);
87 }
88 sec = ticks*tb.numer/tb.denom/1000000000;
89
90 return (time_t)sec;
91#else
92 return time(NULL);
93#endif
94}
95