android/GlueIOIOPort: fix spurious errors after IOIO baud rate change
[xcsoar.git] / src / OS / Clock.cpp
blob6f81c25d1ea7452a9a9482b62db607c8d4e16660
1 /*
2 Copyright_License {
4 XCSoar Glide Computer - http://www.xcsoar.org/
5 Copyright (C) 2000-2013 The XCSoar Project
6 A detailed list of copyright holders can be found in the file "AUTHORS".
8 This program is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License
10 as published by the Free Software Foundation; either version 2
11 of the License, or (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 #include "OS/Clock.hpp"
26 #if defined(__APPLE__)
27 #include <mach/mach_time.h>
28 #elif defined(HAVE_POSIX) && !defined(__CYGWIN__)
29 #include <time.h>
30 #ifndef __GLIBC__
31 #include <sys/time.h>
32 #endif
33 #else /* !HAVE_POSIX */
34 #include <windows.h>
35 #endif /* !HAVE_POSIX */
37 unsigned
38 MonotonicClockMS()
40 #if defined(HAVE_POSIX) && !defined(__CYGWIN__)
41 #ifdef CLOCK_MONOTONIC
42 struct timespec ts;
43 clock_gettime(CLOCK_MONOTONIC, &ts);
44 return ts.tv_sec * 1000 + ts.tv_nsec / 1000000;
45 #elif defined(__APPLE__) /* OS X does not define CLOCK_MONOTONIC */
46 static mach_timebase_info_data_t base;
47 if (base.denom == 0)
48 (void)mach_timebase_info(&base);
50 return (unsigned)((mach_absolute_time() * base.numer)
51 / (1000000 * base.denom));
52 #else
53 /* we have no monotonic clock, fall back to gettimeofday() */
54 struct timeval tv;
55 gettimeofday(&tv, 0);
56 return tv.tv_sec * 1000 + tv.tv_usec / 1000;
57 #endif
58 #else /* !HAVE_POSIX */
59 return ::GetTickCount();
60 #endif /* !HAVE_POSIX */
63 uint64_t
64 MonotonicClockUS()
66 #if defined(HAVE_POSIX) && !defined(__CYGWIN__)
67 #ifdef CLOCK_MONOTONIC
68 struct timespec ts;
69 clock_gettime(CLOCK_MONOTONIC, &ts);
70 return uint64_t(ts.tv_sec) * 1000000 + uint64_t(ts.tv_nsec) / 1000;
71 #elif defined(__APPLE__) /* OS X does not define CLOCK_MONOTONIC */
72 static mach_timebase_info_data_t base;
73 if (base.denom == 0)
74 (void)mach_timebase_info(&base);
76 return (uint64_t(mach_absolute_time()) * uint64_t(base.numer))
77 / (1000 * uint64_t(base.denom));
78 #else
79 /* we have no monotonic clock, fall back to gettimeofday() */
80 struct timeval tv;
81 gettimeofday(&tv, 0);
82 return uint64_t(tv.tv_sec) * 1000 + uint64_t(tv.tv_usec) / 1000;
83 #endif
84 #else /* !HAVE_POSIX */
85 LARGE_INTEGER l_value, l_frequency;
87 if (!::QueryPerformanceCounter(&l_value) ||
88 !::QueryPerformanceFrequency(&l_frequency))
89 return 0;
91 uint64_t value = l_value.QuadPart;
92 uint64_t frequency = l_frequency.QuadPart;
94 if (frequency > 1000000) {
95 value *= 10000;
96 value /= frequency / 100;
97 } else if (frequency < 1000000) {
98 value *= 10000;
99 value /= frequency;
100 value *= 100;
103 return value;
104 #endif /* !HAVE_POSIX */
108 GetSystemUTCOffset()
110 #ifdef HAVE_POSIX
111 // XXX implement
112 return 0;
113 #else
114 TIME_ZONE_INFORMATION TimeZoneInformation;
115 DWORD tzi = GetTimeZoneInformation(&TimeZoneInformation);
117 int offset = -TimeZoneInformation.Bias * 60;
118 if (tzi == TIME_ZONE_ID_STANDARD)
119 offset -= TimeZoneInformation.StandardBias * 60;
121 if (tzi == TIME_ZONE_ID_DAYLIGHT)
122 offset -= TimeZoneInformation.DaylightBias * 60;
124 return offset;
125 #endif