Avoid casts between LARGE_INTEGER and FILETIME.
[wine] / dlls / ntdll / time.c
1 /*
2  * Nt time functions.
3  *
4  * RtlTimeToTimeFields, RtlTimeFieldsToTime and defines are taken from ReactOS and
5  * adapted to wine with special permissions of the author
6  * Rex Jolliff (rex@lvcablemodem.com)
7  *
8  * Copyright 1999 Juergen Schmied
9  *
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.
14  *
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.
19  *
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
23  */
24
25 #include "config.h"
26 #include "wine/port.h"
27
28 #include <stdlib.h>
29 #include <string.h>
30 #include <time.h>
31 #ifdef HAVE_SYS_TIME_H
32 # include <sys/time.h>
33 #endif
34 #ifdef HAVE_UNISTD_H
35 # include <unistd.h>
36 #endif
37 #include "winternl.h"
38 #include "wine/unicode.h"
39 #include "wine/debug.h"
40
41 WINE_DEFAULT_DEBUG_CHANNEL(ntdll);
42
43 #define SETTIME_MAX_ADJUST 120
44
45 /* This structure is used to store strings that represent all of the time zones
46  * in the world. (This is used to help GetTimeZoneInformation)
47  */
48 struct tagTZ_INFO
49 {
50     const char *psTZFromUnix;
51     WCHAR psTZWindows[32];
52     int bias;
53     int dst;
54 };
55
56 static const struct tagTZ_INFO TZ_INFO[] =
57 {
58    {"MHT",
59     {'D','a','t','e','l','i','n','e',' ','S','t','a','n','d','a','r','d',' ',
60      'T','i','m','e','\0'}, -720, 0},
61    {"SST",
62     {'S','a','m','o','a',' ','S','t','a','n','d','a','r','d',' ','T','i','m',
63      'e','\0'}, 660, 0},
64    {"HST",
65     {'H','a','w','a','i','i','a','n',' ','S','t','a','n','d','a','r','d',' ',
66      'T','i','m','e','\0'}, 600, 0},
67    {"AKDT",
68     {'A','l','a','s','k','a','n',' ','S','t','a','n','d','a','r','d',' ','T',
69      'i','m','e','\0'}, 480, 1},
70    {"PDT",
71     {'P','a','c','i','f','i','c',' ','S','t','a','n','d','a','r','d',' ','T',
72      'i','m','e','\0'}, 420, 1},
73    {"MST",
74     {'U','S',' ','M','o','u','n','t','a','i','n',' ','S','t','a','n','d','a',
75      'r','d',' ','T','i','m','e','\0'}, 420, 0},
76    {"MDT",
77     {'M','o','u','n','t','a','i','n',' ','S','t','a','n','d','a','r','d',' ',
78      'T','i','m','e','\0'}, 360, 1},
79    {"CST",
80     {'C','e','n','t','r','a','l',' ','A','m','e','r','i','c','a',' ','S','t',
81      'a','n','d','a','r','d',' ','T','i','m','e','\0'}, 360, 0},
82    {"CDT",
83     {'C','e','n','t','r','a','l',' ','S','t','a','n','d','a','r','d',' ','T',
84      'i','m','e','\0'}, 300, 1},
85    {"COT",
86     {'S','A',' ','P','a','c','i','f','i','c',' ','S','t','a','n','d','a','r',
87      'd',' ','T','i','m','e','\0'}, 300, 0},
88    {"EDT",
89     {'E','a','s','t','e','r','n',' ','S','t','a','n','d','a','r','d',' ','T',
90      'i','m','e','\0'}, 240, 1},
91    {"EST",
92     {'U','S',' ','E','a','s','t','e','r','n',' ','S','t','a','n','d','a','r',
93      'd',' ','T','i','m','e','\0'}, 300, 0},
94    {"ADT",
95     {'A','t','l','a','n','t','i','c',' ','S','t','a','n','d','a','r','d',' ',
96      'T','i','m','e','\0'}, 180, 1},
97    {"VET",
98     {'S','A',' ','W','e','s','t','e','r','n',' ','S','t','a','n','d','a','r',
99      'd',' ','T','i','m','e','\0'}, 240, 0},
100    {"CLT",
101     {'P','a','c','i','f','i','c',' ','S','A',' ','S','t','a','n','d','a','r',
102      'd',' ','T','i','m','e','\0'}, 240, 0},
103    {"NDT",
104     {'N','e','w','f','o','u','n','d','l','a','n','d',' ','S','t','a','n','d',
105      'a','r','d',' ','T','i','m','e','\0'}, 150, 1},
106    {"BRT",
107     {'E','.',' ','S','o','u','t','h',' ','A','m','e','r','i','c','a',' ','S',
108      't','a','n','d','a','r','d',' ','T','i','m','e','\0'}, 180, 0},
109    {"ART",
110     {'S','A',' ','E','a','s','t','e','r','n',' ','S','t','a','n','d','a','r',
111      'd',' ','T','i','m','e','\0'}, 180, 0},
112    {"WGST",
113     {'G','r','e','e','n','l','a','n','d',' ','S','t','a','n','d','a','r','d',
114      ' ','T','i','m','e','\0'}, 120, 1},
115    {"GST",
116     {'M','i','d','-','A','t','l','a','n','t','i','c',' ','S','t','a','n','d',
117      'a','r','d',' ','T','i','m','e','\0'}, 120, 0},
118    {"AZOST",
119     {'A','z','o','r','e','s',' ','S','t','a','n','d','a','r','d',' ','T','i',
120      'm','e','\0'}, 0, 1},
121    {"CVT",
122     {'C','a','p','e',' ','V','e','r','d','e',' ','S','t','a','n','d','a','r',
123      'd',' ','T','i','m','e','\0'}, 60, 0},
124    {"WET",
125     {'G','r','e','e','n','w','i','c','h',' ','S','t','a','n','d','a','r','d',
126      ' ','T','i','m','e','\0'}, 0, 0},
127    {"BST",
128     {'G','M','T',' ','S','t','a','n','d','a','r','d',' ','T','i','m','e','\0'},
129     -60, 1},
130    {"GMT",
131     {'G','M','T',' ','S','t','a','n','d','a','r','d',' ','T','i','m','e','\0'},
132     0, 0},
133    {"CEST",
134     {'C','e','n','t','r','a','l',' ','E','u','r','o','p','e',' ','S','t','a',
135      'n','d','a','r','d',' ','T','i','m','e','\0'}, -120, 1},
136    {"WAT",
137     {'W','.',' ','C','e','n','t','r','a','l',' ','A','f','r','i','c','a',' ',
138      'S','t','a','n','d','a','r','d',' ','T','i','m','e','\0'}, -60, 0},
139    {"EEST",
140     {'E','.',' ','E','u','r','o','p','e',' ','S','t','a','n','d','a','r','d',
141      ' ','T','i','m','e','\0'}, -180, 1},
142    {"EET",
143     {'E','g','y','p','t',' ','S','t','a','n','d','a','r','d',' ','T','i','m',
144      'e','\0'}, -120, 0},
145    {"CAT",
146     {'S','o','u','t','h',' ','A','f','r','i','c','a',' ','S','t','a','n','d',
147      'a','r','d',' ','T','i','m','e','\0'}, -120, 0},
148    {"IST",
149     {'I','s','r','a','e','l',' ','S','t','a','n','d','a','r','d',' ','T','i',
150      'm','e','\0'}, -120, 0},
151    {"ADT",
152     {'A','r','a','b','i','c',' ','S','t','a','n','d','a','r','d',' ','T','i',
153      'm','e','\0'}, -240, 1},
154    {"AST",
155     {'A','r','a','b',' ','S','t','a','n','d','a','r','d',' ','T','i','m','e',
156      '\0'}, -180, 0},
157    {"MSD",
158     {'R','u','s','s','i','a','n',' ','S','t','a','n','d','a','r','d',' ','T',
159      'i','m','e','\0'}, -240, 1},
160    {"EAT",
161     {'E','.',' ','A','f','r','i','c','a',' ','S','t','a','n','d','a','r','d',
162      ' ','T','i','m','e','\0'}, -180, 0},
163    {"IRST",
164     {'I','r','a','n',' ','S','t','a','n','d','a','r','d',' ','T','i','m','e',
165      '\0'}, -270, 1},
166    {"GST",
167     {'A','r','a','b','i','a','n',' ','S','t','a','n','d','a','r','d',' ','T',
168      'i','m','e','\0'}, -240, 0},
169    {"AZST",
170     {'C','a','u','c','a','s','u','s',' ','S','t','a','n','d','a','r','d',' ',
171      'T','i','m','e','\0'}, -300, 1},
172    {"AFT",
173     {'A','f','g','h','a','n','i','s','t','a','n',' ','S','t','a','n','d','a',
174      'r','d',' ','T','i','m','e','\0'}, -270, 0},
175    {"YEKST",
176     {'E','k','a','t','e','r','i','n','b','u','r','g',' ','S','t','a','n','d',
177      'a','r','d',' ','T','i','m','e','\0'}, -360, 1},
178    {"PKT",
179     {'W','e','s','t',' ','A','s','i','a',' ','S','t','a','n','d','a','r','d',
180      ' ','T','i','m','e','\0'}, -300, 0},
181    {"IST",
182     {'I','n','d','i','a',' ','S','t','a','n','d','a','r','d',' ','T','i','m',
183      'e','\0'}, -330, 0},
184    {"NPT",
185     {'N','e','p','a','l',' ','S','t','a','n','d','a','r','d',' ','T','i','m',
186      'e','\0'}, -345, 0},
187    {"ALMST",
188     {'N','.',' ','C','e','n','t','r','a','l',' ','A','s','i','a',' ','S','t',
189      'a','n','d','a','r','d',' ','T','i','m','e','\0'}, -420, 1},
190    {"BDT",
191     {'C','e','n','t','r','a','l',' ','A','s','i','a',' ','S','t','a','n','d',
192      'a','r','d',' ','T','i','m','e','\0'}, -360, 0},
193    {"LKT",
194     {'S','r','i',' ','L','a','n','k','a',' ','S','t','a','n','d','a','r','d',
195      ' ','T','i','m','e','\0'}, -360, 0},
196    {"MMT",
197     {'M','y','a','n','m','a','r',' ','S','t','a','n','d','a','r','d',' ','T',
198      'i','m','e','\0'}, -390, 0},
199    {"ICT",
200     {'S','E',' ','A','s','i','a',' ','S','t','a','n','d','a','r','d',' ','T',
201      'i','m','e','\0'}, -420, 0},
202    {"KRAST",
203     {'N','o','r','t','h',' ','A','s','i','a',' ','S','t','a','n','d','a','r',
204      'd',' ','T','i','m','e','\0'}, -480, 1},
205    {"CST",
206     {'C','h','i','n','a',' ','S','t','a','n','d','a','r','d',' ','T','i','m',
207      'e','\0'}, -480, 0},
208    {"IRKST",
209     {'N','o','r','t','h',' ','A','s','i','a',' ','E','a','s','t',' ','S','t',
210      'a','n','d','a','r','d',' ','T','i','m','e','\0'}, -540, 1},
211    {"SGT",
212     {'M','a','l','a','y',' ','P','e','n','i','n','s','u','l','a',' ','S','t',
213      'a','n','d','a','r','d',' ','T','i','m','e','\0'}, -480, 0},
214    {"WST",
215     {'W','.',' ','A','u','s','t','r','a','l','i','a',' ','S','t','a','n','d',
216      'a','r','d',' ','T','i','m','e','\0'}, -480, 0},
217    {"JST",
218     {'T','o','k','y','o',' ','S','t','a','n','d','a','r','d',' ','T','i','m',
219      'e','\0'}, -540, 0},
220    {"KST",
221     {'K','o','r','e','a',' ','S','t','a','n','d','a','r','d',' ','T','i','m',
222      'e','\0'}, -540, 0},
223    {"YAKST",
224     {'Y','a','k','u','t','s','k',' ','S','t','a','n','d','a','r','d',' ','T',
225      'i','m','e','\0'}, -600, 1},
226    {"CST",
227     {'C','e','n','.',' ','A','u','s','t','r','a','l','i','a',' ','S','t','a',
228      'n','d','a','r','d',' ','T','i','m','e','\0'}, -570, 0},
229    {"EST",
230     {'E','.',' ','A','u','s','t','r','a','l','i','a',' ','S','t','a','n','d',
231      'a','r','d',' ','T','i','m','e','\0'}, -600, 0},
232    {"GST",
233     {'W','e','s','t',' ','P','a','c','i','f','i','c',' ','S','t','a','n','d',
234      'a','r','d',' ','T','i','m','e','\0'}, -600, 0},
235    {"VLAST",
236     {'V','l','a','d','i','v','o','s','t','o','k',' ','S','t','a','n','d','a',
237      'r','d',' ','T','i','m','e','\0'}, -660, 1},
238    {"MAGST",
239     {'C','e','n','t','r','a','l',' ','P','a','c','i','f','i','c',' ','S','t',
240      'a','n','d','a','r','d',' ','T','i','m','e','\0'}, -720, 1},
241    {"NZST",
242     {'N','e','w',' ','Z','e','a','l','a','n','d',' ','S','t','a','n','d','a',
243      'r','d',' ','T','i','m','e','\0'}, -720, 0},
244    {"FJT",
245     {'F','i','j','i',' ','S','t','a','n','d','a','r','d',' ','T','i','m','e',
246      '\0'}, -720, 0},
247    {"TOT",
248     {'T','o','n','g','a',' ','S','t','a','n','d','a','r','d',' ','T','i','m',
249      'e','\0'}, -780, 0}
250 };
251
252 /*********** start of code by Rex Jolliff (rex@lvcablemodem.com) **************/
253
254 #define TICKSPERSEC        10000000
255 #define TICKSPERMSEC       10000
256 #define SECSPERDAY         86400
257 #define SECSPERHOUR        3600
258 #define SECSPERMIN         60
259 #define MINSPERHOUR        60
260 #define HOURSPERDAY        24
261 #define EPOCHWEEKDAY       1  /* Jan 1, 1601 was Monday */
262 #define DAYSPERWEEK        7
263 #define EPOCHYEAR          1601
264 #define DAYSPERNORMALYEAR  365
265 #define DAYSPERLEAPYEAR    366
266 #define MONSPERYEAR        12
267
268 /* 1601 to 1970 is 369 years plus 89 leap days */
269 #define SECS_1601_TO_1970  ((369 * 365 + 89) * (ULONGLONG)86400)
270 /* 1601 to 1980 is 379 years plus 91 leap days */
271 #define SECS_1601_to_1980  ((379 * 365 + 91) * (ULONGLONG)86400)
272
273
274 static const int YearLengths[2] = {DAYSPERNORMALYEAR, DAYSPERLEAPYEAR};
275 static const int MonthLengths[2][MONSPERYEAR] =
276 {
277         { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
278         { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
279 };
280
281 static inline int IsLeapYear(int Year)
282 {
283         return Year % 4 == 0 && (Year % 100 != 0 || Year % 400 == 0) ? 1 : 0;
284 }
285
286 static inline void NormalizeTimeFields(CSHORT *FieldToNormalize, CSHORT *CarryField,int Modulus)
287 {
288         *FieldToNormalize = (CSHORT) (*FieldToNormalize - Modulus);
289         *CarryField = (CSHORT) (*CarryField + 1);
290 }
291
292 /******************************************************************************
293  *       RtlTimeToTimeFields [NTDLL.@]
294  *
295  * Parses Time into a TimeFields structure.
296  *
297  * PARAMS:
298  *   liTime [I]: Time to convert to timefields.
299  *   TimeFields [O]: Pointer to TIME_FIELDS structure to hold parsed info.
300  *
301  * RETURNS:
302  *   Nothing.
303  */
304 VOID WINAPI RtlTimeToTimeFields(
305         const LARGE_INTEGER *liTime,
306         PTIME_FIELDS TimeFields)
307 {
308         const int *Months;
309         int SecondsInDay, CurYear;
310         int LeapYear, CurMonth;
311         long int Days;
312         LONGLONG Time = liTime->QuadPart;
313
314         /* Extract millisecond from time and convert time into seconds */
315         TimeFields->Milliseconds = (CSHORT) ((Time % TICKSPERSEC) / TICKSPERMSEC);
316         Time = Time / TICKSPERSEC;
317
318         /* The native version of RtlTimeToTimeFields does not take leap seconds
319          * into account */
320
321         /* Split the time into days and seconds within the day */
322         Days = Time / SECSPERDAY;
323         SecondsInDay = Time % SECSPERDAY;
324
325         /* compute time of day */
326         TimeFields->Hour = (CSHORT) (SecondsInDay / SECSPERHOUR);
327         SecondsInDay = SecondsInDay % SECSPERHOUR;
328         TimeFields->Minute = (CSHORT) (SecondsInDay / SECSPERMIN);
329         TimeFields->Second = (CSHORT) (SecondsInDay % SECSPERMIN);
330
331         /* compute day of week */
332         TimeFields->Weekday = (CSHORT) ((EPOCHWEEKDAY + Days) % DAYSPERWEEK);
333
334         /* compute year */
335         CurYear = EPOCHYEAR;
336         /* FIXME: handle calendar modifications */
337         while (1)
338         { LeapYear = IsLeapYear(CurYear);
339           if (Days < (long) YearLengths[LeapYear])
340           { break;
341           }
342           CurYear++;
343           Days = Days - (long) YearLengths[LeapYear];
344         }
345         TimeFields->Year = (CSHORT) CurYear;
346
347         /* Compute month of year */
348         Months = MonthLengths[LeapYear];
349         for (CurMonth = 0; Days >= (long) Months[CurMonth]; CurMonth++)
350           Days = Days - (long) Months[CurMonth];
351         TimeFields->Month = (CSHORT) (CurMonth + 1);
352         TimeFields->Day = (CSHORT) (Days + 1);
353 }
354
355 /******************************************************************************
356  *       RtlTimeFieldsToTime [NTDLL.@]
357  *
358  * Converts a TIME_FIELDS structure to time.
359  *
360  * PARAMS:
361  *   ftTimeFields [I]: Time fields structure to convert.
362  *   Time [O]: Converted time.
363  *
364  * RETURNS:
365  *   TRUE: Successfull
366  *   FALSE: Failure.
367  */
368 BOOLEAN WINAPI RtlTimeFieldsToTime(
369         PTIME_FIELDS tfTimeFields,
370         PLARGE_INTEGER Time)
371 {
372         int CurYear, CurMonth;
373         LONGLONG rcTime;
374         TIME_FIELDS TimeFields = *tfTimeFields;
375
376         rcTime = 0;
377
378         /* FIXME: normalize the TIME_FIELDS structure here */
379         while (TimeFields.Second >= SECSPERMIN)
380         { NormalizeTimeFields(&TimeFields.Second, &TimeFields.Minute, SECSPERMIN);
381         }
382         while (TimeFields.Minute >= MINSPERHOUR)
383         { NormalizeTimeFields(&TimeFields.Minute, &TimeFields.Hour, MINSPERHOUR);
384         }
385         while (TimeFields.Hour >= HOURSPERDAY)
386         { NormalizeTimeFields(&TimeFields.Hour, &TimeFields.Day, HOURSPERDAY);
387         }
388         while (TimeFields.Day > MonthLengths[IsLeapYear(TimeFields.Year)][TimeFields.Month - 1])
389         { NormalizeTimeFields(&TimeFields.Day, &TimeFields.Month, SECSPERMIN);
390         }
391         while (TimeFields.Month > MONSPERYEAR)
392         { NormalizeTimeFields(&TimeFields.Month, &TimeFields.Year, MONSPERYEAR);
393         }
394
395         /* FIXME: handle calendar corrections here */
396         for (CurYear = EPOCHYEAR; CurYear < TimeFields.Year; CurYear++)
397         { rcTime += YearLengths[IsLeapYear(CurYear)];
398         }
399         for (CurMonth = 1; CurMonth < TimeFields.Month; CurMonth++)
400         { rcTime += MonthLengths[IsLeapYear(CurYear)][CurMonth - 1];
401         }
402         rcTime += TimeFields.Day - 1;
403         rcTime *= SECSPERDAY;
404         rcTime += TimeFields.Hour * SECSPERHOUR + TimeFields.Minute * SECSPERMIN + TimeFields.Second;
405         rcTime *= TICKSPERSEC;
406         rcTime += TimeFields.Milliseconds * TICKSPERMSEC;
407         Time->QuadPart = rcTime;
408
409         return TRUE;
410 }
411 /************ end of code by Rex Jolliff (rex@lvcablemodem.com) ***************/
412
413 /******************************************************************************
414  *        RtlLocalTimeToSystemTime [NTDLL.@]
415  *
416  * Converts local time to system time.
417  *
418  * PARAMS:
419  *   LocalTime [I]: Localtime to convert.
420  *   SystemTime [O]: SystemTime of the supplied localtime.
421  *
422  * RETURNS:
423  *   Status.
424  */
425 NTSTATUS WINAPI RtlLocalTimeToSystemTime( const LARGE_INTEGER *LocalTime,
426                                           PLARGE_INTEGER SystemTime)
427 {
428     TIME_ZONE_INFORMATION tzinfo;
429
430     TRACE("(%p, %p)\n", LocalTime, SystemTime);
431
432     RtlQueryTimeZoneInformation(&tzinfo);
433     SystemTime->QuadPart = LocalTime->QuadPart + tzinfo.Bias * 60 * (LONGLONG)10000000;
434     return STATUS_SUCCESS;
435 }
436
437 /******************************************************************************
438  *       RtlSystemTimeToLocalTime [NTDLL.@]
439  *
440  * Converts system Time to local time.
441  *
442  * PARAMS:
443  *   SystemTime [I]: System time to convert.
444  *   LocalTime [O]: Local time of the supplied system time.
445  *
446  * RETURNS:
447  *   Nothing.
448  */
449 NTSTATUS WINAPI RtlSystemTimeToLocalTime( const LARGE_INTEGER *SystemTime,
450                                           PLARGE_INTEGER LocalTime )
451 {
452     TIME_ZONE_INFORMATION tzinfo;
453
454     TRACE("(%p, %p)\n", SystemTime, LocalTime);
455
456     RtlQueryTimeZoneInformation(&tzinfo);
457     LocalTime->QuadPart = SystemTime->QuadPart - tzinfo.Bias * 60 * (LONGLONG)10000000;
458     return STATUS_SUCCESS;
459 }
460
461 /******************************************************************************
462  *       RtlTimeToSecondsSince1970 [NTDLL.@]
463  *
464  * Converts Time to seconds since 1970.
465  *
466  * PARAMS:
467  *   time [I]: Time to convert.
468  *   res [O]: Pointer to a LONG to recieve the seconds since 1970.
469  *
470  * RETURNS:
471  *   TRUE: Successfull.
472  *   FALSE: Failure.
473  */
474 BOOLEAN WINAPI RtlTimeToSecondsSince1970( const LARGE_INTEGER *time, PULONG res )
475 {
476     ULONGLONG tmp = ((ULONGLONG)time->s.HighPart << 32) | time->s.LowPart;
477     tmp = RtlLargeIntegerDivide( tmp, 10000000, NULL );
478     tmp -= SECS_1601_TO_1970;
479     if (tmp > 0xffffffff) return FALSE;
480     *res = (DWORD)tmp;
481     return TRUE;
482 }
483
484 /******************************************************************************
485  *       RtlTimeToSecondsSince1980 [NTDLL.@]
486  *
487  * Converts Time to seconds since 1980.
488  *
489  * PARAMS:
490  *   time [I]: Time to convert.
491  *   res [O]: Pointer to a integer to recieve the time since 1980.
492  *
493  * RETURNS:
494  *   TRUE: Successfull
495  *   FALSE: Failure.
496  */
497 BOOLEAN WINAPI RtlTimeToSecondsSince1980( const LARGE_INTEGER *time, LPDWORD res )
498 {
499     ULONGLONG tmp = ((ULONGLONG)time->s.HighPart << 32) | time->s.LowPart;
500     tmp = RtlLargeIntegerDivide( tmp, 10000000, NULL );
501     tmp -= SECS_1601_to_1980;
502     if (tmp > 0xffffffff) return FALSE;
503     *res = (DWORD)tmp;
504     return TRUE;
505 }
506
507 /******************************************************************************
508  *       RtlSecondsSince1970ToTime [NTDLL.@]
509  *
510  * Converts seconds since 1970 to time.
511  *
512  * PARAMS:
513  *   time [I]: Seconds since 1970 to convert.
514  *   res [O]: Seconds since 1970 in Time.
515  *
516  * RETURNS:
517  *   Nothing.
518  */
519 void WINAPI RtlSecondsSince1970ToTime( DWORD time, LARGE_INTEGER *res )
520 {
521     ULONGLONG secs = RtlExtendedIntegerMultiply( time + SECS_1601_TO_1970, 10000000 );
522     res->s.LowPart  = (DWORD)secs;
523     res->s.HighPart = (DWORD)(secs >> 32);
524 }
525
526 /******************************************************************************
527  *       RtlSecondsSince1980ToTime [NTDLL.@]
528  *
529  * Converts seconds since 1980 to time.
530  *
531  * PARAMS:
532  *   time [I]: Seconds since 1980 to convert.
533  *   res [O]: Seconds since 1980 in Time.
534  *
535  * RETURNS:
536  *   Nothing.
537  */
538 void WINAPI RtlSecondsSince1980ToTime( DWORD time, LARGE_INTEGER *res )
539 {
540     ULONGLONG secs = RtlExtendedIntegerMultiply( time + SECS_1601_to_1980, 10000000 );
541     res->s.LowPart  = (DWORD)secs;
542     res->s.HighPart = (DWORD)(secs >> 32);
543 }
544
545 /******************************************************************************
546  *       RtlTimeToElapsedTimeFields [NTDLL.@]
547  *
548  * ??
549  *
550  * PARAMS:
551  *   liTime [?]: ??
552  *   TimeFields [?]: ??
553  *
554  * RETURNS:
555  *   Nothing.
556  *
557  * FIXME:
558  *   Prototype guessed.
559  */
560 VOID WINAPI RtlTimeToElapsedTimeFields(
561         PLARGE_INTEGER liTime,
562         PTIME_FIELDS TimeFields)
563 {
564         FIXME("(%p,%p): stub\n",liTime,TimeFields);
565 }
566
567 /***********************************************************************
568  *       NtQuerySystemTime [NTDLL.@]
569  *       ZwQuerySystemTime [NTDLL.@]
570  *
571  * Gets the current system time.
572  *
573  * PARAMS:
574  *   time [O]: The current system time.
575  *
576  * RETURNS:
577  *   Status.
578  */
579 NTSTATUS WINAPI NtQuerySystemTime( PLARGE_INTEGER time )
580 {
581     struct timeval now;
582
583     gettimeofday( &now, 0 );
584     time->QuadPart = RtlExtendedIntegerMultiply( now.tv_sec+SECS_1601_TO_1970, 10000000 ) + now.tv_usec * 10;
585     return STATUS_SUCCESS;
586 }
587
588 /***********************************************************************
589  *       TIME_GetBias [internal]
590  *
591  * Helper function calculates delta local time from UTC. 
592  *
593  * PARAMS:
594  *   utc [I]: The current utc time.
595  *   pdaylight [I]: Local daylight.
596  *
597  * RETURNS:
598  *   The bias for the current timezone.
599  */
600 static int TIME_GetBias(time_t utc, int *pdaylight)
601 {
602     struct tm *ptm = localtime(&utc);
603     *pdaylight = ptm->tm_isdst; /* daylight for local timezone */
604     ptm = gmtime(&utc);
605     ptm->tm_isdst = *pdaylight; /* use local daylight, not that of Greenwich */
606     return (int)(utc-mktime(ptm));
607 }
608
609 /***********************************************************************
610  *        TIME_GetTZAsStr [internal]
611  *
612  * Helper function that returns the given timezone as a string.
613  *
614  * PARAMS:
615  *   utc [I]: The current utc time.
616  *   bias [I]: The bias of the current timezone.
617  *   dst [I]: ??
618  *
619  * RETURNS:
620  *   Timezone name.
621  *
622  * NOTES:
623  *   This could be done with a hash table instead of merely iterating through a
624  *   table, however with the small amount of entries (60 or so) I didn't think
625  *   it was worth it.
626  */
627 static const WCHAR* TIME_GetTZAsStr (time_t utc, int bias, int dst)
628 {
629    char psTZName[7];
630    struct tm *ptm = localtime(&utc);
631    int i;
632
633    if (!strftime (psTZName, 7, "%Z", ptm))
634       return (NULL);
635
636    for (i=0; i<(sizeof(TZ_INFO) / sizeof(struct tagTZ_INFO)); i++)
637    {
638       if ( strcmp(TZ_INFO[i].psTZFromUnix, psTZName) == 0 &&
639            TZ_INFO[i].bias == bias &&
640            TZ_INFO[i].dst == dst
641          )
642             return TZ_INFO[i].psTZWindows;
643    }
644
645    return (NULL);
646 }
647
648 /***********************************************************************
649  *      RtlQueryTimeZoneInformation [NTDLL.@]
650  *
651  * Returns the timezone.
652  *
653  * PARAMS:
654  *   tzinfo [O]: Retrieves the timezone info.
655  *
656  * RETURNS:
657  *   Status.
658  */
659 NTSTATUS WINAPI RtlQueryTimeZoneInformation(LPTIME_ZONE_INFORMATION tzinfo)
660 {
661     time_t gmt;
662     int bias, daylight;
663     const WCHAR *psTZ;
664
665     memset(tzinfo, 0, sizeof(TIME_ZONE_INFORMATION));
666
667     gmt = time(NULL);
668     bias = TIME_GetBias(gmt, &daylight);
669
670     tzinfo->Bias = -bias / 60;
671     tzinfo->StandardBias = 0;
672     tzinfo->DaylightBias = -60;
673     psTZ = TIME_GetTZAsStr (gmt, (-bias/60), daylight);
674     if (psTZ) strcpyW( tzinfo->StandardName, psTZ );
675     return STATUS_SUCCESS;
676 }
677
678 /***********************************************************************
679  *       RtlSetTimeZoneInformation [NTDLL.@]
680  *
681  * Sets the current time zone.
682  *
683  * PARAMS:
684  *   tzinfo [I]: Timezone information used to set timezone.
685  *
686  * RETURNS:
687  *   Status.
688  *
689  * BUGS:
690  *   Uses the obsolete unix timezone structure and tz_dsttime member.
691  */
692 NTSTATUS WINAPI RtlSetTimeZoneInformation( const TIME_ZONE_INFORMATION *tzinfo )
693 {
694     struct timezone tz;
695
696     tz.tz_minuteswest = tzinfo->Bias;
697 #ifdef DST_NONE
698     tz.tz_dsttime = DST_NONE;
699 #else
700     tz.tz_dsttime = 0;
701 #endif
702     if(!settimeofday(NULL, &tz))
703         return STATUS_SUCCESS;
704     return STATUS_PRIVILEGE_NOT_HELD;
705 }
706
707 /***********************************************************************
708  *        NtSetSystemTime [NTDLL.@]
709  *        ZwSetSystemTime [NTDLL.@]
710  *
711  * Sets the system time.
712  *
713  * PARAM:
714  *   NewTime [I]: The time to set the system time to.
715  *   OldTime [O]: Optional (ie. can be NULL).  Old Time.
716  *
717  * RETURNS:
718  *   Status.
719  */
720 NTSTATUS WINAPI NtSetSystemTime(const LARGE_INTEGER *NewTime, LARGE_INTEGER *OldTime)
721 {
722     TIME_FIELDS tf;
723     struct timeval tv;
724     struct timezone tz;
725     struct tm t;
726     time_t sec, oldsec;
727     int dst, bias;
728     int err;
729
730     /* Return the old time if necessary */
731     if(OldTime)
732         NtQuerySystemTime(OldTime);
733
734     RtlTimeToTimeFields(NewTime, &tf);
735
736     /* call gettimeofday to get the current timezone */
737     gettimeofday(&tv, &tz);
738     oldsec = tv.tv_sec;
739     /* get delta local time from utc */
740     bias = TIME_GetBias(oldsec, &dst);
741
742     /* get the number of seconds */
743     t.tm_sec = tf.Second;
744     t.tm_min = tf.Minute;
745     t.tm_hour = tf.Hour;
746     t.tm_mday = tf.Day;
747     t.tm_mon = tf.Month - 1;
748     t.tm_year = tf.Year - 1900;
749     t.tm_isdst = dst;
750     sec = mktime (&t);
751     /* correct for timezone and daylight */
752     sec += bias;
753
754     /* set the new time */
755     tv.tv_sec = sec;
756     tv.tv_usec = tf.Milliseconds * 1000;
757
758     /* error and sanity check*/
759     if(sec == (time_t)-1 || abs((int)(sec-oldsec)) > SETTIME_MAX_ADJUST) {
760         err = 2;
761     } else {
762 #ifdef HAVE_SETTIMEOFDAY
763         err = settimeofday(&tv, NULL); /* 0 is OK, -1 is error */
764         if(err == 0)
765             return STATUS_SUCCESS;
766 #else
767         err = 1;
768 #endif
769     }
770
771     ERR("Cannot set time to %d/%d/%d %d:%d:%d Time adjustment %ld %s\n",
772             tf.Year, tf.Month, tf.Day, tf.Hour, tf.Minute, tf.Second,
773             sec-oldsec, err == -1 ? "No Permission" :
774                 sec == (time_t)-1 ? "" : "is too large." );
775
776     if(err == 2)
777         return STATUS_INVALID_PARAMETER;
778     else if(err == -1)
779         return STATUS_PRIVILEGE_NOT_HELD;
780     else
781         return STATUS_NOT_IMPLEMENTED;
782 }