Added support for implementing VxDs as separate dlls and loading them
[wine.git] / dlls / ntdll / time.c
blobb88ff8faaafc1111b8f4e966f633fd8f99345e5f
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
276 /* 1601 to 1970 is 369 years plus 89 leap days */
277 #define SECS_1601_TO_1970 ((369 * 365 + 89) * (ULONGLONG)SECSPERDAY)
278 #define TICKS_1601_TO_1970 (SECS_1601_TO_1970 * TICKSPERSEC)
279 /* 1601 to 1980 is 379 years plus 91 leap days */
280 #define SECS_1601_TO_1980 ((379 * 365 + 91) * (ULONGLONG)SECSPERDAY)
281 #define TICKS_1601_TO_1980 (SECS_1601_TO_1980 * TICKSPERSEC)
284 static const int YearLengths[2] = {DAYSPERNORMALYEAR, DAYSPERLEAPYEAR};
285 static const int MonthLengths[2][MONSPERYEAR] =
287 { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
288 { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
291 static inline int IsLeapYear(int Year)
293 return Year % 4 == 0 && (Year % 100 != 0 || Year % 400 == 0) ? 1 : 0;
296 static inline void NormalizeTimeFields(CSHORT *FieldToNormalize, CSHORT *CarryField,int Modulus)
298 *FieldToNormalize = (CSHORT) (*FieldToNormalize - Modulus);
299 *CarryField = (CSHORT) (*CarryField + 1);
302 /***********************************************************************
303 * NTDLL_get_server_timeout
305 * Convert a NTDLL timeout into a timeval struct to send to the server.
307 void NTDLL_get_server_timeout( abs_time_t *when, const LARGE_INTEGER *timeout )
309 UINT remainder;
311 if (!timeout) /* infinite timeout */
313 when->sec = when->usec = 0;
315 else if (timeout->QuadPart <= 0) /* relative timeout */
317 struct timeval tv;
318 ULONG sec = RtlEnlargedUnsignedDivide( -timeout->QuadPart, TICKSPERSEC, &remainder );
319 gettimeofday( &tv, 0 );
320 when->sec = tv.tv_sec + sec;
321 if ((when->usec = tv.tv_usec + (remainder / 10)) >= 1000000)
323 when->usec -= 1000000;
324 when->sec++;
327 else /* absolute time */
329 when->sec = RtlEnlargedUnsignedDivide( timeout->QuadPart - TICKS_1601_TO_1970,
330 TICKSPERSEC, &remainder );
331 when->usec = remainder / 10;
336 /******************************************************************************
337 * RtlTimeToTimeFields [NTDLL.@]
339 * Convert a time into a TIME_FIELDS structure.
341 * PARAMS
342 * liTime [I] Time to convert.
343 * TimeFields [O] Destination for the converted time.
345 * RETURNS
346 * Nothing.
348 VOID WINAPI RtlTimeToTimeFields(
349 const LARGE_INTEGER *liTime,
350 PTIME_FIELDS TimeFields)
352 const int *Months;
353 int SecondsInDay, CurYear;
354 int LeapYear, CurMonth;
355 long int Days;
356 LONGLONG Time = liTime->QuadPart;
358 /* Extract millisecond from time and convert time into seconds */
359 TimeFields->Milliseconds = (CSHORT) ((Time % TICKSPERSEC) / TICKSPERMSEC);
360 Time = Time / TICKSPERSEC;
362 /* The native version of RtlTimeToTimeFields does not take leap seconds
363 * into account */
365 /* Split the time into days and seconds within the day */
366 Days = Time / SECSPERDAY;
367 SecondsInDay = Time % SECSPERDAY;
369 /* compute time of day */
370 TimeFields->Hour = (CSHORT) (SecondsInDay / SECSPERHOUR);
371 SecondsInDay = SecondsInDay % SECSPERHOUR;
372 TimeFields->Minute = (CSHORT) (SecondsInDay / SECSPERMIN);
373 TimeFields->Second = (CSHORT) (SecondsInDay % SECSPERMIN);
375 /* compute day of week */
376 TimeFields->Weekday = (CSHORT) ((EPOCHWEEKDAY + Days) % DAYSPERWEEK);
378 /* compute year */
379 CurYear = EPOCHYEAR;
380 /* FIXME: handle calendar modifications */
381 while (1)
382 { LeapYear = IsLeapYear(CurYear);
383 if (Days < (long) YearLengths[LeapYear])
384 { break;
386 CurYear++;
387 Days = Days - (long) YearLengths[LeapYear];
389 TimeFields->Year = (CSHORT) CurYear;
391 /* Compute month of year */
392 Months = MonthLengths[LeapYear];
393 for (CurMonth = 0; Days >= (long) Months[CurMonth]; CurMonth++)
394 Days = Days - (long) Months[CurMonth];
395 TimeFields->Month = (CSHORT) (CurMonth + 1);
396 TimeFields->Day = (CSHORT) (Days + 1);
399 /******************************************************************************
400 * RtlTimeFieldsToTime [NTDLL.@]
402 * Convert a TIME_FIELDS structure into a time.
404 * PARAMS
405 * ftTimeFields [I] TIME_FIELDS structure to convert.
406 * Time [O] Destination for the converted time.
408 * RETURNS
409 * Success: TRUE.
410 * Failure: FALSE.
412 BOOLEAN WINAPI RtlTimeFieldsToTime(
413 PTIME_FIELDS tfTimeFields,
414 PLARGE_INTEGER Time)
416 int CurYear, CurMonth;
417 LONGLONG rcTime;
418 TIME_FIELDS TimeFields = *tfTimeFields;
420 rcTime = 0;
422 /* FIXME: normalize the TIME_FIELDS structure here */
423 while (TimeFields.Second >= SECSPERMIN)
424 { NormalizeTimeFields(&TimeFields.Second, &TimeFields.Minute, SECSPERMIN);
426 while (TimeFields.Minute >= MINSPERHOUR)
427 { NormalizeTimeFields(&TimeFields.Minute, &TimeFields.Hour, MINSPERHOUR);
429 while (TimeFields.Hour >= HOURSPERDAY)
430 { NormalizeTimeFields(&TimeFields.Hour, &TimeFields.Day, HOURSPERDAY);
432 while (TimeFields.Day > MonthLengths[IsLeapYear(TimeFields.Year)][TimeFields.Month - 1])
433 { NormalizeTimeFields(&TimeFields.Day, &TimeFields.Month, SECSPERMIN);
435 while (TimeFields.Month > MONSPERYEAR)
436 { NormalizeTimeFields(&TimeFields.Month, &TimeFields.Year, MONSPERYEAR);
439 /* FIXME: handle calendar corrections here */
440 for (CurYear = EPOCHYEAR; CurYear < TimeFields.Year; CurYear++)
441 { rcTime += YearLengths[IsLeapYear(CurYear)];
443 for (CurMonth = 1; CurMonth < TimeFields.Month; CurMonth++)
444 { rcTime += MonthLengths[IsLeapYear(CurYear)][CurMonth - 1];
446 rcTime += TimeFields.Day - 1;
447 rcTime *= SECSPERDAY;
448 rcTime += TimeFields.Hour * SECSPERHOUR + TimeFields.Minute * SECSPERMIN + TimeFields.Second;
449 rcTime *= TICKSPERSEC;
450 rcTime += TimeFields.Milliseconds * TICKSPERMSEC;
451 Time->QuadPart = rcTime;
453 return TRUE;
455 /************ end of code by Rex Jolliff (rex@lvcablemodem.com) ***************/
457 /******************************************************************************
458 * RtlLocalTimeToSystemTime [NTDLL.@]
460 * Convert a local time into system time.
462 * PARAMS
463 * LocalTime [I] Local time to convert.
464 * SystemTime [O] Destination for the converted time.
466 * RETURNS
467 * Success: STATUS_SUCCESS.
468 * Failure: An NTSTATUS error code indicating the problem.
470 NTSTATUS WINAPI RtlLocalTimeToSystemTime( const LARGE_INTEGER *LocalTime,
471 PLARGE_INTEGER SystemTime)
473 TIME_ZONE_INFORMATION tzinfo;
475 TRACE("(%p, %p)\n", LocalTime, SystemTime);
477 RtlQueryTimeZoneInformation(&tzinfo);
478 SystemTime->QuadPart = LocalTime->QuadPart + tzinfo.Bias * 60 * (LONGLONG)10000000;
479 return STATUS_SUCCESS;
482 /******************************************************************************
483 * RtlSystemTimeToLocalTime [NTDLL.@]
485 * Convert a system time into a local time.
487 * PARAMS
488 * SystemTime [I] System time to convert.
489 * LocalTime [O] Destination for the converted time.
491 * RETURNS
492 * Success: STATUS_SUCCESS.
493 * Failure: An NTSTATUS error code indicating the problem.
495 NTSTATUS WINAPI RtlSystemTimeToLocalTime( const LARGE_INTEGER *SystemTime,
496 PLARGE_INTEGER LocalTime )
498 TIME_ZONE_INFORMATION tzinfo;
500 TRACE("(%p, %p)\n", SystemTime, LocalTime);
502 RtlQueryTimeZoneInformation(&tzinfo);
503 LocalTime->QuadPart = SystemTime->QuadPart - tzinfo.Bias * 60 * (LONGLONG)10000000;
504 return STATUS_SUCCESS;
507 /******************************************************************************
508 * RtlTimeToSecondsSince1970 [NTDLL.@]
510 * Convert a time into a count of seconds since 1970.
512 * PARAMS
513 * Time [I] Time to convert.
514 * Seconds [O] Destination for the converted time.
516 * RETURNS
517 * Success: TRUE.
518 * Failure: FALSE, if the resulting value will not fit in a DWORD.
520 BOOLEAN WINAPI RtlTimeToSecondsSince1970( const LARGE_INTEGER *Time, LPDWORD Seconds )
522 ULONGLONG tmp = ((ULONGLONG)Time->u.HighPart << 32) | Time->u.LowPart;
523 tmp = RtlLargeIntegerDivide( tmp, 10000000, NULL );
524 tmp -= SECS_1601_TO_1970;
525 if (tmp > 0xffffffff) return FALSE;
526 *Seconds = (DWORD)tmp;
527 return TRUE;
530 /******************************************************************************
531 * RtlTimeToSecondsSince1980 [NTDLL.@]
533 * Convert a time into a count of seconds since 1980.
535 * PARAMS
536 * Time [I] Time to convert.
537 * Seconds [O] Destination for the converted time.
539 * RETURNS
540 * Success: TRUE.
541 * Failure: FALSE, if the resulting value will not fit in a DWORD.
543 BOOLEAN WINAPI RtlTimeToSecondsSince1980( const LARGE_INTEGER *Time, LPDWORD Seconds )
545 ULONGLONG tmp = ((ULONGLONG)Time->u.HighPart << 32) | Time->u.LowPart;
546 tmp = RtlLargeIntegerDivide( tmp, 10000000, NULL );
547 tmp -= SECS_1601_TO_1980;
548 if (tmp > 0xffffffff) return FALSE;
549 *Seconds = (DWORD)tmp;
550 return TRUE;
553 /******************************************************************************
554 * RtlSecondsSince1970ToTime [NTDLL.@]
556 * Convert a count of seconds since 1970 to a time.
558 * PARAMS
559 * Seconds [I] Time to convert.
560 * Time [O] Destination for the converted time.
562 * RETURNS
563 * Nothing.
565 void WINAPI RtlSecondsSince1970ToTime( DWORD Seconds, LARGE_INTEGER *Time )
567 ULONGLONG secs = Seconds * (ULONGLONG)TICKSPERSEC + TICKS_1601_TO_1970;
568 Time->u.LowPart = (DWORD)secs;
569 Time->u.HighPart = (DWORD)(secs >> 32);
572 /******************************************************************************
573 * RtlSecondsSince1980ToTime [NTDLL.@]
575 * Convert a count of seconds since 1980 to a time.
577 * PARAMS
578 * Seconds [I] Time to convert.
579 * Time [O] Destination for the converted time.
581 * RETURNS
582 * Nothing.
584 void WINAPI RtlSecondsSince1980ToTime( DWORD Seconds, LARGE_INTEGER *Time )
586 ULONGLONG secs = Seconds * (ULONGLONG)TICKSPERSEC + TICKS_1601_TO_1980;
587 Time->u.LowPart = (DWORD)secs;
588 Time->u.HighPart = (DWORD)(secs >> 32);
591 /******************************************************************************
592 * RtlTimeToElapsedTimeFields [NTDLL.@]
594 * Convert a time to a count of elapsed seconds.
596 * PARAMS
597 * Time [I] Time to convert.
598 * TimeFields [O] Destination for the converted time.
600 * RETURNS
601 * Nothing.
603 void WINAPI RtlTimeToElapsedTimeFields( const LARGE_INTEGER *Time, PTIME_FIELDS TimeFields )
605 LONGLONG time;
606 UINT rem;
608 time = RtlExtendedLargeIntegerDivide( Time->QuadPart, TICKSPERSEC, &rem );
609 TimeFields->Milliseconds = rem / TICKSPERMSEC;
611 /* time is now in seconds */
612 TimeFields->Year = 0;
613 TimeFields->Month = 0;
614 TimeFields->Day = RtlExtendedLargeIntegerDivide( time, SECSPERDAY, &rem );
616 /* rem is now the remaining seconds in the last day */
617 TimeFields->Second = rem % 60;
618 rem /= 60;
619 TimeFields->Minute = rem % 60;
620 TimeFields->Hour = rem / 60;
623 /***********************************************************************
624 * NtQuerySystemTime [NTDLL.@]
625 * ZwQuerySystemTime [NTDLL.@]
627 * Get the current system time.
629 * PARAMS
630 * Time [O] Destination for the current system time.
632 * RETURNS
633 * Success: STATUS_SUCCESS.
634 * Failure: An NTSTATUS error code indicating the problem.
636 NTSTATUS WINAPI NtQuerySystemTime( PLARGE_INTEGER Time )
638 struct timeval now;
640 gettimeofday( &now, 0 );
641 Time->QuadPart = now.tv_sec * (ULONGLONG)TICKSPERSEC + TICKS_1601_TO_1970;
642 Time->QuadPart += now.tv_usec * 10;
643 return STATUS_SUCCESS;
646 /***********************************************************************
647 * TIME_GetBias [internal]
649 * Helper function calculates delta local time from UTC.
651 * PARAMS
652 * utc [I] The current utc time.
653 * pdaylight [I] Local daylight.
655 * RETURNS
656 * The bias for the current timezone.
658 static int TIME_GetBias(time_t utc, int *pdaylight)
660 struct tm *ptm = localtime(&utc);
661 *pdaylight = ptm->tm_isdst; /* daylight for local timezone */
662 ptm = gmtime(&utc);
663 ptm->tm_isdst = *pdaylight; /* use local daylight, not that of Greenwich */
664 return (int)(utc-mktime(ptm));
667 /***********************************************************************
668 * TIME_GetTZAsStr [internal]
670 * Helper function that returns the given timezone as a string.
672 * PARAMS
673 * utc [I] The current utc time.
674 * bias [I] The bias of the current timezone.
675 * dst [I] ??
677 * RETURNS
678 * Timezone name.
680 * NOTES:
681 * This could be done with a hash table instead of merely iterating through a
682 * table, however with the small amount of entries (60 or so) I didn't think
683 * it was worth it.
685 static const WCHAR* TIME_GetTZAsStr (time_t utc, int bias, int dst)
687 char psTZName[7];
688 struct tm *ptm = localtime(&utc);
689 int i;
691 if (!strftime (psTZName, 7, "%Z", ptm))
692 return (NULL);
694 for (i=0; i<(sizeof(TZ_INFO) / sizeof(struct tagTZ_INFO)); i++)
696 if ( strcmp(TZ_INFO[i].psTZFromUnix, psTZName) == 0 &&
697 TZ_INFO[i].bias == bias &&
698 TZ_INFO[i].dst == dst
700 return TZ_INFO[i].psTZWindows;
703 return NULL;
706 /***********************************************************************
707 * RtlQueryTimeZoneInformation [NTDLL.@]
709 * Get information about the current timezone.
711 * PARAMS
712 * tzinfo [O] Destination for the retrieved timezone info.
714 * RETURNS
715 * Success: STATUS_SUCCESS.
716 * Failure: An NTSTATUS error code indicating the problem.
718 NTSTATUS WINAPI RtlQueryTimeZoneInformation(LPTIME_ZONE_INFORMATION tzinfo)
720 time_t gmt;
721 int bias, daylight;
722 const WCHAR *psTZ;
724 memset(tzinfo, 0, sizeof(TIME_ZONE_INFORMATION));
726 gmt = time(NULL);
727 bias = TIME_GetBias(gmt, &daylight);
729 tzinfo->Bias = -bias / 60;
730 tzinfo->StandardBias = 0;
731 tzinfo->DaylightBias = -60;
732 psTZ = TIME_GetTZAsStr (gmt, (-bias/60), daylight);
733 if (psTZ) strcpyW( tzinfo->StandardName, psTZ );
734 return STATUS_SUCCESS;
737 /***********************************************************************
738 * RtlSetTimeZoneInformation [NTDLL.@]
740 * Set the current time zone information.
742 * PARAMS
743 * tzinfo [I] Timezone information to set.
745 * RETURNS
746 * Success: STATUS_SUCCESS.
747 * Failure: An NTSTATUS error code indicating the problem.
749 * BUGS
750 * Uses the obsolete unix timezone structure and tz_dsttime member.
752 NTSTATUS WINAPI RtlSetTimeZoneInformation( const TIME_ZONE_INFORMATION *tzinfo )
754 struct timezone tz;
756 tz.tz_minuteswest = tzinfo->Bias;
757 #ifdef DST_NONE
758 tz.tz_dsttime = DST_NONE;
759 #else
760 tz.tz_dsttime = 0;
761 #endif
762 if(!settimeofday(NULL, &tz))
763 return STATUS_SUCCESS;
764 return STATUS_PRIVILEGE_NOT_HELD;
767 /***********************************************************************
768 * NtSetSystemTime [NTDLL.@]
769 * ZwSetSystemTime [NTDLL.@]
771 * Set the system time.
773 * PARAMS
774 * NewTime [I] The time to set.
775 * OldTime [O] Optional destination for the previous system time.
777 * RETURNS
778 * Success: STATUS_SUCCESS.
779 * Failure: An NTSTATUS error code indicating the problem.
781 NTSTATUS WINAPI NtSetSystemTime(const LARGE_INTEGER *NewTime, LARGE_INTEGER *OldTime)
783 TIME_FIELDS tf;
784 struct timeval tv;
785 struct timezone tz;
786 struct tm t;
787 time_t sec, oldsec;
788 int dst, bias;
789 int err;
791 /* Return the old time if necessary */
792 if(OldTime)
793 NtQuerySystemTime(OldTime);
795 RtlTimeToTimeFields(NewTime, &tf);
797 /* call gettimeofday to get the current timezone */
798 gettimeofday(&tv, &tz);
799 oldsec = tv.tv_sec;
800 /* get delta local time from utc */
801 bias = TIME_GetBias(oldsec, &dst);
803 /* get the number of seconds */
804 t.tm_sec = tf.Second;
805 t.tm_min = tf.Minute;
806 t.tm_hour = tf.Hour;
807 t.tm_mday = tf.Day;
808 t.tm_mon = tf.Month - 1;
809 t.tm_year = tf.Year - 1900;
810 t.tm_isdst = dst;
811 sec = mktime (&t);
812 /* correct for timezone and daylight */
813 sec += bias;
815 /* set the new time */
816 tv.tv_sec = sec;
817 tv.tv_usec = tf.Milliseconds * 1000;
819 /* error and sanity check*/
820 if(sec == (time_t)-1 || abs((int)(sec-oldsec)) > SETTIME_MAX_ADJUST) {
821 err = 2;
822 } else {
823 #ifdef HAVE_SETTIMEOFDAY
824 err = settimeofday(&tv, NULL); /* 0 is OK, -1 is error */
825 if(err == 0)
826 return STATUS_SUCCESS;
827 #else
828 err = 1;
829 #endif
832 ERR("Cannot set time to %d/%d/%d %d:%d:%d Time adjustment %ld %s\n",
833 tf.Year, tf.Month, tf.Day, tf.Hour, tf.Minute, tf.Second,
834 (long)(sec-oldsec),
835 err == -1 ? "No Permission"
836 : sec == (time_t)-1 ? "" : "is too large." );
838 if(err == 2)
839 return STATUS_INVALID_PARAMETER;
840 else if(err == -1)
841 return STATUS_PRIVILEGE_NOT_HELD;
842 else
843 return STATUS_NOT_IMPLEMENTED;