1 | /* $Id: time2-win.cpp 98103 2023-01-17 14:15:46Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Time, Windows.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2023 Oracle and/or its affiliates.
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox base platform packages, as
|
---|
10 | * available from https://www.virtualbox.org.
|
---|
11 | *
|
---|
12 | * This program is free software; you can redistribute it and/or
|
---|
13 | * modify it under the terms of the GNU General Public License
|
---|
14 | * as published by the Free Software Foundation, in version 3 of the
|
---|
15 | * License.
|
---|
16 | *
|
---|
17 | * This program is distributed in the hope that it will be useful, but
|
---|
18 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
20 | * General Public License for more details.
|
---|
21 | *
|
---|
22 | * You should have received a copy of the GNU General Public License
|
---|
23 | * along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
24 | *
|
---|
25 | * The contents of this file may alternatively be used under the terms
|
---|
26 | * of the Common Development and Distribution License Version 1.0
|
---|
27 | * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
|
---|
28 | * in the VirtualBox distribution, in which case the provisions of the
|
---|
29 | * CDDL are applicable instead of those of the GPL.
|
---|
30 | *
|
---|
31 | * You may elect to license modified versions of this file under the
|
---|
32 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
33 | *
|
---|
34 | * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
|
---|
35 | */
|
---|
36 |
|
---|
37 |
|
---|
38 | /*********************************************************************************************************************************
|
---|
39 | * Header Files *
|
---|
40 | *********************************************************************************************************************************/
|
---|
41 | #define LOG_GROUP RTLOGGROUP_TIME
|
---|
42 | #include <iprt/win/windows.h>
|
---|
43 |
|
---|
44 | #include <iprt/time.h>
|
---|
45 | #include "internal/iprt.h"
|
---|
46 |
|
---|
47 | #include <iprt/assert.h>
|
---|
48 | #include <iprt/errcore.h>
|
---|
49 | #include "internal/time.h"
|
---|
50 |
|
---|
51 | #include "internal-r3-win.h"
|
---|
52 |
|
---|
53 |
|
---|
54 | RTDECL(int) RTTimeSet(PCRTTIMESPEC pTime)
|
---|
55 | {
|
---|
56 | FILETIME FileTime;
|
---|
57 | SYSTEMTIME SysTime;
|
---|
58 | if (FileTimeToSystemTime(RTTimeSpecGetNtFileTime(pTime, &FileTime), &SysTime))
|
---|
59 | {
|
---|
60 | if (SetSystemTime(&SysTime))
|
---|
61 | return VINF_SUCCESS;
|
---|
62 | }
|
---|
63 | return RTErrConvertFromWin32(GetLastError());
|
---|
64 | }
|
---|
65 |
|
---|
66 |
|
---|
67 | RTDECL(PRTTIME) RTTimeLocalExplode(PRTTIME pTime, PCRTTIMESPEC pTimeSpec)
|
---|
68 | {
|
---|
69 | RTTIMESPEC LocalTime;
|
---|
70 | if (g_pfnSystemTimeToTzSpecificLocalTime)
|
---|
71 | {
|
---|
72 | /*
|
---|
73 | * FileTimeToLocalFileTime does not do the right thing, so we'll have
|
---|
74 | * to convert to system time and SystemTimeToTzSpecificLocalTime instead.
|
---|
75 | *
|
---|
76 | * Note! FileTimeToSystemTime drops resoultion down to milliseconds, thus
|
---|
77 | * we have to do the offUTC calculation using milliseconds and adjust
|
---|
78 | * u32Nanosecons by sub milliseconds digits.
|
---|
79 | */
|
---|
80 | SYSTEMTIME SystemTimeIn;
|
---|
81 | FILETIME FileTime;
|
---|
82 | if (FileTimeToSystemTime(RTTimeSpecGetNtFileTime(pTimeSpec, &FileTime), &SystemTimeIn))
|
---|
83 | {
|
---|
84 | SYSTEMTIME SystemTimeOut;
|
---|
85 | if (g_pfnSystemTimeToTzSpecificLocalTime(NULL /* use current TZI */, &SystemTimeIn, &SystemTimeOut))
|
---|
86 | {
|
---|
87 | if (SystemTimeToFileTime(&SystemTimeOut, &FileTime))
|
---|
88 | {
|
---|
89 | RTTimeSpecSetNtFileTime(&LocalTime, &FileTime);
|
---|
90 | pTime = RTTimeExplode(pTime, &LocalTime);
|
---|
91 | if (pTime)
|
---|
92 | {
|
---|
93 | pTime->fFlags = (pTime->fFlags & ~RTTIME_FLAGS_TYPE_MASK) | RTTIME_FLAGS_TYPE_LOCAL;
|
---|
94 | pTime->offUTC = (RTTimeSpecGetMilli(&LocalTime) - RTTimeSpecGetMilli(pTimeSpec)) / RT_MS_1MIN;
|
---|
95 | pTime->u32Nanosecond += RTTimeSpecGetNano(pTimeSpec) % RT_NS_1MS;
|
---|
96 | }
|
---|
97 | return pTime;
|
---|
98 | }
|
---|
99 | }
|
---|
100 | }
|
---|
101 | }
|
---|
102 |
|
---|
103 | /*
|
---|
104 | * The fallback is to use the current offset.
|
---|
105 | * (A better fallback would be to use the offset of the same time of the year.)
|
---|
106 | */
|
---|
107 | LocalTime = *pTimeSpec;
|
---|
108 | int64_t cNsUtcOffset = RTTimeLocalDeltaNano();
|
---|
109 | RTTimeSpecAddNano(&LocalTime, cNsUtcOffset);
|
---|
110 | pTime = RTTimeExplode(pTime, &LocalTime);
|
---|
111 | if (pTime)
|
---|
112 | {
|
---|
113 | pTime->fFlags = (pTime->fFlags & ~RTTIME_FLAGS_TYPE_MASK) | RTTIME_FLAGS_TYPE_LOCAL;
|
---|
114 | pTime->offUTC = cNsUtcOffset / RT_NS_1MIN;
|
---|
115 | }
|
---|
116 | return pTime;
|
---|
117 | }
|
---|
118 |
|
---|
119 |
|
---|
120 | /**
|
---|
121 | * Gets the delta between UTC and local time at the given time.
|
---|
122 | *
|
---|
123 | * @code
|
---|
124 | * RTTIMESPEC LocalTime;
|
---|
125 | * RTTimeNow(&LocalTime);
|
---|
126 | * RTTimeSpecAddNano(&LocalTime, RTTimeLocalDeltaNanoFor(&LocalTime));
|
---|
127 | * @endcode
|
---|
128 | *
|
---|
129 | * @param pTimeSpec The time spec giving the time to get the delta for.
|
---|
130 | * @returns Returns the nanosecond delta between UTC and local time.
|
---|
131 | */
|
---|
132 | RTDECL(int64_t) RTTimeLocalDeltaNanoFor(PCRTTIMESPEC pTimeSpec)
|
---|
133 | {
|
---|
134 | RTTIMESPEC LocalTime;
|
---|
135 | if (g_pfnSystemTimeToTzSpecificLocalTime)
|
---|
136 | {
|
---|
137 | /*
|
---|
138 | * FileTimeToLocalFileTime does not do the right thing, so we'll have
|
---|
139 | * to convert to system time and SystemTimeToTzSpecificLocalTime instead.
|
---|
140 | *
|
---|
141 | * Note! FileTimeToSystemTime drops resoultion down to milliseconds, thus
|
---|
142 | * we have to do the offUTC calculation using milliseconds and adjust
|
---|
143 | * u32Nanosecons by sub milliseconds digits.
|
---|
144 | */
|
---|
145 | SYSTEMTIME SystemTimeIn;
|
---|
146 | FILETIME FileTime;
|
---|
147 | if (FileTimeToSystemTime(RTTimeSpecGetNtFileTime(pTimeSpec, &FileTime), &SystemTimeIn))
|
---|
148 | {
|
---|
149 | SYSTEMTIME SystemTimeOut;
|
---|
150 | if (g_pfnSystemTimeToTzSpecificLocalTime(NULL /* use current TZI */, &SystemTimeIn, &SystemTimeOut))
|
---|
151 | {
|
---|
152 | if (SystemTimeToFileTime(&SystemTimeOut, &FileTime))
|
---|
153 | {
|
---|
154 | RTTimeSpecSetNtFileTime(&LocalTime, &FileTime);
|
---|
155 |
|
---|
156 | return (RTTimeSpecGetMilli(&LocalTime) - RTTimeSpecGetMilli(pTimeSpec)) * RT_NS_1MS;
|
---|
157 | }
|
---|
158 | }
|
---|
159 | }
|
---|
160 | }
|
---|
161 |
|
---|
162 | return RTTimeLocalDeltaNano();
|
---|
163 | }
|
---|
164 |
|
---|