Added MSK timezone info.
[wine/hacks.git] / dlls / ntdll / time.c
blobb982cad6ffe6729ed0a3310c886660a1fb2e2d8a
1 /*
2 * Nt time functions.
4 * RtlTimeToTimeFields, RtlTimeFieldsToTime and defines are taken from ReactOS and
5 * adapted to wine with special permissions of the author. This code is
6 * Copyright 2002 Rex Jolliff (rex@lvcablemodem.com)
8 * Copyright 1999 Juergen Schmied
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 #include "config.h"
26 #include "wine/port.h"
28 #include <stdarg.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <limits.h>
32 #include <time.h>
33 #ifdef HAVE_SYS_TIME_H
34 # include <sys/time.h>
35 #endif
36 #ifdef HAVE_UNISTD_H
37 # include <unistd.h>
38 #endif
40 #define NONAMELESSUNION
41 #define NONAMELESSSTRUCT
42 #include "windef.h"
43 #include "winbase.h"
44 #include "winreg.h"
45 #include "winternl.h"
46 #include "wine/unicode.h"
47 #include "wine/debug.h"
48 #include "ntdll_misc.h"
50 WINE_DEFAULT_DEBUG_CHANNEL(ntdll);
52 static CRITICAL_SECTION TIME_GetBias_section;
53 static CRITICAL_SECTION_DEBUG critsect_debug =
55 0, 0, &TIME_GetBias_section,
56 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
57 0, 0, { 0, (DWORD)(__FILE__ ": TIME_GetBias_section") }
59 static CRITICAL_SECTION TIME_GetBias_section = { &critsect_debug, -1, 0, 0, 0, 0 };
61 /* TimeZone registry key values */
62 static const WCHAR TZInformationKeyW[] = { 'M','a','c','h','i','n','e','\\',
63 'S','Y','S','T','E','M','\\','C','u','r','r','e','n','t','C','o','n','t','r',
64 'o','l','S','e','t','\\','C','o','n','t','r','o','l','\\','T','i','m','e','z',
65 'o','n','e','I','n','f','o','r','m','a','t','i','o','n', 0};
66 static const WCHAR TZStandardStartW[] = {
67 'S','t','a','n','d','a','r','d','s','t','a','r','t', 0};
68 static const WCHAR TZDaylightStartW[] = {
69 'D','a','y','l','i','g','h','t','s','t','a','r','t', 0};
70 static const WCHAR TZDaylightBiasW[] = {
71 'D','a','y','l','i','g','h','t','B','i','a','s', 0};
72 static const WCHAR TZStandardBiasW[] = {
73 'S','t','a','n','d','a','r','d','B','i','a','s', 0};
74 static const WCHAR TZBiasW[] = {'B','i','a','s', 0};
75 static const WCHAR TZDaylightNameW[] = {
76 'D','a','y','l','i','g','h','t','N','a','m','e', 0};
77 static const WCHAR TZStandardNameW[] = {
78 'S','t','a','n','d','a','r','d','N','a','m','e', 0};
81 #define SETTIME_MAX_ADJUST 120
83 /* This structure is used to store strings that represent all of the time zones
84 * in the world. (This is used to help GetTimeZoneInformation)
86 struct tagTZ_INFO
88 const char *psTZFromUnix;
89 WCHAR psTZWindows[32];
90 int bias;
91 int dst;
94 static const struct tagTZ_INFO TZ_INFO[] =
96 {"MHT",
97 {'D','a','t','e','l','i','n','e',' ','S','t','a','n','d','a','r','d',' ',
98 'T','i','m','e','\0'}, -720, 0},
99 {"SST",
100 {'S','a','m','o','a',' ','S','t','a','n','d','a','r','d',' ','T','i','m',
101 'e','\0'}, 660, 0},
102 {"HST",
103 {'H','a','w','a','i','i','a','n',' ','S','t','a','n','d','a','r','d',' ',
104 'T','i','m','e','\0'}, 600, 0},
105 {"AKDT",
106 {'A','l','a','s','k','a','n',' ','S','t','a','n','d','a','r','d',' ','T',
107 'i','m','e','\0'}, 480, 1},
108 {"PST",
109 {'P','a','c','i','f','i','c',' ','S','t','a','n','d','a','r','d',' ','T',
110 'i','m','e','\0'}, 480, 0},
111 {"PDT",
112 {'P','a','c','i','f','i','c',' ','S','t','a','n','d','a','r','d',' ','T',
113 'i','m','e','\0'}, 420, 1},
114 {"MST",
115 {'U','S',' ','M','o','u','n','t','a','i','n',' ','S','t','a','n','d','a',
116 'r','d',' ','T','i','m','e','\0'}, 420, 0},
117 {"MDT",
118 {'M','o','u','n','t','a','i','n',' ','S','t','a','n','d','a','r','d',' ',
119 'T','i','m','e','\0'}, 360, 1},
120 {"CST",
121 {'C','e','n','t','r','a','l',' ','A','m','e','r','i','c','a',' ','S','t',
122 'a','n','d','a','r','d',' ','T','i','m','e','\0'}, 360, 0},
123 {"CDT",
124 {'C','e','n','t','r','a','l',' ','S','t','a','n','d','a','r','d',' ','T',
125 'i','m','e','\0'}, 300, 1},
126 {"COT",
127 {'S','A',' ','P','a','c','i','f','i','c',' ','S','t','a','n','d','a','r',
128 'd',' ','T','i','m','e','\0'}, 300, 0},
129 {"EDT",
130 {'E','a','s','t','e','r','n',' ','S','t','a','n','d','a','r','d',' ','T',
131 'i','m','e','\0'}, 240, 1},
132 {"EST",
133 {'U','S',' ','E','a','s','t','e','r','n',' ','S','t','a','n','d','a','r',
134 'd',' ','T','i','m','e','\0'}, 300, 0},
135 {"ADT",
136 {'A','t','l','a','n','t','i','c',' ','S','t','a','n','d','a','r','d',' ',
137 'T','i','m','e','\0'}, 180, 1},
138 {"VET",
139 {'S','A',' ','W','e','s','t','e','r','n',' ','S','t','a','n','d','a','r',
140 'd',' ','T','i','m','e','\0'}, 240, 0},
141 {"CLT",
142 {'P','a','c','i','f','i','c',' ','S','A',' ','S','t','a','n','d','a','r',
143 'd',' ','T','i','m','e','\0'}, 240, 0},
144 {"NDT",
145 {'N','e','w','f','o','u','n','d','l','a','n','d',' ','S','t','a','n','d',
146 'a','r','d',' ','T','i','m','e','\0'}, 150, 1},
147 {"BRT",
148 {'E','.',' ','S','o','u','t','h',' ','A','m','e','r','i','c','a',' ','S',
149 't','a','n','d','a','r','d',' ','T','i','m','e','\0'}, 180, 0},
150 {"ART",
151 {'S','A',' ','E','a','s','t','e','r','n',' ','S','t','a','n','d','a','r',
152 'd',' ','T','i','m','e','\0'}, 180, 0},
153 {"WGST",
154 {'G','r','e','e','n','l','a','n','d',' ','S','t','a','n','d','a','r','d',
155 ' ','T','i','m','e','\0'}, 120, 1},
156 {"GST",
157 {'M','i','d','-','A','t','l','a','n','t','i','c',' ','S','t','a','n','d',
158 'a','r','d',' ','T','i','m','e','\0'}, 120, 0},
159 {"AZOST",
160 {'A','z','o','r','e','s',' ','S','t','a','n','d','a','r','d',' ','T','i',
161 'm','e','\0'}, 0, 1},
162 {"CVT",
163 {'C','a','p','e',' ','V','e','r','d','e',' ','S','t','a','n','d','a','r',
164 'd',' ','T','i','m','e','\0'}, 60, 0},
165 {"WET",
166 {'G','r','e','e','n','w','i','c','h',' ','S','t','a','n','d','a','r','d',
167 ' ','T','i','m','e','\0'}, 0, 0},
168 {"BST",
169 {'G','M','T',' ','S','t','a','n','d','a','r','d',' ','T','i','m','e','\0'},
170 -60, 1},
171 {"GMT",
172 {'G','M','T',' ','S','t','a','n','d','a','r','d',' ','T','i','m','e','\0'},
173 0, 0},
174 {"UTC",
175 {'G','M','T',' ','S','t','a','n','d','a','r','d',' ','T','i','m','e','\0'},
176 0, 0},
177 {"CET",
178 {'C','e','n','t','r','a','l',' ','E','u','r','o','p','e','a','n',' ',
179 'T','i','m','e','\0'}, -60, 0},
180 {"CEST",
181 {'C','e','n','t','r','a','l',' ','E','u','r','o','p','e',' ','S','t','a',
182 'n','d','a','r','d',' ','T','i','m','e','\0'}, -120, 1},
183 {"MET",
184 {'C','e','n','t','r','a','l',' ','E','u','r','o','p','e',' ','S','t','a',
185 'n','d','a','r','d',' ','T','i','m','e','\0'}, -60, 0},
186 {"MEST",
187 {'C','e','n','t','r','a','l',' ','E','u','r','o','p','e',' ','D','a','y',
188 'l','i','g','h','t',' ','T','i','m','e','\0'}, -120, 1},
189 {"WAT",
190 {'W','.',' ','C','e','n','t','r','a','l',' ','A','f','r','i','c','a',' ',
191 'S','t','a','n','d','a','r','d',' ','T','i','m','e','\0'}, -60, 0},
192 {"EEST",
193 {'E','.',' ','E','u','r','o','p','e',' ','S','t','a','n','d','a','r','d',
194 ' ','T','i','m','e','\0'}, -180, 1},
195 {"EET",
196 {'E','g','y','p','t',' ','S','t','a','n','d','a','r','d',' ','T','i','m',
197 'e','\0'}, -120, 0},
198 {"CAT",
199 {'S','o','u','t','h',' ','A','f','r','i','c','a',' ','S','t','a','n','d',
200 'a','r','d',' ','T','i','m','e','\0'}, -120, 0},
201 {"IST",
202 {'I','s','r','a','e','l',' ','S','t','a','n','d','a','r','d',' ','T','i',
203 'm','e','\0'}, -120, 0},
204 {"MSK",
205 {'R','u','s','s','i','a','n',' ','S','t','a','n','d','a','r','d',' ','T',
206 'i','m','e','\0'}, -180, 0},
207 {"ADT",
208 {'A','r','a','b','i','c',' ','S','t','a','n','d','a','r','d',' ','T','i',
209 'm','e','\0'}, -240, 1},
210 {"AST",
211 {'A','r','a','b',' ','S','t','a','n','d','a','r','d',' ','T','i','m','e',
212 '\0'}, -180, 0},
213 {"MSD",
214 {'R','u','s','s','i','a','n',' ','S','t','a','n','d','a','r','d',' ','T',
215 'i','m','e','\0'}, -240, 1},
216 {"EAT",
217 {'E','.',' ','A','f','r','i','c','a',' ','S','t','a','n','d','a','r','d',
218 ' ','T','i','m','e','\0'}, -180, 0},
219 {"IRST",
220 {'I','r','a','n',' ','S','t','a','n','d','a','r','d',' ','T','i','m','e',
221 '\0'}, -270, 1},
222 {"GST",
223 {'A','r','a','b','i','a','n',' ','S','t','a','n','d','a','r','d',' ','T',
224 'i','m','e','\0'}, -240, 0},
225 {"AZST",
226 {'C','a','u','c','a','s','u','s',' ','S','t','a','n','d','a','r','d',' ',
227 'T','i','m','e','\0'}, -300, 1},
228 {"AFT",
229 {'A','f','g','h','a','n','i','s','t','a','n',' ','S','t','a','n','d','a',
230 'r','d',' ','T','i','m','e','\0'}, -270, 0},
231 {"YEKST",
232 {'E','k','a','t','e','r','i','n','b','u','r','g',' ','S','t','a','n','d',
233 'a','r','d',' ','T','i','m','e','\0'}, -360, 1},
234 {"PKT",
235 {'W','e','s','t',' ','A','s','i','a',' ','S','t','a','n','d','a','r','d',
236 ' ','T','i','m','e','\0'}, -300, 0},
237 {"IST",
238 {'I','n','d','i','a',' ','S','t','a','n','d','a','r','d',' ','T','i','m',
239 'e','\0'}, -330, 0},
240 {"NPT",
241 {'N','e','p','a','l',' ','S','t','a','n','d','a','r','d',' ','T','i','m',
242 'e','\0'}, -345, 0},
243 {"ALMST",
244 {'N','.',' ','C','e','n','t','r','a','l',' ','A','s','i','a',' ','S','t',
245 'a','n','d','a','r','d',' ','T','i','m','e','\0'}, -420, 1},
246 {"BDT",
247 {'C','e','n','t','r','a','l',' ','A','s','i','a',' ','S','t','a','n','d',
248 'a','r','d',' ','T','i','m','e','\0'}, -360, 0},
249 {"LKT",
250 {'S','r','i',' ','L','a','n','k','a',' ','S','t','a','n','d','a','r','d',
251 ' ','T','i','m','e','\0'}, -360, 0},
252 {"MMT",
253 {'M','y','a','n','m','a','r',' ','S','t','a','n','d','a','r','d',' ','T',
254 'i','m','e','\0'}, -390, 0},
255 {"ICT",
256 {'S','E',' ','A','s','i','a',' ','S','t','a','n','d','a','r','d',' ','T',
257 'i','m','e','\0'}, -420, 0},
258 {"KRAST",
259 {'N','o','r','t','h',' ','A','s','i','a',' ','S','t','a','n','d','a','r',
260 'd',' ','T','i','m','e','\0'}, -480, 1},
261 {"CST",
262 {'C','h','i','n','a',' ','S','t','a','n','d','a','r','d',' ','T','i','m',
263 'e','\0'}, -480, 0},
264 {"IRKST",
265 {'N','o','r','t','h',' ','A','s','i','a',' ','E','a','s','t',' ','S','t',
266 'a','n','d','a','r','d',' ','T','i','m','e','\0'}, -540, 1},
267 {"SGT",
268 {'M','a','l','a','y',' ','P','e','n','i','n','s','u','l','a',' ','S','t',
269 'a','n','d','a','r','d',' ','T','i','m','e','\0'}, -480, 0},
270 {"WST",
271 {'W','.',' ','A','u','s','t','r','a','l','i','a',' ','S','t','a','n','d',
272 'a','r','d',' ','T','i','m','e','\0'}, -480, 0},
273 {"JST",
274 {'T','o','k','y','o',' ','S','t','a','n','d','a','r','d',' ','T','i','m',
275 'e','\0'}, -540, 0},
276 {"KST",
277 {'K','o','r','e','a',' ','S','t','a','n','d','a','r','d',' ','T','i','m',
278 'e','\0'}, -540, 0},
279 {"YAKST",
280 {'Y','a','k','u','t','s','k',' ','S','t','a','n','d','a','r','d',' ','T',
281 'i','m','e','\0'}, -600, 1},
282 {"CST",
283 {'C','e','n','.',' ','A','u','s','t','r','a','l','i','a',' ','S','t','a',
284 'n','d','a','r','d',' ','T','i','m','e','\0'}, -570, 0},
285 {"EST",
286 {'E','.',' ','A','u','s','t','r','a','l','i','a',' ','S','t','a','n','d',
287 'a','r','d',' ','T','i','m','e','\0'}, -600, 0},
288 {"GST",
289 {'W','e','s','t',' ','P','a','c','i','f','i','c',' ','S','t','a','n','d',
290 'a','r','d',' ','T','i','m','e','\0'}, -600, 0},
291 {"VLAST",
292 {'V','l','a','d','i','v','o','s','t','o','k',' ','S','t','a','n','d','a',
293 'r','d',' ','T','i','m','e','\0'}, -660, 1},
294 {"MAGST",
295 {'C','e','n','t','r','a','l',' ','P','a','c','i','f','i','c',' ','S','t',
296 'a','n','d','a','r','d',' ','T','i','m','e','\0'}, -720, 1},
297 {"NZST",
298 {'N','e','w',' ','Z','e','a','l','a','n','d',' ','S','t','a','n','d','a',
299 'r','d',' ','T','i','m','e','\0'}, -720, 0},
300 {"FJT",
301 {'F','i','j','i',' ','S','t','a','n','d','a','r','d',' ','T','i','m','e',
302 '\0'}, -720, 0},
303 {"TOT",
304 {'T','o','n','g','a',' ','S','t','a','n','d','a','r','d',' ','T','i','m',
305 'e','\0'}, -780, 0}
308 #define TICKSPERSEC 10000000
309 #define TICKSPERMSEC 10000
310 #define SECSPERDAY 86400
311 #define SECSPERHOUR 3600
312 #define SECSPERMIN 60
313 #define MINSPERHOUR 60
314 #define HOURSPERDAY 24
315 #define EPOCHWEEKDAY 1 /* Jan 1, 1601 was Monday */
316 #define DAYSPERWEEK 7
317 #define EPOCHYEAR 1601
318 #define DAYSPERNORMALYEAR 365
319 #define DAYSPERLEAPYEAR 366
320 #define MONSPERYEAR 12
321 #define DAYSPERQUADRICENTENNIUM (365 * 400 + 97)
322 #define DAYSPERNORMALCENTURY (365 * 100 + 24)
323 #define DAYSPERNORMALQUADRENNIUM (365 * 4 + 1)
325 /* 1601 to 1970 is 369 years plus 89 leap days */
326 #define SECS_1601_TO_1970 ((369 * 365 + 89) * (ULONGLONG)SECSPERDAY)
327 #define TICKS_1601_TO_1970 (SECS_1601_TO_1970 * TICKSPERSEC)
328 /* 1601 to 1980 is 379 years plus 91 leap days */
329 #define SECS_1601_TO_1980 ((379 * 365 + 91) * (ULONGLONG)SECSPERDAY)
330 #define TICKS_1601_TO_1980 (SECS_1601_TO_1980 * TICKSPERSEC)
331 /* max ticks that can be represented as Unix time */
332 #define TICKS_1601_TO_UNIX_MAX ((SECS_1601_TO_1970 + INT_MAX) * TICKSPERSEC)
335 static const int MonthLengths[2][MONSPERYEAR] =
337 { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
338 { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
341 static const int YearDays[2][MONSPERYEAR+1] =
343 { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 },
344 { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 }
347 static inline int IsLeapYear(int Year)
349 return Year % 4 == 0 && (Year % 100 != 0 || Year % 400 == 0) ? 1 : 0;
352 static inline void NormalizeTimeFields(CSHORT *FieldToNormalize, CSHORT *CarryField,int Modulus)
354 *FieldToNormalize = (CSHORT) (*FieldToNormalize - Modulus);
355 *CarryField = (CSHORT) (*CarryField + 1);
358 /***********************************************************************
359 * NTDLL_get_server_timeout
361 * Convert a NTDLL timeout into a timeval struct to send to the server.
363 void NTDLL_get_server_timeout( abs_time_t *when, const LARGE_INTEGER *timeout )
365 UINT remainder;
367 if (!timeout) /* infinite timeout */
369 when->sec = when->usec = 0;
371 else if (timeout->QuadPart <= 0) /* relative timeout */
373 struct timeval tv;
375 if (-timeout->QuadPart > (LONGLONG)INT_MAX * TICKSPERSEC)
376 when->sec = when->usec = INT_MAX;
377 else
379 ULONG sec = RtlEnlargedUnsignedDivide( -timeout->QuadPart, TICKSPERSEC, &remainder );
380 gettimeofday( &tv, 0 );
381 when->sec = tv.tv_sec + sec;
382 if ((when->usec = tv.tv_usec + (remainder / 10)) >= 1000000)
384 when->usec -= 1000000;
385 when->sec++;
387 if (when->sec < tv.tv_sec) /* overflow */
388 when->sec = when->usec = INT_MAX;
391 else /* absolute time */
393 if (timeout->QuadPart < TICKS_1601_TO_1970)
394 when->sec = when->usec = 0;
395 else if (timeout->QuadPart > TICKS_1601_TO_UNIX_MAX)
396 when->sec = when->usec = INT_MAX;
397 else
399 when->sec = RtlEnlargedUnsignedDivide( timeout->QuadPart - TICKS_1601_TO_1970,
400 TICKSPERSEC, &remainder );
401 when->usec = remainder / 10;
407 /******************************************************************************
408 * RtlTimeToTimeFields [NTDLL.@]
410 * Convert a time into a TIME_FIELDS structure.
412 * PARAMS
413 * liTime [I] Time to convert.
414 * TimeFields [O] Destination for the converted time.
416 * RETURNS
417 * Nothing.
419 VOID WINAPI RtlTimeToTimeFields(
420 const LARGE_INTEGER *liTime,
421 PTIME_FIELDS TimeFields)
423 int SecondsInDay, DeltaYear;
424 int LeapYear, CurMonth;
425 long int Days;
426 LONGLONG Time = liTime->QuadPart;
428 /* Extract millisecond from time and convert time into seconds */
429 TimeFields->Milliseconds = (CSHORT) ((Time % TICKSPERSEC) / TICKSPERMSEC);
430 Time = Time / TICKSPERSEC;
432 /* The native version of RtlTimeToTimeFields does not take leap seconds
433 * into account */
435 /* Split the time into days and seconds within the day */
436 Days = Time / SECSPERDAY;
437 SecondsInDay = Time % SECSPERDAY;
439 /* compute time of day */
440 TimeFields->Hour = (CSHORT) (SecondsInDay / SECSPERHOUR);
441 SecondsInDay = SecondsInDay % SECSPERHOUR;
442 TimeFields->Minute = (CSHORT) (SecondsInDay / SECSPERMIN);
443 TimeFields->Second = (CSHORT) (SecondsInDay % SECSPERMIN);
445 /* compute day of week */
446 TimeFields->Weekday = (CSHORT) ((EPOCHWEEKDAY + Days) % DAYSPERWEEK);
448 /* compute year */
449 /* FIXME: handle calendar modifications */
450 TimeFields->Year = EPOCHYEAR;
451 DeltaYear = Days / DAYSPERQUADRICENTENNIUM;
452 TimeFields->Year += DeltaYear * 400;
453 Days -= DeltaYear * DAYSPERQUADRICENTENNIUM;
454 DeltaYear = Days / DAYSPERNORMALCENTURY;
455 if( DeltaYear > 3) DeltaYear = 3; /* fix 31 Dec of 2000 and every
456 400 years after that */
457 TimeFields->Year += DeltaYear * 100;
458 Days -= DeltaYear * DAYSPERNORMALCENTURY;
459 DeltaYear = Days / DAYSPERNORMALQUADRENNIUM;
460 TimeFields->Year += DeltaYear * 4;
461 Days -= DeltaYear * DAYSPERNORMALQUADRENNIUM;
462 DeltaYear = Days / DAYSPERNORMALYEAR;
463 if( DeltaYear > 3) DeltaYear = 3; /* fix 31 Dec of every leap year */
464 TimeFields->Year += DeltaYear;
465 Days -= DeltaYear * DAYSPERNORMALYEAR;
467 LeapYear = IsLeapYear(TimeFields->Year);
469 /* Compute month of year */
470 CurMonth = 1;
471 while (Days >= YearDays[LeapYear][CurMonth]) CurMonth++;
472 TimeFields->Day = Days - YearDays[LeapYear][CurMonth-1] + 1;
473 TimeFields->Month = CurMonth;
476 /******************************************************************************
477 * RtlTimeFieldsToTime [NTDLL.@]
479 * Convert a TIME_FIELDS structure into a time.
481 * PARAMS
482 * ftTimeFields [I] TIME_FIELDS structure to convert.
483 * Time [O] Destination for the converted time.
485 * RETURNS
486 * Success: TRUE.
487 * Failure: FALSE.
489 BOOLEAN WINAPI RtlTimeFieldsToTime(
490 PTIME_FIELDS tfTimeFields,
491 PLARGE_INTEGER Time)
493 int CurYear, DeltaYear;
494 LONGLONG rcTime;
495 TIME_FIELDS TimeFields = *tfTimeFields;
497 rcTime = 0;
499 /* FIXME: normalize the TIME_FIELDS structure here */
500 while (TimeFields.Second >= SECSPERMIN)
501 { NormalizeTimeFields(&TimeFields.Second, &TimeFields.Minute, SECSPERMIN);
503 while (TimeFields.Minute >= MINSPERHOUR)
504 { NormalizeTimeFields(&TimeFields.Minute, &TimeFields.Hour, MINSPERHOUR);
506 while (TimeFields.Hour >= HOURSPERDAY)
507 { NormalizeTimeFields(&TimeFields.Hour, &TimeFields.Day, HOURSPERDAY);
509 while (TimeFields.Day > MonthLengths[IsLeapYear(TimeFields.Year)][TimeFields.Month - 1])
510 { NormalizeTimeFields(&TimeFields.Day, &TimeFields.Month,
511 MonthLengths[IsLeapYear(TimeFields.Year)][TimeFields.Month - 1]);
513 while (TimeFields.Month > MONSPERYEAR)
514 { NormalizeTimeFields(&TimeFields.Month, &TimeFields.Year, MONSPERYEAR);
517 /* FIXME: handle calendar corrections here */
518 CurYear = TimeFields.Year - EPOCHYEAR;
519 DeltaYear = CurYear / 400;
520 CurYear -= DeltaYear * 400;
521 rcTime += DeltaYear * DAYSPERQUADRICENTENNIUM;
522 DeltaYear = CurYear / 100;
523 CurYear -= DeltaYear * 100;
524 rcTime += DeltaYear * DAYSPERNORMALCENTURY;
525 DeltaYear = CurYear / 4;
526 CurYear -= DeltaYear * 4;
527 rcTime += DeltaYear * DAYSPERNORMALQUADRENNIUM;
528 rcTime += CurYear * DAYSPERNORMALYEAR;
529 rcTime += YearDays[IsLeapYear(TimeFields.Year)][TimeFields.Month - 1];
530 rcTime += TimeFields.Day - 1;
531 rcTime *= SECSPERDAY;
532 rcTime += TimeFields.Hour * SECSPERHOUR + TimeFields.Minute * SECSPERMIN + TimeFields.Second;
533 rcTime *= TICKSPERSEC;
534 rcTime += TimeFields.Milliseconds * TICKSPERMSEC;
535 Time->QuadPart = rcTime;
537 return TRUE;
540 /***********************************************************************
541 * TIME_GetBias [internal]
543 * Helper function calculates delta local time from UTC.
545 * PARAMS
546 * utc [I] The current utc time.
547 * pdaylight [I] Local daylight.
549 * RETURNS
550 * The bias for the current timezone.
552 static int TIME_GetBias(time_t utc, int *pdaylight)
554 struct tm *ptm;
555 static time_t last_utc;
556 static int last_bias;
557 static int last_daylight;
558 int ret;
560 RtlEnterCriticalSection( &TIME_GetBias_section );
561 if(utc == last_utc)
563 *pdaylight = last_daylight;
564 ret = last_bias;
565 } else
567 ptm = localtime(&utc);
568 *pdaylight = last_daylight =
569 ptm->tm_isdst; /* daylight for local timezone */
570 ptm = gmtime(&utc);
571 ptm->tm_isdst = *pdaylight; /* use local daylight, not that of Greenwich */
572 last_utc = utc;
573 ret = last_bias = (int)(utc-mktime(ptm));
575 RtlLeaveCriticalSection( &TIME_GetBias_section );
576 return ret;
579 /******************************************************************************
580 * RtlLocalTimeToSystemTime [NTDLL.@]
582 * Convert a local time into system time.
584 * PARAMS
585 * LocalTime [I] Local time to convert.
586 * SystemTime [O] Destination for the converted time.
588 * RETURNS
589 * Success: STATUS_SUCCESS.
590 * Failure: An NTSTATUS error code indicating the problem.
592 NTSTATUS WINAPI RtlLocalTimeToSystemTime( const LARGE_INTEGER *LocalTime,
593 PLARGE_INTEGER SystemTime)
595 time_t gmt;
596 int bias, daylight;
598 TRACE("(%p, %p)\n", LocalTime, SystemTime);
600 gmt = time(NULL);
601 bias = TIME_GetBias(gmt, &daylight);
603 SystemTime->QuadPart = LocalTime->QuadPart - bias * (LONGLONG)10000000;
604 return STATUS_SUCCESS;
607 /******************************************************************************
608 * RtlSystemTimeToLocalTime [NTDLL.@]
610 * Convert a system time into a local time.
612 * PARAMS
613 * SystemTime [I] System time to convert.
614 * LocalTime [O] Destination for the converted time.
616 * RETURNS
617 * Success: STATUS_SUCCESS.
618 * Failure: An NTSTATUS error code indicating the problem.
620 NTSTATUS WINAPI RtlSystemTimeToLocalTime( const LARGE_INTEGER *SystemTime,
621 PLARGE_INTEGER LocalTime )
623 time_t gmt;
624 int bias, daylight;
626 TRACE("(%p, %p)\n", SystemTime, LocalTime);
628 gmt = time(NULL);
629 bias = TIME_GetBias(gmt, &daylight);
631 LocalTime->QuadPart = SystemTime->QuadPart + bias * (LONGLONG)10000000;
632 return STATUS_SUCCESS;
635 /******************************************************************************
636 * RtlTimeToSecondsSince1970 [NTDLL.@]
638 * Convert a time into a count of seconds since 1970.
640 * PARAMS
641 * Time [I] Time to convert.
642 * Seconds [O] Destination for the converted time.
644 * RETURNS
645 * Success: TRUE.
646 * Failure: FALSE, if the resulting value will not fit in a DWORD.
648 BOOLEAN WINAPI RtlTimeToSecondsSince1970( const LARGE_INTEGER *Time, LPDWORD Seconds )
650 ULONGLONG tmp = ((ULONGLONG)Time->u.HighPart << 32) | Time->u.LowPart;
651 tmp = RtlLargeIntegerDivide( tmp, 10000000, NULL );
652 tmp -= SECS_1601_TO_1970;
653 if (tmp > 0xffffffff) return FALSE;
654 *Seconds = (DWORD)tmp;
655 return TRUE;
658 /******************************************************************************
659 * RtlTimeToSecondsSince1980 [NTDLL.@]
661 * Convert a time into a count of seconds since 1980.
663 * PARAMS
664 * Time [I] Time to convert.
665 * Seconds [O] Destination for the converted time.
667 * RETURNS
668 * Success: TRUE.
669 * Failure: FALSE, if the resulting value will not fit in a DWORD.
671 BOOLEAN WINAPI RtlTimeToSecondsSince1980( const LARGE_INTEGER *Time, LPDWORD Seconds )
673 ULONGLONG tmp = ((ULONGLONG)Time->u.HighPart << 32) | Time->u.LowPart;
674 tmp = RtlLargeIntegerDivide( tmp, 10000000, NULL );
675 tmp -= SECS_1601_TO_1980;
676 if (tmp > 0xffffffff) return FALSE;
677 *Seconds = (DWORD)tmp;
678 return TRUE;
681 /******************************************************************************
682 * RtlSecondsSince1970ToTime [NTDLL.@]
684 * Convert a count of seconds since 1970 to a time.
686 * PARAMS
687 * Seconds [I] Time to convert.
688 * Time [O] Destination for the converted time.
690 * RETURNS
691 * Nothing.
693 void WINAPI RtlSecondsSince1970ToTime( DWORD Seconds, LARGE_INTEGER *Time )
695 ULONGLONG secs = Seconds * (ULONGLONG)TICKSPERSEC + TICKS_1601_TO_1970;
696 Time->u.LowPart = (DWORD)secs;
697 Time->u.HighPart = (DWORD)(secs >> 32);
700 /******************************************************************************
701 * RtlSecondsSince1980ToTime [NTDLL.@]
703 * Convert a count of seconds since 1980 to a time.
705 * PARAMS
706 * Seconds [I] Time to convert.
707 * Time [O] Destination for the converted time.
709 * RETURNS
710 * Nothing.
712 void WINAPI RtlSecondsSince1980ToTime( DWORD Seconds, LARGE_INTEGER *Time )
714 ULONGLONG secs = Seconds * (ULONGLONG)TICKSPERSEC + TICKS_1601_TO_1980;
715 Time->u.LowPart = (DWORD)secs;
716 Time->u.HighPart = (DWORD)(secs >> 32);
719 /******************************************************************************
720 * RtlTimeToElapsedTimeFields [NTDLL.@]
722 * Convert a time to a count of elapsed seconds.
724 * PARAMS
725 * Time [I] Time to convert.
726 * TimeFields [O] Destination for the converted time.
728 * RETURNS
729 * Nothing.
731 void WINAPI RtlTimeToElapsedTimeFields( const LARGE_INTEGER *Time, PTIME_FIELDS TimeFields )
733 LONGLONG time;
734 UINT rem;
736 time = RtlExtendedLargeIntegerDivide( Time->QuadPart, TICKSPERSEC, &rem );
737 TimeFields->Milliseconds = rem / TICKSPERMSEC;
739 /* time is now in seconds */
740 TimeFields->Year = 0;
741 TimeFields->Month = 0;
742 TimeFields->Day = RtlExtendedLargeIntegerDivide( time, SECSPERDAY, &rem );
744 /* rem is now the remaining seconds in the last day */
745 TimeFields->Second = rem % 60;
746 rem /= 60;
747 TimeFields->Minute = rem % 60;
748 TimeFields->Hour = rem / 60;
751 /***********************************************************************
752 * NtQuerySystemTime [NTDLL.@]
753 * ZwQuerySystemTime [NTDLL.@]
755 * Get the current system time.
757 * PARAMS
758 * Time [O] Destination for the current system time.
760 * RETURNS
761 * Success: STATUS_SUCCESS.
762 * Failure: An NTSTATUS error code indicating the problem.
764 NTSTATUS WINAPI NtQuerySystemTime( PLARGE_INTEGER Time )
766 struct timeval now;
768 gettimeofday( &now, 0 );
769 Time->QuadPart = now.tv_sec * (ULONGLONG)TICKSPERSEC + TICKS_1601_TO_1970;
770 Time->QuadPart += now.tv_usec * 10;
771 return STATUS_SUCCESS;
774 /***********************************************************************
775 * TIME_GetTZAsStr [internal]
777 * Helper function that returns the given timezone as a string.
779 * PARAMS
780 * utc [I] The current utc time.
781 * bias [I] The bias of the current timezone.
782 * dst [I] ??
784 * RETURNS
785 * Timezone name.
787 * NOTES:
788 * This could be done with a hash table instead of merely iterating through a
789 * table, however with the small amount of entries (60 or so) I didn't think
790 * it was worth it.
792 static const WCHAR* TIME_GetTZAsStr (time_t utc, int bias, int dst)
794 char psTZName[7];
795 struct tm *ptm = localtime(&utc);
796 unsigned int i;
798 if (!strftime (psTZName, 7, "%Z", ptm))
799 return (NULL);
801 for (i=0; i<(sizeof(TZ_INFO) / sizeof(struct tagTZ_INFO)); i++)
803 if ( strcmp(TZ_INFO[i].psTZFromUnix, psTZName) == 0 &&
804 TZ_INFO[i].bias == bias &&
805 TZ_INFO[i].dst == dst
807 return TZ_INFO[i].psTZWindows;
809 FIXME("Can't match system time zone name \"%s\" to an entry in TZ_INFO\n",psTZName);
810 FIXME(" Please add appropriate entry to TZ_INFO and submit as patch to wine-patches\n");
811 return NULL;
814 /*** TIME_GetTimeZoneInfoFromReg: helper for GetTimeZoneInformation ***/
817 static int TIME_GetTimeZoneInfoFromReg(LPTIME_ZONE_INFORMATION tzinfo)
819 BYTE buf[90];
820 KEY_VALUE_PARTIAL_INFORMATION * KpInfo =
821 (KEY_VALUE_PARTIAL_INFORMATION *) buf;
822 HKEY hkey;
823 DWORD size;
824 OBJECT_ATTRIBUTES attr;
825 UNICODE_STRING nameW;
827 attr.Length = sizeof(attr);
828 attr.RootDirectory = 0;
829 attr.ObjectName = &nameW;
830 attr.Attributes = 0;
831 attr.SecurityDescriptor = NULL;
832 attr.SecurityQualityOfService = NULL;
833 RtlInitUnicodeString( &nameW, TZInformationKeyW);
834 if (!NtOpenKey( &hkey, KEY_ALL_ACCESS, &attr )) {
836 #define GTZIFR_N( valkey, tofield) \
837 RtlInitUnicodeString( &nameW, valkey );\
838 if (!NtQueryValueKey( hkey, &nameW, KeyValuePartialInformation, KpInfo,\
839 sizeof(buf), &size )) { \
840 if( size >= (sizeof((tofield)) + \
841 offsetof(KEY_VALUE_PARTIAL_INFORMATION,Data))); { \
842 memcpy(&(tofield), \
843 KpInfo->Data, sizeof(tofield)); \
846 #define GTZIFR_S( valkey, tofield) \
847 RtlInitUnicodeString( &nameW, valkey );\
848 if (!NtQueryValueKey( hkey, &nameW, KeyValuePartialInformation, KpInfo,\
849 sizeof(buf), &size )) { \
850 strncpyW( tofield, (WCHAR*) KpInfo->Data, \
851 sizeof( tofield) / sizeof(WCHAR) ); \
854 GTZIFR_N( TZStandardStartW, tzinfo->StandardDate)
855 GTZIFR_N( TZDaylightStartW, tzinfo->DaylightDate)
856 GTZIFR_N( TZBiasW, tzinfo->Bias)
857 GTZIFR_N( TZStandardBiasW, tzinfo->StandardBias)
858 GTZIFR_N( TZDaylightBiasW, tzinfo->DaylightBias)
859 GTZIFR_S( TZStandardNameW, tzinfo->StandardName)
860 GTZIFR_S( TZDaylightNameW, tzinfo->DaylightName)
862 #undef GTZIFR_N
863 #undef GTZIFR_S
864 NtClose( hkey );
865 return 1;
867 return 0;
870 /***********************************************************************
871 * RtlQueryTimeZoneInformation [NTDLL.@]
873 * Get information about the current timezone.
875 * PARAMS
876 * tzinfo [O] Destination for the retrieved timezone info.
878 * RETURNS
879 * Success: STATUS_SUCCESS.
880 * Failure: An NTSTATUS error code indicating the problem.
882 NTSTATUS WINAPI RtlQueryTimeZoneInformation(LPTIME_ZONE_INFORMATION tzinfo)
884 time_t gmt;
885 int bias, daylight;
886 const WCHAR *psTZ;
888 memset(tzinfo, 0, sizeof(TIME_ZONE_INFORMATION));
890 if( !TIME_GetTimeZoneInfoFromReg(tzinfo)) {
892 gmt = time(NULL);
893 bias = TIME_GetBias(gmt, &daylight);
895 tzinfo->Bias = -bias / 60;
896 tzinfo->StandardBias = 0;
897 tzinfo->DaylightBias = -60;
898 tzinfo->StandardName[0]='\0';
899 tzinfo->DaylightName[0]='\0';
900 psTZ = TIME_GetTZAsStr (gmt, (-bias/60), daylight);
901 if (psTZ) strcpyW( tzinfo->StandardName, psTZ );
903 return STATUS_SUCCESS;
906 /***********************************************************************
907 * RtlSetTimeZoneInformation [NTDLL.@]
909 * Set the current time zone information.
911 * PARAMS
912 * tzinfo [I] Timezone information to set.
914 * RETURNS
915 * Success: STATUS_SUCCESS.
916 * Failure: An NTSTATUS error code indicating the problem.
918 * BUGS
919 * Uses the obsolete unix timezone structure and tz_dsttime member.
921 NTSTATUS WINAPI RtlSetTimeZoneInformation( const TIME_ZONE_INFORMATION *tzinfo )
923 #ifdef HAVE_SETTIMEOFDAY
924 struct timezone tz;
926 tz.tz_minuteswest = tzinfo->Bias;
927 #ifdef DST_NONE
928 tz.tz_dsttime = DST_NONE;
929 #else
930 tz.tz_dsttime = 0;
931 #endif
932 if(!settimeofday(NULL, &tz))
933 return STATUS_SUCCESS;
934 #endif
935 return STATUS_PRIVILEGE_NOT_HELD;
938 /***********************************************************************
939 * NtSetSystemTime [NTDLL.@]
940 * ZwSetSystemTime [NTDLL.@]
942 * Set the system time.
944 * PARAMS
945 * NewTime [I] The time to set.
946 * OldTime [O] Optional destination for the previous system time.
948 * RETURNS
949 * Success: STATUS_SUCCESS.
950 * Failure: An NTSTATUS error code indicating the problem.
952 NTSTATUS WINAPI NtSetSystemTime(const LARGE_INTEGER *NewTime, LARGE_INTEGER *OldTime)
954 TIME_FIELDS tf;
955 struct timeval tv;
956 struct timezone tz;
957 struct tm t;
958 time_t sec, oldsec;
959 int dst, bias;
960 int err;
962 /* Return the old time if necessary */
963 if(OldTime)
964 NtQuerySystemTime(OldTime);
966 RtlTimeToTimeFields(NewTime, &tf);
968 /* call gettimeofday to get the current timezone */
969 gettimeofday(&tv, &tz);
970 oldsec = tv.tv_sec;
971 /* get delta local time from utc */
972 bias = TIME_GetBias(oldsec, &dst);
974 /* get the number of seconds */
975 t.tm_sec = tf.Second;
976 t.tm_min = tf.Minute;
977 t.tm_hour = tf.Hour;
978 t.tm_mday = tf.Day;
979 t.tm_mon = tf.Month - 1;
980 t.tm_year = tf.Year - 1900;
981 t.tm_isdst = dst;
982 sec = mktime (&t);
983 /* correct for timezone and daylight */
984 sec += bias;
986 /* set the new time */
987 tv.tv_sec = sec;
988 tv.tv_usec = tf.Milliseconds * 1000;
990 /* error and sanity check*/
991 if(sec == (time_t)-1 || abs((int)(sec-oldsec)) > SETTIME_MAX_ADJUST) {
992 err = 2;
993 } else {
994 #ifdef HAVE_SETTIMEOFDAY
995 err = settimeofday(&tv, NULL); /* 0 is OK, -1 is error */
996 if(err == 0)
997 return STATUS_SUCCESS;
998 #else
999 err = 1;
1000 #endif
1003 ERR("Cannot set time to %d/%d/%d %d:%d:%d Time adjustment %ld %s\n",
1004 tf.Year, tf.Month, tf.Day, tf.Hour, tf.Minute, tf.Second,
1005 (long)(sec-oldsec),
1006 err == -1 ? "No Permission"
1007 : sec == (time_t)-1 ? "" : "is too large." );
1009 if(err == 2)
1010 return STATUS_INVALID_PARAMETER;
1011 else if(err == -1)
1012 return STATUS_PRIVILEGE_NOT_HELD;
1013 else
1014 return STATUS_NOT_IMPLEMENTED;