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 {'E','a','s','t','e','r','n',' ','C','e','n','t','r','a','l',' ','S','t','a','n','d','a','r',
137 'd',' ','T','i','m','e','\0'}, 300, 0},
139 {'A','t','l','a','n','t','i','c',' ','S','t','a','n','d','a','r','d',' ',
140 'T','i','m','e','\0'}, 180, 1},
142 {'S','A',' ','W','e','s','t','e','r','n',' ','S','t','a','n','d','a','r',
143 'd',' ','T','i','m','e','\0'}, 240, 0},
145 {'P','a','c','i','f','i','c',' ','S','A',' ','S','t','a','n','d','a','r',
146 'd',' ','T','i','m','e','\0'}, 240, 0},
148 {'N','e','w','f','o','u','n','d','l','a','n','d',' ','S','t','a','n','d',
149 'a','r','d',' ','T','i','m','e','\0'}, 150, 1},
151 {'B','r','a','z','i','l','i','a','n',' ','S','t','a','n','d','a','r','d',
152 ' ','T','i','m','e','\0'}, 180, 0},
154 {'B','r','a','z','i','l','i','a','n',' ','S','u','m','m','e','r',
155 ' ','T','i','m','e','\0'}, 120, 1},
157 {'S','A',' ','E','a','s','t','e','r','n',' ','S','t','a','n','d','a','r',
158 'd',' ','T','i','m','e','\0'}, 180, 0},
160 {'G','r','e','e','n','l','a','n','d',' ','S','t','a','n','d','a','r','d',
161 ' ','T','i','m','e','\0'}, 120, 1},
163 {'M','i','d','-','A','t','l','a','n','t','i','c',' ','S','t','a','n','d',
164 'a','r','d',' ','T','i','m','e','\0'}, 120, 0},
166 {'A','z','o','r','e','s',' ','S','t','a','n','d','a','r','d',' ','T','i',
167 'm','e','\0'}, 0, 1},
169 {'C','a','p','e',' ','V','e','r','d','e',' ','S','t','a','n','d','a','r',
170 'd',' ','T','i','m','e','\0'}, 60, 0},
172 {'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},
174 {'G','r','e','e','n','w','i','c','h',' ','S','t','a','n','d','a','r','d',
175 ' ','T','i','m','e','\0'}, 0, 0},
177 {'G','M','T',' ','S','t','a','n','d','a','r','d',' ','T','i','m','e','\0'},
180 {'I','r','i','s','h',' ','S','u','m','m','e','r',' ','T','i','m','e','\0'},
183 {'G','M','T',' ','S','t','a','n','d','a','r','d',' ','T','i','m','e','\0'},
186 {'G','M','T',' ','S','t','a','n','d','a','r','d',' ','T','i','m','e','\0'},
189 {'C','e','n','t','r','a','l',' ','E','u','r','o','p','e','a','n',' ',
190 'T','i','m','e','\0'}, -60, 0},
192 {'C','e','n','t','r','a','l',' ','E','u','r','o','p','e',' ','S','t','a',
193 'n','d','a','r','d',' ','T','i','m','e','\0'}, -120, 1},
195 {'C','e','n','t','r','a','l',' ','E','u','r','o','p','e',' ','S','t','a',
196 'n','d','a','r','d',' ','T','i','m','e','\0'}, -60, 0},
198 {'C','e','n','t','r','a','l',' ','E','u','r','o','p','e',' ','D','a','y',
199 'l','i','g','h','t',' ','T','i','m','e','\0'}, -120, 1},
201 {'W','.',' ','C','e','n','t','r','a','l',' ','A','f','r','i','c','a',' ',
202 'S','t','a','n','d','a','r','d',' ','T','i','m','e','\0'}, -60, 0},
204 {'E','.',' ','E','u','r','o','p','e',' ','S','t','a','n','d','a','r','d',
205 ' ','T','i','m','e','\0'}, -180, 1},
207 {'E','g','y','p','t',' ','S','t','a','n','d','a','r','d',' ','T','i','m',
210 {'C','e','n','t','r','a','l',' ','A','f','r','i','c','a','n',' '
211 ,'T','i','m','e','\0'}, -120, 0},
213 {'S','o','u','t','h',' ','A','f','r','i','c','a',' ','S','t','a','n','d',
214 'a','r','d',' ','T','i','m','e','\0'}, -120, 0},
216 {'I','s','r','a','e','l',' ','S','t','a','n','d','a','r','d',' ','T','i',
217 'm','e','\0'}, -120, 0},
219 {'R','u','s','s','i','a','n',' ','S','t','a','n','d','a','r','d',' ','T',
220 'i','m','e','\0'}, -180, 0},
222 {'A','r','a','b','i','c',' ','S','t','a','n','d','a','r','d',' ','T','i',
223 'm','e','\0'}, -240, 1},
225 {'A','r','a','b',' ','S','t','a','n','d','a','r','d',' ','T','i','m','e',
228 {'R','u','s','s','i','a','n',' ','S','t','a','n','d','a','r','d',' ','T',
229 'i','m','e','\0'}, -240, 1},
231 {'E','.',' ','A','f','r','i','c','a',' ','S','t','a','n','d','a','r','d',
232 ' ','T','i','m','e','\0'}, -180, 0},
234 {'I','r','a','n',' ','S','t','a','n','d','a','r','d',' ','T','i','m','e',
237 {'A','r','a','b','i','a','n',' ','S','t','a','n','d','a','r','d',' ','T',
238 'i','m','e','\0'}, -240, 0},
240 {'C','a','u','c','a','s','u','s',' ','S','t','a','n','d','a','r','d',' ',
241 'T','i','m','e','\0'}, -300, 1},
243 {'A','f','g','h','a','n','i','s','t','a','n',' ','S','t','a','n','d','a',
244 'r','d',' ','T','i','m','e','\0'}, -270, 0},
246 {'S','a','m','a','r','a',' ','S','t','a','n','d','a','r','d',' ','T','i',
247 'm','e','\0'}, -270, 1},
249 {'E','k','a','t','e','r','i','n','b','u','r','g',' ','S','t','a','n','d',
250 'a','r','d',' ','T','i','m','e','\0'}, -360, 1},
252 {'W','e','s','t',' ','A','s','i','a',' ','S','t','a','n','d','a','r','d',
253 ' ','T','i','m','e','\0'}, -300, 0},
255 {'I','n','d','i','a',' ','S','t','a','n','d','a','r','d',' ','T','i','m',
258 {'N','e','p','a','l',' ','S','t','a','n','d','a','r','d',' ','T','i','m',
261 {'N','.',' ','C','e','n','t','r','a','l',' ','A','s','i','a',' ','S','t',
262 'a','n','d','a','r','d',' ','T','i','m','e','\0'}, -420, 1},
264 {'C','e','n','t','r','a','l',' ','A','s','i','a',' ','S','t','a','n','d',
265 'a','r','d',' ','T','i','m','e','\0'}, -360, 0},
267 {'S','r','i',' ','L','a','n','k','a',' ','S','t','a','n','d','a','r','d',
268 ' ','T','i','m','e','\0'}, -360, 0},
270 {'M','y','a','n','m','a','r',' ','S','t','a','n','d','a','r','d',' ','T',
271 'i','m','e','\0'}, -390, 0},
273 {'S','E',' ','A','s','i','a',' ','S','t','a','n','d','a','r','d',' ','T',
274 'i','m','e','\0'}, -420, 0},
276 {'N','o','r','t','h',' ','A','s','i','a',' ','S','t','a','n','d','a','r',
277 'd',' ','T','i','m','e','\0'}, -480, 1},
279 {'C','h','i','n','a',' ','S','t','a','n','d','a','r','d',' ','T','i','m',
282 {'N','o','r','t','h',' ','A','s','i','a',' ','E','a','s','t',' ','S','t',
283 'a','n','d','a','r','d',' ','T','i','m','e','\0'}, -540, 1},
285 {'M','a','l','a','y',' ','P','e','n','i','n','s','u','l','a',' ','S','t',
286 'a','n','d','a','r','d',' ','T','i','m','e','\0'}, -480, 0},
288 {'W','.',' ','A','u','s','t','r','a','l','i','a',' ','S','t','a','n','d',
289 'a','r','d',' ','T','i','m','e','\0'}, -480, 0},
291 {'T','o','k','y','o',' ','S','t','a','n','d','a','r','d',' ','T','i','m',
294 {'K','o','r','e','a',' ','S','t','a','n','d','a','r','d',' ','T','i','m',
297 {'Y','a','k','u','t','s','k',' ','S','t','a','n','d','a','r','d',' ','T',
298 'i','m','e','\0'}, -600, 1},
300 {'C','e','n','.',' ','A','u','s','t','r','a','l','i','a',' ','S','t','a',
301 'n','d','a','r','d',' ','T','i','m','e','\0'}, -570, 0},
303 {'E','.',' ','A','u','s','t','r','a','l','i','a',' ','S','t','a','n','d',
304 'a','r','d',' ','T','i','m','e','\0'}, -600, 0},
306 {'E','.',' ','A','u','s','t','r','a','l','i','a',' ','S','t','a','n','d',
307 'a','r','d',' ','T','i','m','e','\0'}, -660, 1},
309 {'W','e','s','t',' ','P','a','c','i','f','i','c',' ','S','t','a','n','d',
310 'a','r','d',' ','T','i','m','e','\0'}, -600, 0},
312 {'V','l','a','d','i','v','o','s','t','o','k',' ','S','t','a','n','d','a',
313 'r','d',' ','T','i','m','e','\0'}, -660, 1},
315 {'C','e','n','t','r','a','l',' ','P','a','c','i','f','i','c',' ','S','t',
316 'a','n','d','a','r','d',' ','T','i','m','e','\0'}, -720, 1},
318 {'N','e','w',' ','Z','e','a','l','a','n','d',' ','S','t','a','n','d','a',
319 'r','d',' ','T','i','m','e','\0'}, -720, 0},
321 {'N','e','w',' ','Z','e','a','l','a','n','d',' ','D','a','y','l','i','g',
322 'h','t',' ','T','i','m','e','\0'}, -780, 1},
324 {'F','i','j','i',' ','S','t','a','n','d','a','r','d',' ','T','i','m','e',
327 {'T','o','n','g','a',' ','S','t','a','n','d','a','r','d',' ','T','i','m',
330 {'N','o','v','o','s','i','b','i','r','s','k',' ','S','t','a','n','d','a',
331 'r','d',' ','T','i','m','e','\0'}, -360, 1},
333 {'A','n','a','d','y','r',' ','S','t','a','n','d','a','r','d',' ','T','i',
334 'm','e','\0'}, -720, 1},
336 {'H','o','n','g',' ','K','o','n','g',' ','S','t','a','n','d','a','r','d',
337 ' ','T','i','m','e','\0'}, -480, 0},
339 {'U','r','u','g','u','a','y','a','n',' ','T','i','m','e','\0'}, 180, 0}
342 #define TICKSPERSEC 10000000
343 #define TICKSPERMSEC 10000
344 #define SECSPERDAY 86400
345 #define SECSPERHOUR 3600
346 #define SECSPERMIN 60
347 #define MINSPERHOUR 60
348 #define HOURSPERDAY 24
349 #define EPOCHWEEKDAY 1 /* Jan 1, 1601 was Monday */
350 #define DAYSPERWEEK 7
351 #define EPOCHYEAR 1601
352 #define DAYSPERNORMALYEAR 365
353 #define DAYSPERLEAPYEAR 366
354 #define MONSPERYEAR 12
355 #define DAYSPERQUADRICENTENNIUM (365 * 400 + 97)
356 #define DAYSPERNORMALCENTURY (365 * 100 + 24)
357 #define DAYSPERNORMALQUADRENNIUM (365 * 4 + 1)
359 /* 1601 to 1970 is 369 years plus 89 leap days */
360 #define SECS_1601_TO_1970 ((369 * 365 + 89) * (ULONGLONG)SECSPERDAY)
361 #define TICKS_1601_TO_1970 (SECS_1601_TO_1970 * TICKSPERSEC)
362 /* 1601 to 1980 is 379 years plus 91 leap days */
363 #define SECS_1601_TO_1980 ((379 * 365 + 91) * (ULONGLONG)SECSPERDAY)
364 #define TICKS_1601_TO_1980 (SECS_1601_TO_1980 * TICKSPERSEC)
365 /* max ticks that can be represented as Unix time */
366 #define TICKS_1601_TO_UNIX_MAX ((SECS_1601_TO_1970 + INT_MAX) * TICKSPERSEC)
369 static const int MonthLengths
[2][MONSPERYEAR
] =
371 { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
372 { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
375 static inline int IsLeapYear(int Year
)
377 return Year
% 4 == 0 && (Year
% 100 != 0 || Year
% 400 == 0) ? 1 : 0;
380 /***********************************************************************
381 * NTDLL_get_server_timeout
383 * Convert a NTDLL timeout into a timeval struct to send to the server.
385 void NTDLL_get_server_timeout( abs_time_t
*when
, const LARGE_INTEGER
*timeout
)
389 if (!timeout
) /* infinite timeout */
391 when
->sec
= when
->usec
= 0;
393 else if (timeout
->QuadPart
<= 0) /* relative timeout */
397 if (-timeout
->QuadPart
> (LONGLONG
)INT_MAX
* TICKSPERSEC
)
398 when
->sec
= when
->usec
= INT_MAX
;
401 ULONG sec
= RtlEnlargedUnsignedDivide( -timeout
->QuadPart
, TICKSPERSEC
, &remainder
);
402 gettimeofday( &tv
, 0 );
403 when
->sec
= tv
.tv_sec
+ sec
;
404 if ((when
->usec
= tv
.tv_usec
+ (remainder
/ 10)) >= 1000000)
406 when
->usec
-= 1000000;
409 if (when
->sec
< tv
.tv_sec
) /* overflow */
410 when
->sec
= when
->usec
= INT_MAX
;
413 else /* absolute time */
415 if (timeout
->QuadPart
< TICKS_1601_TO_1970
)
416 when
->sec
= when
->usec
= 0;
417 else if (timeout
->QuadPart
> TICKS_1601_TO_UNIX_MAX
)
418 when
->sec
= when
->usec
= INT_MAX
;
421 when
->sec
= RtlEnlargedUnsignedDivide( timeout
->QuadPart
- TICKS_1601_TO_1970
,
422 TICKSPERSEC
, &remainder
);
423 when
->usec
= remainder
/ 10;
429 /***********************************************************************
430 * NTDLL_from_server_timeout
432 * Convert a timeval struct from the server into an NTDLL timeout.
434 void NTDLL_from_server_timeout( LARGE_INTEGER
*timeout
, const abs_time_t
*when
)
436 timeout
->QuadPart
= when
->sec
* (ULONGLONG
)TICKSPERSEC
+ TICKS_1601_TO_1970
;
437 timeout
->QuadPart
+= when
->usec
* 10;
441 /******************************************************************************
442 * RtlTimeToTimeFields [NTDLL.@]
444 * Convert a time into a TIME_FIELDS structure.
447 * liTime [I] Time to convert.
448 * TimeFields [O] Destination for the converted time.
453 VOID WINAPI
RtlTimeToTimeFields(
454 const LARGE_INTEGER
*liTime
,
455 PTIME_FIELDS TimeFields
)
458 long int cleaps
, years
, yearday
, months
;
462 /* Extract millisecond from time and convert time into seconds */
463 TimeFields
->Milliseconds
=
464 (CSHORT
) (( liTime
->QuadPart
% TICKSPERSEC
) / TICKSPERMSEC
);
465 Time
= liTime
->QuadPart
/ TICKSPERSEC
;
467 /* The native version of RtlTimeToTimeFields does not take leap seconds
470 /* Split the time into days and seconds within the day */
471 Days
= Time
/ SECSPERDAY
;
472 SecondsInDay
= Time
% SECSPERDAY
;
474 /* compute time of day */
475 TimeFields
->Hour
= (CSHORT
) (SecondsInDay
/ SECSPERHOUR
);
476 SecondsInDay
= SecondsInDay
% SECSPERHOUR
;
477 TimeFields
->Minute
= (CSHORT
) (SecondsInDay
/ SECSPERMIN
);
478 TimeFields
->Second
= (CSHORT
) (SecondsInDay
% SECSPERMIN
);
480 /* compute day of week */
481 TimeFields
->Weekday
= (CSHORT
) ((EPOCHWEEKDAY
+ Days
) % DAYSPERWEEK
);
483 /* compute year, month and day of month. */
484 cleaps
=( 3 * ((4 * Days
+ 1227) / DAYSPERQUADRICENTENNIUM
) + 3 ) / 4;
485 Days
+= 28188 + cleaps
;
486 years
= (20 * Days
- 2442) / (5 * DAYSPERNORMALQUADRENNIUM
);
487 yearday
= Days
- (years
* DAYSPERNORMALQUADRENNIUM
)/4;
488 months
= (64 * yearday
) / 1959;
489 /* the result is based on a year starting on March.
490 * To convert take 12 from Januari and Februari and
491 * increase the year by one. */
493 TimeFields
->Month
= months
- 1;
494 TimeFields
->Year
= years
+ 1524;
496 TimeFields
->Month
= months
- 13;
497 TimeFields
->Year
= years
+ 1525;
499 /* calculation of day of month is based on the wonderful
500 * sequence of INT( n * 30.6): it reproduces the
501 * 31-30-31-30-31-31 month lengths exactly for small n's */
502 TimeFields
->Day
= yearday
- (1959 * months
) / 64 ;
506 /******************************************************************************
507 * RtlTimeFieldsToTime [NTDLL.@]
509 * Convert a TIME_FIELDS structure into a time.
512 * ftTimeFields [I] TIME_FIELDS structure to convert.
513 * Time [O] Destination for the converted time.
519 BOOLEAN WINAPI
RtlTimeFieldsToTime(
520 PTIME_FIELDS tfTimeFields
,
523 int month
, year
, cleaps
, day
;
525 /* FIXME: normalize the TIME_FIELDS structure here */
526 /* No, native just returns 0 (error) if the fields are not */
527 if( tfTimeFields
->Milliseconds
< 0 || tfTimeFields
->Milliseconds
> 999 ||
528 tfTimeFields
->Second
< 0 || tfTimeFields
->Second
> 59 ||
529 tfTimeFields
->Minute
< 0 || tfTimeFields
->Minute
> 59 ||
530 tfTimeFields
->Hour
< 0 || tfTimeFields
->Hour
> 23 ||
531 tfTimeFields
->Month
< 1 || tfTimeFields
->Month
> 12 ||
532 tfTimeFields
->Day
< 1 ||
533 tfTimeFields
->Day
> MonthLengths
534 [ tfTimeFields
->Month
==2 || IsLeapYear(tfTimeFields
->Year
)]
535 [ tfTimeFields
->Month
- 1] ||
536 tfTimeFields
->Year
< 1601 )
539 /* now calculate a day count from the date
540 * First start counting years from March. This way the leap days
541 * are added at the end of the year, not somewhere in the middle.
542 * Formula's become so much less complicate that way.
543 * To convert: add 12 to the month numbers of Jan and Feb, and
544 * take 1 from the year */
545 if(tfTimeFields
->Month
< 3) {
546 month
= tfTimeFields
->Month
+ 13;
547 year
= tfTimeFields
->Year
- 1;
549 month
= tfTimeFields
->Month
+ 1;
550 year
= tfTimeFields
->Year
;
552 cleaps
= (3 * (year
/ 100) + 3) / 4; /* nr of "century leap years"*/
553 day
= (36525 * year
) / 100 - cleaps
+ /* year * dayperyr, corrected */
554 (1959 * month
) / 64 + /* months * daypermonth */
555 tfTimeFields
->Day
- /* day of the month */
556 584817 ; /* zero that on 1601-01-01 */
559 Time
->QuadPart
= (((((LONGLONG
) day
* HOURSPERDAY
+
560 tfTimeFields
->Hour
) * MINSPERHOUR
+
561 tfTimeFields
->Minute
) * SECSPERMIN
+
562 tfTimeFields
->Second
) * 1000 +
563 tfTimeFields
->Milliseconds
) * TICKSPERMSEC
;
568 /***********************************************************************
569 * TIME_GetBias [internal]
571 * Helper function calculates delta local time from UTC.
574 * utc [I] The current utc time.
575 * pdaylight [I] Local daylight.
578 * The bias for the current timezone.
580 static int TIME_GetBias(time_t utc
, int *pdaylight
)
583 static time_t last_utc
;
584 static int last_bias
;
585 static int last_daylight
;
588 RtlEnterCriticalSection( &TIME_GetBias_section
);
591 *pdaylight
= last_daylight
;
595 ptm
= localtime(&utc
);
596 *pdaylight
= last_daylight
=
597 ptm
->tm_isdst
; /* daylight for local timezone */
599 ptm
->tm_isdst
= *pdaylight
; /* use local daylight, not that of Greenwich */
601 ret
= last_bias
= (int)(utc
-mktime(ptm
));
603 RtlLeaveCriticalSection( &TIME_GetBias_section
);
607 /******************************************************************************
608 * RtlLocalTimeToSystemTime [NTDLL.@]
610 * Convert a local time into system time.
613 * LocalTime [I] Local time to convert.
614 * SystemTime [O] Destination for the converted time.
617 * Success: STATUS_SUCCESS.
618 * Failure: An NTSTATUS error code indicating the problem.
620 NTSTATUS WINAPI
RtlLocalTimeToSystemTime( const LARGE_INTEGER
*LocalTime
,
621 PLARGE_INTEGER SystemTime
)
626 TRACE("(%p, %p)\n", LocalTime
, SystemTime
);
629 bias
= TIME_GetBias(gmt
, &daylight
);
631 SystemTime
->QuadPart
= LocalTime
->QuadPart
- bias
* (LONGLONG
)TICKSPERSEC
;
632 return STATUS_SUCCESS
;
635 /******************************************************************************
636 * RtlSystemTimeToLocalTime [NTDLL.@]
638 * Convert a system time into a local time.
641 * SystemTime [I] System time to convert.
642 * LocalTime [O] Destination for the converted time.
645 * Success: STATUS_SUCCESS.
646 * Failure: An NTSTATUS error code indicating the problem.
648 NTSTATUS WINAPI
RtlSystemTimeToLocalTime( const LARGE_INTEGER
*SystemTime
,
649 PLARGE_INTEGER LocalTime
)
654 TRACE("(%p, %p)\n", SystemTime
, LocalTime
);
657 bias
= TIME_GetBias(gmt
, &daylight
);
659 LocalTime
->QuadPart
= SystemTime
->QuadPart
+ bias
* (LONGLONG
)TICKSPERSEC
;
660 return STATUS_SUCCESS
;
663 /******************************************************************************
664 * RtlTimeToSecondsSince1970 [NTDLL.@]
666 * Convert a time into a count of seconds since 1970.
669 * Time [I] Time to convert.
670 * Seconds [O] Destination for the converted time.
674 * Failure: FALSE, if the resulting value will not fit in a DWORD.
676 BOOLEAN WINAPI
RtlTimeToSecondsSince1970( const LARGE_INTEGER
*Time
, LPDWORD Seconds
)
678 ULONGLONG tmp
= ((ULONGLONG
)Time
->u
.HighPart
<< 32) | Time
->u
.LowPart
;
679 tmp
= RtlLargeIntegerDivide( tmp
, TICKSPERSEC
, NULL
);
680 tmp
-= SECS_1601_TO_1970
;
681 if (tmp
> 0xffffffff) return FALSE
;
682 *Seconds
= (DWORD
)tmp
;
686 /******************************************************************************
687 * RtlTimeToSecondsSince1980 [NTDLL.@]
689 * Convert a time into a count of seconds since 1980.
692 * Time [I] Time to convert.
693 * Seconds [O] Destination for the converted time.
697 * Failure: FALSE, if the resulting value will not fit in a DWORD.
699 BOOLEAN WINAPI
RtlTimeToSecondsSince1980( const LARGE_INTEGER
*Time
, LPDWORD Seconds
)
701 ULONGLONG tmp
= ((ULONGLONG
)Time
->u
.HighPart
<< 32) | Time
->u
.LowPart
;
702 tmp
= RtlLargeIntegerDivide( tmp
, TICKSPERSEC
, NULL
);
703 tmp
-= SECS_1601_TO_1980
;
704 if (tmp
> 0xffffffff) return FALSE
;
705 *Seconds
= (DWORD
)tmp
;
709 /******************************************************************************
710 * RtlSecondsSince1970ToTime [NTDLL.@]
712 * Convert a count of seconds since 1970 to a time.
715 * Seconds [I] Time to convert.
716 * Time [O] Destination for the converted time.
721 void WINAPI
RtlSecondsSince1970ToTime( DWORD Seconds
, LARGE_INTEGER
*Time
)
723 ULONGLONG secs
= Seconds
* (ULONGLONG
)TICKSPERSEC
+ TICKS_1601_TO_1970
;
724 Time
->u
.LowPart
= (DWORD
)secs
;
725 Time
->u
.HighPart
= (DWORD
)(secs
>> 32);
728 /******************************************************************************
729 * RtlSecondsSince1980ToTime [NTDLL.@]
731 * Convert a count of seconds since 1980 to a time.
734 * Seconds [I] Time to convert.
735 * Time [O] Destination for the converted time.
740 void WINAPI
RtlSecondsSince1980ToTime( DWORD Seconds
, LARGE_INTEGER
*Time
)
742 ULONGLONG secs
= Seconds
* (ULONGLONG
)TICKSPERSEC
+ TICKS_1601_TO_1980
;
743 Time
->u
.LowPart
= (DWORD
)secs
;
744 Time
->u
.HighPart
= (DWORD
)(secs
>> 32);
747 /******************************************************************************
748 * RtlTimeToElapsedTimeFields [NTDLL.@]
750 * Convert a time to a count of elapsed seconds.
753 * Time [I] Time to convert.
754 * TimeFields [O] Destination for the converted time.
759 void WINAPI
RtlTimeToElapsedTimeFields( const LARGE_INTEGER
*Time
, PTIME_FIELDS TimeFields
)
764 time
= RtlExtendedLargeIntegerDivide( Time
->QuadPart
, TICKSPERSEC
, &rem
);
765 TimeFields
->Milliseconds
= rem
/ TICKSPERMSEC
;
767 /* time is now in seconds */
768 TimeFields
->Year
= 0;
769 TimeFields
->Month
= 0;
770 TimeFields
->Day
= RtlExtendedLargeIntegerDivide( time
, SECSPERDAY
, &rem
);
772 /* rem is now the remaining seconds in the last day */
773 TimeFields
->Second
= rem
% 60;
775 TimeFields
->Minute
= rem
% 60;
776 TimeFields
->Hour
= rem
/ 60;
779 /***********************************************************************
780 * NtQuerySystemTime [NTDLL.@]
781 * ZwQuerySystemTime [NTDLL.@]
783 * Get the current system time.
786 * Time [O] Destination for the current system time.
789 * Success: STATUS_SUCCESS.
790 * Failure: An NTSTATUS error code indicating the problem.
792 NTSTATUS WINAPI
NtQuerySystemTime( PLARGE_INTEGER Time
)
796 gettimeofday( &now
, 0 );
797 Time
->QuadPart
= now
.tv_sec
* (ULONGLONG
)TICKSPERSEC
+ TICKS_1601_TO_1970
;
798 Time
->QuadPart
+= now
.tv_usec
* 10;
799 return STATUS_SUCCESS
;
802 /***********************************************************************
803 * TIME_GetTZAsStr [internal]
805 * Helper function that returns the given timezone as a string.
808 * utc [I] The current utc time.
809 * bias [I] The bias of the current timezone.
816 * This could be done with a hash table instead of merely iterating through a
817 * table, however with the small amount of entries (60 or so) I didn't think
820 static const WCHAR
* TIME_GetTZAsStr (time_t utc
, int bias
, int dst
)
823 struct tm
*ptm
= localtime(&utc
);
826 if (!strftime (psTZName
, 7, "%Z", ptm
))
829 for (i
=0; i
<(sizeof(TZ_INFO
) / sizeof(struct tagTZ_INFO
)); i
++)
831 if ( strcmp(TZ_INFO
[i
].psTZFromUnix
, psTZName
) == 0 &&
832 TZ_INFO
[i
].bias
== bias
&&
833 TZ_INFO
[i
].dst
== dst
835 return TZ_INFO
[i
].psTZWindows
;
837 FIXME("Can't match system time zone name \"%s\", bias=%d and dst=%d "
838 "to an entry in TZ_INFO. Please add appropriate entry to "
839 "TZ_INFO and submit as patch to wine-patches\n",psTZName
,bias
,dst
);
843 /*** TIME_GetTimeZoneInfoFromReg: helper for GetTimeZoneInformation ***/
846 static int TIME_GetTimeZoneInfoFromReg(LPTIME_ZONE_INFORMATION tzinfo
)
849 KEY_VALUE_PARTIAL_INFORMATION
* KpInfo
=
850 (KEY_VALUE_PARTIAL_INFORMATION
*) buf
;
853 OBJECT_ATTRIBUTES attr
;
854 UNICODE_STRING nameW
;
856 attr
.Length
= sizeof(attr
);
857 attr
.RootDirectory
= 0;
858 attr
.ObjectName
= &nameW
;
860 attr
.SecurityDescriptor
= NULL
;
861 attr
.SecurityQualityOfService
= NULL
;
862 RtlInitUnicodeString( &nameW
, TZInformationKeyW
);
863 if (!NtOpenKey( &hkey
, KEY_ALL_ACCESS
, &attr
)) {
865 #define GTZIFR_N( valkey, tofield) \
866 RtlInitUnicodeString( &nameW, valkey );\
867 if (!NtQueryValueKey( hkey, &nameW, KeyValuePartialInformation, KpInfo,\
868 sizeof(buf), &size )) { \
869 if( size >= (sizeof((tofield)) + \
870 offsetof(KEY_VALUE_PARTIAL_INFORMATION,Data))); { \
872 KpInfo->Data, sizeof(tofield)); \
875 #define GTZIFR_S( valkey, tofield) \
876 RtlInitUnicodeString( &nameW, valkey );\
877 if (!NtQueryValueKey( hkey, &nameW, KeyValuePartialInformation, KpInfo,\
878 sizeof(buf), &size )) { \
879 size_t len = (strlenW( (WCHAR*)KpInfo->Data ) + 1) * sizeof(WCHAR); \
880 if (len > sizeof(tofield)) len = sizeof(tofield); \
881 memcpy( tofield, KpInfo->Data, len ); \
882 tofield[(len/sizeof(WCHAR))-1] = 0; \
885 GTZIFR_N( TZStandardStartW
, tzinfo
->StandardDate
)
886 GTZIFR_N( TZDaylightStartW
, tzinfo
->DaylightDate
)
887 GTZIFR_N( TZBiasW
, tzinfo
->Bias
)
888 GTZIFR_N( TZStandardBiasW
, tzinfo
->StandardBias
)
889 GTZIFR_N( TZDaylightBiasW
, tzinfo
->DaylightBias
)
890 GTZIFR_S( TZStandardNameW
, tzinfo
->StandardName
)
891 GTZIFR_S( TZDaylightNameW
, tzinfo
->DaylightName
)
901 /***********************************************************************
902 * RtlQueryTimeZoneInformation [NTDLL.@]
904 * Get information about the current timezone.
907 * tzinfo [O] Destination for the retrieved timezone info.
910 * Success: STATUS_SUCCESS.
911 * Failure: An NTSTATUS error code indicating the problem.
913 NTSTATUS WINAPI
RtlQueryTimeZoneInformation(LPTIME_ZONE_INFORMATION tzinfo
)
919 memset(tzinfo
, 0, sizeof(TIME_ZONE_INFORMATION
));
921 if( !TIME_GetTimeZoneInfoFromReg(tzinfo
)) {
924 bias
= TIME_GetBias(gmt
, &daylight
);
926 tzinfo
->Bias
= -bias
/ 60;
927 tzinfo
->StandardBias
= 0;
928 tzinfo
->DaylightBias
= -60;
929 tzinfo
->StandardName
[0]='\0';
930 tzinfo
->DaylightName
[0]='\0';
931 psTZ
= TIME_GetTZAsStr (gmt
, (-bias
/60), daylight
);
932 if (psTZ
) strcpyW( tzinfo
->StandardName
, psTZ
);
934 return STATUS_SUCCESS
;
937 /***********************************************************************
938 * RtlSetTimeZoneInformation [NTDLL.@]
940 * Set the current time zone information.
943 * tzinfo [I] Timezone information to set.
946 * Success: STATUS_SUCCESS.
947 * Failure: An NTSTATUS error code indicating the problem.
950 * Uses the obsolete unix timezone structure and tz_dsttime member.
952 NTSTATUS WINAPI
RtlSetTimeZoneInformation( const TIME_ZONE_INFORMATION
*tzinfo
)
954 #ifdef HAVE_SETTIMEOFDAY
957 tz
.tz_minuteswest
= tzinfo
->Bias
;
959 tz
.tz_dsttime
= DST_NONE
;
963 if(!settimeofday(NULL
, &tz
))
964 return STATUS_SUCCESS
;
966 return STATUS_PRIVILEGE_NOT_HELD
;
969 /***********************************************************************
970 * NtSetSystemTime [NTDLL.@]
971 * ZwSetSystemTime [NTDLL.@]
973 * Set the system time.
976 * NewTime [I] The time to set.
977 * OldTime [O] Optional destination for the previous system time.
980 * Success: STATUS_SUCCESS.
981 * Failure: An NTSTATUS error code indicating the problem.
983 NTSTATUS WINAPI
NtSetSystemTime(const LARGE_INTEGER
*NewTime
, LARGE_INTEGER
*OldTime
)
993 /* Return the old time if necessary */
995 NtQuerySystemTime(OldTime
);
997 RtlTimeToTimeFields(NewTime
, &tf
);
999 /* call gettimeofday to get the current timezone */
1000 gettimeofday(&tv
, &tz
);
1002 /* get delta local time from utc */
1003 bias
= TIME_GetBias(oldsec
, &dst
);
1005 /* get the number of seconds */
1006 t
.tm_sec
= tf
.Second
;
1007 t
.tm_min
= tf
.Minute
;
1008 t
.tm_hour
= tf
.Hour
;
1010 t
.tm_mon
= tf
.Month
- 1;
1011 t
.tm_year
= tf
.Year
- 1900;
1014 /* correct for timezone and daylight */
1017 /* set the new time */
1019 tv
.tv_usec
= tf
.Milliseconds
* 1000;
1021 /* error and sanity check*/
1022 if(sec
== (time_t)-1 || abs((int)(sec
-oldsec
)) > SETTIME_MAX_ADJUST
) {
1025 #ifdef HAVE_SETTIMEOFDAY
1026 err
= settimeofday(&tv
, NULL
); /* 0 is OK, -1 is error */
1028 return STATUS_SUCCESS
;
1034 ERR("Cannot set time to %d/%d/%d %d:%d:%d Time adjustment %ld %s\n",
1035 tf
.Year
, tf
.Month
, tf
.Day
, tf
.Hour
, tf
.Minute
, tf
.Second
,
1037 err
== -1 ? "No Permission"
1038 : sec
== (time_t)-1 ? "" : "is too large." );
1041 return STATUS_INVALID_PARAMETER
;
1043 return STATUS_PRIVILEGE_NOT_HELD
;
1045 return STATUS_NOT_IMPLEMENTED
;