iotivity 0.9.0
[platform/upstream/iotivity.git] / service / protocol-plugin / plugins / mqtt-light / lib / time_mosq.c
1 /*
2 Copyright (c) 2013 Roger Light <roger@atchoo.org>
3 All rights reserved.
4
5 Redistribution and use in source and binary forms, with or without
6 modification, 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 2. 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.
13 3. 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
17 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 POSSIBILITY 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
47 static bool tick64 = false;
48
49 void _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
65 time_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