1 | /* $Id: time.cpp,v 1.24 2003-01-03 11:26:43 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 | //******************************************************************************
|
---|
41 | BOOL 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 | //******************************************************************************
|
---|
54 | BOOL 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 | //******************************************************************************
|
---|
66 | BOOL 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 | //******************************************************************************
|
---|
84 | BOOL 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 | //******************************************************************************
|
---|
101 | BOOL 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 | //******************************************************************************
|
---|
113 | DWORD 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 | //******************************************************************************
|
---|
121 | void 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 | //******************************************************************************
|
---|
134 | BOOL 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 | //******************************************************************************
|
---|
147 | void 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 | //******************************************************************************
|
---|
160 | BOOL 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 | //******************************************************************************
|
---|
172 | BOOL WIN32API SystemTimeToFileTime(const SYSTEMTIME *lpSystemTime, LPFILETIME lpFileTime)
|
---|
173 | {
|
---|
174 | return O32_SystemTimeToFileTime(lpSystemTime, lpFileTime);
|
---|
175 | }
|
---|
176 | //******************************************************************************
|
---|
177 | //******************************************************************************
|
---|
178 | BOOL WIN32API SystemTimeToTzSpecificLocalTime(LPTIME_ZONE_INFORMATION lpTimeZone,
|
---|
179 | LPSYSTEMTIME lpSystemTime,
|
---|
180 | LPSYSTEMTIME lpLocalTime)
|
---|
181 | {
|
---|
182 | return O32_SystemTimeToTzSpecificLocalTime(lpTimeZone, lpSystemTime, lpLocalTime);
|
---|
183 | }
|
---|
184 | //******************************************************************************
|
---|
185 | //******************************************************************************
|
---|
186 | DWORD TimeZoneName(LPSTR name, LPWSTR text, BOOL read)
|
---|
187 | {
|
---|
188 | CHAR AsciiText_1[32];
|
---|
189 | CHAR AsciiText_2[32];
|
---|
190 | HKEY hKeyOptions;
|
---|
191 | DWORD dwKeyType;
|
---|
192 | DWORD dwDataLength = sizeof(AsciiText_1);
|
---|
193 | DWORD dwDataLength_2;
|
---|
194 | BOOL Set = FALSE;
|
---|
195 |
|
---|
196 | if (read) {
|
---|
197 | ZeroMemory(text, 2*sizeof(AsciiText_1));
|
---|
198 | }
|
---|
199 | else {
|
---|
200 | UnicodeToAscii(text, &AsciiText_2[0]);
|
---|
201 | dwDataLength_2 = strlen(AsciiText_2);
|
---|
202 | }
|
---|
203 | DWORD rc = RegOpenKeyExA(HKEY_LOCAL_MACHINE,
|
---|
204 | "SOFTWARE\\TIME\\TIME_SETTING",
|
---|
205 | 0,
|
---|
206 | (read) ? KEY_READ : KEY_ALL_ACCESS,
|
---|
207 | &hKeyOptions);
|
---|
208 | if (rc == ERROR_SUCCESS)
|
---|
209 | {
|
---|
210 | rc = RegQueryValueExA(hKeyOptions,
|
---|
211 | name,
|
---|
212 | 0,
|
---|
213 | &dwKeyType,
|
---|
214 | (LPBYTE)AsciiText_1,
|
---|
215 | &dwDataLength);
|
---|
216 | if (rc == ERROR_SUCCESS) {
|
---|
217 | if (read) AsciiToUnicode(&AsciiText_1[0], text);
|
---|
218 | else Set = (strcmp(AsciiText_1,AsciiText_2) != 0);
|
---|
219 | }
|
---|
220 | else Set = (!read);
|
---|
221 | }
|
---|
222 | else {
|
---|
223 | if (!read) {
|
---|
224 | rc = RegCreateKeyA(HKEY_LOCAL_MACHINE,
|
---|
225 | "SOFTWARE\\TIME\\TIME_SETTING",
|
---|
226 | &hKeyOptions);
|
---|
227 | Set = (rc == ERROR_SUCCESS);
|
---|
228 | }
|
---|
229 | }
|
---|
230 | if (Set) rc = RegSetValueExA(hKeyOptions,
|
---|
231 | name,
|
---|
232 | 0,
|
---|
233 | REG_SZ,
|
---|
234 | (LPBYTE)AsciiText_2,
|
---|
235 | dwDataLength_2);
|
---|
236 |
|
---|
237 | RegCloseKey(hKeyOptions);
|
---|
238 | return rc;
|
---|
239 | }
|
---|
240 | //******************************************************************************
|
---|
241 | DWORD WIN32API GetTimeZoneInformation(LPTIME_ZONE_INFORMATION lpTimeZone)
|
---|
242 | {
|
---|
243 | DWORD ret = O32_GetTimeZoneInformation(lpTimeZone);
|
---|
244 |
|
---|
245 | //Convert timezone names to unicode as WGSS (wrongly) returns ascii strings
|
---|
246 | TimeZoneName("STANDARD_NAME", &lpTimeZone->StandardName[0], TRUE);
|
---|
247 | TimeZoneName("DAYLIGHT_NAME", &lpTimeZone->DaylightName[0], TRUE);
|
---|
248 | return ret;
|
---|
249 | }
|
---|
250 | //******************************************************************************
|
---|
251 | //******************************************************************************
|
---|
252 | BOOL WIN32API SetTimeZoneInformation(const LPTIME_ZONE_INFORMATION lpTimeZone)
|
---|
253 | {
|
---|
254 | //Convert timezone names to ascii as WGSS (wrongly) expects that
|
---|
255 | TimeZoneName("STANDARD_NAME", &lpTimeZone->StandardName[0], FALSE);
|
---|
256 | TimeZoneName("DAYLIGHT_NAME", &lpTimeZone->DaylightName[0], FALSE);
|
---|
257 |
|
---|
258 | return O32_SetTimeZoneInformation(lpTimeZone);
|
---|
259 | }
|
---|
260 | /*****************************************************************************
|
---|
261 | * Name : DWORD GetSystemTimeAsFileTime
|
---|
262 | * Purpose : The GetSystemTimeAsFileTime function obtains the current system
|
---|
263 | * date and time. The information is in Coordinated Universal Time (UCT) format.
|
---|
264 | * Parameters: LLPFILETIME lLPSYSTEMTIMEAsFileTime
|
---|
265 | * Variables :
|
---|
266 | * Result :
|
---|
267 | * Remark :
|
---|
268 | * Status :
|
---|
269 | *
|
---|
270 | * Author : Patrick Haller [Mon, 1998/06/15 08:00]
|
---|
271 | *****************************************************************************/
|
---|
272 |
|
---|
273 | VOID WIN32API GetSystemTimeAsFileTime(LPFILETIME lpSystemTimeAsFileTime)
|
---|
274 | {
|
---|
275 | SYSTEMTIME st;
|
---|
276 |
|
---|
277 | GetSystemTime(&st);
|
---|
278 | SystemTimeToFileTime(&st, lpSystemTimeAsFileTime);
|
---|
279 | }
|
---|
280 | //******************************************************************************
|
---|
281 | //******************************************************************************
|
---|
282 |
|
---|
283 |
|
---|