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