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__)
33 #else /* !HAVE_POSIX */
35 #endif /* !HAVE_POSIX */
40 #if defined(HAVE_POSIX) && !defined(__CYGWIN__)
41 #ifdef CLOCK_MONOTONIC
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
;
48 (void)mach_timebase_info(&base
);
50 return (unsigned)((mach_absolute_time() * base
.numer
)
51 / (1000000 * base
.denom
));
53 /* we have no monotonic clock, fall back to gettimeofday() */
56 return tv
.tv_sec
* 1000 + tv
.tv_usec
/ 1000;
58 #else /* !HAVE_POSIX */
59 return ::GetTickCount();
60 #endif /* !HAVE_POSIX */
66 #if defined(HAVE_POSIX) && !defined(__CYGWIN__)
67 #ifdef CLOCK_MONOTONIC
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
;
74 (void)mach_timebase_info(&base
);
76 return (uint64_t(mach_absolute_time()) * uint64_t(base
.numer
))
77 / (1000 * uint64_t(base
.denom
));
79 /* we have no monotonic clock, fall back to gettimeofday() */
82 return uint64_t(tv
.tv_sec
) * 1000 + uint64_t(tv
.tv_usec
) / 1000;
84 #else /* !HAVE_POSIX */
85 LARGE_INTEGER l_value
, l_frequency
;
87 if (!::QueryPerformanceCounter(&l_value
) ||
88 !::QueryPerformanceFrequency(&l_frequency
))
91 uint64_t value
= l_value
.QuadPart
;
92 uint64_t frequency
= l_frequency
.QuadPart
;
94 if (frequency
> 1000000) {
96 value
/= frequency
/ 100;
97 } else if (frequency
< 1000000) {
104 #endif /* !HAVE_POSIX */
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;