source: trunk/src/ntdll/time.c@ 21916

Last change on this file since 21916 was 21916, checked in by dmik, 14 years ago

Merge branch gcc-kmk to trunk.

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