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
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)
88 const char *psTZFromUnix;
89 WCHAR psTZWindows[32];
94 static const struct tagTZ_INFO TZ_INFO[] =
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},
100 {'S','a','m','o','a',' ','S','t','a','n','d','a','r','d',' ','T','i','m',
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},
106 {'A','l','a','s','k','a','n',' ','S','t','a','n','d','a','r','d',' ','T',
107 'i','m','e','\0'}, 480, 1},
109 {'P','a','c','i','f','i','c',' ','S','t','a','n','d','a','r','d',' ','T',
110 'i','m','e','\0'}, 480, 0},
112 {'P','a','c','i','f','i','c',' ','S','t','a','n','d','a','r','d',' ','T',
113 'i','m','e','\0'}, 420, 1},
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},
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},
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},
124 {'C','e','n','t','r','a','l',' ','S','t','a','n','d','a','r','d',' ','T',
125 'i','m','e','\0'}, 300, 1},
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},
130 {'E','a','s','t','e','r','n',' ','S','t','a','n','d','a','r','d',' ','T',
131 'i','m','e','\0'}, 240, 1},
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},
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},
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},
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},
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},
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},
151 {'E','.',' ','B','r','a','z','i','l','i','a','n',' ','S','t','a','n','d',
152 'a','r','d',' ','T','i','m','e','\0'}, 180, 0},
154 {'S','A',' ','E','a','s','t','e','r','n',' ','S','t','a','n','d','a','r',
155 'd',' ','T','i','m','e','\0'}, 180, 0},
157 {'G','r','e','e','n','l','a','n','d',' ','S','t','a','n','d','a','r','d',
158 ' ','T','i','m','e','\0'}, 120, 1},
160 {'M','i','d','-','A','t','l','a','n','t','i','c',' ','S','t','a','n','d',
161 'a','r','d',' ','T','i','m','e','\0'}, 120, 0},
163 {'A','z','o','r','e','s',' ','S','t','a','n','d','a','r','d',' ','T','i',
164 'm','e','\0'}, 0, 1},
166 {'C','a','p','e',' ','V','e','r','d','e',' ','S','t','a','n','d','a','r',
167 'd',' ','T','i','m','e','\0'}, 60, 0},
169 {'G','r','e','e','n','w','i','c','h',' ','S','t','a','n','d','a','r','d',
170 ' ','T','i','m','e','\0'}, 0, 0},
172 {'G','M','T',' ','S','t','a','n','d','a','r','d',' ','T','i','m','e','\0'},
175 {'G','M','T',' ','S','t','a','n','d','a','r','d',' ','T','i','m','e','\0'},
178 {'G','M','T',' ','S','t','a','n','d','a','r','d',' ','T','i','m','e','\0'},
181 {'C','e','n','t','r','a','l',' ','E','u','r','o','p','e','a','n',' ',
182 'T','i','m','e','\0'}, -60, 0},
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'}, -120, 1},
187 {'C','e','n','t','r','a','l',' ','E','u','r','o','p','e',' ','S','t','a',
188 'n','d','a','r','d',' ','T','i','m','e','\0'}, -60, 0},
190 {'C','e','n','t','r','a','l',' ','E','u','r','o','p','e',' ','D','a','y',
191 'l','i','g','h','t',' ','T','i','m','e','\0'}, -120, 1},
193 {'W','.',' ','C','e','n','t','r','a','l',' ','A','f','r','i','c','a',' ',
194 'S','t','a','n','d','a','r','d',' ','T','i','m','e','\0'}, -60, 0},
196 {'E','.',' ','E','u','r','o','p','e',' ','S','t','a','n','d','a','r','d',
197 ' ','T','i','m','e','\0'}, -180, 1},
199 {'E','g','y','p','t',' ','S','t','a','n','d','a','r','d',' ','T','i','m',
202 {'C','e','n','t','r','a','l',' ','A','f','r','i','c','a','n',' '
203 ,'T','i','m','e','\0'}, -120, 0},
205 {'S','o','u','t','h',' ','A','f','r','i','c','a',' ','S','t','a','n','d',
206 'a','r','d',' ','T','i','m','e','\0'}, -120, 0},
208 {'I','s','r','a','e','l',' ','S','t','a','n','d','a','r','d',' ','T','i',
209 'm','e','\0'}, -120, 0},
211 {'R','u','s','s','i','a','n',' ','S','t','a','n','d','a','r','d',' ','T',
212 'i','m','e','\0'}, -180, 0},
214 {'A','r','a','b','i','c',' ','S','t','a','n','d','a','r','d',' ','T','i',
215 'm','e','\0'}, -240, 1},
217 {'A','r','a','b',' ','S','t','a','n','d','a','r','d',' ','T','i','m','e',
220 {'R','u','s','s','i','a','n',' ','S','t','a','n','d','a','r','d',' ','T',
221 'i','m','e','\0'}, -240, 1},
223 {'E','.',' ','A','f','r','i','c','a',' ','S','t','a','n','d','a','r','d',
224 ' ','T','i','m','e','\0'}, -180, 0},
226 {'I','r','a','n',' ','S','t','a','n','d','a','r','d',' ','T','i','m','e',
229 {'A','r','a','b','i','a','n',' ','S','t','a','n','d','a','r','d',' ','T',
230 'i','m','e','\0'}, -240, 0},
232 {'C','a','u','c','a','s','u','s',' ','S','t','a','n','d','a','r','d',' ',
233 'T','i','m','e','\0'}, -300, 1},
235 {'A','f','g','h','a','n','i','s','t','a','n',' ','S','t','a','n','d','a',
236 'r','d',' ','T','i','m','e','\0'}, -270, 0},
238 {'S','a','m','a','r','a',' ','S','t','a','n','d','a','r','d',' ','T','i',
239 'm','e','\0'}, -270, 1},
241 {'E','k','a','t','e','r','i','n','b','u','r','g',' ','S','t','a','n','d',
242 'a','r','d',' ','T','i','m','e','\0'}, -360, 1},
244 {'W','e','s','t',' ','A','s','i','a',' ','S','t','a','n','d','a','r','d',
245 ' ','T','i','m','e','\0'}, -300, 0},
247 {'I','n','d','i','a',' ','S','t','a','n','d','a','r','d',' ','T','i','m',
250 {'N','e','p','a','l',' ','S','t','a','n','d','a','r','d',' ','T','i','m',
253 {'N','.',' ','C','e','n','t','r','a','l',' ','A','s','i','a',' ','S','t',
254 'a','n','d','a','r','d',' ','T','i','m','e','\0'}, -420, 1},
256 {'C','e','n','t','r','a','l',' ','A','s','i','a',' ','S','t','a','n','d',
257 'a','r','d',' ','T','i','m','e','\0'}, -360, 0},
259 {'S','r','i',' ','L','a','n','k','a',' ','S','t','a','n','d','a','r','d',
260 ' ','T','i','m','e','\0'}, -360, 0},
262 {'M','y','a','n','m','a','r',' ','S','t','a','n','d','a','r','d',' ','T',
263 'i','m','e','\0'}, -390, 0},
265 {'S','E',' ','A','s','i','a',' ','S','t','a','n','d','a','r','d',' ','T',
266 'i','m','e','\0'}, -420, 0},
268 {'N','o','r','t','h',' ','A','s','i','a',' ','S','t','a','n','d','a','r',
269 'd',' ','T','i','m','e','\0'}, -480, 1},
271 {'C','h','i','n','a',' ','S','t','a','n','d','a','r','d',' ','T','i','m',
274 {'N','o','r','t','h',' ','A','s','i','a',' ','E','a','s','t',' ','S','t',
275 'a','n','d','a','r','d',' ','T','i','m','e','\0'}, -540, 1},
277 {'M','a','l','a','y',' ','P','e','n','i','n','s','u','l','a',' ','S','t',
278 'a','n','d','a','r','d',' ','T','i','m','e','\0'}, -480, 0},
280 {'W','.',' ','A','u','s','t','r','a','l','i','a',' ','S','t','a','n','d',
281 'a','r','d',' ','T','i','m','e','\0'}, -480, 0},
283 {'T','o','k','y','o',' ','S','t','a','n','d','a','r','d',' ','T','i','m',
286 {'K','o','r','e','a',' ','S','t','a','n','d','a','r','d',' ','T','i','m',
289 {'Y','a','k','u','t','s','k',' ','S','t','a','n','d','a','r','d',' ','T',
290 'i','m','e','\0'}, -600, 1},
292 {'C','e','n','.',' ','A','u','s','t','r','a','l','i','a',' ','S','t','a',
293 'n','d','a','r','d',' ','T','i','m','e','\0'}, -570, 0},
295 {'E','.',' ','A','u','s','t','r','a','l','i','a',' ','S','t','a','n','d',
296 'a','r','d',' ','T','i','m','e','\0'}, -600, 0},
298 {'E','.',' ','A','u','s','t','r','a','l','i','a',' ','S','t','a','n','d',
299 'a','r','d',' ','T','i','m','e','\0'}, -660, 1},
301 {'W','e','s','t',' ','P','a','c','i','f','i','c',' ','S','t','a','n','d',
302 'a','r','d',' ','T','i','m','e','\0'}, -600, 0},
304 {'V','l','a','d','i','v','o','s','t','o','k',' ','S','t','a','n','d','a',
305 'r','d',' ','T','i','m','e','\0'}, -660, 1},
307 {'C','e','n','t','r','a','l',' ','P','a','c','i','f','i','c',' ','S','t',
308 'a','n','d','a','r','d',' ','T','i','m','e','\0'}, -720, 1},
310 {'N','e','w',' ','Z','e','a','l','a','n','d',' ','S','t','a','n','d','a',
311 'r','d',' ','T','i','m','e','\0'}, -720, 0},
313 {'F','i','j','i',' ','S','t','a','n','d','a','r','d',' ','T','i','m','e',
316 {'T','o','n','g','a',' ','S','t','a','n','d','a','r','d',' ','T','i','m',
319 {'N','o','v','o','s','i','b','i','r','s','k',' ','S','t','a','n','d','a',
320 'r','d',' ','T','i','m','e','\0'}, -360, 1}
323 #define TICKSPERSEC 10000000
324 #define TICKSPERMSEC 10000
325 #define SECSPERDAY 86400
326 #define SECSPERHOUR 3600
327 #define SECSPERMIN 60
328 #define MINSPERHOUR 60
329 #define HOURSPERDAY 24
330 #define EPOCHWEEKDAY 1 /* Jan 1, 1601 was Monday */
331 #define DAYSPERWEEK 7
332 #define EPOCHYEAR 1601
333 #define DAYSPERNORMALYEAR 365
334 #define DAYSPERLEAPYEAR 366
335 #define MONSPERYEAR 12
336 #define DAYSPERQUADRICENTENNIUM (365 * 400 + 97)
337 #define DAYSPERNORMALCENTURY (365 * 100 + 24)
338 #define DAYSPERNORMALQUADRENNIUM (365 * 4 + 1)
340 /* 1601 to 1970 is 369 years plus 89 leap days */
341 #define SECS_1601_TO_1970 ((369 * 365 + 89) * (ULONGLONG)SECSPERDAY)
342 #define TICKS_1601_TO_1970 (SECS_1601_TO_1970 * TICKSPERSEC)
343 /* 1601 to 1980 is 379 years plus 91 leap days */
344 #define SECS_1601_TO_1980 ((379 * 365 + 91) * (ULONGLONG)SECSPERDAY)
345 #define TICKS_1601_TO_1980 (SECS_1601_TO_1980 * TICKSPERSEC)
346 /* max ticks that can be represented as Unix time */
347 #define TICKS_1601_TO_UNIX_MAX ((SECS_1601_TO_1970 + INT_MAX) * TICKSPERSEC)
350 static const int MonthLengths[2][MONSPERYEAR] =
352 { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
353 { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
356 static inline int IsLeapYear(int Year)
358 return Year % 4 == 0 && (Year % 100 != 0 || Year % 400 == 0) ? 1 : 0;
361 /***********************************************************************
362 * NTDLL_get_server_timeout
364 * Convert a NTDLL timeout into a timeval struct to send to the server.
366 void NTDLL_get_server_timeout( abs_time_t *when, const LARGE_INTEGER *timeout )
370 if (!timeout) /* infinite timeout */
372 when->sec = when->usec = 0;
374 else if (timeout->QuadPart <= 0) /* relative timeout */
378 if (-timeout->QuadPart > (LONGLONG)INT_MAX * TICKSPERSEC)
379 when->sec = when->usec = INT_MAX;
382 ULONG sec = RtlEnlargedUnsignedDivide( -timeout->QuadPart, TICKSPERSEC, &remainder );
383 gettimeofday( &tv, 0 );
384 when->sec = tv.tv_sec + sec;
385 if ((when->usec = tv.tv_usec + (remainder / 10)) >= 1000000)
387 when->usec -= 1000000;
390 if (when->sec < tv.tv_sec) /* overflow */
391 when->sec = when->usec = INT_MAX;
394 else /* absolute time */
396 if (timeout->QuadPart < TICKS_1601_TO_1970)
397 when->sec = when->usec = 0;
398 else if (timeout->QuadPart > TICKS_1601_TO_UNIX_MAX)
399 when->sec = when->usec = INT_MAX;
402 when->sec = RtlEnlargedUnsignedDivide( timeout->QuadPart - TICKS_1601_TO_1970,
403 TICKSPERSEC, &remainder );
404 when->usec = remainder / 10;
410 /***********************************************************************
411 * NTDLL_from_server_timeout
413 * Convert a timeval struct from the server into an NTDLL timeout.
415 void NTDLL_from_server_timeout( LARGE_INTEGER *timeout, const abs_time_t *when )
417 timeout->QuadPart = when->sec * (ULONGLONG)TICKSPERSEC + TICKS_1601_TO_1970;
418 timeout->QuadPart += when->usec * 10;
422 /******************************************************************************
423 * RtlTimeToTimeFields [NTDLL.@]
425 * Convert a time into a TIME_FIELDS structure.
428 * liTime [I] Time to convert.
429 * TimeFields [O] Destination for the converted time.
434 VOID WINAPI RtlTimeToTimeFields(
435 const LARGE_INTEGER *liTime,
436 PTIME_FIELDS TimeFields)
439 long int cleaps, years, yearday, months;
443 /* Extract millisecond from time and convert time into seconds */
444 TimeFields->Milliseconds =
445 (CSHORT) (( liTime->QuadPart % TICKSPERSEC) / TICKSPERMSEC);
446 Time = liTime->QuadPart / TICKSPERSEC;
448 /* The native version of RtlTimeToTimeFields does not take leap seconds
451 /* Split the time into days and seconds within the day */
452 Days = Time / SECSPERDAY;
453 SecondsInDay = Time % SECSPERDAY;
455 /* compute time of day */
456 TimeFields->Hour = (CSHORT) (SecondsInDay / SECSPERHOUR);
457 SecondsInDay = SecondsInDay % SECSPERHOUR;
458 TimeFields->Minute = (CSHORT) (SecondsInDay / SECSPERMIN);
459 TimeFields->Second = (CSHORT) (SecondsInDay % SECSPERMIN);
461 /* compute day of week */
462 TimeFields->Weekday = (CSHORT) ((EPOCHWEEKDAY + Days) % DAYSPERWEEK);
464 /* compute year, month and day of month. */
465 cleaps=( 3 * ((4 * Days + 1227) / DAYSPERQUADRICENTENNIUM) + 3 ) / 4;
466 Days += 28188 + cleaps;
467 years = (20 * Days - 2442) / (5 * DAYSPERNORMALQUADRENNIUM);
468 yearday = Days - (years * DAYSPERNORMALQUADRENNIUM)/4;
469 months = (64 * yearday) / 1959;
470 /* the result is based on a year starting on March.
471 * To convert take 12 from Januari and Februari and
472 * increase the year by one. */
474 TimeFields->Month = months - 1;
475 TimeFields->Year = years + 1524;
477 TimeFields->Month = months - 13;
478 TimeFields->Year = years + 1525;
480 /* calculation of day of month is based on the wonderful
481 * sequence of INT( n * 30.6): it reproduces the
482 * 31-30-31-30-31-31 month lengths exactly for small n's */
483 TimeFields->Day = yearday - (1959 * months) / 64 ;
487 /******************************************************************************
488 * RtlTimeFieldsToTime [NTDLL.@]
490 * Convert a TIME_FIELDS structure into a time.
493 * ftTimeFields [I] TIME_FIELDS structure to convert.
494 * Time [O] Destination for the converted time.
500 BOOLEAN WINAPI RtlTimeFieldsToTime(
501 PTIME_FIELDS tfTimeFields,
504 int month, year, cleaps, day;
506 /* FIXME: normalize the TIME_FIELDS structure here */
507 /* No, native just returns 0 (error) if the fields are not */
508 if( tfTimeFields->Milliseconds< 0 || tfTimeFields->Milliseconds > 999 ||
509 tfTimeFields->Second < 0 || tfTimeFields->Second > 59 ||
510 tfTimeFields->Minute < 0 || tfTimeFields->Minute > 59 ||
511 tfTimeFields->Hour < 0 || tfTimeFields->Hour > 23 ||
512 tfTimeFields->Month < 1 || tfTimeFields->Month > 12 ||
513 tfTimeFields->Day < 1 ||
514 tfTimeFields->Day > MonthLengths
515 [ tfTimeFields->Month ==2 || IsLeapYear(tfTimeFields->Year)]
516 [ tfTimeFields->Month - 1] ||
517 tfTimeFields->Year < 1601 )
520 /* now calculate a day count from the date
521 * First start counting years from March. This way the leap days
522 * are added at the end of the year, not somewhere in the middle.
523 * Formula's become so much less complicate that way.
524 * To convert: add 12 to the month numbers of Jan and Feb, and
525 * take 1 from the year */
526 if(tfTimeFields->Month < 3) {
527 month = tfTimeFields->Month + 13;
528 year = tfTimeFields->Year - 1;
530 month = tfTimeFields->Month + 1;
531 year = tfTimeFields->Year;
533 cleaps = (3 * (year / 100) + 3) / 4; /* nr of "century leap years"*/
534 day = (36525 * year) / 100 - cleaps + /* year * dayperyr, corrected */
535 (1959 * month) / 64 + /* months * daypermonth */
536 tfTimeFields->Day - /* day of the month */
537 584817 ; /* zero that on 1601-01-01 */
540 Time->QuadPart = (((((LONGLONG) day * HOURSPERDAY +
541 tfTimeFields->Hour) * MINSPERHOUR +
542 tfTimeFields->Minute) * SECSPERMIN +
543 tfTimeFields->Second ) * 1000 +
544 tfTimeFields->Milliseconds ) * TICKSPERMSEC;
549 /***********************************************************************
550 * TIME_GetBias [internal]
552 * Helper function calculates delta local time from UTC.
555 * utc [I] The current utc time.
556 * pdaylight [I] Local daylight.
559 * The bias for the current timezone.
561 static int TIME_GetBias(time_t utc, int *pdaylight)
564 static time_t last_utc;
565 static int last_bias;
566 static int last_daylight;
569 RtlEnterCriticalSection( &TIME_GetBias_section );
572 *pdaylight = last_daylight;
576 ptm = localtime(&utc);
577 *pdaylight = last_daylight =
578 ptm->tm_isdst; /* daylight for local timezone */
580 ptm->tm_isdst = *pdaylight; /* use local daylight, not that of Greenwich */
582 ret = last_bias = (int)(utc-mktime(ptm));
584 RtlLeaveCriticalSection( &TIME_GetBias_section );
588 /******************************************************************************
589 * RtlLocalTimeToSystemTime [NTDLL.@]
591 * Convert a local time into system time.
594 * LocalTime [I] Local time to convert.
595 * SystemTime [O] Destination for the converted time.
598 * Success: STATUS_SUCCESS.
599 * Failure: An NTSTATUS error code indicating the problem.
601 NTSTATUS WINAPI RtlLocalTimeToSystemTime( const LARGE_INTEGER *LocalTime,
602 PLARGE_INTEGER SystemTime)
607 TRACE("(%p, %p)\n", LocalTime, SystemTime);
610 bias = TIME_GetBias(gmt, &daylight);
612 SystemTime->QuadPart = LocalTime->QuadPart - bias * (LONGLONG)TICKSPERSEC;
613 return STATUS_SUCCESS;
616 /******************************************************************************
617 * RtlSystemTimeToLocalTime [NTDLL.@]
619 * Convert a system time into a local time.
622 * SystemTime [I] System time to convert.
623 * LocalTime [O] Destination for the converted time.
626 * Success: STATUS_SUCCESS.
627 * Failure: An NTSTATUS error code indicating the problem.
629 NTSTATUS WINAPI RtlSystemTimeToLocalTime( const LARGE_INTEGER *SystemTime,
630 PLARGE_INTEGER LocalTime )
635 TRACE("(%p, %p)\n", SystemTime, LocalTime);
638 bias = TIME_GetBias(gmt, &daylight);
640 LocalTime->QuadPart = SystemTime->QuadPart + bias * (LONGLONG)TICKSPERSEC;
641 return STATUS_SUCCESS;
644 /******************************************************************************
645 * RtlTimeToSecondsSince1970 [NTDLL.@]
647 * Convert a time into a count of seconds since 1970.
650 * Time [I] Time to convert.
651 * Seconds [O] Destination for the converted time.
655 * Failure: FALSE, if the resulting value will not fit in a DWORD.
657 BOOLEAN WINAPI RtlTimeToSecondsSince1970( const LARGE_INTEGER *Time, LPDWORD Seconds )
659 ULONGLONG tmp = ((ULONGLONG)Time->u.HighPart << 32) | Time->u.LowPart;
660 tmp = RtlLargeIntegerDivide( tmp, TICKSPERSEC, NULL );
661 tmp -= SECS_1601_TO_1970;
662 if (tmp > 0xffffffff) return FALSE;
663 *Seconds = (DWORD)tmp;
667 /******************************************************************************
668 * RtlTimeToSecondsSince1980 [NTDLL.@]
670 * Convert a time into a count of seconds since 1980.
673 * Time [I] Time to convert.
674 * Seconds [O] Destination for the converted time.
678 * Failure: FALSE, if the resulting value will not fit in a DWORD.
680 BOOLEAN WINAPI RtlTimeToSecondsSince1980( const LARGE_INTEGER *Time, LPDWORD Seconds )
682 ULONGLONG tmp = ((ULONGLONG)Time->u.HighPart << 32) | Time->u.LowPart;
683 tmp = RtlLargeIntegerDivide( tmp, TICKSPERSEC, NULL );
684 tmp -= SECS_1601_TO_1980;
685 if (tmp > 0xffffffff) return FALSE;
686 *Seconds = (DWORD)tmp;
690 /******************************************************************************
691 * RtlSecondsSince1970ToTime [NTDLL.@]
693 * Convert a count of seconds since 1970 to a time.
696 * Seconds [I] Time to convert.
697 * Time [O] Destination for the converted time.
702 void WINAPI RtlSecondsSince1970ToTime( DWORD Seconds, LARGE_INTEGER *Time )
704 ULONGLONG secs = Seconds * (ULONGLONG)TICKSPERSEC + TICKS_1601_TO_1970;
705 Time->u.LowPart = (DWORD)secs;
706 Time->u.HighPart = (DWORD)(secs >> 32);
709 /******************************************************************************
710 * RtlSecondsSince1980ToTime [NTDLL.@]
712 * Convert a count of seconds since 1980 to a time.
715 * Seconds [I] Time to convert.
716 * Time [O] Destination for the converted time.
721 void WINAPI RtlSecondsSince1980ToTime( DWORD Seconds, LARGE_INTEGER *Time )
723 ULONGLONG secs = Seconds * (ULONGLONG)TICKSPERSEC + TICKS_1601_TO_1980;
724 Time->u.LowPart = (DWORD)secs;
725 Time->u.HighPart = (DWORD)(secs >> 32);
728 /******************************************************************************
729 * RtlTimeToElapsedTimeFields [NTDLL.@]
731 * Convert a time to a count of elapsed seconds.
734 * Time [I] Time to convert.
735 * TimeFields [O] Destination for the converted time.
740 void WINAPI RtlTimeToElapsedTimeFields( const LARGE_INTEGER *Time, PTIME_FIELDS TimeFields )
745 time = RtlExtendedLargeIntegerDivide( Time->QuadPart, TICKSPERSEC, &rem );
746 TimeFields->Milliseconds = rem / TICKSPERMSEC;
748 /* time is now in seconds */
749 TimeFields->Year = 0;
750 TimeFields->Month = 0;
751 TimeFields->Day = RtlExtendedLargeIntegerDivide( time, SECSPERDAY, &rem );
753 /* rem is now the remaining seconds in the last day */
754 TimeFields->Second = rem % 60;
756 TimeFields->Minute = rem % 60;
757 TimeFields->Hour = rem / 60;
760 /***********************************************************************
761 * NtQuerySystemTime [NTDLL.@]
762 * ZwQuerySystemTime [NTDLL.@]
764 * Get the current system time.
767 * Time [O] Destination for the current system time.
770 * Success: STATUS_SUCCESS.
771 * Failure: An NTSTATUS error code indicating the problem.
773 NTSTATUS WINAPI NtQuerySystemTime( PLARGE_INTEGER Time )
777 gettimeofday( &now, 0 );
778 Time->QuadPart = now.tv_sec * (ULONGLONG)TICKSPERSEC + TICKS_1601_TO_1970;
779 Time->QuadPart += now.tv_usec * 10;
780 return STATUS_SUCCESS;
783 /***********************************************************************
784 * TIME_GetTZAsStr [internal]
786 * Helper function that returns the given timezone as a string.
789 * utc [I] The current utc time.
790 * bias [I] The bias of the current timezone.
797 * This could be done with a hash table instead of merely iterating through a
798 * table, however with the small amount of entries (60 or so) I didn't think
801 static const WCHAR* TIME_GetTZAsStr (time_t utc, int bias, int dst)
804 struct tm *ptm = localtime(&utc);
807 if (!strftime (psTZName, 7, "%Z", ptm))
810 for (i=0; i<(sizeof(TZ_INFO) / sizeof(struct tagTZ_INFO)); i++)
812 if ( strcmp(TZ_INFO[i].psTZFromUnix, psTZName) == 0 &&
813 TZ_INFO[i].bias == bias &&
814 TZ_INFO[i].dst == dst
816 return TZ_INFO[i].psTZWindows;
818 FIXME("Can't match system time zone name \"%s\" to an entry in TZ_INFO\n",psTZName);
819 FIXME(" Please add appropriate entry to TZ_INFO and submit as patch to wine-patches\n");
823 /*** TIME_GetTimeZoneInfoFromReg: helper for GetTimeZoneInformation ***/
826 static int TIME_GetTimeZoneInfoFromReg(LPTIME_ZONE_INFORMATION tzinfo)
829 KEY_VALUE_PARTIAL_INFORMATION * KpInfo =
830 (KEY_VALUE_PARTIAL_INFORMATION *) buf;
833 OBJECT_ATTRIBUTES attr;
834 UNICODE_STRING nameW;
836 attr.Length = sizeof(attr);
837 attr.RootDirectory = 0;
838 attr.ObjectName = &nameW;
840 attr.SecurityDescriptor = NULL;
841 attr.SecurityQualityOfService = NULL;
842 RtlInitUnicodeString( &nameW, TZInformationKeyW);
843 if (!NtOpenKey( &hkey, KEY_ALL_ACCESS, &attr )) {
845 #define GTZIFR_N( valkey, tofield) \
846 RtlInitUnicodeString( &nameW, valkey );\
847 if (!NtQueryValueKey( hkey, &nameW, KeyValuePartialInformation, KpInfo,\
848 sizeof(buf), &size )) { \
849 if( size >= (sizeof((tofield)) + \
850 offsetof(KEY_VALUE_PARTIAL_INFORMATION,Data))); { \
852 KpInfo->Data, sizeof(tofield)); \
855 #define GTZIFR_S( valkey, tofield) \
856 RtlInitUnicodeString( &nameW, valkey );\
857 if (!NtQueryValueKey( hkey, &nameW, KeyValuePartialInformation, KpInfo,\
858 sizeof(buf), &size )) { \
859 strncpyW( tofield, (WCHAR*) KpInfo->Data, \
860 sizeof( tofield) / sizeof(WCHAR) ); \
863 GTZIFR_N( TZStandardStartW, tzinfo->StandardDate)
864 GTZIFR_N( TZDaylightStartW, tzinfo->DaylightDate)
865 GTZIFR_N( TZBiasW, tzinfo->Bias)
866 GTZIFR_N( TZStandardBiasW, tzinfo->StandardBias)
867 GTZIFR_N( TZDaylightBiasW, tzinfo->DaylightBias)
868 GTZIFR_S( TZStandardNameW, tzinfo->StandardName)
869 GTZIFR_S( TZDaylightNameW, tzinfo->DaylightName)
879 /***********************************************************************
880 * RtlQueryTimeZoneInformation [NTDLL.@]
882 * Get information about the current timezone.
885 * tzinfo [O] Destination for the retrieved timezone info.
888 * Success: STATUS_SUCCESS.
889 * Failure: An NTSTATUS error code indicating the problem.
891 NTSTATUS WINAPI RtlQueryTimeZoneInformation(LPTIME_ZONE_INFORMATION tzinfo)
897 memset(tzinfo, 0, sizeof(TIME_ZONE_INFORMATION));
899 if( !TIME_GetTimeZoneInfoFromReg(tzinfo)) {
902 bias = TIME_GetBias(gmt, &daylight);
904 tzinfo->Bias = -bias / 60;
905 tzinfo->StandardBias = 0;
906 tzinfo->DaylightBias = -60;
907 tzinfo->StandardName[0]='\0';
908 tzinfo->DaylightName[0]='\0';
909 psTZ = TIME_GetTZAsStr (gmt, (-bias/60), daylight);
910 if (psTZ) strcpyW( tzinfo->StandardName, psTZ );
912 return STATUS_SUCCESS;
915 /***********************************************************************
916 * RtlSetTimeZoneInformation [NTDLL.@]
918 * Set the current time zone information.
921 * tzinfo [I] Timezone information to set.
924 * Success: STATUS_SUCCESS.
925 * Failure: An NTSTATUS error code indicating the problem.
928 * Uses the obsolete unix timezone structure and tz_dsttime member.
930 NTSTATUS WINAPI RtlSetTimeZoneInformation( const TIME_ZONE_INFORMATION *tzinfo )
932 #ifdef HAVE_SETTIMEOFDAY
935 tz.tz_minuteswest = tzinfo->Bias;
937 tz.tz_dsttime = DST_NONE;
941 if(!settimeofday(NULL, &tz))
942 return STATUS_SUCCESS;
944 return STATUS_PRIVILEGE_NOT_HELD;
947 /***********************************************************************
948 * NtSetSystemTime [NTDLL.@]
949 * ZwSetSystemTime [NTDLL.@]
951 * Set the system time.
954 * NewTime [I] The time to set.
955 * OldTime [O] Optional destination for the previous system time.
958 * Success: STATUS_SUCCESS.
959 * Failure: An NTSTATUS error code indicating the problem.
961 NTSTATUS WINAPI NtSetSystemTime(const LARGE_INTEGER *NewTime, LARGE_INTEGER *OldTime)
971 /* Return the old time if necessary */
973 NtQuerySystemTime(OldTime);
975 RtlTimeToTimeFields(NewTime, &tf);
977 /* call gettimeofday to get the current timezone */
978 gettimeofday(&tv, &tz);
980 /* get delta local time from utc */
981 bias = TIME_GetBias(oldsec, &dst);
983 /* get the number of seconds */
984 t.tm_sec = tf.Second;
985 t.tm_min = tf.Minute;
988 t.tm_mon = tf.Month - 1;
989 t.tm_year = tf.Year - 1900;
992 /* correct for timezone and daylight */
995 /* set the new time */
997 tv.tv_usec = tf.Milliseconds * 1000;
999 /* error and sanity check*/
1000 if(sec == (time_t)-1 || abs((int)(sec-oldsec)) > SETTIME_MAX_ADJUST) {
1003 #ifdef HAVE_SETTIMEOFDAY
1004 err = settimeofday(&tv, NULL); /* 0 is OK, -1 is error */
1006 return STATUS_SUCCESS;
1012 ERR("Cannot set time to %d/%d/%d %d:%d:%d Time adjustment %ld %s\n",
1013 tf.Year, tf.Month, tf.Day, tf.Hour, tf.Minute, tf.Second,
1015 err == -1 ? "No Permission"
1016 : sec == (time_t)-1 ? "" : "is too large." );
1019 return STATUS_INVALID_PARAMETER;
1021 return STATUS_PRIVILEGE_NOT_HELD;
1023 return STATUS_NOT_IMPLEMENTED;