source: trunk/src/kernel32/time.cpp@ 9647

Last change on this file since 9647 was 9647, checked in by sandervl, 23 years ago

Get/SetTimeZoneInformation changes; RegQueryValue(Ex)W length fixes

File size: 12.8 KB
Line 
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
22#include <os2win.h>
23
24#include <winnls.h>
25#include "winuser.h"
26#include <stdlib.h>
27#include <string.h>
28#include <stdio.h>
29#include <time.h>
30#include "unicode.h"
31#include "oslibtime.h"
32#include "winreg.h"
33
34#define DBG_LOCALLOG DBG_time
35#include "dbglocal.h"
36
37
38//******************************************************************************
39//File time (UTC) to MS-DOS date & time values (also UTC)
40//******************************************************************************
41BOOL WIN32API FileTimeToDosDateTime(const FILETIME *lpFileTime, LPWORD lpDosDate,
42 LPWORD lpDosTime)
43{
44 if(lpFileTime == NULL || lpDosDate == NULL || lpDosTime == NULL )
45 {
46 SetLastError(ERROR_INVALID_PARAMETER);
47 return FALSE;
48 }
49 return O32_FileTimeToDosDateTime(lpFileTime, lpDosDate, lpDosTime);
50}
51//******************************************************************************
52//File time (UTC) to local time
53//******************************************************************************
54BOOL WIN32API FileTimeToLocalFileTime(const FILETIME *lpFileTime, LPFILETIME lpLocalTime)
55{
56 if(lpFileTime == NULL || lpLocalTime == NULL || lpFileTime == lpLocalTime)
57 {
58 SetLastError(ERROR_INVALID_PARAMETER);
59 return FALSE;
60 }
61 return O32_FileTimeToLocalFileTime(lpFileTime, lpLocalTime);
62}
63//******************************************************************************
64//Local time to File time (UTC)
65//******************************************************************************
66BOOL WIN32API LocalFileTimeToFileTime(const FILETIME *lpLocalFileTime,
67 LPFILETIME lpFileTime)
68{
69 BOOL ret;
70
71 if(!lpLocalFileTime || !lpFileTime || lpLocalFileTime == lpFileTime) {
72 dprintf(("!WARNING!: invalid parameter"));
73 SetLastError(ERROR_INVALID_PARAMETER);
74 return FALSE;
75 }
76 dprintf(("KERNEL32: LocalFileTimeToFileTime %x%x", lpLocalFileTime->dwHighDateTime, lpLocalFileTime->dwLowDateTime));
77 ret = O32_LocalFileTimeToFileTime(lpLocalFileTime, lpFileTime);
78 dprintf(("KERNEL32: LocalFileTimeToFileTime %x%x -> %d", lpFileTime->dwHighDateTime, lpFileTime->dwLowDateTime, ret));
79 return ret;
80}
81//******************************************************************************
82//File time (UTC) to System time (UTC)
83//******************************************************************************
84BOOL WIN32API FileTimeToSystemTime(const FILETIME *lpFileTime, LPSYSTEMTIME lpSystemTime)
85{
86 BOOL ret;
87
88 if(lpFileTime == NULL || lpSystemTime == NULL)
89 {
90 SetLastError(ERROR_INVALID_PARAMETER);
91 return FALSE;
92 }
93
94 ret = O32_FileTimeToSystemTime(lpFileTime, lpSystemTime);
95 dprintf(("time: %d-%d-%d %d:%d:%d", lpSystemTime->wDay, lpSystemTime->wMonth, lpSystemTime->wYear, lpSystemTime->wHour, lpSystemTime->wMinute, lpSystemTime->wSecond));
96 return ret;
97}
98//******************************************************************************
99//MS-DOS date & time values (UTC) to File time (also UTC)
100//******************************************************************************
101BOOL WIN32API DosDateTimeToFileTime(WORD wDosDate, WORD wDosTime, LPFILETIME pFileTime)
102{
103 dprintf(("%x %x", wDosDate, wDosTime));
104
105 if(pFileTime == NULL) {
106 SetLastError(ERROR_INVALID_PARAMETER);
107 return FALSE;
108 }
109 return O32_DosDateTimeToFileTime(wDosDate, wDosTime, pFileTime);
110}
111//******************************************************************************
112//******************************************************************************
113DWORD WIN32API GetTickCount(void)
114{
115 return OSLibDosGetTickCount();
116}
117//******************************************************************************
118//The GetLocalTime function retrieves the current local date and time.
119//(in local time)
120//******************************************************************************
121void WIN32API GetLocalTime(LPSYSTEMTIME lpLocalTime)
122{
123 if(lpLocalTime == NULL)
124 {
125 SetLastError(ERROR_INVALID_PARAMETER);
126 return;
127 }
128 O32_GetLocalTime(lpLocalTime);
129}
130//******************************************************************************
131//The SetLocalTime function sets the current local time and date.
132//(in local time)
133//******************************************************************************
134BOOL WIN32API SetLocalTime(const SYSTEMTIME *lpLocalTime)
135{
136 if(lpLocalTime == NULL)
137 {
138 SetLastError(ERROR_INVALID_PARAMETER);
139 return FALSE;
140 }
141 return O32_SetLocalTime(lpLocalTime);
142}
143//******************************************************************************
144//The GetSystemTime function retrieves the current system date and time.
145//The system time is expressed in Coordinated Universal Time (UTC).
146//******************************************************************************
147void WIN32API GetSystemTime(LPSYSTEMTIME lpSystemTime)
148{
149 if(lpSystemTime == NULL)
150 {
151 SetLastError(ERROR_INVALID_PARAMETER);
152 return;
153 }
154 O32_GetSystemTime(lpSystemTime);
155}
156//******************************************************************************
157//The SetSystemTime function sets the current system time and date.
158//The system time is expressed in Coordinated Universal Time (UCT).
159//******************************************************************************
160BOOL WIN32API SetSystemTime(const SYSTEMTIME *lpSystemTime)
161{
162 if(lpSystemTime == NULL)
163 {
164 SetLastError(ERROR_INVALID_PARAMETER);
165 return FALSE;
166 }
167 return O32_SetSystemTime(lpSystemTime);
168}
169//******************************************************************************
170//System time (UTC) to File time (UTC)
171//******************************************************************************
172BOOL WIN32API SystemTimeToFileTime(const SYSTEMTIME *lpSystemTime, LPFILETIME lpFileTime)
173{
174 return O32_SystemTimeToFileTime(lpSystemTime, lpFileTime);
175}
176//******************************************************************************
177//******************************************************************************
178BOOL WIN32API SystemTimeToTzSpecificLocalTime(LPTIME_ZONE_INFORMATION lpTimeZone,
179 LPSYSTEMTIME lpSystemTime,
180 LPSYSTEMTIME lpLocalTime)
181{
182 return O32_SystemTimeToTzSpecificLocalTime(lpTimeZone, lpSystemTime, lpLocalTime);
183}
184//******************************************************************************
185static const LPSTR szTZBias = "Bias";
186static const LPSTR szTZActiveTimeBias = "ActiveTimeBias";
187
188static const LPWSTR szTZStandardName = (LPWSTR)L"StandardName";
189static const LPSTR szTZStandardBias = "StandardBias";
190static const LPSTR szTZStandardStart = "StandardStart";
191
192static const LPWSTR szTZDaylightName = (LPWSTR)L"DaylightName";
193static const LPSTR szTZDaylightBias = "DaylightBias";
194static const LPSTR szTZDaylightStart = "DaylightStart";
195static const LPSTR KEY_WINDOWS_TZ = "SYSTEM\\CurrentControlSet\\Control\\TimeZoneInformation";
196//******************************************************************************
197DWORD WIN32API GetTimeZoneInformation(LPTIME_ZONE_INFORMATION lpTimeZone)
198{
199 TIME_ZONE_INFORMATION tzone;
200 int len;
201
202 HKEY hkey;
203
204 if(RegCreateKeyA(HKEY_LOCAL_MACHINE, KEY_WINDOWS_TZ, &hkey) == ERROR_SUCCESS)
205 {
206 DWORD type, size;
207 DWORD rc;
208
209 size = sizeof(lpTimeZone->Bias);
210 rc = RegQueryValueExA(hkey, szTZBias,0,&type, (LPBYTE)&lpTimeZone->Bias, &size);
211 if(rc || type != REG_DWORD) {
212 goto fail;
213 }
214 size = sizeof(lpTimeZone->StandardName);
215 rc = RegQueryValueExW(hkey, szTZStandardName, 0, &type, (LPBYTE)lpTimeZone->StandardName, &size);
216 if(rc || type != REG_SZ) {
217 goto fail;
218 }
219 size = sizeof(lpTimeZone->StandardBias);
220 rc = RegQueryValueExA(hkey, szTZStandardBias,0,&type, (LPBYTE)&lpTimeZone->StandardBias, &size);
221 if(rc || type != REG_DWORD) {
222 goto fail;
223 }
224 size = sizeof(lpTimeZone->StandardDate);
225 rc = RegQueryValueExA(hkey, szTZStandardStart,0,&type, (LPBYTE)&lpTimeZone->StandardDate, &size);
226 if(rc || type != REG_BINARY) {
227 goto fail;
228 }
229
230 size = sizeof(lpTimeZone->DaylightName);
231 rc = RegQueryValueExW(hkey, szTZDaylightName, 0, &type, (LPBYTE)lpTimeZone->DaylightName, &size);
232 if(rc || type != REG_SZ) {
233 goto fail;
234 }
235 size = sizeof(lpTimeZone->DaylightBias);
236 rc = RegQueryValueExA(hkey, szTZDaylightBias,0,&type, (LPBYTE)&lpTimeZone->DaylightBias, &size);
237 if(rc || type != REG_DWORD) {
238 goto fail;
239 }
240 size = sizeof(lpTimeZone->DaylightDate);
241 rc = RegQueryValueExA(hkey, szTZDaylightStart,0,&type, (LPBYTE)&lpTimeZone->DaylightDate, &size);
242 if(rc || type != REG_BINARY) {
243 goto fail;
244 }
245 RegCloseKey(hkey);
246
247 //TODO: we should return whether or we are in standard or daylight time
248 return TIME_ZONE_ID_STANDARD;
249 }
250 else
251 {//get it from WGSS
252fail:
253 DWORD ret = O32_GetTimeZoneInformation(&tzone);
254
255 *lpTimeZone = tzone;
256
257 //Convert timezone names to unicode as WGSS (wrongly) returns ascii strings
258 len = sizeof(tzone.StandardName)/sizeof(WCHAR);
259 lstrcpynAtoW(lpTimeZone->StandardName, (LPSTR)tzone.StandardName, len);
260 lpTimeZone->StandardName[len] = 0;
261
262 lstrcpynAtoW(lpTimeZone->DaylightName, (LPSTR)tzone.DaylightName, len);
263 lpTimeZone->DaylightName[len] = 0;
264 return ret;
265 }
266}
267//******************************************************************************
268//******************************************************************************
269BOOL WIN32API SetTimeZoneInformation(const LPTIME_ZONE_INFORMATION lpTimeZone)
270{
271 TIME_ZONE_INFORMATION tzone = *lpTimeZone;
272 int len;
273 HKEY hkey;
274
275 if(RegCreateKeyA(HKEY_LOCAL_MACHINE, KEY_WINDOWS_TZ, &hkey) != ERROR_SUCCESS)
276 {
277 dprintf(("ERROR: SetTimeZoneInformation: Unable to create key"));
278 return FALSE;
279 }
280 RegSetValueExA(hkey, szTZBias,0,REG_DWORD, (LPBYTE)&lpTimeZone->Bias, sizeof(lpTimeZone->Bias));
281 RegSetValueExA(hkey, szTZActiveTimeBias,0,REG_DWORD, (LPBYTE)&lpTimeZone->Bias, sizeof(lpTimeZone->Bias));
282
283 RegSetValueExW(hkey, szTZStandardName, 0, REG_SZ, (LPBYTE)lpTimeZone->StandardName, lstrlenW(lpTimeZone->StandardName));
284 RegSetValueExA(hkey, szTZStandardBias,0,REG_DWORD, (LPBYTE)&lpTimeZone->StandardBias, sizeof(lpTimeZone->StandardBias));
285 RegSetValueExA(hkey, szTZStandardStart,0,REG_BINARY, (LPBYTE)&lpTimeZone->StandardDate, sizeof(lpTimeZone->StandardDate));
286
287 RegSetValueExW(hkey, szTZDaylightName, 0, REG_SZ, (LPBYTE)lpTimeZone->DaylightName, lstrlenW(lpTimeZone->DaylightName));
288 RegSetValueExA(hkey, szTZDaylightBias,0,REG_DWORD, (LPBYTE)&lpTimeZone->DaylightBias, sizeof(lpTimeZone->DaylightBias));
289 RegSetValueExA(hkey, szTZDaylightStart,0,REG_BINARY, (LPBYTE)&lpTimeZone->DaylightDate, sizeof(lpTimeZone->DaylightDate));
290 RegCloseKey(hkey);
291
292 //Convert timezone names to ascii as WGSS (wrongly) expects that
293 len = sizeof(tzone.StandardName)/sizeof(WCHAR);
294 lstrcpynWtoA((LPSTR)tzone.StandardName, lpTimeZone->StandardName, len);
295 tzone.StandardName[len] = 0;
296
297 lstrcpynWtoA((LPSTR)tzone.DaylightName, lpTimeZone->DaylightName, len);
298 tzone.DaylightName[len] = 0;
299
300 return O32_SetTimeZoneInformation(&tzone);
301}
302/*****************************************************************************
303 * Name : DWORD GetSystemTimeAsFileTime
304 * Purpose : The GetSystemTimeAsFileTime function obtains the current system
305 * date and time. The information is in Coordinated Universal Time (UCT) format.
306 * Parameters: LLPFILETIME lLPSYSTEMTIMEAsFileTime
307 * Variables :
308 * Result :
309 * Remark :
310 * Status :
311 *
312 * Author : Patrick Haller [Mon, 1998/06/15 08:00]
313 *****************************************************************************/
314
315VOID WIN32API GetSystemTimeAsFileTime(LPFILETIME lpSystemTimeAsFileTime)
316{
317 SYSTEMTIME st;
318
319 GetSystemTime(&st);
320 SystemTimeToFileTime(&st, lpSystemTimeAsFileTime);
321}
322//******************************************************************************
323//******************************************************************************
324
325
Note: See TracBrowser for help on using the repository browser.