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