Speed up RtlTimeToTimeFields.
[wine/multimedia.git] / dlls / ntdll / time.c
blobbd10c5085545e1b530c183fe45df4561dd802638
1 /*
2 * Nt time functions.
4 * RtlTimeToTimeFields, RtlTimeFieldsToTime and defines are taken from ReactOS and
5 * adapted to wine with special permissions of the author. This code is
6 * Copyright 2002 Rex Jolliff (rex@lvcablemodem.com)
8 * Copyright 1999 Juergen Schmied
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 #include "config.h"
26 #include "wine/port.h"
28 #include <stdarg.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <time.h>
32 #ifdef HAVE_SYS_TIME_H
33 # include <sys/time.h>
34 #endif
35 #ifdef HAVE_UNISTD_H
36 # include <unistd.h>
37 #endif
39 #define NONAMELESSUNION
40 #define NONAMELESSSTRUCT
41 #include "windef.h"
42 #include "winbase.h"
43 #include "winreg.h"
44 #include "winternl.h"
45 #include "wine/unicode.h"
46 #include "wine/debug.h"
47 #include "ntdll_misc.h"
49 WINE_DEFAULT_DEBUG_CHANNEL(ntdll);
51 #define SETTIME_MAX_ADJUST 120
53 /* This structure is used to store strings that represent all of the time zones
54 * in the world. (This is used to help GetTimeZoneInformation)
56 struct tagTZ_INFO
58 const char *psTZFromUnix;
59 WCHAR psTZWindows[32];
60 int bias;
61 int dst;
64 static const struct tagTZ_INFO TZ_INFO[] =
66 {"MHT",
67 {'D','a','t','e','l','i','n','e',' ','S','t','a','n','d','a','r','d',' ',
68 'T','i','m','e','\0'}, -720, 0},
69 {"SST",
70 {'S','a','m','o','a',' ','S','t','a','n','d','a','r','d',' ','T','i','m',
71 'e','\0'}, 660, 0},
72 {"HST",
73 {'H','a','w','a','i','i','a','n',' ','S','t','a','n','d','a','r','d',' ',
74 'T','i','m','e','\0'}, 600, 0},
75 {"AKDT",
76 {'A','l','a','s','k','a','n',' ','S','t','a','n','d','a','r','d',' ','T',
77 'i','m','e','\0'}, 480, 1},
78 {"PDT",
79 {'P','a','c','i','f','i','c',' ','S','t','a','n','d','a','r','d',' ','T',
80 'i','m','e','\0'}, 420, 1},
81 {"MST",
82 {'U','S',' ','M','o','u','n','t','a','i','n',' ','S','t','a','n','d','a',
83 'r','d',' ','T','i','m','e','\0'}, 420, 0},
84 {"MDT",
85 {'M','o','u','n','t','a','i','n',' ','S','t','a','n','d','a','r','d',' ',
86 'T','i','m','e','\0'}, 360, 1},
87 {"CST",
88 {'C','e','n','t','r','a','l',' ','A','m','e','r','i','c','a',' ','S','t',
89 'a','n','d','a','r','d',' ','T','i','m','e','\0'}, 360, 0},
90 {"CDT",
91 {'C','e','n','t','r','a','l',' ','S','t','a','n','d','a','r','d',' ','T',
92 'i','m','e','\0'}, 300, 1},
93 {"COT",
94 {'S','A',' ','P','a','c','i','f','i','c',' ','S','t','a','n','d','a','r',
95 'd',' ','T','i','m','e','\0'}, 300, 0},
96 {"EDT",
97 {'E','a','s','t','e','r','n',' ','S','t','a','n','d','a','r','d',' ','T',
98 'i','m','e','\0'}, 240, 1},
99 {"EST",
100 {'U','S',' ','E','a','s','t','e','r','n',' ','S','t','a','n','d','a','r',
101 'd',' ','T','i','m','e','\0'}, 300, 0},
102 {"ADT",
103 {'A','t','l','a','n','t','i','c',' ','S','t','a','n','d','a','r','d',' ',
104 'T','i','m','e','\0'}, 180, 1},
105 {"VET",
106 {'S','A',' ','W','e','s','t','e','r','n',' ','S','t','a','n','d','a','r',
107 'd',' ','T','i','m','e','\0'}, 240, 0},
108 {"CLT",
109 {'P','a','c','i','f','i','c',' ','S','A',' ','S','t','a','n','d','a','r',
110 'd',' ','T','i','m','e','\0'}, 240, 0},
111 {"NDT",
112 {'N','e','w','f','o','u','n','d','l','a','n','d',' ','S','t','a','n','d',
113 'a','r','d',' ','T','i','m','e','\0'}, 150, 1},
114 {"BRT",
115 {'E','.',' ','S','o','u','t','h',' ','A','m','e','r','i','c','a',' ','S',
116 't','a','n','d','a','r','d',' ','T','i','m','e','\0'}, 180, 0},
117 {"ART",
118 {'S','A',' ','E','a','s','t','e','r','n',' ','S','t','a','n','d','a','r',
119 'd',' ','T','i','m','e','\0'}, 180, 0},
120 {"WGST",
121 {'G','r','e','e','n','l','a','n','d',' ','S','t','a','n','d','a','r','d',
122 ' ','T','i','m','e','\0'}, 120, 1},
123 {"GST",
124 {'M','i','d','-','A','t','l','a','n','t','i','c',' ','S','t','a','n','d',
125 'a','r','d',' ','T','i','m','e','\0'}, 120, 0},
126 {"AZOST",
127 {'A','z','o','r','e','s',' ','S','t','a','n','d','a','r','d',' ','T','i',
128 'm','e','\0'}, 0, 1},
129 {"CVT",
130 {'C','a','p','e',' ','V','e','r','d','e',' ','S','t','a','n','d','a','r',
131 'd',' ','T','i','m','e','\0'}, 60, 0},
132 {"WET",
133 {'G','r','e','e','n','w','i','c','h',' ','S','t','a','n','d','a','r','d',
134 ' ','T','i','m','e','\0'}, 0, 0},
135 {"BST",
136 {'G','M','T',' ','S','t','a','n','d','a','r','d',' ','T','i','m','e','\0'},
137 -60, 1},
138 {"GMT",
139 {'G','M','T',' ','S','t','a','n','d','a','r','d',' ','T','i','m','e','\0'},
140 0, 0},
141 {"CEST",
142 {'C','e','n','t','r','a','l',' ','E','u','r','o','p','e',' ','S','t','a',
143 'n','d','a','r','d',' ','T','i','m','e','\0'}, -120, 1},
144 {"WAT",
145 {'W','.',' ','C','e','n','t','r','a','l',' ','A','f','r','i','c','a',' ',
146 'S','t','a','n','d','a','r','d',' ','T','i','m','e','\0'}, -60, 0},
147 {"EEST",
148 {'E','.',' ','E','u','r','o','p','e',' ','S','t','a','n','d','a','r','d',
149 ' ','T','i','m','e','\0'}, -180, 1},
150 {"EET",
151 {'E','g','y','p','t',' ','S','t','a','n','d','a','r','d',' ','T','i','m',
152 'e','\0'}, -120, 0},
153 {"CAT",
154 {'S','o','u','t','h',' ','A','f','r','i','c','a',' ','S','t','a','n','d',
155 'a','r','d',' ','T','i','m','e','\0'}, -120, 0},
156 {"IST",
157 {'I','s','r','a','e','l',' ','S','t','a','n','d','a','r','d',' ','T','i',
158 'm','e','\0'}, -120, 0},
159 {"ADT",
160 {'A','r','a','b','i','c',' ','S','t','a','n','d','a','r','d',' ','T','i',
161 'm','e','\0'}, -240, 1},
162 {"AST",
163 {'A','r','a','b',' ','S','t','a','n','d','a','r','d',' ','T','i','m','e',
164 '\0'}, -180, 0},
165 {"MSD",
166 {'R','u','s','s','i','a','n',' ','S','t','a','n','d','a','r','d',' ','T',
167 'i','m','e','\0'}, -240, 1},
168 {"EAT",
169 {'E','.',' ','A','f','r','i','c','a',' ','S','t','a','n','d','a','r','d',
170 ' ','T','i','m','e','\0'}, -180, 0},
171 {"IRST",
172 {'I','r','a','n',' ','S','t','a','n','d','a','r','d',' ','T','i','m','e',
173 '\0'}, -270, 1},
174 {"GST",
175 {'A','r','a','b','i','a','n',' ','S','t','a','n','d','a','r','d',' ','T',
176 'i','m','e','\0'}, -240, 0},
177 {"AZST",
178 {'C','a','u','c','a','s','u','s',' ','S','t','a','n','d','a','r','d',' ',
179 'T','i','m','e','\0'}, -300, 1},
180 {"AFT",
181 {'A','f','g','h','a','n','i','s','t','a','n',' ','S','t','a','n','d','a',
182 'r','d',' ','T','i','m','e','\0'}, -270, 0},
183 {"YEKST",
184 {'E','k','a','t','e','r','i','n','b','u','r','g',' ','S','t','a','n','d',
185 'a','r','d',' ','T','i','m','e','\0'}, -360, 1},
186 {"PKT",
187 {'W','e','s','t',' ','A','s','i','a',' ','S','t','a','n','d','a','r','d',
188 ' ','T','i','m','e','\0'}, -300, 0},
189 {"IST",
190 {'I','n','d','i','a',' ','S','t','a','n','d','a','r','d',' ','T','i','m',
191 'e','\0'}, -330, 0},
192 {"NPT",
193 {'N','e','p','a','l',' ','S','t','a','n','d','a','r','d',' ','T','i','m',
194 'e','\0'}, -345, 0},
195 {"ALMST",
196 {'N','.',' ','C','e','n','t','r','a','l',' ','A','s','i','a',' ','S','t',
197 'a','n','d','a','r','d',' ','T','i','m','e','\0'}, -420, 1},
198 {"BDT",
199 {'C','e','n','t','r','a','l',' ','A','s','i','a',' ','S','t','a','n','d',
200 'a','r','d',' ','T','i','m','e','\0'}, -360, 0},
201 {"LKT",
202 {'S','r','i',' ','L','a','n','k','a',' ','S','t','a','n','d','a','r','d',
203 ' ','T','i','m','e','\0'}, -360, 0},
204 {"MMT",
205 {'M','y','a','n','m','a','r',' ','S','t','a','n','d','a','r','d',' ','T',
206 'i','m','e','\0'}, -390, 0},
207 {"ICT",
208 {'S','E',' ','A','s','i','a',' ','S','t','a','n','d','a','r','d',' ','T',
209 'i','m','e','\0'}, -420, 0},
210 {"KRAST",
211 {'N','o','r','t','h',' ','A','s','i','a',' ','S','t','a','n','d','a','r',
212 'd',' ','T','i','m','e','\0'}, -480, 1},
213 {"CST",
214 {'C','h','i','n','a',' ','S','t','a','n','d','a','r','d',' ','T','i','m',
215 'e','\0'}, -480, 0},
216 {"IRKST",
217 {'N','o','r','t','h',' ','A','s','i','a',' ','E','a','s','t',' ','S','t',
218 'a','n','d','a','r','d',' ','T','i','m','e','\0'}, -540, 1},
219 {"SGT",
220 {'M','a','l','a','y',' ','P','e','n','i','n','s','u','l','a',' ','S','t',
221 'a','n','d','a','r','d',' ','T','i','m','e','\0'}, -480, 0},
222 {"WST",
223 {'W','.',' ','A','u','s','t','r','a','l','i','a',' ','S','t','a','n','d',
224 'a','r','d',' ','T','i','m','e','\0'}, -480, 0},
225 {"JST",
226 {'T','o','k','y','o',' ','S','t','a','n','d','a','r','d',' ','T','i','m',
227 'e','\0'}, -540, 0},
228 {"KST",
229 {'K','o','r','e','a',' ','S','t','a','n','d','a','r','d',' ','T','i','m',
230 'e','\0'}, -540, 0},
231 {"YAKST",
232 {'Y','a','k','u','t','s','k',' ','S','t','a','n','d','a','r','d',' ','T',
233 'i','m','e','\0'}, -600, 1},
234 {"CST",
235 {'C','e','n','.',' ','A','u','s','t','r','a','l','i','a',' ','S','t','a',
236 'n','d','a','r','d',' ','T','i','m','e','\0'}, -570, 0},
237 {"EST",
238 {'E','.',' ','A','u','s','t','r','a','l','i','a',' ','S','t','a','n','d',
239 'a','r','d',' ','T','i','m','e','\0'}, -600, 0},
240 {"GST",
241 {'W','e','s','t',' ','P','a','c','i','f','i','c',' ','S','t','a','n','d',
242 'a','r','d',' ','T','i','m','e','\0'}, -600, 0},
243 {"VLAST",
244 {'V','l','a','d','i','v','o','s','t','o','k',' ','S','t','a','n','d','a',
245 'r','d',' ','T','i','m','e','\0'}, -660, 1},
246 {"MAGST",
247 {'C','e','n','t','r','a','l',' ','P','a','c','i','f','i','c',' ','S','t',
248 'a','n','d','a','r','d',' ','T','i','m','e','\0'}, -720, 1},
249 {"NZST",
250 {'N','e','w',' ','Z','e','a','l','a','n','d',' ','S','t','a','n','d','a',
251 'r','d',' ','T','i','m','e','\0'}, -720, 0},
252 {"FJT",
253 {'F','i','j','i',' ','S','t','a','n','d','a','r','d',' ','T','i','m','e',
254 '\0'}, -720, 0},
255 {"TOT",
256 {'T','o','n','g','a',' ','S','t','a','n','d','a','r','d',' ','T','i','m',
257 'e','\0'}, -780, 0}
260 /*********** start of code by Rex Jolliff (rex@lvcablemodem.com) **************/
262 #define TICKSPERSEC 10000000
263 #define TICKSPERMSEC 10000
264 #define SECSPERDAY 86400
265 #define SECSPERHOUR 3600
266 #define SECSPERMIN 60
267 #define MINSPERHOUR 60
268 #define HOURSPERDAY 24
269 #define EPOCHWEEKDAY 1 /* Jan 1, 1601 was Monday */
270 #define DAYSPERWEEK 7
271 #define EPOCHYEAR 1601
272 #define DAYSPERNORMALYEAR 365
273 #define DAYSPERLEAPYEAR 366
274 #define MONSPERYEAR 12
275 #define DAYSPERQUADRICENTENNIUM (365 * 400 + 97)
276 #define DAYSPERNORMALCENTURY (365 * 100 + 24)
277 #define DAYSPERNORMALQUADRENNIUM (365 * 4 + 1)
279 /* 1601 to 1970 is 369 years plus 89 leap days */
280 #define SECS_1601_TO_1970 ((369 * 365 + 89) * (ULONGLONG)SECSPERDAY)
281 #define TICKS_1601_TO_1970 (SECS_1601_TO_1970 * TICKSPERSEC)
282 /* 1601 to 1980 is 379 years plus 91 leap days */
283 #define SECS_1601_TO_1980 ((379 * 365 + 91) * (ULONGLONG)SECSPERDAY)
284 #define TICKS_1601_TO_1980 (SECS_1601_TO_1980 * TICKSPERSEC)
287 static const int YearLengths[2] = {DAYSPERNORMALYEAR, DAYSPERLEAPYEAR};
288 static const int MonthLengths[2][MONSPERYEAR] =
290 { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
291 { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
294 static inline int IsLeapYear(int Year)
296 return Year % 4 == 0 && (Year % 100 != 0 || Year % 400 == 0) ? 1 : 0;
299 static inline void NormalizeTimeFields(CSHORT *FieldToNormalize, CSHORT *CarryField,int Modulus)
301 *FieldToNormalize = (CSHORT) (*FieldToNormalize - Modulus);
302 *CarryField = (CSHORT) (*CarryField + 1);
305 /***********************************************************************
306 * NTDLL_get_server_timeout
308 * Convert a NTDLL timeout into a timeval struct to send to the server.
310 void NTDLL_get_server_timeout( abs_time_t *when, const LARGE_INTEGER *timeout )
312 UINT remainder;
314 if (!timeout) /* infinite timeout */
316 when->sec = when->usec = 0;
318 else if (timeout->QuadPart <= 0) /* relative timeout */
320 struct timeval tv;
321 ULONG sec = RtlEnlargedUnsignedDivide( -timeout->QuadPart, TICKSPERSEC, &remainder );
322 gettimeofday( &tv, 0 );
323 when->sec = tv.tv_sec + sec;
324 if ((when->usec = tv.tv_usec + (remainder / 10)) >= 1000000)
326 when->usec -= 1000000;
327 when->sec++;
330 else /* absolute time */
332 when->sec = RtlEnlargedUnsignedDivide( timeout->QuadPart - TICKS_1601_TO_1970,
333 TICKSPERSEC, &remainder );
334 when->usec = remainder / 10;
339 /******************************************************************************
340 * RtlTimeToTimeFields [NTDLL.@]
342 * Convert a time into a TIME_FIELDS structure.
344 * PARAMS
345 * liTime [I] Time to convert.
346 * TimeFields [O] Destination for the converted time.
348 * RETURNS
349 * Nothing.
351 VOID WINAPI RtlTimeToTimeFields(
352 const LARGE_INTEGER *liTime,
353 PTIME_FIELDS TimeFields)
355 const int *Months;
356 int SecondsInDay, DeltaYear;
357 int LeapYear, CurMonth;
358 long int Days;
359 LONGLONG Time = liTime->QuadPart;
361 /* Extract millisecond from time and convert time into seconds */
362 TimeFields->Milliseconds = (CSHORT) ((Time % TICKSPERSEC) / TICKSPERMSEC);
363 Time = Time / TICKSPERSEC;
365 /* The native version of RtlTimeToTimeFields does not take leap seconds
366 * into account */
368 /* Split the time into days and seconds within the day */
369 Days = Time / SECSPERDAY;
370 SecondsInDay = Time % SECSPERDAY;
372 /* compute time of day */
373 TimeFields->Hour = (CSHORT) (SecondsInDay / SECSPERHOUR);
374 SecondsInDay = SecondsInDay % SECSPERHOUR;
375 TimeFields->Minute = (CSHORT) (SecondsInDay / SECSPERMIN);
376 TimeFields->Second = (CSHORT) (SecondsInDay % SECSPERMIN);
378 /* compute day of week */
379 TimeFields->Weekday = (CSHORT) ((EPOCHWEEKDAY + Days) % DAYSPERWEEK);
381 /* compute year */
382 /* FIXME: handle calendar modifications */
383 TimeFields->Year = EPOCHYEAR;
384 DeltaYear = Days / DAYSPERQUADRICENTENNIUM;
385 TimeFields->Year += DeltaYear * 400;
386 Days -= DeltaYear * DAYSPERQUADRICENTENNIUM;
387 DeltaYear = Days / DAYSPERNORMALCENTURY;
388 TimeFields->Year += DeltaYear * 100;
389 Days -= DeltaYear * DAYSPERNORMALCENTURY;
390 DeltaYear = Days / DAYSPERNORMALQUADRENNIUM;
391 TimeFields->Year += DeltaYear * 4;
392 Days -= DeltaYear * DAYSPERNORMALQUADRENNIUM;
393 DeltaYear = Days / DAYSPERNORMALYEAR;
394 TimeFields->Year += DeltaYear;
395 Days -= DeltaYear * DAYSPERNORMALYEAR;
397 LeapYear = IsLeapYear(TimeFields->Year);
399 /* Compute month of year */
400 Months = MonthLengths[LeapYear];
401 for (CurMonth = 0; Days >= (long) Months[CurMonth]; CurMonth++)
402 Days = Days - (long) Months[CurMonth];
403 TimeFields->Month = (CSHORT) (CurMonth + 1);
404 TimeFields->Day = (CSHORT) (Days + 1);
407 /******************************************************************************
408 * RtlTimeFieldsToTime [NTDLL.@]
410 * Convert a TIME_FIELDS structure into a time.
412 * PARAMS
413 * ftTimeFields [I] TIME_FIELDS structure to convert.
414 * Time [O] Destination for the converted time.
416 * RETURNS
417 * Success: TRUE.
418 * Failure: FALSE.
420 BOOLEAN WINAPI RtlTimeFieldsToTime(
421 PTIME_FIELDS tfTimeFields,
422 PLARGE_INTEGER Time)
424 int CurYear, CurMonth;
425 LONGLONG rcTime;
426 TIME_FIELDS TimeFields = *tfTimeFields;
428 rcTime = 0;
430 /* FIXME: normalize the TIME_FIELDS structure here */
431 while (TimeFields.Second >= SECSPERMIN)
432 { NormalizeTimeFields(&TimeFields.Second, &TimeFields.Minute, SECSPERMIN);
434 while (TimeFields.Minute >= MINSPERHOUR)
435 { NormalizeTimeFields(&TimeFields.Minute, &TimeFields.Hour, MINSPERHOUR);
437 while (TimeFields.Hour >= HOURSPERDAY)
438 { NormalizeTimeFields(&TimeFields.Hour, &TimeFields.Day, HOURSPERDAY);
440 while (TimeFields.Day > MonthLengths[IsLeapYear(TimeFields.Year)][TimeFields.Month - 1])
441 { NormalizeTimeFields(&TimeFields.Day, &TimeFields.Month, SECSPERMIN);
443 while (TimeFields.Month > MONSPERYEAR)
444 { NormalizeTimeFields(&TimeFields.Month, &TimeFields.Year, MONSPERYEAR);
447 /* FIXME: handle calendar corrections here */
448 for (CurYear = EPOCHYEAR; CurYear < TimeFields.Year; CurYear++)
449 { rcTime += YearLengths[IsLeapYear(CurYear)];
451 for (CurMonth = 1; CurMonth < TimeFields.Month; CurMonth++)
452 { rcTime += MonthLengths[IsLeapYear(CurYear)][CurMonth - 1];
454 rcTime += TimeFields.Day - 1;
455 rcTime *= SECSPERDAY;
456 rcTime += TimeFields.Hour * SECSPERHOUR + TimeFields.Minute * SECSPERMIN + TimeFields.Second;
457 rcTime *= TICKSPERSEC;
458 rcTime += TimeFields.Milliseconds * TICKSPERMSEC;
459 Time->QuadPart = rcTime;
461 return TRUE;
463 /************ end of code by Rex Jolliff (rex@lvcablemodem.com) ***************/
465 /******************************************************************************
466 * RtlLocalTimeToSystemTime [NTDLL.@]
468 * Convert a local time into system time.
470 * PARAMS
471 * LocalTime [I] Local time to convert.
472 * SystemTime [O] Destination for the converted time.
474 * RETURNS
475 * Success: STATUS_SUCCESS.
476 * Failure: An NTSTATUS error code indicating the problem.
478 NTSTATUS WINAPI RtlLocalTimeToSystemTime( const LARGE_INTEGER *LocalTime,
479 PLARGE_INTEGER SystemTime)
481 TIME_ZONE_INFORMATION tzinfo;
483 TRACE("(%p, %p)\n", LocalTime, SystemTime);
485 RtlQueryTimeZoneInformation(&tzinfo);
486 SystemTime->QuadPart = LocalTime->QuadPart + tzinfo.Bias * 60 * (LONGLONG)10000000;
487 return STATUS_SUCCESS;
490 /******************************************************************************
491 * RtlSystemTimeToLocalTime [NTDLL.@]
493 * Convert a system time into a local time.
495 * PARAMS
496 * SystemTime [I] System time to convert.
497 * LocalTime [O] Destination for the converted time.
499 * RETURNS
500 * Success: STATUS_SUCCESS.
501 * Failure: An NTSTATUS error code indicating the problem.
503 NTSTATUS WINAPI RtlSystemTimeToLocalTime( const LARGE_INTEGER *SystemTime,
504 PLARGE_INTEGER LocalTime )
506 TIME_ZONE_INFORMATION tzinfo;
508 TRACE("(%p, %p)\n", SystemTime, LocalTime);
510 RtlQueryTimeZoneInformation(&tzinfo);
511 LocalTime->QuadPart = SystemTime->QuadPart - tzinfo.Bias * 60 * (LONGLONG)10000000;
512 return STATUS_SUCCESS;
515 /******************************************************************************
516 * RtlTimeToSecondsSince1970 [NTDLL.@]
518 * Convert a time into a count of seconds since 1970.
520 * PARAMS
521 * Time [I] Time to convert.
522 * Seconds [O] Destination for the converted time.
524 * RETURNS
525 * Success: TRUE.
526 * Failure: FALSE, if the resulting value will not fit in a DWORD.
528 BOOLEAN WINAPI RtlTimeToSecondsSince1970( const LARGE_INTEGER *Time, LPDWORD Seconds )
530 ULONGLONG tmp = ((ULONGLONG)Time->u.HighPart << 32) | Time->u.LowPart;
531 tmp = RtlLargeIntegerDivide( tmp, 10000000, NULL );
532 tmp -= SECS_1601_TO_1970;
533 if (tmp > 0xffffffff) return FALSE;
534 *Seconds = (DWORD)tmp;
535 return TRUE;
538 /******************************************************************************
539 * RtlTimeToSecondsSince1980 [NTDLL.@]
541 * Convert a time into a count of seconds since 1980.
543 * PARAMS
544 * Time [I] Time to convert.
545 * Seconds [O] Destination for the converted time.
547 * RETURNS
548 * Success: TRUE.
549 * Failure: FALSE, if the resulting value will not fit in a DWORD.
551 BOOLEAN WINAPI RtlTimeToSecondsSince1980( const LARGE_INTEGER *Time, LPDWORD Seconds )
553 ULONGLONG tmp = ((ULONGLONG)Time->u.HighPart << 32) | Time->u.LowPart;
554 tmp = RtlLargeIntegerDivide( tmp, 10000000, NULL );
555 tmp -= SECS_1601_TO_1980;
556 if (tmp > 0xffffffff) return FALSE;
557 *Seconds = (DWORD)tmp;
558 return TRUE;
561 /******************************************************************************
562 * RtlSecondsSince1970ToTime [NTDLL.@]
564 * Convert a count of seconds since 1970 to a time.
566 * PARAMS
567 * Seconds [I] Time to convert.
568 * Time [O] Destination for the converted time.
570 * RETURNS
571 * Nothing.
573 void WINAPI RtlSecondsSince1970ToTime( DWORD Seconds, LARGE_INTEGER *Time )
575 ULONGLONG secs = Seconds * (ULONGLONG)TICKSPERSEC + TICKS_1601_TO_1970;
576 Time->u.LowPart = (DWORD)secs;
577 Time->u.HighPart = (DWORD)(secs >> 32);
580 /******************************************************************************
581 * RtlSecondsSince1980ToTime [NTDLL.@]
583 * Convert a count of seconds since 1980 to a time.
585 * PARAMS
586 * Seconds [I] Time to convert.
587 * Time [O] Destination for the converted time.
589 * RETURNS
590 * Nothing.
592 void WINAPI RtlSecondsSince1980ToTime( DWORD Seconds, LARGE_INTEGER *Time )
594 ULONGLONG secs = Seconds * (ULONGLONG)TICKSPERSEC + TICKS_1601_TO_1980;
595 Time->u.LowPart = (DWORD)secs;
596 Time->u.HighPart = (DWORD)(secs >> 32);
599 /******************************************************************************
600 * RtlTimeToElapsedTimeFields [NTDLL.@]
602 * Convert a time to a count of elapsed seconds.
604 * PARAMS
605 * Time [I] Time to convert.
606 * TimeFields [O] Destination for the converted time.
608 * RETURNS
609 * Nothing.
611 void WINAPI RtlTimeToElapsedTimeFields( const LARGE_INTEGER *Time, PTIME_FIELDS TimeFields )
613 LONGLONG time;
614 UINT rem;
616 time = RtlExtendedLargeIntegerDivide( Time->QuadPart, TICKSPERSEC, &rem );
617 TimeFields->Milliseconds = rem / TICKSPERMSEC;
619 /* time is now in seconds */
620 TimeFields->Year = 0;
621 TimeFields->Month = 0;
622 TimeFields->Day = RtlExtendedLargeIntegerDivide( time, SECSPERDAY, &rem );
624 /* rem is now the remaining seconds in the last day */
625 TimeFields->Second = rem % 60;
626 rem /= 60;
627 TimeFields->Minute = rem % 60;
628 TimeFields->Hour = rem / 60;
631 /***********************************************************************
632 * NtQuerySystemTime [NTDLL.@]
633 * ZwQuerySystemTime [NTDLL.@]
635 * Get the current system time.
637 * PARAMS
638 * Time [O] Destination for the current system time.
640 * RETURNS
641 * Success: STATUS_SUCCESS.
642 * Failure: An NTSTATUS error code indicating the problem.
644 NTSTATUS WINAPI NtQuerySystemTime( PLARGE_INTEGER Time )
646 struct timeval now;
648 gettimeofday( &now, 0 );
649 Time->QuadPart = now.tv_sec * (ULONGLONG)TICKSPERSEC + TICKS_1601_TO_1970;
650 Time->QuadPart += now.tv_usec * 10;
651 return STATUS_SUCCESS;
654 /***********************************************************************
655 * TIME_GetBias [internal]
657 * Helper function calculates delta local time from UTC.
659 * PARAMS
660 * utc [I] The current utc time.
661 * pdaylight [I] Local daylight.
663 * RETURNS
664 * The bias for the current timezone.
666 static int TIME_GetBias(time_t utc, int *pdaylight)
668 struct tm *ptm = localtime(&utc);
669 *pdaylight = ptm->tm_isdst; /* daylight for local timezone */
670 ptm = gmtime(&utc);
671 ptm->tm_isdst = *pdaylight; /* use local daylight, not that of Greenwich */
672 return (int)(utc-mktime(ptm));
675 /***********************************************************************
676 * TIME_GetTZAsStr [internal]
678 * Helper function that returns the given timezone as a string.
680 * PARAMS
681 * utc [I] The current utc time.
682 * bias [I] The bias of the current timezone.
683 * dst [I] ??
685 * RETURNS
686 * Timezone name.
688 * NOTES:
689 * This could be done with a hash table instead of merely iterating through a
690 * table, however with the small amount of entries (60 or so) I didn't think
691 * it was worth it.
693 static const WCHAR* TIME_GetTZAsStr (time_t utc, int bias, int dst)
695 char psTZName[7];
696 struct tm *ptm = localtime(&utc);
697 int i;
699 if (!strftime (psTZName, 7, "%Z", ptm))
700 return (NULL);
702 for (i=0; i<(sizeof(TZ_INFO) / sizeof(struct tagTZ_INFO)); i++)
704 if ( strcmp(TZ_INFO[i].psTZFromUnix, psTZName) == 0 &&
705 TZ_INFO[i].bias == bias &&
706 TZ_INFO[i].dst == dst
708 return TZ_INFO[i].psTZWindows;
711 return NULL;
714 /***********************************************************************
715 * RtlQueryTimeZoneInformation [NTDLL.@]
717 * Get information about the current timezone.
719 * PARAMS
720 * tzinfo [O] Destination for the retrieved timezone info.
722 * RETURNS
723 * Success: STATUS_SUCCESS.
724 * Failure: An NTSTATUS error code indicating the problem.
726 NTSTATUS WINAPI RtlQueryTimeZoneInformation(LPTIME_ZONE_INFORMATION tzinfo)
728 time_t gmt;
729 int bias, daylight;
730 const WCHAR *psTZ;
732 memset(tzinfo, 0, sizeof(TIME_ZONE_INFORMATION));
734 gmt = time(NULL);
735 bias = TIME_GetBias(gmt, &daylight);
737 tzinfo->Bias = -bias / 60;
738 tzinfo->StandardBias = 0;
739 tzinfo->DaylightBias = -60;
740 psTZ = TIME_GetTZAsStr (gmt, (-bias/60), daylight);
741 if (psTZ) strcpyW( tzinfo->StandardName, psTZ );
742 return STATUS_SUCCESS;
745 /***********************************************************************
746 * RtlSetTimeZoneInformation [NTDLL.@]
748 * Set the current time zone information.
750 * PARAMS
751 * tzinfo [I] Timezone information to set.
753 * RETURNS
754 * Success: STATUS_SUCCESS.
755 * Failure: An NTSTATUS error code indicating the problem.
757 * BUGS
758 * Uses the obsolete unix timezone structure and tz_dsttime member.
760 NTSTATUS WINAPI RtlSetTimeZoneInformation( const TIME_ZONE_INFORMATION *tzinfo )
762 struct timezone tz;
764 tz.tz_minuteswest = tzinfo->Bias;
765 #ifdef DST_NONE
766 tz.tz_dsttime = DST_NONE;
767 #else
768 tz.tz_dsttime = 0;
769 #endif
770 if(!settimeofday(NULL, &tz))
771 return STATUS_SUCCESS;
772 return STATUS_PRIVILEGE_NOT_HELD;
775 /***********************************************************************
776 * NtSetSystemTime [NTDLL.@]
777 * ZwSetSystemTime [NTDLL.@]
779 * Set the system time.
781 * PARAMS
782 * NewTime [I] The time to set.
783 * OldTime [O] Optional destination for the previous system time.
785 * RETURNS
786 * Success: STATUS_SUCCESS.
787 * Failure: An NTSTATUS error code indicating the problem.
789 NTSTATUS WINAPI NtSetSystemTime(const LARGE_INTEGER *NewTime, LARGE_INTEGER *OldTime)
791 TIME_FIELDS tf;
792 struct timeval tv;
793 struct timezone tz;
794 struct tm t;
795 time_t sec, oldsec;
796 int dst, bias;
797 int err;
799 /* Return the old time if necessary */
800 if(OldTime)
801 NtQuerySystemTime(OldTime);
803 RtlTimeToTimeFields(NewTime, &tf);
805 /* call gettimeofday to get the current timezone */
806 gettimeofday(&tv, &tz);
807 oldsec = tv.tv_sec;
808 /* get delta local time from utc */
809 bias = TIME_GetBias(oldsec, &dst);
811 /* get the number of seconds */
812 t.tm_sec = tf.Second;
813 t.tm_min = tf.Minute;
814 t.tm_hour = tf.Hour;
815 t.tm_mday = tf.Day;
816 t.tm_mon = tf.Month - 1;
817 t.tm_year = tf.Year - 1900;
818 t.tm_isdst = dst;
819 sec = mktime (&t);
820 /* correct for timezone and daylight */
821 sec += bias;
823 /* set the new time */
824 tv.tv_sec = sec;
825 tv.tv_usec = tf.Milliseconds * 1000;
827 /* error and sanity check*/
828 if(sec == (time_t)-1 || abs((int)(sec-oldsec)) > SETTIME_MAX_ADJUST) {
829 err = 2;
830 } else {
831 #ifdef HAVE_SETTIMEOFDAY
832 err = settimeofday(&tv, NULL); /* 0 is OK, -1 is error */
833 if(err == 0)
834 return STATUS_SUCCESS;
835 #else
836 err = 1;
837 #endif
840 ERR("Cannot set time to %d/%d/%d %d:%d:%d Time adjustment %ld %s\n",
841 tf.Year, tf.Month, tf.Day, tf.Hour, tf.Minute, tf.Second,
842 (long)(sec-oldsec),
843 err == -1 ? "No Permission"
844 : sec == (time_t)-1 ? "" : "is too large." );
846 if(err == 2)
847 return STATUS_INVALID_PARAMETER;
848 else if(err == -1)
849 return STATUS_PRIVILEGE_NOT_HELD;
850 else
851 return STATUS_NOT_IMPLEMENTED;