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
26 #include "wine/port.h"
33 #ifdef HAVE_SYS_TIME_H
34 # include <sys/time.h>
40 #define NONAMELESSUNION
41 #define NONAMELESSSTRUCT
44 #include "wine/unicode.h"
45 #include "wine/debug.h"
46 #include "ntdll_misc.h"
48 WINE_DEFAULT_DEBUG_CHANNEL(ntdll);
50 static RTL_CRITICAL_SECTION TIME_GetBias_section;
51 static RTL_CRITICAL_SECTION_DEBUG critsect_debug =
53 0, 0, &TIME_GetBias_section,
54 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
55 0, 0, { 0, (DWORD)(__FILE__ ": TIME_GetBias_section") }
57 static RTL_CRITICAL_SECTION TIME_GetBias_section = { &critsect_debug, -1, 0, 0, 0, 0 };
59 /* TimeZone registry key values */
60 static const WCHAR TZInformationKeyW[] = { 'M','a','c','h','i','n','e','\\',
61 'S','Y','S','T','E','M','\\','C','u','r','r','e','n','t','C','o','n','t','r',
62 'o','l','S','e','t','\\','C','o','n','t','r','o','l','\\','T','i','m','e','z',
63 'o','n','e','I','n','f','o','r','m','a','t','i','o','n', 0};
64 static const WCHAR TZStandardStartW[] = {
65 'S','t','a','n','d','a','r','d','s','t','a','r','t', 0};
66 static const WCHAR TZDaylightStartW[] = {
67 'D','a','y','l','i','g','h','t','s','t','a','r','t', 0};
68 static const WCHAR TZDaylightBiasW[] = {
69 'D','a','y','l','i','g','h','t','B','i','a','s', 0};
70 static const WCHAR TZStandardBiasW[] = {
71 'S','t','a','n','d','a','r','d','B','i','a','s', 0};
72 static const WCHAR TZBiasW[] = {'B','i','a','s', 0};
73 static const WCHAR TZDaylightNameW[] = {
74 'D','a','y','l','i','g','h','t','N','a','m','e', 0};
75 static const WCHAR TZStandardNameW[] = {
76 'S','t','a','n','d','a','r','d','N','a','m','e', 0};
79 #define SETTIME_MAX_ADJUST 120
81 /* This structure is used to store strings that represent all of the time zones
82 * in the world. (This is used to help GetTimeZoneInformation)
86 const char *psTZFromUnix;
87 WCHAR psTZWindows[32];
92 static const struct tagTZ_INFO TZ_INFO[] =
95 {'D','a','t','e','l','i','n','e',' ','S','t','a','n','d','a','r','d',' ',
96 'T','i','m','e','\0'}, -720, 0},
98 {'S','a','m','o','a',' ','S','t','a','n','d','a','r','d',' ','T','i','m',
101 {'H','a','w','a','i','i','a','n',' ','S','t','a','n','d','a','r','d',' ',
102 'T','i','m','e','\0'}, 600, 0},
104 {'A','l','a','s','k','a','n',' ','S','t','a','n','d','a','r','d',' ','T',
105 'i','m','e','\0'}, 480, 1},
107 {'P','a','c','i','f','i','c',' ','S','t','a','n','d','a','r','d',' ','T',
108 'i','m','e','\0'}, 480, 0},
110 {'P','a','c','i','f','i','c',' ','S','t','a','n','d','a','r','d',' ','T',
111 'i','m','e','\0'}, 420, 1},
113 {'U','S',' ','M','o','u','n','t','a','i','n',' ','S','t','a','n','d','a',
114 'r','d',' ','T','i','m','e','\0'}, 420, 0},
116 {'M','o','u','n','t','a','i','n',' ','S','t','a','n','d','a','r','d',' ',
117 'T','i','m','e','\0'}, 360, 1},
119 {'C','e','n','t','r','a','l',' ','A','m','e','r','i','c','a',' ','S','t',
120 'a','n','d','a','r','d',' ','T','i','m','e','\0'}, 360, 0},
122 {'C','e','n','t','r','a','l',' ','S','t','a','n','d','a','r','d',' ','T',
123 'i','m','e','\0'}, 300, 1},
125 {'S','A',' ','P','a','c','i','f','i','c',' ','S','t','a','n','d','a','r',
126 'd',' ','T','i','m','e','\0'}, 300, 0},
128 {'E','a','s','t','e','r','n',' ','S','t','a','n','d','a','r','d',' ','T',
129 'i','m','e','\0'}, 240, 1},
131 {'U','S',' ','E','a','s','t','e','r','n',' ','S','t','a','n','d','a','r',
132 'd',' ','T','i','m','e','\0'}, 300, 0},
134 {'E','a','s','t','e','r','n',' ','C','e','n','t','r','a','l',' ','S','t','a','n','d','a','r',
135 'd',' ','T','i','m','e','\0'}, 300, 0},
137 {'A','t','l','a','n','t','i','c',' ','S','t','a','n','d','a','r','d',' ',
138 'T','i','m','e','\0'}, 180, 1},
140 {'S','A',' ','W','e','s','t','e','r','n',' ','S','t','a','n','d','a','r',
141 'd',' ','T','i','m','e','\0'}, 240, 0},
143 {'P','a','c','i','f','i','c',' ','S','A',' ','S','t','a','n','d','a','r',
144 'd',' ','T','i','m','e','\0'}, 240, 0},
146 {'N','e','w','f','o','u','n','d','l','a','n','d',' ','S','t','a','n','d',
147 'a','r','d',' ','T','i','m','e','\0'}, 150, 1},
149 {'B','r','a','z','i','l','i','a','n',' ','S','t','a','n','d','a','r','d',
150 ' ','T','i','m','e','\0'}, 180, 0},
152 {'B','r','a','z','i','l','i','a','n',' ','S','u','m','m','e','r',
153 ' ','T','i','m','e','\0'}, 120, 1},
155 {'S','A',' ','E','a','s','t','e','r','n',' ','S','t','a','n','d','a','r',
156 'd',' ','T','i','m','e','\0'}, 180, 0},
158 {'G','r','e','e','n','l','a','n','d',' ','S','t','a','n','d','a','r','d',
159 ' ','T','i','m','e','\0'}, 120, 1},
161 {'M','i','d','-','A','t','l','a','n','t','i','c',' ','S','t','a','n','d',
162 'a','r','d',' ','T','i','m','e','\0'}, 120, 0},
164 {'A','z','o','r','e','s',' ','S','t','a','n','d','a','r','d',' ','T','i',
165 'm','e','\0'}, 0, 1},
167 {'C','a','p','e',' ','V','e','r','d','e',' ','S','t','a','n','d','a','r',
168 'd',' ','T','i','m','e','\0'}, 60, 0},
170 {'W','e','s','t','e','r','n',' ','E','u','r','o','p','e','a','n',' ','S','u','m','m','e','r',' ','T','i','m','e','\0'}, -60, 1},
172 {'G','r','e','e','n','w','i','c','h',' ','S','t','a','n','d','a','r','d',
173 ' ','T','i','m','e','\0'}, 0, 0},
175 {'G','M','T',' ','S','t','a','n','d','a','r','d',' ','T','i','m','e','\0'},
178 {'I','r','i','s','h',' ','S','u','m','m','e','r',' ','T','i','m','e','\0'},
181 {'G','M','T',' ','S','t','a','n','d','a','r','d',' ','T','i','m','e','\0'},
184 {'G','M','T',' ','S','t','a','n','d','a','r','d',' ','T','i','m','e','\0'},
187 {'C','e','n','t','r','a','l',' ','E','u','r','o','p','e','a','n',' ',
188 'T','i','m','e','\0'}, -60, 0},
190 {'C','e','n','t','r','a','l',' ','E','u','r','o','p','e',' ','S','t','a',
191 'n','d','a','r','d',' ','T','i','m','e','\0'}, -120, 1},
193 {'C','e','n','t','r','a','l',' ','E','u','r','o','p','e',' ','S','t','a',
194 'n','d','a','r','d',' ','T','i','m','e','\0'}, -60, 0},
196 {'C','e','n','t','r','a','l',' ','E','u','r','o','p','e',' ','D','a','y',
197 'l','i','g','h','t',' ','T','i','m','e','\0'}, -120, 1},
199 {'W','.',' ','C','e','n','t','r','a','l',' ','A','f','r','i','c','a',' ',
200 'S','t','a','n','d','a','r','d',' ','T','i','m','e','\0'}, -60, 0},
202 {'E','.',' ','E','u','r','o','p','e',' ','S','t','a','n','d','a','r','d',
203 ' ','T','i','m','e','\0'}, -180, 1},
205 {'E','g','y','p','t',' ','S','t','a','n','d','a','r','d',' ','T','i','m',
208 {'C','e','n','t','r','a','l',' ','A','f','r','i','c','a','n',' '
209 ,'T','i','m','e','\0'}, -120, 0},
211 {'S','o','u','t','h',' ','A','f','r','i','c','a',' ','S','t','a','n','d',
212 'a','r','d',' ','T','i','m','e','\0'}, -120, 0},
214 {'I','s','r','a','e','l',' ','S','t','a','n','d','a','r','d',' ','T','i',
215 'm','e','\0'}, -120, 0},
217 {'R','u','s','s','i','a','n',' ','S','t','a','n','d','a','r','d',' ','T',
218 'i','m','e','\0'}, -180, 0},
220 {'A','r','a','b','i','c',' ','S','t','a','n','d','a','r','d',' ','T','i',
221 'm','e','\0'}, -240, 1},
223 {'A','r','a','b',' ','S','t','a','n','d','a','r','d',' ','T','i','m','e',
226 {'R','u','s','s','i','a','n',' ','S','t','a','n','d','a','r','d',' ','T',
227 'i','m','e','\0'}, -240, 1},
229 {'E','.',' ','A','f','r','i','c','a',' ','S','t','a','n','d','a','r','d',
230 ' ','T','i','m','e','\0'}, -180, 0},
232 {'I','r','a','n',' ','S','t','a','n','d','a','r','d',' ','T','i','m','e',
235 {'A','r','a','b','i','a','n',' ','S','t','a','n','d','a','r','d',' ','T',
236 'i','m','e','\0'}, -240, 0},
238 {'C','a','u','c','a','s','u','s',' ','S','t','a','n','d','a','r','d',' ',
239 'T','i','m','e','\0'}, -300, 1},
241 {'A','f','g','h','a','n','i','s','t','a','n',' ','S','t','a','n','d','a',
242 'r','d',' ','T','i','m','e','\0'}, -270, 0},
244 {'S','a','m','a','r','a',' ','S','t','a','n','d','a','r','d',' ','T','i',
245 'm','e','\0'}, -270, 1},
247 {'E','k','a','t','e','r','i','n','b','u','r','g',' ','S','t','a','n','d',
248 'a','r','d',' ','T','i','m','e','\0'}, -360, 1},
250 {'W','e','s','t',' ','A','s','i','a',' ','S','t','a','n','d','a','r','d',
251 ' ','T','i','m','e','\0'}, -300, 0},
253 {'I','n','d','i','a',' ','S','t','a','n','d','a','r','d',' ','T','i','m',
256 {'N','e','p','a','l',' ','S','t','a','n','d','a','r','d',' ','T','i','m',
259 {'N','.',' ','C','e','n','t','r','a','l',' ','A','s','i','a',' ','S','t',
260 'a','n','d','a','r','d',' ','T','i','m','e','\0'}, -420, 1},
262 {'C','e','n','t','r','a','l',' ','A','s','i','a',' ','S','t','a','n','d',
263 'a','r','d',' ','T','i','m','e','\0'}, -360, 0},
265 {'S','r','i',' ','L','a','n','k','a',' ','S','t','a','n','d','a','r','d',
266 ' ','T','i','m','e','\0'}, -360, 0},
268 {'M','y','a','n','m','a','r',' ','S','t','a','n','d','a','r','d',' ','T',
269 'i','m','e','\0'}, -390, 0},
271 {'S','E',' ','A','s','i','a',' ','S','t','a','n','d','a','r','d',' ','T',
272 'i','m','e','\0'}, -420, 0},
274 {'N','o','r','t','h',' ','A','s','i','a',' ','S','t','a','n','d','a','r',
275 'd',' ','T','i','m','e','\0'}, -480, 1},
277 {'C','h','i','n','a',' ','S','t','a','n','d','a','r','d',' ','T','i','m',
280 {'N','o','r','t','h',' ','A','s','i','a',' ','E','a','s','t',' ','S','t',
281 'a','n','d','a','r','d',' ','T','i','m','e','\0'}, -540, 1},
283 {'M','a','l','a','y',' ','P','e','n','i','n','s','u','l','a',' ','S','t',
284 'a','n','d','a','r','d',' ','T','i','m','e','\0'}, -480, 0},
286 {'W','.',' ','A','u','s','t','r','a','l','i','a',' ','S','t','a','n','d',
287 'a','r','d',' ','T','i','m','e','\0'}, -480, 0},
289 {'T','o','k','y','o',' ','S','t','a','n','d','a','r','d',' ','T','i','m',
292 {'K','o','r','e','a',' ','S','t','a','n','d','a','r','d',' ','T','i','m',
295 {'Y','a','k','u','t','s','k',' ','S','t','a','n','d','a','r','d',' ','T',
296 'i','m','e','\0'}, -600, 1},
298 {'C','e','n','.',' ','A','u','s','t','r','a','l','i','a',' ','S','t','a',
299 'n','d','a','r','d',' ','T','i','m','e','\0'}, -570, 0},
301 {'E','.',' ','A','u','s','t','r','a','l','i','a',' ','S','t','a','n','d',
302 'a','r','d',' ','T','i','m','e','\0'}, -600, 0},
304 {'E','.',' ','A','u','s','t','r','a','l','i','a',' ','S','t','a','n','d',
305 'a','r','d',' ','T','i','m','e','\0'}, -660, 1},
307 {'W','e','s','t',' ','P','a','c','i','f','i','c',' ','S','t','a','n','d',
308 'a','r','d',' ','T','i','m','e','\0'}, -600, 0},
310 {'V','l','a','d','i','v','o','s','t','o','k',' ','S','t','a','n','d','a',
311 'r','d',' ','T','i','m','e','\0'}, -660, 1},
313 {'C','e','n','t','r','a','l',' ','P','a','c','i','f','i','c',' ','S','t',
314 'a','n','d','a','r','d',' ','T','i','m','e','\0'}, -720, 1},
316 {'N','e','w',' ','Z','e','a','l','a','n','d',' ','S','t','a','n','d','a',
317 'r','d',' ','T','i','m','e','\0'}, -720, 0},
319 {'N','e','w',' ','Z','e','a','l','a','n','d',' ','D','a','y','l','i','g',
320 'h','t',' ','T','i','m','e','\0'}, -780, 1},
322 {'F','i','j','i',' ','S','t','a','n','d','a','r','d',' ','T','i','m','e',
325 {'T','o','n','g','a',' ','S','t','a','n','d','a','r','d',' ','T','i','m',
328 {'N','o','v','o','s','i','b','i','r','s','k',' ','S','t','a','n','d','a',
329 'r','d',' ','T','i','m','e','\0'}, -360, 1},
331 {'A','n','a','d','y','r',' ','S','t','a','n','d','a','r','d',' ','T','i',
332 'm','e','\0'}, -720, 1},
334 {'H','o','n','g',' ','K','o','n','g',' ','S','t','a','n','d','a','r','d',
335 ' ','T','i','m','e','\0'}, -480, 0},
337 {'U','r','u','g','u','a','y','a','n',' ','T','i','m','e','\0'}, 180, 0}
340 #define TICKSPERSEC 10000000
341 #define TICKSPERMSEC 10000
342 #define SECSPERDAY 86400
343 #define SECSPERHOUR 3600
344 #define SECSPERMIN 60
345 #define MINSPERHOUR 60
346 #define HOURSPERDAY 24
347 #define EPOCHWEEKDAY 1 /* Jan 1, 1601 was Monday */
348 #define DAYSPERWEEK 7
349 #define EPOCHYEAR 1601
350 #define DAYSPERNORMALYEAR 365
351 #define DAYSPERLEAPYEAR 366
352 #define MONSPERYEAR 12
353 #define DAYSPERQUADRICENTENNIUM (365 * 400 + 97)
354 #define DAYSPERNORMALCENTURY (365 * 100 + 24)
355 #define DAYSPERNORMALQUADRENNIUM (365 * 4 + 1)
357 /* 1601 to 1970 is 369 years plus 89 leap days */
358 #define SECS_1601_TO_1970 ((369 * 365 + 89) * (ULONGLONG)SECSPERDAY)
359 #define TICKS_1601_TO_1970 (SECS_1601_TO_1970 * TICKSPERSEC)
360 /* 1601 to 1980 is 379 years plus 91 leap days */
361 #define SECS_1601_TO_1980 ((379 * 365 + 91) * (ULONGLONG)SECSPERDAY)
362 #define TICKS_1601_TO_1980 (SECS_1601_TO_1980 * TICKSPERSEC)
363 /* max ticks that can be represented as Unix time */
364 #define TICKS_1601_TO_UNIX_MAX ((SECS_1601_TO_1970 + INT_MAX) * TICKSPERSEC)
367 static const int MonthLengths[2][MONSPERYEAR] =
369 { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
370 { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
373 static inline int IsLeapYear(int Year)
375 return Year % 4 == 0 && (Year % 100 != 0 || Year % 400 == 0) ? 1 : 0;
378 /***********************************************************************
379 * NTDLL_get_server_timeout
381 * Convert a NTDLL timeout into a timeval struct to send to the server.
383 void NTDLL_get_server_timeout( abs_time_t *when, const LARGE_INTEGER *timeout )
387 if (!timeout) /* infinite timeout */
389 when->sec = when->usec = 0;
391 else if (timeout->QuadPart <= 0) /* relative timeout */
395 if (-timeout->QuadPart > (LONGLONG)INT_MAX * TICKSPERSEC)
396 when->sec = when->usec = INT_MAX;
399 ULONG sec = RtlEnlargedUnsignedDivide( -timeout->QuadPart, TICKSPERSEC, &remainder );
400 gettimeofday( &tv, 0 );
401 when->sec = tv.tv_sec + sec;
402 if ((when->usec = tv.tv_usec + (remainder / 10)) >= 1000000)
404 when->usec -= 1000000;
407 if (when->sec < tv.tv_sec) /* overflow */
408 when->sec = when->usec = INT_MAX;
411 else /* absolute time */
413 if (timeout->QuadPart < TICKS_1601_TO_1970)
414 when->sec = when->usec = 0;
415 else if (timeout->QuadPart > TICKS_1601_TO_UNIX_MAX)
416 when->sec = when->usec = INT_MAX;
419 when->sec = RtlEnlargedUnsignedDivide( timeout->QuadPart - TICKS_1601_TO_1970,
420 TICKSPERSEC, &remainder );
421 when->usec = remainder / 10;
427 /***********************************************************************
428 * NTDLL_from_server_timeout
430 * Convert a timeval struct from the server into an NTDLL timeout.
432 void NTDLL_from_server_timeout( LARGE_INTEGER *timeout, const abs_time_t *when )
434 timeout->QuadPart = when->sec * (ULONGLONG)TICKSPERSEC + TICKS_1601_TO_1970;
435 timeout->QuadPart += when->usec * 10;
439 /******************************************************************************
440 * RtlTimeToTimeFields [NTDLL.@]
442 * Convert a time into a TIME_FIELDS structure.
445 * liTime [I] Time to convert.
446 * TimeFields [O] Destination for the converted time.
451 VOID WINAPI RtlTimeToTimeFields(
452 const LARGE_INTEGER *liTime,
453 PTIME_FIELDS TimeFields)
456 long int cleaps, years, yearday, months;
460 /* Extract millisecond from time and convert time into seconds */
461 TimeFields->Milliseconds =
462 (CSHORT) (( liTime->QuadPart % TICKSPERSEC) / TICKSPERMSEC);
463 Time = liTime->QuadPart / TICKSPERSEC;
465 /* The native version of RtlTimeToTimeFields does not take leap seconds
468 /* Split the time into days and seconds within the day */
469 Days = Time / SECSPERDAY;
470 SecondsInDay = Time % SECSPERDAY;
472 /* compute time of day */
473 TimeFields->Hour = (CSHORT) (SecondsInDay / SECSPERHOUR);
474 SecondsInDay = SecondsInDay % SECSPERHOUR;
475 TimeFields->Minute = (CSHORT) (SecondsInDay / SECSPERMIN);
476 TimeFields->Second = (CSHORT) (SecondsInDay % SECSPERMIN);
478 /* compute day of week */
479 TimeFields->Weekday = (CSHORT) ((EPOCHWEEKDAY + Days) % DAYSPERWEEK);
481 /* compute year, month and day of month. */
482 cleaps=( 3 * ((4 * Days + 1227) / DAYSPERQUADRICENTENNIUM) + 3 ) / 4;
483 Days += 28188 + cleaps;
484 years = (20 * Days - 2442) / (5 * DAYSPERNORMALQUADRENNIUM);
485 yearday = Days - (years * DAYSPERNORMALQUADRENNIUM)/4;
486 months = (64 * yearday) / 1959;
487 /* the result is based on a year starting on March.
488 * To convert take 12 from Januari and Februari and
489 * increase the year by one. */
491 TimeFields->Month = months - 1;
492 TimeFields->Year = years + 1524;
494 TimeFields->Month = months - 13;
495 TimeFields->Year = years + 1525;
497 /* calculation of day of month is based on the wonderful
498 * sequence of INT( n * 30.6): it reproduces the
499 * 31-30-31-30-31-31 month lengths exactly for small n's */
500 TimeFields->Day = yearday - (1959 * months) / 64 ;
504 /******************************************************************************
505 * RtlTimeFieldsToTime [NTDLL.@]
507 * Convert a TIME_FIELDS structure into a time.
510 * ftTimeFields [I] TIME_FIELDS structure to convert.
511 * Time [O] Destination for the converted time.
517 BOOLEAN WINAPI RtlTimeFieldsToTime(
518 PTIME_FIELDS tfTimeFields,
521 int month, year, cleaps, day;
523 /* FIXME: normalize the TIME_FIELDS structure here */
524 /* No, native just returns 0 (error) if the fields are not */
525 if( tfTimeFields->Milliseconds< 0 || tfTimeFields->Milliseconds > 999 ||
526 tfTimeFields->Second < 0 || tfTimeFields->Second > 59 ||
527 tfTimeFields->Minute < 0 || tfTimeFields->Minute > 59 ||
528 tfTimeFields->Hour < 0 || tfTimeFields->Hour > 23 ||
529 tfTimeFields->Month < 1 || tfTimeFields->Month > 12 ||
530 tfTimeFields->Day < 1 ||
531 tfTimeFields->Day > MonthLengths
532 [ tfTimeFields->Month ==2 || IsLeapYear(tfTimeFields->Year)]
533 [ tfTimeFields->Month - 1] ||
534 tfTimeFields->Year < 1601 )
537 /* now calculate a day count from the date
538 * First start counting years from March. This way the leap days
539 * are added at the end of the year, not somewhere in the middle.
540 * Formula's become so much less complicate that way.
541 * To convert: add 12 to the month numbers of Jan and Feb, and
542 * take 1 from the year */
543 if(tfTimeFields->Month < 3) {
544 month = tfTimeFields->Month + 13;
545 year = tfTimeFields->Year - 1;
547 month = tfTimeFields->Month + 1;
548 year = tfTimeFields->Year;
550 cleaps = (3 * (year / 100) + 3) / 4; /* nr of "century leap years"*/
551 day = (36525 * year) / 100 - cleaps + /* year * dayperyr, corrected */
552 (1959 * month) / 64 + /* months * daypermonth */
553 tfTimeFields->Day - /* day of the month */
554 584817 ; /* zero that on 1601-01-01 */
557 Time->QuadPart = (((((LONGLONG) day * HOURSPERDAY +
558 tfTimeFields->Hour) * MINSPERHOUR +
559 tfTimeFields->Minute) * SECSPERMIN +
560 tfTimeFields->Second ) * 1000 +
561 tfTimeFields->Milliseconds ) * TICKSPERMSEC;
566 /***********************************************************************
567 * TIME_GetBias [internal]
569 * Helper function calculates delta local time from UTC.
572 * utc [I] The current utc time.
573 * pdaylight [I] Local daylight.
576 * The bias for the current timezone.
578 static int TIME_GetBias(time_t utc, int *pdaylight)
581 static time_t last_utc;
582 static int last_bias;
583 static int last_daylight;
586 RtlEnterCriticalSection( &TIME_GetBias_section );
589 *pdaylight = last_daylight;
593 ptm = localtime(&utc);
594 *pdaylight = last_daylight =
595 ptm->tm_isdst; /* daylight for local timezone */
597 ptm->tm_isdst = *pdaylight; /* use local daylight, not that of Greenwich */
599 ret = last_bias = (int)(utc-mktime(ptm));
601 RtlLeaveCriticalSection( &TIME_GetBias_section );
605 /******************************************************************************
606 * RtlLocalTimeToSystemTime [NTDLL.@]
608 * Convert a local time into system time.
611 * LocalTime [I] Local time to convert.
612 * SystemTime [O] Destination for the converted time.
615 * Success: STATUS_SUCCESS.
616 * Failure: An NTSTATUS error code indicating the problem.
618 NTSTATUS WINAPI RtlLocalTimeToSystemTime( const LARGE_INTEGER *LocalTime,
619 PLARGE_INTEGER SystemTime)
624 TRACE("(%p, %p)\n", LocalTime, SystemTime);
627 bias = TIME_GetBias(gmt, &daylight);
629 SystemTime->QuadPart = LocalTime->QuadPart - bias * (LONGLONG)TICKSPERSEC;
630 return STATUS_SUCCESS;
633 /******************************************************************************
634 * RtlSystemTimeToLocalTime [NTDLL.@]
636 * Convert a system time into a local time.
639 * SystemTime [I] System time to convert.
640 * LocalTime [O] Destination for the converted time.
643 * Success: STATUS_SUCCESS.
644 * Failure: An NTSTATUS error code indicating the problem.
646 NTSTATUS WINAPI RtlSystemTimeToLocalTime( const LARGE_INTEGER *SystemTime,
647 PLARGE_INTEGER LocalTime )
652 TRACE("(%p, %p)\n", SystemTime, LocalTime);
655 bias = TIME_GetBias(gmt, &daylight);
657 LocalTime->QuadPart = SystemTime->QuadPart + bias * (LONGLONG)TICKSPERSEC;
658 return STATUS_SUCCESS;
661 /******************************************************************************
662 * RtlTimeToSecondsSince1970 [NTDLL.@]
664 * Convert a time into a count of seconds since 1970.
667 * Time [I] Time to convert.
668 * Seconds [O] Destination for the converted time.
672 * Failure: FALSE, if the resulting value will not fit in a DWORD.
674 BOOLEAN WINAPI RtlTimeToSecondsSince1970( const LARGE_INTEGER *Time, LPDWORD Seconds )
676 ULONGLONG tmp = ((ULONGLONG)Time->u.HighPart << 32) | Time->u.LowPart;
677 tmp = RtlLargeIntegerDivide( tmp, TICKSPERSEC, NULL );
678 tmp -= SECS_1601_TO_1970;
679 if (tmp > 0xffffffff) return FALSE;
680 *Seconds = (DWORD)tmp;
684 /******************************************************************************
685 * RtlTimeToSecondsSince1980 [NTDLL.@]
687 * Convert a time into a count of seconds since 1980.
690 * Time [I] Time to convert.
691 * Seconds [O] Destination for the converted time.
695 * Failure: FALSE, if the resulting value will not fit in a DWORD.
697 BOOLEAN WINAPI RtlTimeToSecondsSince1980( const LARGE_INTEGER *Time, LPDWORD Seconds )
699 ULONGLONG tmp = ((ULONGLONG)Time->u.HighPart << 32) | Time->u.LowPart;
700 tmp = RtlLargeIntegerDivide( tmp, TICKSPERSEC, NULL );
701 tmp -= SECS_1601_TO_1980;
702 if (tmp > 0xffffffff) return FALSE;
703 *Seconds = (DWORD)tmp;
707 /******************************************************************************
708 * RtlSecondsSince1970ToTime [NTDLL.@]
710 * Convert a count of seconds since 1970 to a time.
713 * Seconds [I] Time to convert.
714 * Time [O] Destination for the converted time.
719 void WINAPI RtlSecondsSince1970ToTime( DWORD Seconds, LARGE_INTEGER *Time )
721 ULONGLONG secs = Seconds * (ULONGLONG)TICKSPERSEC + TICKS_1601_TO_1970;
722 Time->u.LowPart = (DWORD)secs;
723 Time->u.HighPart = (DWORD)(secs >> 32);
726 /******************************************************************************
727 * RtlSecondsSince1980ToTime [NTDLL.@]
729 * Convert a count of seconds since 1980 to a time.
732 * Seconds [I] Time to convert.
733 * Time [O] Destination for the converted time.
738 void WINAPI RtlSecondsSince1980ToTime( DWORD Seconds, LARGE_INTEGER *Time )
740 ULONGLONG secs = Seconds * (ULONGLONG)TICKSPERSEC + TICKS_1601_TO_1980;
741 Time->u.LowPart = (DWORD)secs;
742 Time->u.HighPart = (DWORD)(secs >> 32);
745 /******************************************************************************
746 * RtlTimeToElapsedTimeFields [NTDLL.@]
748 * Convert a time to a count of elapsed seconds.
751 * Time [I] Time to convert.
752 * TimeFields [O] Destination for the converted time.
757 void WINAPI RtlTimeToElapsedTimeFields( const LARGE_INTEGER *Time, PTIME_FIELDS TimeFields )
762 time = RtlExtendedLargeIntegerDivide( Time->QuadPart, TICKSPERSEC, &rem );
763 TimeFields->Milliseconds = rem / TICKSPERMSEC;
765 /* time is now in seconds */
766 TimeFields->Year = 0;
767 TimeFields->Month = 0;
768 TimeFields->Day = RtlExtendedLargeIntegerDivide( time, SECSPERDAY, &rem );
770 /* rem is now the remaining seconds in the last day */
771 TimeFields->Second = rem % 60;
773 TimeFields->Minute = rem % 60;
774 TimeFields->Hour = rem / 60;
777 /***********************************************************************
778 * NtQuerySystemTime [NTDLL.@]
779 * ZwQuerySystemTime [NTDLL.@]
781 * Get the current system time.
784 * Time [O] Destination for the current system time.
787 * Success: STATUS_SUCCESS.
788 * Failure: An NTSTATUS error code indicating the problem.
790 NTSTATUS WINAPI NtQuerySystemTime( PLARGE_INTEGER Time )
794 gettimeofday( &now, 0 );
795 Time->QuadPart = now.tv_sec * (ULONGLONG)TICKSPERSEC + TICKS_1601_TO_1970;
796 Time->QuadPart += now.tv_usec * 10;
797 return STATUS_SUCCESS;
800 /******************************************************************************
801 * NtQueryPerformanceCounter [NTDLL.@]
803 * Note: Windows uses a timer clocked at a multiple of 1193182 Hz. There is a
804 * good number of applications that crash when the returned frequency is either
805 * lower or higher then what Windows gives. Also too high counter values are
806 * reported to give problems.
808 NTSTATUS WINAPI NtQueryPerformanceCounter( PLARGE_INTEGER Counter, PLARGE_INTEGER Frequency )
812 if (!Counter) return STATUS_ACCESS_VIOLATION;
813 gettimeofday( &now, 0 );
814 /* convert a counter that increments at a rate of 1 MHz
815 * to one of 1.193182 MHz, with some care for arithmetic
816 * overflow ( will not overflow for 5000 years ) and
817 * good accuracy ( 105/88 = 1.19318182) */
818 Counter->QuadPart = (((now.tv_sec - server_start_time) * (ULONGLONG)1000000 + now.tv_usec) * 105) / 88;
819 if (Frequency) Frequency->QuadPart = 1193182;
820 return STATUS_SUCCESS;
823 /***********************************************************************
824 * TIME_GetTZAsStr [internal]
826 * Helper function that returns the given timezone as a string.
829 * utc [I] The current utc time.
830 * bias [I] The bias of the current timezone.
837 * This could be done with a hash table instead of merely iterating through a
838 * table, however with the small amount of entries (60 or so) I didn't think
841 static const WCHAR* TIME_GetTZAsStr (time_t utc, int bias, int dst)
844 struct tm *ptm = localtime(&utc);
847 if (!strftime (psTZName, 7, "%Z", ptm))
850 for (i=0; i<(sizeof(TZ_INFO) / sizeof(struct tagTZ_INFO)); i++)
852 if ( strcmp(TZ_INFO[i].psTZFromUnix, psTZName) == 0 &&
853 TZ_INFO[i].bias == bias &&
854 TZ_INFO[i].dst == dst
856 return TZ_INFO[i].psTZWindows;
858 FIXME("Can't match system time zone name \"%s\", bias=%d and dst=%d "
859 "to an entry in TZ_INFO. Please add appropriate entry to "
860 "TZ_INFO and submit as patch to wine-patches\n",psTZName,bias,dst);
864 /*** TIME_GetTimeZoneInfoFromReg: helper for GetTimeZoneInformation ***/
867 static int TIME_GetTimeZoneInfoFromReg(RTL_TIME_ZONE_INFORMATION *tzinfo)
870 KEY_VALUE_PARTIAL_INFORMATION * KpInfo =
871 (KEY_VALUE_PARTIAL_INFORMATION *) buf;
874 OBJECT_ATTRIBUTES attr;
875 UNICODE_STRING nameW;
877 attr.Length = sizeof(attr);
878 attr.RootDirectory = 0;
879 attr.ObjectName = &nameW;
881 attr.SecurityDescriptor = NULL;
882 attr.SecurityQualityOfService = NULL;
883 RtlInitUnicodeString( &nameW, TZInformationKeyW);
884 if (!NtOpenKey( &hkey, KEY_ALL_ACCESS, &attr )) {
886 #define GTZIFR_N( valkey, tofield) \
887 RtlInitUnicodeString( &nameW, valkey );\
888 if (!NtQueryValueKey( hkey, &nameW, KeyValuePartialInformation, KpInfo,\
889 sizeof(buf), &size )) { \
890 if( size >= (sizeof((tofield)) + \
891 offsetof(KEY_VALUE_PARTIAL_INFORMATION,Data))); { \
893 KpInfo->Data, sizeof(tofield)); \
896 #define GTZIFR_S( valkey, tofield) \
897 RtlInitUnicodeString( &nameW, valkey );\
898 if (!NtQueryValueKey( hkey, &nameW, KeyValuePartialInformation, KpInfo,\
899 sizeof(buf), &size )) { \
900 size_t len = (strlenW( (WCHAR*)KpInfo->Data ) + 1) * sizeof(WCHAR); \
901 if (len > sizeof(tofield)) len = sizeof(tofield); \
902 memcpy( tofield, KpInfo->Data, len ); \
903 tofield[(len/sizeof(WCHAR))-1] = 0; \
906 GTZIFR_N( TZStandardStartW, tzinfo->StandardDate)
907 GTZIFR_N( TZDaylightStartW, tzinfo->DaylightDate)
908 GTZIFR_N( TZBiasW, tzinfo->Bias)
909 GTZIFR_N( TZStandardBiasW, tzinfo->StandardBias)
910 GTZIFR_N( TZDaylightBiasW, tzinfo->DaylightBias)
911 GTZIFR_S( TZStandardNameW, tzinfo->StandardName)
912 GTZIFR_S( TZDaylightNameW, tzinfo->DaylightName)
922 /***********************************************************************
923 * RtlQueryTimeZoneInformation [NTDLL.@]
925 * Get information about the current timezone.
928 * tzinfo [O] Destination for the retrieved timezone info.
931 * Success: STATUS_SUCCESS.
932 * Failure: An NTSTATUS error code indicating the problem.
934 NTSTATUS WINAPI RtlQueryTimeZoneInformation(RTL_TIME_ZONE_INFORMATION *tzinfo)
940 memset(tzinfo, 0, sizeof(RTL_TIME_ZONE_INFORMATION));
942 if( !TIME_GetTimeZoneInfoFromReg(tzinfo)) {
945 bias = TIME_GetBias(gmt, &daylight);
947 tzinfo->Bias = -bias / 60;
948 tzinfo->StandardBias = 0;
949 tzinfo->DaylightBias = -60;
950 tzinfo->StandardName[0]='\0';
951 tzinfo->DaylightName[0]='\0';
952 psTZ = TIME_GetTZAsStr (gmt, (-bias/60), daylight);
953 if (psTZ) strcpyW( tzinfo->StandardName, psTZ );
955 return STATUS_SUCCESS;
958 /***********************************************************************
959 * RtlSetTimeZoneInformation [NTDLL.@]
961 * Set the current time zone information.
964 * tzinfo [I] Timezone information to set.
967 * Success: STATUS_SUCCESS.
968 * Failure: An NTSTATUS error code indicating the problem.
971 * Uses the obsolete unix timezone structure and tz_dsttime member.
973 NTSTATUS WINAPI RtlSetTimeZoneInformation( const RTL_TIME_ZONE_INFORMATION *tzinfo )
975 #ifdef HAVE_SETTIMEOFDAY
978 tz.tz_minuteswest = tzinfo->Bias;
980 tz.tz_dsttime = DST_NONE;
984 if(!settimeofday(NULL, &tz))
985 return STATUS_SUCCESS;
987 return STATUS_PRIVILEGE_NOT_HELD;
990 /***********************************************************************
991 * NtSetSystemTime [NTDLL.@]
992 * ZwSetSystemTime [NTDLL.@]
994 * Set the system time.
997 * NewTime [I] The time to set.
998 * OldTime [O] Optional destination for the previous system time.
1001 * Success: STATUS_SUCCESS.
1002 * Failure: An NTSTATUS error code indicating the problem.
1004 NTSTATUS WINAPI NtSetSystemTime(const LARGE_INTEGER *NewTime, LARGE_INTEGER *OldTime)
1014 /* Return the old time if necessary */
1016 NtQuerySystemTime(OldTime);
1018 RtlTimeToTimeFields(NewTime, &tf);
1020 /* call gettimeofday to get the current timezone */
1021 gettimeofday(&tv, &tz);
1023 /* get delta local time from utc */
1024 bias = TIME_GetBias(oldsec, &dst);
1026 /* get the number of seconds */
1027 t.tm_sec = tf.Second;
1028 t.tm_min = tf.Minute;
1029 t.tm_hour = tf.Hour;
1031 t.tm_mon = tf.Month - 1;
1032 t.tm_year = tf.Year - 1900;
1035 /* correct for timezone and daylight */
1038 /* set the new time */
1040 tv.tv_usec = tf.Milliseconds * 1000;
1042 /* error and sanity check*/
1043 if(sec == (time_t)-1 || abs((int)(sec-oldsec)) > SETTIME_MAX_ADJUST) {
1046 #ifdef HAVE_SETTIMEOFDAY
1047 err = settimeofday(&tv, NULL); /* 0 is OK, -1 is error */
1049 return STATUS_SUCCESS;
1055 ERR("Cannot set time to %d/%d/%d %d:%d:%d Time adjustment %ld %s\n",
1056 tf.Year, tf.Month, tf.Day, tf.Hour, tf.Minute, tf.Second,
1058 err == -1 ? "No Permission"
1059 : sec == (time_t)-1 ? "" : "is too large." );
1062 return STATUS_INVALID_PARAMETER;
1064 return STATUS_PRIVILEGE_NOT_HELD;
1066 return STATUS_NOT_IMPLEMENTED;