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 {'S','A',' ','E','a','s','t','e','r','n',' ','S','t','a','n','d','a','r',
152 'd',' ','T','i','m','e','\0'}, 180, 0},
154 {'G','r','e','e','n','l','a','n','d',' ','S','t','a','n','d','a','r','d',
155 ' ','T','i','m','e','\0'}, 120, 1},
157 {'M','i','d','-','A','t','l','a','n','t','i','c',' ','S','t','a','n','d',
158 'a','r','d',' ','T','i','m','e','\0'}, 120, 0},
160 {'A','z','o','r','e','s',' ','S','t','a','n','d','a','r','d',' ','T','i',
161 'm','e','\0'}, 0, 1},
163 {'C','a','p','e',' ','V','e','r','d','e',' ','S','t','a','n','d','a','r',
164 'd',' ','T','i','m','e','\0'}, 60, 0},
166 {'G','r','e','e','n','w','i','c','h',' ','S','t','a','n','d','a','r','d',
167 ' ','T','i','m','e','\0'}, 0, 0},
169 {'G','M','T',' ','S','t','a','n','d','a','r','d',' ','T','i','m','e','\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 {'C','e','n','t','r','a','l',' ','E','u','r','o','p','e','a','n',' ',
179 'T','i','m','e','\0'}, -60, 0},
181 {'C','e','n','t','r','a','l',' ','E','u','r','o','p','e',' ','S','t','a',
182 'n','d','a','r','d',' ','T','i','m','e','\0'}, -120, 1},
184 {'C','e','n','t','r','a','l',' ','E','u','r','o','p','e',' ','S','t','a',
185 'n','d','a','r','d',' ','T','i','m','e','\0'}, -60, 0},
187 {'C','e','n','t','r','a','l',' ','E','u','r','o','p','e',' ','D','a','y',
188 'l','i','g','h','t',' ','T','i','m','e','\0'}, -120, 1},
190 {'W','.',' ','C','e','n','t','r','a','l',' ','A','f','r','i','c','a',' ',
191 'S','t','a','n','d','a','r','d',' ','T','i','m','e','\0'}, -60, 0},
193 {'E','.',' ','E','u','r','o','p','e',' ','S','t','a','n','d','a','r','d',
194 ' ','T','i','m','e','\0'}, -180, 1},
196 {'E','g','y','p','t',' ','S','t','a','n','d','a','r','d',' ','T','i','m',
199 {'C','e','n','t','r','a','l',' ','A','f','r','i','c','a','n',' '
200 ,'T','i','m','e','\0'}, -120, 0},
202 {'S','o','u','t','h',' ','A','f','r','i','c','a',' ','S','t','a','n','d',
203 'a','r','d',' ','T','i','m','e','\0'}, -120, 0},
205 {'I','s','r','a','e','l',' ','S','t','a','n','d','a','r','d',' ','T','i',
206 'm','e','\0'}, -120, 0},
208 {'R','u','s','s','i','a','n',' ','S','t','a','n','d','a','r','d',' ','T',
209 'i','m','e','\0'}, -180, 0},
211 {'A','r','a','b','i','c',' ','S','t','a','n','d','a','r','d',' ','T','i',
212 'm','e','\0'}, -240, 1},
214 {'A','r','a','b',' ','S','t','a','n','d','a','r','d',' ','T','i','m','e',
217 {'R','u','s','s','i','a','n',' ','S','t','a','n','d','a','r','d',' ','T',
218 'i','m','e','\0'}, -240, 1},
220 {'E','.',' ','A','f','r','i','c','a',' ','S','t','a','n','d','a','r','d',
221 ' ','T','i','m','e','\0'}, -180, 0},
223 {'I','r','a','n',' ','S','t','a','n','d','a','r','d',' ','T','i','m','e',
226 {'A','r','a','b','i','a','n',' ','S','t','a','n','d','a','r','d',' ','T',
227 'i','m','e','\0'}, -240, 0},
229 {'C','a','u','c','a','s','u','s',' ','S','t','a','n','d','a','r','d',' ',
230 'T','i','m','e','\0'}, -300, 1},
232 {'A','f','g','h','a','n','i','s','t','a','n',' ','S','t','a','n','d','a',
233 'r','d',' ','T','i','m','e','\0'}, -270, 0},
235 {'E','k','a','t','e','r','i','n','b','u','r','g',' ','S','t','a','n','d',
236 'a','r','d',' ','T','i','m','e','\0'}, -360, 1},
238 {'W','e','s','t',' ','A','s','i','a',' ','S','t','a','n','d','a','r','d',
239 ' ','T','i','m','e','\0'}, -300, 0},
241 {'I','n','d','i','a',' ','S','t','a','n','d','a','r','d',' ','T','i','m',
244 {'N','e','p','a','l',' ','S','t','a','n','d','a','r','d',' ','T','i','m',
247 {'N','.',' ','C','e','n','t','r','a','l',' ','A','s','i','a',' ','S','t',
248 'a','n','d','a','r','d',' ','T','i','m','e','\0'}, -420, 1},
250 {'C','e','n','t','r','a','l',' ','A','s','i','a',' ','S','t','a','n','d',
251 'a','r','d',' ','T','i','m','e','\0'}, -360, 0},
253 {'S','r','i',' ','L','a','n','k','a',' ','S','t','a','n','d','a','r','d',
254 ' ','T','i','m','e','\0'}, -360, 0},
256 {'M','y','a','n','m','a','r',' ','S','t','a','n','d','a','r','d',' ','T',
257 'i','m','e','\0'}, -390, 0},
259 {'S','E',' ','A','s','i','a',' ','S','t','a','n','d','a','r','d',' ','T',
260 'i','m','e','\0'}, -420, 0},
262 {'N','o','r','t','h',' ','A','s','i','a',' ','S','t','a','n','d','a','r',
263 'd',' ','T','i','m','e','\0'}, -480, 1},
265 {'C','h','i','n','a',' ','S','t','a','n','d','a','r','d',' ','T','i','m',
268 {'N','o','r','t','h',' ','A','s','i','a',' ','E','a','s','t',' ','S','t',
269 'a','n','d','a','r','d',' ','T','i','m','e','\0'}, -540, 1},
271 {'M','a','l','a','y',' ','P','e','n','i','n','s','u','l','a',' ','S','t',
272 'a','n','d','a','r','d',' ','T','i','m','e','\0'}, -480, 0},
274 {'W','.',' ','A','u','s','t','r','a','l','i','a',' ','S','t','a','n','d',
275 'a','r','d',' ','T','i','m','e','\0'}, -480, 0},
277 {'T','o','k','y','o',' ','S','t','a','n','d','a','r','d',' ','T','i','m',
280 {'K','o','r','e','a',' ','S','t','a','n','d','a','r','d',' ','T','i','m',
283 {'Y','a','k','u','t','s','k',' ','S','t','a','n','d','a','r','d',' ','T',
284 'i','m','e','\0'}, -600, 1},
286 {'C','e','n','.',' ','A','u','s','t','r','a','l','i','a',' ','S','t','a',
287 'n','d','a','r','d',' ','T','i','m','e','\0'}, -570, 0},
289 {'E','.',' ','A','u','s','t','r','a','l','i','a',' ','S','t','a','n','d',
290 'a','r','d',' ','T','i','m','e','\0'}, -600, 0},
292 {'W','e','s','t',' ','P','a','c','i','f','i','c',' ','S','t','a','n','d',
293 'a','r','d',' ','T','i','m','e','\0'}, -600, 0},
295 {'V','l','a','d','i','v','o','s','t','o','k',' ','S','t','a','n','d','a',
296 'r','d',' ','T','i','m','e','\0'}, -660, 1},
298 {'C','e','n','t','r','a','l',' ','P','a','c','i','f','i','c',' ','S','t',
299 'a','n','d','a','r','d',' ','T','i','m','e','\0'}, -720, 1},
301 {'N','e','w',' ','Z','e','a','l','a','n','d',' ','S','t','a','n','d','a',
302 'r','d',' ','T','i','m','e','\0'}, -720, 0},
304 {'F','i','j','i',' ','S','t','a','n','d','a','r','d',' ','T','i','m','e',
307 {'T','o','n','g','a',' ','S','t','a','n','d','a','r','d',' ','T','i','m',
310 {'N','o','v','o','s','i','b','i','r','s','k',' ','S','t','a','n','d','a',
311 'r','d',' ','T','i','m','e','\0'}, -360, 1}
314 #define TICKSPERSEC 10000000
315 #define TICKSPERMSEC 10000
316 #define SECSPERDAY 86400
317 #define SECSPERHOUR 3600
318 #define SECSPERMIN 60
319 #define MINSPERHOUR 60
320 #define HOURSPERDAY 24
321 #define EPOCHWEEKDAY 1 /* Jan 1, 1601 was Monday */
322 #define DAYSPERWEEK 7
323 #define EPOCHYEAR 1601
324 #define DAYSPERNORMALYEAR 365
325 #define DAYSPERLEAPYEAR 366
326 #define MONSPERYEAR 12
327 #define DAYSPERQUADRICENTENNIUM (365 * 400 + 97)
328 #define DAYSPERNORMALCENTURY (365 * 100 + 24)
329 #define DAYSPERNORMALQUADRENNIUM (365 * 4 + 1)
331 /* 1601 to 1970 is 369 years plus 89 leap days */
332 #define SECS_1601_TO_1970 ((369 * 365 + 89) * (ULONGLONG)SECSPERDAY)
333 #define TICKS_1601_TO_1970 (SECS_1601_TO_1970 * TICKSPERSEC)
334 /* 1601 to 1980 is 379 years plus 91 leap days */
335 #define SECS_1601_TO_1980 ((379 * 365 + 91) * (ULONGLONG)SECSPERDAY)
336 #define TICKS_1601_TO_1980 (SECS_1601_TO_1980 * TICKSPERSEC)
337 /* max ticks that can be represented as Unix time */
338 #define TICKS_1601_TO_UNIX_MAX ((SECS_1601_TO_1970 + INT_MAX) * TICKSPERSEC)
341 static const int MonthLengths[2][MONSPERYEAR] =
343 { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
344 { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
347 static inline int IsLeapYear(int Year)
349 return Year % 4 == 0 && (Year % 100 != 0 || Year % 400 == 0) ? 1 : 0;
352 /***********************************************************************
353 * NTDLL_get_server_timeout
355 * Convert a NTDLL timeout into a timeval struct to send to the server.
357 void NTDLL_get_server_timeout( abs_time_t *when, const LARGE_INTEGER *timeout )
361 if (!timeout) /* infinite timeout */
363 when->sec = when->usec = 0;
365 else if (timeout->QuadPart <= 0) /* relative timeout */
369 if (-timeout->QuadPart > (LONGLONG)INT_MAX * TICKSPERSEC)
370 when->sec = when->usec = INT_MAX;
373 ULONG sec = RtlEnlargedUnsignedDivide( -timeout->QuadPart, TICKSPERSEC, &remainder );
374 gettimeofday( &tv, 0 );
375 when->sec = tv.tv_sec + sec;
376 if ((when->usec = tv.tv_usec + (remainder / 10)) >= 1000000)
378 when->usec -= 1000000;
381 if (when->sec < tv.tv_sec) /* overflow */
382 when->sec = when->usec = INT_MAX;
385 else /* absolute time */
387 if (timeout->QuadPart < TICKS_1601_TO_1970)
388 when->sec = when->usec = 0;
389 else if (timeout->QuadPart > TICKS_1601_TO_UNIX_MAX)
390 when->sec = when->usec = INT_MAX;
393 when->sec = RtlEnlargedUnsignedDivide( timeout->QuadPart - TICKS_1601_TO_1970,
394 TICKSPERSEC, &remainder );
395 when->usec = remainder / 10;
401 /***********************************************************************
402 * NTDLL_from_server_timeout
404 * Convert a timeval struct from the server into an NTDLL timeout.
406 void NTDLL_from_server_timeout( LARGE_INTEGER *timeout, const abs_time_t *when )
408 timeout->QuadPart = when->sec * (ULONGLONG)TICKSPERSEC + TICKS_1601_TO_1970;
409 timeout->QuadPart += when->usec * 10;
413 /******************************************************************************
414 * RtlTimeToTimeFields [NTDLL.@]
416 * Convert a time into a TIME_FIELDS structure.
419 * liTime [I] Time to convert.
420 * TimeFields [O] Destination for the converted time.
425 VOID WINAPI RtlTimeToTimeFields(
426 const LARGE_INTEGER *liTime,
427 PTIME_FIELDS TimeFields)
430 long int cleaps, years, yearday, months;
434 /* Extract millisecond from time and convert time into seconds */
435 TimeFields->Milliseconds =
436 (CSHORT) (( liTime->QuadPart % TICKSPERSEC) / TICKSPERMSEC);
437 Time = liTime->QuadPart / TICKSPERSEC;
439 /* The native version of RtlTimeToTimeFields does not take leap seconds
442 /* Split the time into days and seconds within the day */
443 Days = Time / SECSPERDAY;
444 SecondsInDay = Time % SECSPERDAY;
446 /* compute time of day */
447 TimeFields->Hour = (CSHORT) (SecondsInDay / SECSPERHOUR);
448 SecondsInDay = SecondsInDay % SECSPERHOUR;
449 TimeFields->Minute = (CSHORT) (SecondsInDay / SECSPERMIN);
450 TimeFields->Second = (CSHORT) (SecondsInDay % SECSPERMIN);
452 /* compute day of week */
453 TimeFields->Weekday = (CSHORT) ((EPOCHWEEKDAY + Days) % DAYSPERWEEK);
455 /* compute year, month and day of month. */
456 cleaps=( 3 * ((4 * Days + 1227) / DAYSPERQUADRICENTENNIUM) + 3 ) / 4;
457 Days += 28188 + cleaps;
458 years = (20 * Days - 2442) / (5 * DAYSPERNORMALQUADRENNIUM);
459 yearday = Days - (years * DAYSPERNORMALQUADRENNIUM)/4;
460 months = (64 * yearday) / 1959;
461 /* the result is based on a year starting on March.
462 * To convert take 12 from Januari and Februari and
463 * increase the year by one. */
465 TimeFields->Month = months - 1;
466 TimeFields->Year = years + 1524;
468 TimeFields->Month = months - 13;
469 TimeFields->Year = years + 1525;
471 /* calculation of day of month is based on the wonderful
472 * sequence of INT( n * 30.6): it reproduces the
473 * 31-30-31-30-31-31 month lengths exactly for small n's */
474 TimeFields->Day = yearday - (1959 * months) / 64 ;
478 /******************************************************************************
479 * RtlTimeFieldsToTime [NTDLL.@]
481 * Convert a TIME_FIELDS structure into a time.
484 * ftTimeFields [I] TIME_FIELDS structure to convert.
485 * Time [O] Destination for the converted time.
491 BOOLEAN WINAPI RtlTimeFieldsToTime(
492 PTIME_FIELDS tfTimeFields,
495 int month, year, cleaps, day;
497 /* FIXME: normalize the TIME_FIELDS structure here */
498 /* No, native just returns 0 (error) if the fields are not */
499 if( tfTimeFields->Milliseconds< 0 || tfTimeFields->Milliseconds > 999 ||
500 tfTimeFields->Second < 0 || tfTimeFields->Second > 59 ||
501 tfTimeFields->Minute < 0 || tfTimeFields->Minute > 59 ||
502 tfTimeFields->Hour < 0 || tfTimeFields->Hour > 23 ||
503 tfTimeFields->Month < 1 || tfTimeFields->Month > 12 ||
504 tfTimeFields->Day < 1 ||
505 tfTimeFields->Day > MonthLengths
506 [ tfTimeFields->Month ==2 || IsLeapYear(tfTimeFields->Year)]
507 [ tfTimeFields->Month - 1] ||
508 tfTimeFields->Year < 1601 )
511 /* now calculate a day count from the date
512 * First start counting years from March. This way the leap days
513 * are added at the end of the year, not somewhere in the middle.
514 * Formula's become so much less complicate that way.
515 * To convert: add 12 to the month numbers of Jan and Feb, and
516 * take 1 from the year */
517 if(tfTimeFields->Month < 3) {
518 month = tfTimeFields->Month + 13;
519 year = tfTimeFields->Year - 1;
521 month = tfTimeFields->Month + 1;
522 year = tfTimeFields->Year;
524 cleaps = (3 * (year / 100) + 3) / 4; /* nr of "century leap years"*/
525 day = (36525 * year) / 100 - cleaps + /* year * dayperyr, corrected */
526 (1959 * month) / 64 + /* months * daypermonth */
527 tfTimeFields->Day - /* day of the month */
528 584817 ; /* zero that on 1601-01-01 */
531 Time->QuadPart = (((((LONGLONG) day * HOURSPERDAY +
532 tfTimeFields->Hour) * MINSPERHOUR +
533 tfTimeFields->Minute) * SECSPERMIN +
534 tfTimeFields->Second ) * 1000 +
535 tfTimeFields->Milliseconds ) * TICKSPERMSEC;
540 /***********************************************************************
541 * TIME_GetBias [internal]
543 * Helper function calculates delta local time from UTC.
546 * utc [I] The current utc time.
547 * pdaylight [I] Local daylight.
550 * The bias for the current timezone.
552 static int TIME_GetBias(time_t utc, int *pdaylight)
555 static time_t last_utc;
556 static int last_bias;
557 static int last_daylight;
560 RtlEnterCriticalSection( &TIME_GetBias_section );
563 *pdaylight = last_daylight;
567 ptm = localtime(&utc);
568 *pdaylight = last_daylight =
569 ptm->tm_isdst; /* daylight for local timezone */
571 ptm->tm_isdst = *pdaylight; /* use local daylight, not that of Greenwich */
573 ret = last_bias = (int)(utc-mktime(ptm));
575 RtlLeaveCriticalSection( &TIME_GetBias_section );
579 /******************************************************************************
580 * RtlLocalTimeToSystemTime [NTDLL.@]
582 * Convert a local time into system time.
585 * LocalTime [I] Local time to convert.
586 * SystemTime [O] Destination for the converted time.
589 * Success: STATUS_SUCCESS.
590 * Failure: An NTSTATUS error code indicating the problem.
592 NTSTATUS WINAPI RtlLocalTimeToSystemTime( const LARGE_INTEGER *LocalTime,
593 PLARGE_INTEGER SystemTime)
598 TRACE("(%p, %p)\n", LocalTime, SystemTime);
601 bias = TIME_GetBias(gmt, &daylight);
603 SystemTime->QuadPart = LocalTime->QuadPart - bias * (LONGLONG)10000000;
604 return STATUS_SUCCESS;
607 /******************************************************************************
608 * RtlSystemTimeToLocalTime [NTDLL.@]
610 * Convert a system time into a local time.
613 * SystemTime [I] System time to convert.
614 * LocalTime [O] Destination for the converted time.
617 * Success: STATUS_SUCCESS.
618 * Failure: An NTSTATUS error code indicating the problem.
620 NTSTATUS WINAPI RtlSystemTimeToLocalTime( const LARGE_INTEGER *SystemTime,
621 PLARGE_INTEGER LocalTime )
626 TRACE("(%p, %p)\n", SystemTime, LocalTime);
629 bias = TIME_GetBias(gmt, &daylight);
631 LocalTime->QuadPart = SystemTime->QuadPart + bias * (LONGLONG)10000000;
632 return STATUS_SUCCESS;
635 /******************************************************************************
636 * RtlTimeToSecondsSince1970 [NTDLL.@]
638 * Convert a time into a count of seconds since 1970.
641 * Time [I] Time to convert.
642 * Seconds [O] Destination for the converted time.
646 * Failure: FALSE, if the resulting value will not fit in a DWORD.
648 BOOLEAN WINAPI RtlTimeToSecondsSince1970( const LARGE_INTEGER *Time, LPDWORD Seconds )
650 ULONGLONG tmp = ((ULONGLONG)Time->u.HighPart << 32) | Time->u.LowPart;
651 tmp = RtlLargeIntegerDivide( tmp, 10000000, NULL );
652 tmp -= SECS_1601_TO_1970;
653 if (tmp > 0xffffffff) return FALSE;
654 *Seconds = (DWORD)tmp;
658 /******************************************************************************
659 * RtlTimeToSecondsSince1980 [NTDLL.@]
661 * Convert a time into a count of seconds since 1980.
664 * Time [I] Time to convert.
665 * Seconds [O] Destination for the converted time.
669 * Failure: FALSE, if the resulting value will not fit in a DWORD.
671 BOOLEAN WINAPI RtlTimeToSecondsSince1980( const LARGE_INTEGER *Time, LPDWORD Seconds )
673 ULONGLONG tmp = ((ULONGLONG)Time->u.HighPart << 32) | Time->u.LowPart;
674 tmp = RtlLargeIntegerDivide( tmp, 10000000, NULL );
675 tmp -= SECS_1601_TO_1980;
676 if (tmp > 0xffffffff) return FALSE;
677 *Seconds = (DWORD)tmp;
681 /******************************************************************************
682 * RtlSecondsSince1970ToTime [NTDLL.@]
684 * Convert a count of seconds since 1970 to a time.
687 * Seconds [I] Time to convert.
688 * Time [O] Destination for the converted time.
693 void WINAPI RtlSecondsSince1970ToTime( DWORD Seconds, LARGE_INTEGER *Time )
695 ULONGLONG secs = Seconds * (ULONGLONG)TICKSPERSEC + TICKS_1601_TO_1970;
696 Time->u.LowPart = (DWORD)secs;
697 Time->u.HighPart = (DWORD)(secs >> 32);
700 /******************************************************************************
701 * RtlSecondsSince1980ToTime [NTDLL.@]
703 * Convert a count of seconds since 1980 to a time.
706 * Seconds [I] Time to convert.
707 * Time [O] Destination for the converted time.
712 void WINAPI RtlSecondsSince1980ToTime( DWORD Seconds, LARGE_INTEGER *Time )
714 ULONGLONG secs = Seconds * (ULONGLONG)TICKSPERSEC + TICKS_1601_TO_1980;
715 Time->u.LowPart = (DWORD)secs;
716 Time->u.HighPart = (DWORD)(secs >> 32);
719 /******************************************************************************
720 * RtlTimeToElapsedTimeFields [NTDLL.@]
722 * Convert a time to a count of elapsed seconds.
725 * Time [I] Time to convert.
726 * TimeFields [O] Destination for the converted time.
731 void WINAPI RtlTimeToElapsedTimeFields( const LARGE_INTEGER *Time, PTIME_FIELDS TimeFields )
736 time = RtlExtendedLargeIntegerDivide( Time->QuadPart, TICKSPERSEC, &rem );
737 TimeFields->Milliseconds = rem / TICKSPERMSEC;
739 /* time is now in seconds */
740 TimeFields->Year = 0;
741 TimeFields->Month = 0;
742 TimeFields->Day = RtlExtendedLargeIntegerDivide( time, SECSPERDAY, &rem );
744 /* rem is now the remaining seconds in the last day */
745 TimeFields->Second = rem % 60;
747 TimeFields->Minute = rem % 60;
748 TimeFields->Hour = rem / 60;
751 /***********************************************************************
752 * NtQuerySystemTime [NTDLL.@]
753 * ZwQuerySystemTime [NTDLL.@]
755 * Get the current system time.
758 * Time [O] Destination for the current system time.
761 * Success: STATUS_SUCCESS.
762 * Failure: An NTSTATUS error code indicating the problem.
764 NTSTATUS WINAPI NtQuerySystemTime( PLARGE_INTEGER Time )
768 gettimeofday( &now, 0 );
769 Time->QuadPart = now.tv_sec * (ULONGLONG)TICKSPERSEC + TICKS_1601_TO_1970;
770 Time->QuadPart += now.tv_usec * 10;
771 return STATUS_SUCCESS;
774 /***********************************************************************
775 * TIME_GetTZAsStr [internal]
777 * Helper function that returns the given timezone as a string.
780 * utc [I] The current utc time.
781 * bias [I] The bias of the current timezone.
788 * This could be done with a hash table instead of merely iterating through a
789 * table, however with the small amount of entries (60 or so) I didn't think
792 static const WCHAR* TIME_GetTZAsStr (time_t utc, int bias, int dst)
795 struct tm *ptm = localtime(&utc);
798 if (!strftime (psTZName, 7, "%Z", ptm))
801 for (i=0; i<(sizeof(TZ_INFO) / sizeof(struct tagTZ_INFO)); i++)
803 if ( strcmp(TZ_INFO[i].psTZFromUnix, psTZName) == 0 &&
804 TZ_INFO[i].bias == bias &&
805 TZ_INFO[i].dst == dst
807 return TZ_INFO[i].psTZWindows;
809 FIXME("Can't match system time zone name \"%s\" to an entry in TZ_INFO\n",psTZName);
810 FIXME(" Please add appropriate entry to TZ_INFO and submit as patch to wine-patches\n");
814 /*** TIME_GetTimeZoneInfoFromReg: helper for GetTimeZoneInformation ***/
817 static int TIME_GetTimeZoneInfoFromReg(LPTIME_ZONE_INFORMATION tzinfo)
820 KEY_VALUE_PARTIAL_INFORMATION * KpInfo =
821 (KEY_VALUE_PARTIAL_INFORMATION *) buf;
824 OBJECT_ATTRIBUTES attr;
825 UNICODE_STRING nameW;
827 attr.Length = sizeof(attr);
828 attr.RootDirectory = 0;
829 attr.ObjectName = &nameW;
831 attr.SecurityDescriptor = NULL;
832 attr.SecurityQualityOfService = NULL;
833 RtlInitUnicodeString( &nameW, TZInformationKeyW);
834 if (!NtOpenKey( &hkey, KEY_ALL_ACCESS, &attr )) {
836 #define GTZIFR_N( valkey, tofield) \
837 RtlInitUnicodeString( &nameW, valkey );\
838 if (!NtQueryValueKey( hkey, &nameW, KeyValuePartialInformation, KpInfo,\
839 sizeof(buf), &size )) { \
840 if( size >= (sizeof((tofield)) + \
841 offsetof(KEY_VALUE_PARTIAL_INFORMATION,Data))); { \
843 KpInfo->Data, sizeof(tofield)); \
846 #define GTZIFR_S( valkey, tofield) \
847 RtlInitUnicodeString( &nameW, valkey );\
848 if (!NtQueryValueKey( hkey, &nameW, KeyValuePartialInformation, KpInfo,\
849 sizeof(buf), &size )) { \
850 strncpyW( tofield, (WCHAR*) KpInfo->Data, \
851 sizeof( tofield) / sizeof(WCHAR) ); \
854 GTZIFR_N( TZStandardStartW, tzinfo->StandardDate)
855 GTZIFR_N( TZDaylightStartW, tzinfo->DaylightDate)
856 GTZIFR_N( TZBiasW, tzinfo->Bias)
857 GTZIFR_N( TZStandardBiasW, tzinfo->StandardBias)
858 GTZIFR_N( TZDaylightBiasW, tzinfo->DaylightBias)
859 GTZIFR_S( TZStandardNameW, tzinfo->StandardName)
860 GTZIFR_S( TZDaylightNameW, tzinfo->DaylightName)
870 /***********************************************************************
871 * RtlQueryTimeZoneInformation [NTDLL.@]
873 * Get information about the current timezone.
876 * tzinfo [O] Destination for the retrieved timezone info.
879 * Success: STATUS_SUCCESS.
880 * Failure: An NTSTATUS error code indicating the problem.
882 NTSTATUS WINAPI RtlQueryTimeZoneInformation(LPTIME_ZONE_INFORMATION tzinfo)
888 memset(tzinfo, 0, sizeof(TIME_ZONE_INFORMATION));
890 if( !TIME_GetTimeZoneInfoFromReg(tzinfo)) {
893 bias = TIME_GetBias(gmt, &daylight);
895 tzinfo->Bias = -bias / 60;
896 tzinfo->StandardBias = 0;
897 tzinfo->DaylightBias = -60;
898 tzinfo->StandardName[0]='\0';
899 tzinfo->DaylightName[0]='\0';
900 psTZ = TIME_GetTZAsStr (gmt, (-bias/60), daylight);
901 if (psTZ) strcpyW( tzinfo->StandardName, psTZ );
903 return STATUS_SUCCESS;
906 /***********************************************************************
907 * RtlSetTimeZoneInformation [NTDLL.@]
909 * Set the current time zone information.
912 * tzinfo [I] Timezone information to set.
915 * Success: STATUS_SUCCESS.
916 * Failure: An NTSTATUS error code indicating the problem.
919 * Uses the obsolete unix timezone structure and tz_dsttime member.
921 NTSTATUS WINAPI RtlSetTimeZoneInformation( const TIME_ZONE_INFORMATION *tzinfo )
923 #ifdef HAVE_SETTIMEOFDAY
926 tz.tz_minuteswest = tzinfo->Bias;
928 tz.tz_dsttime = DST_NONE;
932 if(!settimeofday(NULL, &tz))
933 return STATUS_SUCCESS;
935 return STATUS_PRIVILEGE_NOT_HELD;
938 /***********************************************************************
939 * NtSetSystemTime [NTDLL.@]
940 * ZwSetSystemTime [NTDLL.@]
942 * Set the system time.
945 * NewTime [I] The time to set.
946 * OldTime [O] Optional destination for the previous system time.
949 * Success: STATUS_SUCCESS.
950 * Failure: An NTSTATUS error code indicating the problem.
952 NTSTATUS WINAPI NtSetSystemTime(const LARGE_INTEGER *NewTime, LARGE_INTEGER *OldTime)
962 /* Return the old time if necessary */
964 NtQuerySystemTime(OldTime);
966 RtlTimeToTimeFields(NewTime, &tf);
968 /* call gettimeofday to get the current timezone */
969 gettimeofday(&tv, &tz);
971 /* get delta local time from utc */
972 bias = TIME_GetBias(oldsec, &dst);
974 /* get the number of seconds */
975 t.tm_sec = tf.Second;
976 t.tm_min = tf.Minute;
979 t.tm_mon = tf.Month - 1;
980 t.tm_year = tf.Year - 1900;
983 /* correct for timezone and daylight */
986 /* set the new time */
988 tv.tv_usec = tf.Milliseconds * 1000;
990 /* error and sanity check*/
991 if(sec == (time_t)-1 || abs((int)(sec-oldsec)) > SETTIME_MAX_ADJUST) {
994 #ifdef HAVE_SETTIMEOFDAY
995 err = settimeofday(&tv, NULL); /* 0 is OK, -1 is error */
997 return STATUS_SUCCESS;
1003 ERR("Cannot set time to %d/%d/%d %d:%d:%d Time adjustment %ld %s\n",
1004 tf.Year, tf.Month, tf.Day, tf.Hour, tf.Minute, tf.Second,
1006 err == -1 ? "No Permission"
1007 : sec == (time_t)-1 ? "" : "is too large." );
1010 return STATUS_INVALID_PARAMETER;
1012 return STATUS_PRIVILEGE_NOT_HELD;
1014 return STATUS_NOT_IMPLEMENTED;