1 | /* $Id: time.cpp,v 1.25 2003-01-08 14:25:40 sandervl Exp $ */
|
---|
2 |
|
---|
3 | /*
|
---|
4 | * Win32 time/date API functions
|
---|
5 | *
|
---|
6 | * Copyright 1998-2002 Sander van Leeuwen (sandervl@xs4all.nl)
|
---|
7 | * Copyright 1999 Christoph Bratschi (cbratschi@datacomm.ch)
|
---|
8 | *
|
---|
9 | *
|
---|
10 | * Project Odin Software License can be found in LICENSE.TXT
|
---|
11 | */
|
---|
12 |
|
---|
13 |
|
---|
14 | /*****************************************************************************
|
---|
15 | * Includes *
|
---|
16 | *****************************************************************************/
|
---|
17 |
|
---|
18 | #include <odin.h>
|
---|
19 | #include <odinwrap.h>
|
---|
20 | #include <os2sel.h>
|
---|
21 | #include <FastInfoBlocks.h>
|
---|
22 |
|
---|
23 | #include <os2win.h>
|
---|
24 |
|
---|
25 | #include <winnls.h>
|
---|
26 | #include "winuser.h"
|
---|
27 | #include <stdlib.h>
|
---|
28 | #include <string.h>
|
---|
29 | #include <stdio.h>
|
---|
30 | #include <time.h>
|
---|
31 | #include "unicode.h"
|
---|
32 | #include "oslibtime.h"
|
---|
33 | #include "winreg.h"
|
---|
34 |
|
---|
35 | #define DBG_LOCALLOG DBG_time
|
---|
36 | #include "dbglocal.h"
|
---|
37 |
|
---|
38 |
|
---|
39 | //******************************************************************************
|
---|
40 | //File time (UTC) to MS-DOS date & time values (also UTC)
|
---|
41 | //******************************************************************************
|
---|
42 | BOOL WIN32API FileTimeToDosDateTime(const FILETIME *lpFileTime, LPWORD lpDosDate,
|
---|
43 | LPWORD lpDosTime)
|
---|
44 | {
|
---|
45 | if(lpFileTime == NULL || lpDosDate == NULL || lpDosTime == NULL )
|
---|
46 | {
|
---|
47 | SetLastError(ERROR_INVALID_PARAMETER);
|
---|
48 | return FALSE;
|
---|
49 | }
|
---|
50 | return O32_FileTimeToDosDateTime(lpFileTime, lpDosDate, lpDosTime);
|
---|
51 | }
|
---|
52 | //******************************************************************************
|
---|
53 | //File time (UTC) to local time
|
---|
54 | //******************************************************************************
|
---|
55 | BOOL WIN32API FileTimeToLocalFileTime(const FILETIME *lpFileTime, LPFILETIME lpLocalTime)
|
---|
56 | {
|
---|
57 | if(lpFileTime == NULL || lpLocalTime == NULL || lpFileTime == lpLocalTime)
|
---|
58 | {
|
---|
59 | SetLastError(ERROR_INVALID_PARAMETER);
|
---|
60 | return FALSE;
|
---|
61 | }
|
---|
62 | return O32_FileTimeToLocalFileTime(lpFileTime, lpLocalTime);
|
---|
63 | }
|
---|
64 | //******************************************************************************
|
---|
65 | //Local time to File time (UTC)
|
---|
66 | //******************************************************************************
|
---|
67 | BOOL WIN32API LocalFileTimeToFileTime(const FILETIME *lpLocalFileTime,
|
---|
68 | LPFILETIME lpFileTime)
|
---|
69 | {
|
---|
70 | BOOL ret;
|
---|
71 |
|
---|
72 | if(!lpLocalFileTime || !lpFileTime || lpLocalFileTime == lpFileTime) {
|
---|
73 | dprintf(("!WARNING!: invalid parameter"));
|
---|
74 | SetLastError(ERROR_INVALID_PARAMETER);
|
---|
75 | return FALSE;
|
---|
76 | }
|
---|
77 | dprintf(("KERNEL32: LocalFileTimeToFileTime %x%x", lpLocalFileTime->dwHighDateTime, lpLocalFileTime->dwLowDateTime));
|
---|
78 | ret = O32_LocalFileTimeToFileTime(lpLocalFileTime, lpFileTime);
|
---|
79 | dprintf(("KERNEL32: LocalFileTimeToFileTime %x%x -> %d", lpFileTime->dwHighDateTime, lpFileTime->dwLowDateTime, ret));
|
---|
80 | return ret;
|
---|
81 | }
|
---|
82 | //******************************************************************************
|
---|
83 | //File time (UTC) to System time (UTC)
|
---|
84 | //******************************************************************************
|
---|
85 | BOOL WIN32API FileTimeToSystemTime(const FILETIME *lpFileTime, LPSYSTEMTIME lpSystemTime)
|
---|
86 | {
|
---|
87 | BOOL ret;
|
---|
88 |
|
---|
89 | if(lpFileTime == NULL || lpSystemTime == NULL)
|
---|
90 | {
|
---|
91 | SetLastError(ERROR_INVALID_PARAMETER);
|
---|
92 | return FALSE;
|
---|
93 | }
|
---|
94 |
|
---|
95 | ret = O32_FileTimeToSystemTime(lpFileTime, lpSystemTime);
|
---|
96 | dprintf(("time: %d-%d-%d %02d:%02d:%02d", lpSystemTime->wDay, lpSystemTime->wMonth, lpSystemTime->wYear, lpSystemTime->wHour, lpSystemTime->wMinute, lpSystemTime->wSecond));
|
---|
97 | return ret;
|
---|
98 | }
|
---|
99 | //******************************************************************************
|
---|
100 | //MS-DOS date & time values (UTC) to File time (also UTC)
|
---|
101 | //******************************************************************************
|
---|
102 | BOOL WIN32API DosDateTimeToFileTime(WORD wDosDate, WORD wDosTime, LPFILETIME pFileTime)
|
---|
103 | {
|
---|
104 | dprintf(("%x %x", wDosDate, wDosTime));
|
---|
105 |
|
---|
106 | if(pFileTime == NULL) {
|
---|
107 | SetLastError(ERROR_INVALID_PARAMETER);
|
---|
108 | return FALSE;
|
---|
109 | }
|
---|
110 | return O32_DosDateTimeToFileTime(wDosDate, wDosTime, pFileTime);
|
---|
111 | }
|
---|
112 | //******************************************************************************
|
---|
113 | //******************************************************************************
|
---|
114 | DWORD WIN32API GetTickCount(void)
|
---|
115 | {
|
---|
116 | return OSLibDosGetTickCount();
|
---|
117 | }
|
---|
118 | //******************************************************************************
|
---|
119 | //The GetLocalTime function retrieves the current local date and time.
|
---|
120 | //(in local time)
|
---|
121 | //******************************************************************************
|
---|
122 | void WIN32API GetLocalTime(LPSYSTEMTIME lpLocalTime)
|
---|
123 | {
|
---|
124 | if(lpLocalTime == NULL)
|
---|
125 | {
|
---|
126 | SetLastError(ERROR_INVALID_PARAMETER);
|
---|
127 | return;
|
---|
128 | }
|
---|
129 | O32_GetLocalTime(lpLocalTime);
|
---|
130 | }
|
---|
131 | //******************************************************************************
|
---|
132 | //The SetLocalTime function sets the current local time and date.
|
---|
133 | //(in local time)
|
---|
134 | //******************************************************************************
|
---|
135 | BOOL WIN32API SetLocalTime(const SYSTEMTIME *lpLocalTime)
|
---|
136 | {
|
---|
137 | if(lpLocalTime == NULL)
|
---|
138 | {
|
---|
139 | SetLastError(ERROR_INVALID_PARAMETER);
|
---|
140 | return FALSE;
|
---|
141 | }
|
---|
142 | return O32_SetLocalTime(lpLocalTime);
|
---|
143 | }
|
---|
144 | //******************************************************************************
|
---|
145 | //The GetSystemTime function retrieves the current system date and time.
|
---|
146 | //The system time is expressed in Coordinated Universal Time (UTC).
|
---|
147 | //******************************************************************************
|
---|
148 | void WIN32API GetSystemTime(LPSYSTEMTIME lpSystemTime)
|
---|
149 | {
|
---|
150 | if(lpSystemTime == NULL)
|
---|
151 | {
|
---|
152 | SetLastError(ERROR_INVALID_PARAMETER);
|
---|
153 | return;
|
---|
154 | }
|
---|
155 | O32_GetSystemTime(lpSystemTime);
|
---|
156 | dprintf2(("time: %d-%d-%d %02d:%02d:%02d", lpSystemTime->wDay, lpSystemTime->wMonth, lpSystemTime->wYear, lpSystemTime->wHour, lpSystemTime->wMinute, lpSystemTime->wSecond));
|
---|
157 | }
|
---|
158 | //******************************************************************************
|
---|
159 | //The SetSystemTime function sets the current system time and date.
|
---|
160 | //The system time is expressed in Coordinated Universal Time (UCT).
|
---|
161 | //******************************************************************************
|
---|
162 | BOOL WIN32API SetSystemTime(const SYSTEMTIME *lpSystemTime)
|
---|
163 | {
|
---|
164 | if(lpSystemTime == NULL)
|
---|
165 | {
|
---|
166 | SetLastError(ERROR_INVALID_PARAMETER);
|
---|
167 | return FALSE;
|
---|
168 | }
|
---|
169 | return O32_SetSystemTime(lpSystemTime);
|
---|
170 | }
|
---|
171 | //******************************************************************************
|
---|
172 | //System time (UTC) to File time (UTC)
|
---|
173 | //******************************************************************************
|
---|
174 | BOOL WIN32API SystemTimeToFileTime(const SYSTEMTIME *lpSystemTime, LPFILETIME lpFileTime)
|
---|
175 | {
|
---|
176 | return O32_SystemTimeToFileTime(lpSystemTime, lpFileTime);
|
---|
177 | }
|
---|
178 | //******************************************************************************
|
---|
179 | //******************************************************************************
|
---|
180 | BOOL WIN32API SystemTimeToTzSpecificLocalTime(LPTIME_ZONE_INFORMATION lpTimeZone,
|
---|
181 | LPSYSTEMTIME lpSystemTime,
|
---|
182 | LPSYSTEMTIME lpLocalTime)
|
---|
183 | {
|
---|
184 | return O32_SystemTimeToTzSpecificLocalTime(lpTimeZone, lpSystemTime, lpLocalTime);
|
---|
185 | }
|
---|
186 | //******************************************************************************
|
---|
187 | static const LPSTR szTZBias = "Bias";
|
---|
188 | static const LPSTR szTZActiveTimeBias = "ActiveTimeBias";
|
---|
189 |
|
---|
190 | static const LPWSTR szTZStandardName = (LPWSTR)L"StandardName";
|
---|
191 | static const LPSTR szTZStandardBias = "StandardBias";
|
---|
192 | static const LPSTR szTZStandardStart = "StandardStart";
|
---|
193 |
|
---|
194 | static const LPWSTR szTZDaylightName = (LPWSTR)L"DaylightName";
|
---|
195 | static const LPSTR szTZDaylightBias = "DaylightBias";
|
---|
196 | static const LPSTR szTZDaylightStart = "DaylightStart";
|
---|
197 | static const LPSTR KEY_WINDOWS_TZ = "SYSTEM\\CurrentControlSet\\Control\\TimeZoneInformation";
|
---|
198 | //******************************************************************************
|
---|
199 | DWORD WIN32API GetTimeZoneInformation(LPTIME_ZONE_INFORMATION lpTimeZone)
|
---|
200 | {
|
---|
201 | TIME_ZONE_INFORMATION tzone;
|
---|
202 | int len;
|
---|
203 |
|
---|
204 | HKEY hkey;
|
---|
205 |
|
---|
206 | if(RegCreateKeyA(HKEY_LOCAL_MACHINE, KEY_WINDOWS_TZ, &hkey) == ERROR_SUCCESS)
|
---|
207 | {
|
---|
208 | DWORD type, size;
|
---|
209 | DWORD rc;
|
---|
210 |
|
---|
211 | size = sizeof(lpTimeZone->Bias);
|
---|
212 | rc = RegQueryValueExA(hkey, szTZBias,0,&type, (LPBYTE)&lpTimeZone->Bias, &size);
|
---|
213 | if(rc || type != REG_DWORD) {
|
---|
214 | goto fail;
|
---|
215 | }
|
---|
216 | size = sizeof(lpTimeZone->StandardName);
|
---|
217 | rc = RegQueryValueExW(hkey, szTZStandardName, 0, &type, (LPBYTE)lpTimeZone->StandardName, &size);
|
---|
218 | if(rc || type != REG_SZ) {
|
---|
219 | goto fail;
|
---|
220 | }
|
---|
221 | size = sizeof(lpTimeZone->StandardBias);
|
---|
222 | rc = RegQueryValueExA(hkey, szTZStandardBias,0,&type, (LPBYTE)&lpTimeZone->StandardBias, &size);
|
---|
223 | if(rc || type != REG_DWORD) {
|
---|
224 | goto fail;
|
---|
225 | }
|
---|
226 | size = sizeof(lpTimeZone->StandardDate);
|
---|
227 | rc = RegQueryValueExA(hkey, szTZStandardStart,0,&type, (LPBYTE)&lpTimeZone->StandardDate, &size);
|
---|
228 | if(rc || type != REG_BINARY) {
|
---|
229 | goto fail;
|
---|
230 | }
|
---|
231 |
|
---|
232 | size = sizeof(lpTimeZone->DaylightName);
|
---|
233 | rc = RegQueryValueExW(hkey, szTZDaylightName, 0, &type, (LPBYTE)lpTimeZone->DaylightName, &size);
|
---|
234 | if(rc || type != REG_SZ) {
|
---|
235 | goto fail;
|
---|
236 | }
|
---|
237 | size = sizeof(lpTimeZone->DaylightBias);
|
---|
238 | rc = RegQueryValueExA(hkey, szTZDaylightBias,0,&type, (LPBYTE)&lpTimeZone->DaylightBias, &size);
|
---|
239 | if(rc || type != REG_DWORD) {
|
---|
240 | goto fail;
|
---|
241 | }
|
---|
242 | size = sizeof(lpTimeZone->DaylightDate);
|
---|
243 | rc = RegQueryValueExA(hkey, szTZDaylightStart,0,&type, (LPBYTE)&lpTimeZone->DaylightDate, &size);
|
---|
244 | if(rc || type != REG_BINARY) {
|
---|
245 | goto fail;
|
---|
246 | }
|
---|
247 | RegCloseKey(hkey);
|
---|
248 |
|
---|
249 | dprintf(("Bias %x", lpTimeZone->Bias));
|
---|
250 | dprintf(("StandardName %ls", lpTimeZone->StandardName));
|
---|
251 | dprintf(("StandardBias %x", lpTimeZone->StandardBias));
|
---|
252 | dprintf(("StandardDate %d-%d-%d-%d", lpTimeZone->StandardDate.wYear, lpTimeZone->StandardDate.wMonth, lpTimeZone->StandardDate.wDay, lpTimeZone->StandardDate.wDayOfWeek));
|
---|
253 | dprintf(("DaylightName %ls", lpTimeZone->DaylightName));
|
---|
254 | dprintf(("DaylightBias %x", lpTimeZone->DaylightBias));
|
---|
255 | dprintf(("DaylightDate %d-%d-%d-%d\n", lpTimeZone->DaylightDate.wYear, lpTimeZone->DaylightDate.wMonth, lpTimeZone->DaylightDate.wDay, lpTimeZone->DaylightDate.wDayOfWeek));
|
---|
256 |
|
---|
257 | //TODO: determine daylight or standard time
|
---|
258 | return TIME_ZONE_ID_UNKNOWN;
|
---|
259 | }
|
---|
260 | else
|
---|
261 | {//get it from WGSS
|
---|
262 | fail:
|
---|
263 | DWORD ret = O32_GetTimeZoneInformation(&tzone);
|
---|
264 |
|
---|
265 | *lpTimeZone = tzone;
|
---|
266 |
|
---|
267 | //Convert timezone names to unicode as WGSS (wrongly) returns ascii strings
|
---|
268 | len = sizeof(tzone.StandardName)/sizeof(WCHAR);
|
---|
269 | lstrcpynAtoW(lpTimeZone->StandardName, (LPSTR)tzone.StandardName, len);
|
---|
270 | lpTimeZone->StandardName[len] = 0;
|
---|
271 |
|
---|
272 | lstrcpynAtoW(lpTimeZone->DaylightName, (LPSTR)tzone.DaylightName, len);
|
---|
273 | lpTimeZone->DaylightName[len] = 0;
|
---|
274 |
|
---|
275 | dprintf(("Bias %x", lpTimeZone->Bias));
|
---|
276 | dprintf(("StandardName %ls", lpTimeZone->StandardName));
|
---|
277 | dprintf(("StandardBias %x", lpTimeZone->StandardBias));
|
---|
278 | dprintf(("StandardDate %d-%d-%d-%d", lpTimeZone->StandardDate.wYear, lpTimeZone->StandardDate.wMonth, lpTimeZone->StandardDate.wDay, lpTimeZone->StandardDate.wDayOfWeek));
|
---|
279 | dprintf(("DaylightName %ls", lpTimeZone->DaylightName));
|
---|
280 | dprintf(("DaylightBias %x", lpTimeZone->DaylightBias));
|
---|
281 | dprintf(("DaylightDate %d-%d-%d-%d\n", lpTimeZone->DaylightDate.wYear, lpTimeZone->DaylightDate.wMonth, lpTimeZone->DaylightDate.wDay, lpTimeZone->DaylightDate.wDayOfWeek));
|
---|
282 | return ret;
|
---|
283 | }
|
---|
284 | }
|
---|
285 | //******************************************************************************
|
---|
286 | //******************************************************************************
|
---|
287 | BOOL WIN32API SetTimeZoneInformation(const LPTIME_ZONE_INFORMATION lpTimeZone)
|
---|
288 | {
|
---|
289 | TIME_ZONE_INFORMATION tzone = *lpTimeZone;
|
---|
290 | int len;
|
---|
291 | HKEY hkey;
|
---|
292 |
|
---|
293 | if(RegCreateKeyA(HKEY_LOCAL_MACHINE, KEY_WINDOWS_TZ, &hkey) != ERROR_SUCCESS)
|
---|
294 | {
|
---|
295 | dprintf(("ERROR: SetTimeZoneInformation: Unable to create key"));
|
---|
296 | return FALSE;
|
---|
297 | }
|
---|
298 | RegSetValueExA(hkey, szTZBias,0,REG_DWORD, (LPBYTE)&lpTimeZone->Bias, sizeof(lpTimeZone->Bias));
|
---|
299 | RegSetValueExA(hkey, szTZActiveTimeBias,0,REG_DWORD, (LPBYTE)&lpTimeZone->Bias, sizeof(lpTimeZone->Bias));
|
---|
300 |
|
---|
301 | RegSetValueExW(hkey, szTZStandardName, 0, REG_SZ, (LPBYTE)lpTimeZone->StandardName, lstrlenW(lpTimeZone->StandardName));
|
---|
302 | RegSetValueExA(hkey, szTZStandardBias,0,REG_DWORD, (LPBYTE)&lpTimeZone->StandardBias, sizeof(lpTimeZone->StandardBias));
|
---|
303 | RegSetValueExA(hkey, szTZStandardStart,0,REG_BINARY, (LPBYTE)&lpTimeZone->StandardDate, sizeof(lpTimeZone->StandardDate));
|
---|
304 |
|
---|
305 | RegSetValueExW(hkey, szTZDaylightName, 0, REG_SZ, (LPBYTE)lpTimeZone->DaylightName, lstrlenW(lpTimeZone->DaylightName));
|
---|
306 | RegSetValueExA(hkey, szTZDaylightBias,0,REG_DWORD, (LPBYTE)&lpTimeZone->DaylightBias, sizeof(lpTimeZone->DaylightBias));
|
---|
307 | RegSetValueExA(hkey, szTZDaylightStart,0,REG_BINARY, (LPBYTE)&lpTimeZone->DaylightDate, sizeof(lpTimeZone->DaylightDate));
|
---|
308 | RegCloseKey(hkey);
|
---|
309 |
|
---|
310 | //Convert timezone names to ascii as WGSS (wrongly) expects that
|
---|
311 | len = sizeof(tzone.StandardName)/sizeof(WCHAR);
|
---|
312 | lstrcpynWtoA((LPSTR)tzone.StandardName, lpTimeZone->StandardName, len);
|
---|
313 | tzone.StandardName[len] = 0;
|
---|
314 |
|
---|
315 | lstrcpynWtoA((LPSTR)tzone.DaylightName, lpTimeZone->DaylightName, len);
|
---|
316 | tzone.DaylightName[len] = 0;
|
---|
317 |
|
---|
318 | return O32_SetTimeZoneInformation(&tzone);
|
---|
319 | }
|
---|
320 | /*****************************************************************************
|
---|
321 | * Name : DWORD GetSystemTimeAsFileTime
|
---|
322 | * Purpose : The GetSystemTimeAsFileTime function obtains the current system
|
---|
323 | * date and time. The information is in Coordinated Universal Time (UCT) format.
|
---|
324 | * Parameters: LLPFILETIME lLPSYSTEMTIMEAsFileTime
|
---|
325 | * Variables :
|
---|
326 | * Result :
|
---|
327 | * Remark :
|
---|
328 | * Status :
|
---|
329 | *
|
---|
330 | * Author : Patrick Haller [Mon, 1998/06/15 08:00]
|
---|
331 | *****************************************************************************/
|
---|
332 | VOID WIN32API GetSystemTimeAsFileTime(LPFILETIME lpSystemTimeAsFileTime)
|
---|
333 | {
|
---|
334 | /*
|
---|
335 | * Speculative time caching.
|
---|
336 | * We assume GetSystemTime is using DosGetDateTime.
|
---|
337 | * We assume DosGetDateTime uses the global info segment.
|
---|
338 | * We assume that SIS_MsCount is updated when the rest of the date/time
|
---|
339 | * members of the global info segment is updated.
|
---|
340 | *
|
---|
341 | * Possible sideffects:
|
---|
342 | * - The code doens't take in account changes of timezone, and hence will
|
---|
343 | * be wrong until the next timer tick. This isn't a problem I think.
|
---|
344 | * -
|
---|
345 | */
|
---|
346 | #if 1
|
---|
347 | static FILETIME LastFileTime;
|
---|
348 | static ULONG LastMsCount = -1;
|
---|
349 | if (fibGetMsCount() == LastMsCount) {
|
---|
350 | *lpSystemTimeAsFileTime = LastFileTime;
|
---|
351 | }
|
---|
352 | else
|
---|
353 | {
|
---|
354 | SYSTEMTIME st;
|
---|
355 | ULONG ulNewMsCount = fibGetMsCount();
|
---|
356 | GetSystemTime(&st);
|
---|
357 | SystemTimeToFileTime(&st, lpSystemTimeAsFileTime);
|
---|
358 | LastFileTime = *lpSystemTimeAsFileTime;
|
---|
359 | LastMsCount = ulNewMsCount;
|
---|
360 | }
|
---|
361 | dprintf2(("Time %08x%08x", lpSystemTimeAsFileTime->dwHighDateTime, lpSystemTimeAsFileTime->dwLowDateTime));
|
---|
362 | #else
|
---|
363 | SYSTEMTIME st;
|
---|
364 | GetSystemTime(&st);
|
---|
365 | SystemTimeToFileTime(&st, lpSystemTimeAsFileTime);
|
---|
366 | dprintf2(("Time %08x%08x", lpSystemTimeAsFileTime->dwHighDateTime, lpSystemTimeAsFileTime->dwLowDateTime));
|
---|
367 | #endif
|
---|
368 | }
|
---|
369 | //******************************************************************************
|
---|
370 | //******************************************************************************
|
---|
371 |
|
---|
372 |
|
---|