VirtualBox

source: vbox/trunk/src/VBox/Runtime/r3/win/time-win.cpp@ 27653

Last change on this file since 27653 was 26212, checked in by vboxsync, 15 years ago

time-win.cpp: missing headers

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 6.9 KB
Line 
1/* $Id: time-win.cpp 26212 2010-02-03 16:59:08Z vboxsync $ */
2/** @file
3 * IPRT - Time, Windows.
4 */
5
6/*
7 * Copyright (C) 2006-2010 Sun Microsystems, Inc.
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 *
17 * The contents of this file may alternatively be used under the terms
18 * of the Common Development and Distribution License Version 1.0
19 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
20 * VirtualBox OSE distribution, in which case the provisions of the
21 * CDDL are applicable instead of those of the GPL.
22 *
23 * You may elect to license modified versions of this file under the
24 * terms and conditions of either the GPL or the CDDL or both.
25 *
26 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
27 * Clara, CA 95054 USA or visit http://www.sun.com if you need
28 * additional information or have any questions.
29 */
30
31
32/*******************************************************************************
33* Header Files *
34*******************************************************************************/
35#define LOG_GROUP RTLOGGROUP_TIME
36#include <Windows.h>
37
38#include <iprt/time.h>
39#include "internal/iprt.h"
40
41#include <iprt/asm.h>
42#include <iprt/assert.h>
43#include <iprt/err.h>
44#include "internal/time.h"
45
46#define USE_TICK_COUNT
47//#define USE_PERFORMANCE_COUNTER
48#if 0//defined(RT_ARCH_X86) || defined(RT_ARCH_AMD64)
49# define USE_INTERRUPT_TIME
50#else
51//# define USE_FILE_TIME
52#endif
53
54
55#ifdef USE_INTERRUPT_TIME
56
57typedef struct _MY_KSYSTEM_TIME
58{
59 ULONG LowPart;
60 LONG High1Time;
61 LONG High2Time;
62} MY_KSYSTEM_TIME;
63
64typedef struct _MY_KUSER_SHARED_DATA
65{
66 ULONG TickCountLowDeprecated;
67 ULONG TickCountMultiplier;
68 volatile MY_KSYSTEM_TIME InterruptTime;
69 /* The rest is not relevant. */
70} MY_KUSER_SHARED_DATA, *PMY_KUSER_SHARED_DATA;
71
72#endif /* USE_INTERRUPT_TIME */
73
74
75DECLINLINE(uint64_t) rtTimeGetSystemNanoTS(void)
76{
77#if defined USE_TICK_COUNT
78 /*
79 * This would work if it didn't flip over every 49 (or so) days.
80 */
81 return (uint64_t)GetTickCount() * (uint64_t)1000000;
82
83#elif defined USE_PERFORMANCE_COUNTER
84 /*
85 * Slow and not derived from InterruptTime.
86 */
87 static LARGE_INTEGER llFreq;
88 static unsigned uMult;
89 if (!llFreq.QuadPart)
90 {
91 if (!QueryPerformanceFrequency(&llFreq))
92 return (uint64_t)GetTickCount() * (uint64_t)1000000;
93 llFreq.QuadPart /= 1000;
94 uMult = 1000000; /* no math genius, but this seemed to help avoiding floating point. */
95 }
96
97 LARGE_INTEGER ll;
98 if (QueryPerformanceCounter(&ll))
99 return (ll.QuadPart * uMult) / llFreq.QuadPart;
100 else
101 return (uint64_t)GetTickCount() * (uint64_t)1000000;
102
103#elif defined USE_FILE_TIME
104 /*
105 * This is SystemTime not InterruptTime.
106 */
107 uint64_t u64; /* manual say larger integer, should be safe to assume it's the same. */
108 GetSystemTimeAsFileTime((LPFILETIME)&u64);
109 return u64 * 100;
110
111#elif defined USE_INTERRUPT_TIME
112 /*
113 * This is exactly what we want, but we have to obtain it by non-official
114 * means.
115 */
116 static MY_KUSER_SHARED_DATA *s_pUserSharedData = NULL;
117 if (!s_pUserSharedData)
118 {
119 /** @todo find official way of getting this or some more clever
120 * detection algorithm if necessary. The com debugger class
121 * exports this too, windbg knows it too... */
122 s_pUserSharedData = (MY_ KUSER_SHARED_DATA *)(uintptr_t)0x7ffe0000;
123 }
124
125 /* use interrupt time */
126 LARGE_INTEGER Time;
127 do
128 {
129 Time.HighPart = s_pUserSharedData->InterruptTime.High1Time;
130 Time.LowPart = s_pUserSharedData->InterruptTime.LowPart;
131 } while (s_pUserSharedData->InterruptTime.High2Time != Time.HighPart);
132
133 return (uint64_t)Time.QuadPart * 100;
134
135#else
136# error "Must select a method bright guy!"
137#endif
138}
139
140
141RTDECL(uint64_t) RTTimeSystemNanoTS(void)
142{
143 return rtTimeGetSystemNanoTS();
144}
145
146
147RTDECL(uint64_t) RTTimeSystemMilliTS(void)
148{
149 return rtTimeGetSystemNanoTS();
150}
151
152
153RTDECL(PRTTIMESPEC) RTTimeNow(PRTTIMESPEC pTime)
154{
155 uint64_t u64;
156 AssertCompile(sizeof(u64) == sizeof(FILETIME));
157 GetSystemTimeAsFileTime((LPFILETIME)&u64);
158 return RTTimeSpecSetNtTime(pTime, u64);
159}
160
161
162RTDECL(int) RTTimeSet(PCRTTIMESPEC pTime)
163{
164 FILETIME FileTime;
165 SYSTEMTIME SysTime;
166 if (FileTimeToSystemTime(RTTimeSpecGetNtFileTime(pTime, &FileTime), &SysTime))
167 {
168 if (SetSystemTime(&SysTime))
169 return VINF_SUCCESS;
170 }
171 return RTErrConvertFromWin32(GetLastError());
172}
173
174
175RTDECL(PRTTIMESPEC) RTTimeLocalNow(PRTTIMESPEC pTime)
176{
177 uint64_t u64;
178 AssertCompile(sizeof(u64) == sizeof(FILETIME));
179 GetSystemTimeAsFileTime((LPFILETIME)&u64);
180 uint64_t u64Local;
181 if (!FileTimeToLocalFileTime((FILETIME const *)&u64, (LPFILETIME)&u64Local))
182 u64Local = u64;
183 return RTTimeSpecSetNtTime(pTime, u64Local);
184}
185
186
187RTDECL(int64_t) RTTimeLocalDeltaNano(void)
188{
189 /*
190 * UTC = local + Tzi.Bias;
191 * The bias is given in minutes.
192 */
193 TIME_ZONE_INFORMATION Tzi;
194 Tzi.Bias = 0;
195 if (GetTimeZoneInformation(&Tzi) != TIME_ZONE_ID_INVALID)
196 return -(int64_t)Tzi.Bias * 60*1000*1000*1000;
197 return 0;
198}
199
200
201RTDECL(PRTTIME) RTTimeLocalExplode(PRTTIME pTime, PCRTTIMESPEC pTimeSpec)
202{
203 /*
204 * FileTimeToLocalFileTime does not do the right thing, so we'll have
205 * to convert to system time and SystemTimeToTzSpecificLocalTime instead.
206 */
207 RTTIMESPEC LocalTime;
208 SYSTEMTIME SystemTimeIn;
209 FILETIME FileTime;
210 if (FileTimeToSystemTime(RTTimeSpecGetNtFileTime(pTimeSpec, &FileTime), &SystemTimeIn))
211 {
212 SYSTEMTIME SystemTimeOut;
213 if (SystemTimeToTzSpecificLocalTime(NULL /* use current TZI */,
214 &SystemTimeIn,
215 &SystemTimeOut))
216 {
217 if (SystemTimeToFileTime(&SystemTimeOut, &FileTime))
218 {
219 RTTimeSpecSetNtFileTime(&LocalTime, &FileTime);
220 pTime = RTTimeExplode(pTime, &LocalTime);
221 if (pTime)
222 pTime->fFlags = (pTime->fFlags & ~RTTIME_FLAGS_TYPE_MASK) | RTTIME_FLAGS_TYPE_LOCAL;
223 return pTime;
224 }
225 }
226 }
227
228 /*
229 * The fallback is to use the current offset.
230 * (A better fallback would be to use the offset of the same time of the year.)
231 */
232 LocalTime = *pTimeSpec;
233 RTTimeSpecAddNano(&LocalTime, RTTimeLocalDeltaNano());
234 pTime = RTTimeExplode(pTime, &LocalTime);
235 if (pTime)
236 pTime->fFlags = (pTime->fFlags & ~RTTIME_FLAGS_TYPE_MASK) | RTTIME_FLAGS_TYPE_LOCAL;
237 return pTime;
238}
239
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette